package com.ruoyi.szxc.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.szxc.domain.SzxcHjAddress;
import com.ruoyi.szxc.service.ISzxcHjAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 户主信息Controller
 * 
 * @author hs
 * @date 2024-03-15
 */
@RestController
@RequestMapping("/szxc/address")
public class SzxcHjAddressController extends BaseController
{
    @Autowired
    private ISzxcHjAddressService szxcHjAddressService;

    /**
     * 查询户主信息列表
     */
    @PreAuthorize("@ss.hasPermi('szxc:address:list')")
    @GetMapping("/list")
    public TableDataInfo list(SzxcHjAddress szxcHjAddress)
    {
        startPage();
        List<SzxcHjAddress> list = szxcHjAddressService.selectSzxcHjAddressList(szxcHjAddress);
        return getDataTable(list);
    }

    /**
     * 导出户主信息列表
     */
    @PreAuthorize("@ss.hasPermi('szxc:address:export')")
    @Log(title = "户主信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SzxcHjAddress szxcHjAddress)
    {
        List<SzxcHjAddress> list = szxcHjAddressService.selectSzxcHjAddressList(szxcHjAddress);
        ExcelUtil<SzxcHjAddress> util = new ExcelUtil<SzxcHjAddress>(SzxcHjAddress.class);
        util.exportExcel(response, list, "户主信息数据");
    }

    /**
     * 获取户主信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('szxc:address:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(szxcHjAddressService.selectSzxcHjAddressById(id));
    }

    /**
     * 新增户主信息
     */
    @PreAuthorize("@ss.hasPermi('szxc:address:add')")
    @Log(title = "户主信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SzxcHjAddress szxcHjAddress)
    {
        //根据前端传递过来的部门存储，如果没传则存储当前用户的部门
        if(szxcHjAddress.getDeptId()==null){
            szxcHjAddress.setDeptId(getDeptId());
        }
        szxcHjAddress.setUserId(getUserId());
        return szxcHjAddressService.insertSzxcHjAddress(szxcHjAddress);
    }

    /**
     * 修改户主信息
     */
    @PreAuthorize("@ss.hasPermi('szxc:address:edit')")
    @Log(title = "户主信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SzxcHjAddress szxcHjAddress)
    {
        return szxcHjAddressService.updateSzxcHjAddress(szxcHjAddress);
    }

    /**
     * 删除户主信息
     */
    @PreAuthorize("@ss.hasPermi('szxc:address:remove')")
    @Log(title = "户主信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(szxcHjAddressService.deleteSzxcHjAddressByIds(ids));
    }
}
