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.SzxcJmInfo;
import com.ruoyi.szxc.service.ISzxcJmInfoService;
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-16
 */
@RestController
@RequestMapping("/szxc/jminfo")
public class SzxcJmInfoController extends BaseController
{
    @Autowired
    private ISzxcJmInfoService szxcJmInfoService;

    /**
     * 查询居民信息列表
     */
    @PreAuthorize("@ss.hasPermi('szxc:jminfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(SzxcJmInfo szxcJmInfo)
    {
        startPage();
        List<SzxcJmInfo> list = szxcJmInfoService.selectSzxcJmInfoList(szxcJmInfo);
        return getDataTable(list);
    }

    /**
     * 导出居民信息列表
     */
    @PreAuthorize("@ss.hasPermi('szxc:jminfo:export')")
    @Log(title = "居民信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SzxcJmInfo szxcJmInfo)
    {
        List<SzxcJmInfo> list = szxcJmInfoService.selectSzxcJmInfoList(szxcJmInfo);
        ExcelUtil<SzxcJmInfo> util = new ExcelUtil<SzxcJmInfo>(SzxcJmInfo.class);
        util.exportExcel(response, list, "居民信息数据");
    }

    /**
     * 获取居民信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('szxc:jminfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(szxcJmInfoService.selectSzxcJmInfoById(id));
    }

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

    /**
     * 修改居民信息
     */
    @PreAuthorize("@ss.hasPermi('szxc:jminfo:edit')")
    @Log(title = "居民信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SzxcJmInfo szxcJmInfo)
    {
        return szxcJmInfoService.updateSzxcJmInfo(szxcJmInfo);
    }

    /**
     * 删除居民信息
     */
    @PreAuthorize("@ss.hasPermi('szxc:jminfo:remove')")
    @Log(title = "居民信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(szxcJmInfoService.deleteSzxcJmInfoByIds(ids));
    }
}
