package com.ruoyi.web.controller.info;

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

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.UserInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.KeyPeopleInfo;
import com.ruoyi.system.service.IKeyPeopleInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 重点人群信息Controller
 * 
 * @author boman
 * @date 2022-09-27
 */
@RestController
@RequestMapping("/keyPeople/info")
public class KeyPeopleInfoController extends BaseController
{
    @Autowired
    private IKeyPeopleInfoService keyPeopleInfoService;

    /**
     * 查询重点人群信息列表
     */
    @PreAuthorize("@ss.hasPermi('keyPeople:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(KeyPeopleInfo keyPeopleInfo)
    {
        startPage();
        List<KeyPeopleInfo> list = keyPeopleInfoService.selectKeyPeopleInfoList(keyPeopleInfo);
        return getDataTable(list);
    }

    /**
     * 导出重点人群信息列表
     */
    @PreAuthorize("@ss.hasPermi('keyPeople:info:export')")
    @Log(title = "重点人群信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, KeyPeopleInfo keyPeopleInfo)
    {
        List<KeyPeopleInfo> list = keyPeopleInfoService.selectKeyPeopleInfoList(keyPeopleInfo);
        ExcelUtil<KeyPeopleInfo> util = new ExcelUtil<KeyPeopleInfo>(KeyPeopleInfo.class);
        util.exportExcel(response, list, "重点人群信息数据");
    }

    @Log(title = "导入重点人群信息", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('keyPeople:info:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<KeyPeopleInfo> util = new ExcelUtil<KeyPeopleInfo>(KeyPeopleInfo.class);
        List<KeyPeopleInfo> userList = util.importExcel(file.getInputStream());
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (userList == null) {
            return AjaxResult.error("表格格式错误，请按照表格模板格式上传人员名单");
        }

        System.out.println("接口开始========");
        StringBuilder sb = new StringBuilder();
        for (KeyPeopleInfo userInfo : userList) {
            if (userInfo == null) {
                return AjaxResult.error("表格格式错误，请按照表格模板格式上传人员名单");
            }
            //插入部门id
            userInfo.setDeptId(user.getDeptId().toString());
            if (userInfo.getIdCard().length() != 18) {
                sb.append(userInfo.getName()).append(",");
            }
        }

        if (StringUtils.isNotEmpty(sb.toString())) {
            return AjaxResult.error("操作失败,以下人员身份证格式错误【" + sb.toString() + "】");
        }
        keyPeopleInfoService.importUser(userList);
        return AjaxResult.success("正在处理中,请稍后刷新页面");
    }

    /**
     * 获取重点人群信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('keyPeople:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(keyPeopleInfoService.selectKeyPeopleInfoById(id));
    }

    /**
     * 新增重点人群信息
     */
    @PreAuthorize("@ss.hasPermi('keyPeople:info:add')")
    @Log(title = "重点人群信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody KeyPeopleInfo keyPeopleInfo)
    {
        return toAjax(keyPeopleInfoService.insertKeyPeopleInfo(keyPeopleInfo));
    }

    /**
     * 修改重点人群信息
     */
    @PreAuthorize("@ss.hasPermi('keyPeople:info:edit')")
    @Log(title = "重点人群信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody KeyPeopleInfo keyPeopleInfo)
    {
        return toAjax(keyPeopleInfoService.updateKeyPeopleInfo(keyPeopleInfo));
    }

    /**
     * 删除重点人群信息
     */
    @PreAuthorize("@ss.hasPermi('keyPeople:info:remove')")
    @Log(title = "重点人群信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(keyPeopleInfoService.deleteKeyPeopleInfoByIds(ids));
    }
}
