package com.mosty.base.manager.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mosty.base.manager.mapper.TbJcglYbfjMapper;
import com.mosty.base.manager.service.TbJcglXfllService;
import com.mosty.base.model.dto.jcgl.TbJcglXfllDto;
import com.mosty.base.model.dto.jcgl.TbJcglXfllSearchQuery;
import com.mosty.base.model.dto.jcgl.TbJcglZyjzSearchDto;
import com.mosty.base.model.entity.jcgl.TbJcglXfll;
import com.mosty.base.model.entity.jcgl.Ybfj;
import com.mosty.base.model.query.jcgl.TbJcglXfllQuery;
import com.mosty.base.model.query.jcgl.TbJcglXfllQueryAllDto;
import com.mosty.base.model.vo.jcgl.TbJcglXfllVo;
import com.mosty.base.model.vo.jcgl.TbJcglZyjz;
import com.mosty.common.base.domain.ResponseResult;
import com.mosty.common.base.entity.log.BusinessType;
import com.mosty.common.base.entity.log.Log;
import com.mosty.common.base.exception.BusinessException;
import com.mosty.common.config.Excel.ExcelUtil;
import com.mosty.common.core.business.entity.SysUser;
import com.mosty.common.core.business.entity.Ybmj;
import com.mosty.common.core.business.mapper.SysUserMapper;
import com.mosty.common.core.business.mapper.YbmjMapper;
import com.mosty.common.token.JwtSysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 巡防力量表;(tb_jcgl_xfll)表控制层
 *
 * @author : Lhh
 * @date : 2022-6-4
 */
@RestController
@AllArgsConstructor
@Api(tags = "巡防力量表对象功能接口")
@RequestMapping("/tbJcglXfll")
public class TbJcglXfllController {

    private final TbJcglXfllService tbJcglXfllService;
    private final TbJcglYbfjMapper tbJcglYbfjMapper;
    private final YbmjMapper ybmjMapper;
    private final SysUserMapper sysUserMapper;


    @GetMapping("/ybmjtb")
    @JwtSysUser
    @ApiOperation(value = "宜宾民警同步")
    @Log(title = "宜宾民警同步", businessType = BusinessType.OTHER)
    public ResponseResult<Void> ybmjtb() {
//        return ResponseResult.success(tbJcglXfllService.queryById(id));
        List<Ybmj> ybmjs = ybmjMapper.selectList(new QueryWrapper<>());
        if (!CollectionUtils.isEmpty(ybmjs)) {
            for (Ybmj ybmj : ybmjs) {
                TbJcglXfllDto tbJcglXfllDto = new TbJcglXfllDto();
                tbJcglXfllDto.setFl("01");
                tbJcglXfllDto.setXm(ybmj.getXm());
                tbJcglXfllDto.setSfzh(ybmj.getSfzh());
                //计算男女
                int gender = Integer.parseInt(ybmj.getSfzh().substring(16, 17));
                if (gender % 2 == 1) {
                    tbJcglXfllDto.setXbdm("1");
                } else {
                    tbJcglXfllDto.setXbdm("2");
                }
                tbJcglXfllDto.setMzdm("01");
                tbJcglXfllDto.setZzmm("1");
                tbJcglXfllDto.setJh(ybmj.getJh());
                tbJcglXfllDto.setLx("02");
                tbJcglXfllDto.setLxdh(ybmj.getDh());
                tbJcglXfllDto.setSsbmdm(ybmj.getSsbmdm());
                SysUser user = sysUserMapper.selectOne(new QueryWrapper<SysUser>().eq("id_entity_card", ybmj.getSfzh()));
                if (user != null) {
                    tbJcglXfllDto.setRyid(String.valueOf(user.getId()));
                }
                tbJcglXfllService.add(tbJcglXfllDto);
                ybmjMapper.deleteById(ybmj);
            }
        }
        return ResponseResult.success();
    }


    @GetMapping("/ybfjtb")
    @JwtSysUser
    @ApiOperation(value = "宜宾辅警同步")
    @Log(title = "宜宾辅警同步", businessType = BusinessType.OTHER)
    public ResponseResult<Void> ybfjtb() {
//        return ResponseResult.success(tbJcglXfllService.queryById(id));
        List<Ybfj> ybfjs = tbJcglYbfjMapper.selectList(new QueryWrapper<>());
        if (!CollectionUtils.isEmpty(ybfjs)) {
            for (Ybfj ybfj : ybfjs) {
                TbJcglXfllDto tbJcglXfllDto = new TbJcglXfllDto();
                tbJcglXfllDto.setFl("02");

                tbJcglXfllDto.setXm(ybfj.getXm());
                tbJcglXfllDto.setSfzh(ybfj.getSfzh());
                //计算男女
                int gender = Integer.parseInt(ybfj.getSfzh().substring(16, 17));
                if (gender % 2 == 1) {
                    tbJcglXfllDto.setXbdm("1");
                } else {
                    tbJcglXfllDto.setXbdm("2");
                }
                tbJcglXfllDto.setMzdm("01");
                tbJcglXfllDto.setZzmm("1");
                tbJcglXfllDto.setJh(ybfj.getJh());
                tbJcglXfllDto.setLx("02");
                tbJcglXfllDto.setLxdh(ybfj.getDh());
                tbJcglXfllDto.setSsbmdm(ybfj.getSsbmdm());
                tbJcglXfllService.add(tbJcglXfllDto);
                tbJcglYbfjMapper.deleteById(ybfj);
            }
        }
        return ResponseResult.success();
    }

    @GetMapping("{id}")
    @JwtSysUser
    @ApiOperation(value = "通过ID查询单条数据")
    @Log(title = "巡防力量ID查询", businessType = BusinessType.OTHER)
    @ApiImplicitParam(name = "id", value = "巡防力量ID", paramType = "path", required = true, dataType = "String")
    public ResponseResult<TbJcglXfllVo> queryById(@PathVariable("id") String id) {
        return ResponseResult.success(tbJcglXfllService.queryById(id));
    }

    @ApiOperation(value = "分页查询", httpMethod = "GET")
    @JwtSysUser
    @GetMapping
    @Log(title = "巡防力量分页查询", businessType = BusinessType.OTHER)
    public ResponseResult<IPage<TbJcglXfllVo>> paginQuery(TbJcglXfllQuery tbJcglXfll) {
        IPage<TbJcglXfllVo> page = tbJcglXfllService.getList(tbJcglXfll);
        return ResponseResult.success(page);
    }

    @ApiOperation(value = "巡防力量查询--第三方接口", httpMethod = "GET")
    @GetMapping("getXfllPage")
    public ResponseResult<IPage<TbJcglXfllVo>> getXfllPage(TbJcglXfllQuery tbJcglXfll) {
        IPage<TbJcglXfllVo> page = tbJcglXfllService.getXfllPage(tbJcglXfll);
        return ResponseResult.success(page);
    }

    @ApiOperation(value = "分页查询", httpMethod = "GET")
    @JwtSysUser
    @GetMapping("/querySfzh")
    @Log(title = "查询身份证号集合", businessType = BusinessType.OTHER)
    public ResponseResult querySfzh() {
        List<String> list = tbJcglXfllService.querySfzh();
        return ResponseResult.success(list);
    }

    @ApiOperation("新增数据")
    @PostMapping
    @JwtSysUser
    @Log(title = "巡防力量新增", businessType = BusinessType.INSERT)
    public ResponseResult<Boolean> add(@RequestBody TbJcglXfllDto tbJcglXfllDto) {
        tbJcglXfllService.add(tbJcglXfllDto);
        return ResponseResult.success();
    }

    @ApiOperation("验证人员身份证是否存在")
    @GetMapping("/check/")
    @ApiImplicitParam(name = "sfzh", value = "巡防力量身份证号", paramType = "query", required = true, dataType = "String")
    public ResponseResult<Boolean> add(@RequestParam("sfzh") String sfzh) {
        tbJcglXfllService.cheskXfll(sfzh);
        return ResponseResult.success();
    }

    @ApiOperation("更新数据")
    @JwtSysUser
    @PutMapping
    @Log(title = "巡防力量修改", businessType = BusinessType.UPDATE)
    public ResponseResult<Boolean> edit(@RequestBody TbJcglXfllDto tbJcglXfllDto) {
        tbJcglXfllService.edit(tbJcglXfllDto);
        return ResponseResult.success();
    }

    @ApiOperation("通过主键删除数据")
    @JwtSysUser
    @PostMapping("deleteList")
    @Log(title = "巡防力量删除", businessType = BusinessType.DELETE)
    @ApiImplicitParam(name = "id", value = "巡防力量ID", paramType = "query", required = true, dataType = "String")
    public ResponseResult deleteById(@RequestBody List<String> id) {
        return ResponseResult.success(tbJcglXfllService.deleteList(id));
    }

    @ApiOperation("通过主键删除数据(单个)")
    @PostMapping("delXfll/{id}")
    @Log(title = "通过主键删除数据(单个)", businessType = BusinessType.DELETE)
    public ResponseResult deleteById(@PathVariable("id") String id) {
        List<String> list = new ArrayList<String>() {{
            this.add(id);
        }};
        return ResponseResult.success(tbJcglXfllService.deleteList(list));
    }

    @ApiOperation("大屏-勤务中心-警力情况")
    @JwtSysUser
    @PostMapping("selectAllPolice")
    @Log(title = "大屏-勤务中心-警力情况", businessType = BusinessType.OTHER)
    @ApiImplicitParam(name = "id", value = "巡防力量ID", paramType = "query", required = true, dataType = "String")
    public ResponseResult selectAllPolice(@RequestBody TbJcglXfllDto tbJcglXfllDto) {
        return ResponseResult.success(tbJcglXfllService.selectAllPolice(tbJcglXfllDto));
    }

    @ApiOperation("查询部门下所有的辅警信息")
    @JwtSysUser
    @PostMapping("getFjList/{deptid}")
    public ResponseResult<List<com.mosty.base.model.dto.jcgl.TbJcglXfllVo>> getFjList(@PathVariable("deptid") String deptid) {
        return ResponseResult.success(tbJcglXfllService.getFjList(deptid));
    }

    @ApiOperation("查询部门下的专业警种信息")
    @JwtSysUser
    @PostMapping("getZyjzList")
    public ResponseResult<IPage<TbJcglZyjz>> getZyjzList(@RequestBody TbJcglZyjzSearchDto dto) {
        return ResponseResult.success(tbJcglXfllService.getZyjzList(dto));
    }

    @ApiOperation("查询部门下的巡防力量信息")
    @PostMapping("getXfll")
    public ResponseResult<List<TbJcglXfllVo>> getXfll(@RequestBody TbJcglXfllQueryAllDto dto) {
        return ResponseResult.success(tbJcglXfllService.getXfll(dto));
    }

    @ApiOperation("查询巡防力量 民警、辅警")
    @JwtSysUser
    @GetMapping("getXfllList")
    public ResponseResult<IPage<TbJcglXfllVo>> getXfllList(TbJcglXfllSearchQuery dto) {
        if (dto.getPageCurrent() == null || dto.getPageSize() == null) {
            throw new BusinessException("需要分页参数！！");
        }
        return ResponseResult.success(tbJcglXfllService.getXfllList(dto));
    }

    @ApiOperation("根据身份证查询巡组力量信息")
    @PostMapping("getXfllBySfz/{sfzh}")
    public ResponseResult<TbJcglXfllDto> getXfllBySfz(@PathVariable("sfzh") String sfzh) {
        return ResponseResult.success(tbJcglXfllService.getXfllBySfz(sfzh));
    }


    @ApiOperation("通过主键离职人员")
    @JwtSysUser
    @PostMapping("lzList")
    @Log(title = "巡防力量离职", businessType = BusinessType.DELETE)
    @ApiImplicitParam(name = "id", value = "巡防力量ID", paramType = "query", required = true, dataType = "String")
    public ResponseResult lzList(@RequestBody List<String> id) {
        tbJcglXfllService.lzList(id);
        return ResponseResult.success();
    }

    @ApiOperation("通过主键离职人员(单个)")
    @PostMapping("lzXfll/{id}")
    @Log(title = "通过主键离职人员(单个)", businessType = BusinessType.DELETE)
    public ResponseResult lzXfll(@PathVariable("id") String id) {
        List<String> list = new ArrayList<String>() {{
            this.add(id);
        }};
        tbJcglXfllService.lzList(list);
        return ResponseResult.success();
    }

    @ApiOperation(value = "获取离职人员列表")
    @PostMapping("getLzPage")
    public ResponseResult<IPage<TbJcglXfll>> getLzPage(@RequestBody TbJcglXfllQuery tbJcglXfll) {
        IPage<TbJcglXfll> page = tbJcglXfllService.getLzPage(tbJcglXfll);
        return ResponseResult.success(page);
    }


    @ApiOperation(value = "查询部门下面人员数量")
    @GetMapping("getRysl")
    @JwtSysUser
    public ResponseResult<Integer> getRysl(String ssbmdm) {
        return ResponseResult.success(tbJcglXfllService.getRysl(ssbmdm));
    }

    @ApiOperation(value = "人员统计")
    @GetMapping("getRytj")
    @JwtSysUser
    public ResponseResult<Map<String, Object>> getRytj(String ssbmdm) {
        return ResponseResult.success(tbJcglXfllService.getRytj(ssbmdm));
    }

    @ApiOperation("获取部门列表")
    @GetMapping("/getBmAll")
    @JwtSysUser
    public ResponseResult<List<TbJcglXfll>> getBmAll() {
        return ResponseResult.success(this.tbJcglXfllService.getBmAll());
    }

    @Log(title = "导出巡防力量", businessType = BusinessType.EXPORT)
    @PostMapping("/exportZY")
    @JwtSysUser
    public void export(HttpServletResponse response) {
        List<TbJcglXfll> list = tbJcglXfllService.export();
        ExcelUtil<TbJcglXfll> util = new ExcelUtil<
                >(TbJcglXfll.class);
        util.exportExcel(response, list, "巡逻辅警导出", "巡逻辅警导出");
    }


    @Log(title = "导入巡防力量", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    @JwtSysUser
    public ResponseResult<String> importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<TbJcglXfll> util = new ExcelUtil<TbJcglXfll>(TbJcglXfll.class);
        List<TbJcglXfll> userList = util.importExcel(file.getInputStream());
        String s = this.tbJcglXfllService.importData(userList, updateSupport);
        return ResponseResult.success(s);
    }


    @Log(title = "模板下载", businessType = BusinessType.IMPORT)
    @GetMapping("/importTemplate")
    @JwtSysUser
    public void importTemplate(HttpServletResponse response) throws Exception {
        ExcelUtil<TbJcglXfll> util = new ExcelUtil<TbJcglXfll>(TbJcglXfll.class);
        util.importTemplateExcel(response, "巡防力量");
    }
}