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 com.ruoyi.system.domain.UserInfoOne;
import com.ruoyi.system.service.IUserInfoService;
import org.springframework.scheduling.annotation.EnableAsync;
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.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import static com.ruoyi.common.utils.SecurityUtils.getLoginUser;

/**
 * 导入人员信息Controller
 *
 * @author ruoyi
 * @date 2022-08-11
 */
@RestController
@RequestMapping("/system/info")
@EnableAsync
public class UserInfoController extends BaseController {
    @Autowired
    private IUserInfoService userInfoService;

    /**
     * 查询导入人员信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(UserInfo userInfo) {
        startPage();
        List<UserInfo> list = userInfoService.selectUserInfoList(userInfo);
        return getDataTable(list);
    }

    /**
     * 单个人员信息对比查询
     */
    @GetMapping("/oneDateList")
    public TableDataInfo oneDateList(UserInfoOne userInfoOne) throws Exception {
        startPage();
        List<UserInfoOne> list = userInfoService.oneDateList(userInfoOne);
        return getDataTable(list);
    }

    /**
     * 单个人员信息对比
     */
    @GetMapping("/oneDate")
    public AjaxResult oneDate(UserInfo userInfo) throws Exception {
        return userInfoService.oneDate(userInfo);
    }

    /***
     * 信息对比
     */
    @GetMapping("/date")
    public AjaxResult date(UserInfo userInfo) throws Exception {

        if (StringUtils.isEmpty(userInfo.getStartTime()) || StringUtils.isEmpty(userInfo.getEndTime()) ||
                StringUtils.isEmpty(userInfo.getDetectionNumber()) || StringUtils.isEmpty(userInfo.getJobStyle()) ||
                StringUtils.isEmpty(userInfo.getFocusCrowdStyle())) {
            return AjaxResult.error("请将对比参数填写完整后查询（开始时间，结束时间，检测频次（次数），职业类别，重点人群分类）");
        }
        SysUser user = getLoginUser().getUser();
        userInfoService.date(userInfo, user);
        return AjaxResult.success("正在处理中,请稍后刷新页面");
    }


    /**
     * 导出导入人员信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:info:export')")
    @Log(title = "导入人员信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserInfo userInfo) {
        List<UserInfo> list = userInfoService.selectUserInfoList(userInfo);
        ExcelUtil<UserInfo> util = new ExcelUtil<UserInfo>(UserInfo.class);
        util.exportExcel(response, list, "导入人员信息数据");
    }


    /**
     * 核酸比对人员导入
     *
     * @param file
     * @param jobStyle        jobStyle
     * @param focusCrowdStyle focusCrowdStyle
     * @param detectionNumber detectionNumber
     * @param detectionScope  detectionScope
     * @param startTime       startTime
     * @param endTime         比对结束时间
     * @return
     * @throws Exception
     */
    @Log(title = "导入人员信息", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:info:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, String jobStyle, String focusCrowdStyle, String detectionNumber,
                                 String detectionScope, String startTime, String endTime) throws Exception {
        ExcelUtil<UserInfo> util = new ExcelUtil<UserInfo>(UserInfo.class);
        List<UserInfo> userList = util.importExcel(file.getInputStream());
        SysUser user = SecurityUtils.getLoginUser().getUser();
        //String operName = getUsername();
        userInfoService.importUser(userList, jobStyle, focusCrowdStyle, detectionNumber, detectionScope, startTime, endTime, user);

        if (userList == null) {
            AjaxResult.error("表格格式错误，请按照表格模板格式上传人员名单");
        }
        StringBuilder sb = new StringBuilder();
        for (UserInfo userInfo : userList) {
            if (userInfo == null) {
                AjaxResult.error("表格格式错误，请按照表格模板格式上传人员名单");
            }
            if (userInfo.getIdCard().length() != 18) {
                sb.append(userInfo.getName()).append(",");
            }
        }
        if (StringUtils.isNotEmpty(sb.toString())) {
            AjaxResult.error("操作失败,以下人员身份证格式错误【" + sb.toString() + "】");
        }

        return AjaxResult.success("正在处理中,请稍后刷新页面");
    }


    /**
     * 获取导入人员信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(userInfoService.selectUserInfoById(id));
    }

    /**
     * 新增导入人员信息
     */
    @Log(title = "导入人员信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserInfo userInfo) {
        return toAjax(userInfoService.insertUserInfo(userInfo));
    }

    /**
     * 修改导入人员信息
     */
    @Log(title = "导入人员信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserInfo userInfo) {
        return toAjax(userInfoService.updateUserInfo(userInfo));
    }

    /**
     * 删除导入人员信息
     */
    @Log(title = "导入人员信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(userInfoService.deleteUserInfoByIds(ids));
    }

    /**
     * 删除导入人员信息
     */
    @Log(title = "导入人员信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/one/{ids}")
    public AjaxResult removeOne(@PathVariable Long[] ids) {
        return toAjax(userInfoService.deleteUserInfoOneByIds(ids));
    }

    /**
     * 删除导入人员信息
     */
    /*@Log(title = "导入人员信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(userInfoService.deleteUserInfoById(id));
    }*/

}
