package com.boman.web.core.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.boman.common.core.utils.DateUtils;
import com.boman.common.core.utils.SecurityUtils;
import com.boman.common.core.utils.poi.ExcelUtil;
import com.boman.common.core.web.controller.BaseController;
import com.boman.common.log.annotation.Log;
import com.boman.common.log.enums.BusinessType;
import com.boman.common.security.annotation.PreAuthorize;
import com.boman.domain.*;
import com.boman.domain.dto.AjaxResult;
import com.boman.web.core.domain.AccountingData;
import com.boman.web.core.domain.JsonRequest;
import com.boman.web.core.service.czrk.ICzrkService;
import com.boman.web.core.utils.AuthUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 常住人口Controller
 *
 * @author ruoyi
 * @date 2022-01-14
 */
@RestController
@RequestMapping("/core/czrk")
@Slf4j
public class CzrkController extends BaseController {

    @Autowired
    private ICzrkService czrkService;

    /**
     * 查询户籍人口信息
     * 2022/3/22重新修复的PC人员信息查看户籍人口接口
     */
    @GetMapping("/list")
    public TableDataInfo list(Czrk czrk) {
        List<Czrk> list = czrkService.listByCzrk(czrk);
        return getDataTable(list);
    }

    /**
     * 2022/3/24修改的根据czrkId查询该用户信息
     * @param id id
     * @return com.boman.domain.dto.AjaxResult
     */
    @GetMapping("/getById/{id}")
    public AjaxResult getById(@PathVariable Long id) {
        return AjaxResult.success("成功", czrkService.getById(id));
    }

    /**
     * 统计中点击新增的列表
     * @param czrk
     * @return
     */
    @GetMapping("/listByXz")
    public TableDataInfo listByXz(Czrk czrk) {
        List<Czrk> list = czrkService.listByXz(czrk);
        return getDataTable(list);
    }

    @GetMapping("/listByJs")
    public TableDataInfo listByJs(Czrk czrk) {
        List<Czrk> list = czrkService.listByJs(czrk);
        return getDataTable(list);
    }

    /**
     * 获取常住人口详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:czrk:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(czrkService.selectCzrkById(id));
    }

    /***
     * 功能描述: 根据户主修改家庭成员的户籍与居住信息
     * @return com.boman.domain.dto.AjaxResult
     */
    @GetMapping("/synchronization/{code}")
    public AjaxResult synchronization(@PathVariable("code") String code) {
        return czrkService.synchronization(code);
    }

    /**
     * 2022/3/24修改的PC人员信息新增人员接口
     * 新增户籍人口
     */
    @Log(title = "户籍人口", businessType = BusinessType.INSERT)
    @PostMapping("/addCzrk")
    public AjaxResult add(@RequestBody Czrk czrk) {
        return czrkService.insertCzrk(czrk);
    }

    /**
     * 修改户籍人口/修改并确认
     */
    @Log(title = "户籍人口", businessType = BusinessType.UPDATE)
    @PostMapping("/editCzrk")
    public AjaxResult edit(@RequestBody Czrk czrk) {
        return czrkService.updateCzrk(czrk);
    }

    /**
     * 功能描述: 删除常住人口，人员认领中的删除也是这个接口
     */
//    @PreAuthorize("@ss.hasPermi('system:czrk:remove')")
    @Log(title = "常住人口", businessType = BusinessType.DELETE)
    @GetMapping("/deleteCzrk")
    public AjaxResult deleteCzrk(Czrk czrk) {
        return toAjax(czrkService.deleteCzrk(czrk));
    }

    /**
     * 功能描述: 回收站的还原功能
     *
     * @return com.boman.domain.dto.AjaxResult
     */
    @GetMapping("/reduction/{id}")
    public AjaxResult reduction(@PathVariable("id") String id) {
        return toAjax(czrkService.reduction(id));
    }

    /**
     * 功能描述: 根据户号查找家庭中所有人员
     *
     * @return com.boman.domain.dto.AjaxResult
     */
    @GetMapping("/family/{code}")
    public AjaxResult family(@PathVariable("code") String code) {
        return AjaxResult.success(czrkService.family(code));
    }

    /**
     * 功能描述: 当前单位：镇，村：7个，组：300个，常住人口：5000人，新增 2人  减少 2人
     *
     * @return com.boman.domain.dto.AjaxResult
     */
    @GetMapping("/stsByCzrk")
    public AjaxResult stsByCzrk() {
        return AjaxResult.success("成功", czrkService.stsByCzrk());
    }

    /**
     * 功能描述: 当前单位：镇，村：7个，组：300个，常住人口：5000人，新增 2人  减少 2人
     * {@link IndexController#stsByCzrkHomePage()}
     *
     * @return com.boman.domain.dto.AjaxResult
     */
    @Deprecated
    @GetMapping("/stsByCzrkHomePage")
    public AjaxResult stsByCzrkHomePage() {
        return AjaxResult.success("成功", czrkService.stsByCzrkHomePage());
    }



    /**
     * 根据身份证号查询用户信息
     */
    @PostMapping("/findHjInfo")
    public AjaxResult findHjInfo(@RequestBody Czrk czrk) {
        Czrk hjInfo = czrkService.findHjInfo(czrk);
        return AjaxResult.success(hjInfo);
    }


    /**
     * 变更接口,接收status 状态值和id
     * @param czrk
     * @return
     */
    @Log(title = "变更接口", businessType = BusinessType.UPDATE)
    @PostMapping("/changeCzrk")
    public AjaxResult changeCzrk(@RequestBody Czrk czrk) {
        return czrkService.changeCzrk(czrk);
    }


    /**
     * 查询历史变更信息
     * @param czrk
     * @return
     */
    @PostMapping("/changeCzrkHistory")
    public AjaxResult changeCzrkHistory(@RequestBody Czrk czrk) {
        return czrkService.changeCzrkHistory(czrk);
    }

    /**
     * 查询非正常人员列表(死亡,失踪)
     */
    @PostMapping("/unusualList")
    public TableDataInfo unusualList(@RequestBody Czrk czrk) {
        List<Czrk> list = czrkService.unusualList(czrk);
        return getDataTable(list);
    }

    /**
     * 外部访问czrk的字段 史前写
     */
    @GetMapping("/open/getInfo")
    public AjaxResult getInfo(String appKey, String appSecret, String idCard) {
        return czrkService.getInfo(appKey, appSecret, idCard);
    }

    /**
     * 暂未测试
     * 外部访问czrk的字段
     * RSE加密请求
     */
    @PostMapping("/open/getInfoJson")
    public AjaxResult getInfoJson(@RequestBody  JsonRequest jsonRequest) {
        return czrkService.getInfoJson(jsonRequest);
    }

    /**
     * 人员数据导出
     *
     */
    @Log(title = "人员信息", businessType = BusinessType.EXPORT)
    @PreAuthorize(hasPermi = "system:czrk:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody Czrk czrk) throws IOException
    {
        List<Czrk> czrkList = czrkService.getAllCzrk(czrk);
        ExcelUtil<Czrk> util = new ExcelUtil<>(Czrk.class);
        util.exportExcel(response, czrkList, "人员数据");
    }


    /**
     * 人员数据导出（只导出填写到潜山市的数据）
     *
     */
    @Log(title = "人员信息", businessType = BusinessType.EXPORT)
    @PreAuthorize(hasPermi = "system:czrk:exportQianShan")
    @PostMapping("/exportQianShanHj")
    public void exportQianShan(HttpServletResponse response) throws IOException
    {
        List<Czrk> czrkList = czrkService.exportQianShan();
        ExcelUtil<Czrk> util = new ExcelUtil<>(Czrk.class);
        util.exportExcel(response, czrkList, "人员数据");
    }

    /**
     * 人员数据导出(只导出填写镇没有填写村的数据)
     *
     */
    @Log(title = "人员信息", businessType = BusinessType.EXPORT)
    @PreAuthorize(hasPermi = "system:czrk:exportTownHj")
    @PostMapping("/exportTownHj")
    public void exportTown(HttpServletResponse response, @RequestBody Czrk czrk) throws IOException
    {
        List<Czrk> czrkList = czrkService.getAllCzrkHjVillage(czrk);
        ExcelUtil<Czrk> util = new ExcelUtil<>(Czrk.class);
        util.exportExcel(response, czrkList, "人员数据");
    }


    @Log(title = "人员信息", businessType = BusinessType.IMPORT)
    @PreAuthorize(hasPermi = "system:czrk:importData")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file,String source) throws Exception
    {
        ExcelUtil<ThirdPartInfo> util = new ExcelUtil<ThirdPartInfo>(ThirdPartInfo.class);
        List<ThirdPartInfo> thirdPartInfoList = util.importExcel(file.getInputStream());
        //String operName = SecurityUtils.getUsername();
        String message = czrkService.importUser(thirdPartInfoList,source);
        return AjaxResult.success(message);
    }

    /**
     * 查询死亡人口导入信息
     *
     */
    @GetMapping("/listDie")
    public TableDataInfo listDie(ThirdPartInfo thirdPartInfo) {
        List<ThirdPartInfo> list = czrkService.listDie(thirdPartInfo);
        return getDataTable(list);
    }

    /***
     * 删除导入的死亡人员（修改数据状态，逻辑删除）
     * @param idCard
     * @return AjaxResult
     */
    @GetMapping("/delecteDie")
    public AjaxResult delecteDie(String idCard) {
        return czrkService.delecteDie(idCard);
    }

    /***
     * 批量删除导入的死亡人员
     */
    /*@GetMapping("/delecteDieList")
    public AjaxResult delecteDieList(List<String> idCardList) {
        return czrkService.delecteDie(idCard);
    }*/

    /**
     * 匹配新生儿父亲或母亲信息
     * @param id 新生儿信息id
     * @param idCard 新生儿父亲或母亲身份证号码
     * @return
     */
    @GetMapping("/findXsInfo")
    public AjaxResult findXsInfo(String id,String idCard) {
        return czrkService.findXsInfo(id,idCard);
    }

    /***
     * 新增新生儿信息
     */
    @PostMapping("/addBabyCzrk")
    public AjaxResult addBabyCzrk(@RequestBody Czrk czrk) {
        return czrkService.addBabyCzrk(czrk);
    }

    /**
     * 疫苗接种统计
     */
    @GetMapping("/vaccineStatistics")
    public AjaxResult vaccineStatistics(String code,String lenth) {
        return czrkService.vaccineStatistics(code,lenth);
    }


    /**
     * 一键未认领(把未填写到村级别的户籍用户设置为未认领,根据不同镇来操作)
     */
    @GetMapping("/setNoRl")
    @PreAuthorize(hasPermi = "system:czrk:setNoRl")
    public AjaxResult setNoRl() {
        return  toAjax(czrkService.setNoRl()) ;
    }
}
