package com.junmp.jyzb.controller;

import cn.hutool.core.util.ObjectUtil;
import com.junmp.jyzb.api.bean.dto.CabinetPoliceDto;
import com.junmp.jyzb.api.bean.dto.fetchingDataDto.PoliceDto;
import com.junmp.jyzb.api.bean.dto.fetchingDataDto.UserDto;
import com.junmp.jyzb.api.bean.dto.PolicemanDto;
import com.junmp.jyzb.api.bean.query.CabinetReq;
import com.junmp.jyzb.api.bean.query.PolicemanReq;
import com.junmp.jyzb.api.bean.query.QueryReassignmentReq;
import com.junmp.jyzb.api.bean.req.CabinetPolicemanReq;
import com.junmp.jyzb.api.bean.req.CabinetUploadReq;
import com.junmp.jyzb.api.bean.req.UpdatePolicemanReq;
import com.junmp.jyzb.api.bean.req.UpdateReassignmentReq;
import com.junmp.jyzb.entity.*;
import com.junmp.jyzb.service.PolicemanService;
import com.junmp.v2.common.bean.request.ValidationApi;
import com.junmp.v2.common.bean.response.ApiRes;
import com.junmp.v2.db.api.page.PageResult;
import com.junmp.v2.sys.api.bean.user.SysUserDto;
import com.junmp.v2.sys.api.bean.user.req.SysUserReq;
import com.junmp.v2.sys.api.bean.user.req.UserOrgReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/Police")
@Api(tags = "[基础信息]警员基础信息模块")
public class PoliceController {

    @Resource
    public PolicemanService policemanService;

    @PostMapping(path="/AddPoliceman",name="添加警员信息#logType=30")
    @ApiOperation("添加警员信息")
    public ApiRes<String> AddPoliceman(@RequestBody @Validated(ValidationApi.add.class) UpdatePolicemanReq req) {
        String policeId = policemanService.AddPoliceman(req);
        return ApiRes.success(policeId);
    }
    @PostMapping(path="/AddPolicemanFromCab",name="添加警员信息(单警柜使用)#logType=30")
    @ApiOperation("添加警员信息")
    public ApiRes<PolicemanDto> AddPolicemanFromCab(@RequestBody @Validated(ValidationApi.add.class) CabinetPolicemanReq req) {
        PolicemanDto policeDto = policemanService.AddPolicemanFromCab(req);
        return ApiRes.success(policeDto);
    }
    //添加警员时先将没有组织机构号的警员查询出来
    @PostMapping(path="/GetPoliceWithoutOrg",name="获取没有组织机构的警员信息#enable")
    @ApiOperation("/获取没有组织机构的警员信息")
    public ApiRes<List<Policeman>> GetPoliceWithoutOrg(){
        return ApiRes.success(policemanService.GetPoliceWithoutOrg());
    }

    //添加警员时先将没有组织机构号的警员查询出来
    @PostMapping(path="/GetPoliceWithoutOrgPage",name="获取没有组织机构的警员信息#enable")
    @ApiOperation("/获取没有组织机构的警员信息(分页)")
    public ApiRes<PageResult<Policeman>> GetPoliceWithoutOrgPage(@RequestBody UpdatePolicemanReq req ){
        return ApiRes.success(policemanService.GetPoliceWithoutOrgPage(req));
    }

    @PostMapping(path="/DeletePoliceFromCab",name="警柜禁用警员信息#logType=30")
    @ApiOperation("删除警员信息")
    public ApiRes<PolicemanDto> DeletePoliceFromCab(@RequestBody @Validated(ValidationApi.delete.class) CabinetPolicemanReq req) {
        PolicemanDto b = policemanService.DeletePoliceFromCab(req);
        return ApiRes.success(b);
    }
    @PostMapping(path="/DeletePolice",name="删除警员信息#logType=30")
    @ApiOperation("删除警员信息")
    public ApiRes<Boolean> DeletePolice(@RequestBody @Validated(ValidationApi.delete.class) UpdatePolicemanReq req) {
        boolean b = policemanService.DeletePolice(req);
        if (!b){
            return ApiRes.failure("删除失败");
        }
        return ApiRes.success(b);
    }
    //添加人脸和指纹照片或修改
//    @PostMapping(path="/addFaceAndfinger",name="添加警员人脸照片和指纹照片#logType=30")
//    @ApiOperation("添加警员人脸照片和指纹照片")
//    public ApiRes<Boolean> addFaceAndfinger(@RequestBody UpdatePolicemanReq req){
//        return ApiRes.success(policemanService.addFaceAndfinger(req));
//    }


    @PostMapping(path="/ChangeUserState",name="修改账号状态#logType=30")
    @ApiOperation("修改账号状态")
    public ApiRes<Boolean> ChangeUserState(@RequestBody @Validated(ValidationApi.updateStatus.class) UpdatePolicemanReq req) {
        boolean b = policemanService.ChangePoliceState(req);
        if (!b){
            return ApiRes.failure("修改失败");
        }
        return ApiRes.success(b);
    }

    @PostMapping(path="/UpdatePolice",name="修改警员信息#logType=30")
    @ApiOperation("修改警员信息")
    public ApiRes<Boolean> UpdatePolice(@RequestBody @Validated(ValidationApi.edit.class) UpdatePolicemanReq req) {
        boolean b = policemanService.UpdatePolice(req);
        if (!b){
            return ApiRes.failure("修改失败");
        }
        return ApiRes.success(b);
    }
    @PostMapping(path="/UploadPolicemanList",name="上传当前所有警员信息#logType=30")
    @ApiOperation("上传当前所有警员信息")
    public ApiRes<Boolean> UploadPolicemanList(@RequestBody @Validated(ValidationApi.edit.class) CabinetUploadReq req) {
        boolean b = policemanService.UploadPolicemanList(req);
        if (!b){
            return ApiRes.failure("修改失败");
        }
        return ApiRes.success(b);
    }
    @PostMapping(path="/UpdatePoliceFromCab",name="修改警员信息(警柜使用)#logType=30")
    @ApiOperation("修改警员信息")
    public ApiRes<PolicemanDto> UpdatePoliceFromCab(@RequestBody @Validated(ValidationApi.edit.class) CabinetPolicemanReq req) {
        PolicemanDto b = policemanService.UpdatePoliceFromCab(req);
        return ApiRes.success(b);
    }
    //重置密码
    @PostMapping(path="/reSetPassword",name="重置密码#logType=30")
    @ApiOperation("重置密码")
    public ApiRes<Boolean> reSetPassword(@RequestBody @Validated(SysUserReq.resetPwd.class) SysUserReq req){
        return ApiRes.success(policemanService.reSetPassword(req));
    }
    //修改密码
    @PostMapping(path="/updatePassword",name="修改密码#logType=30")
    @ApiOperation("修改密码")
    public ApiRes<Boolean> updatePassword(@RequestBody UpdatePolicemanReq req){
        return ApiRes.success(policemanService.updatePassword(req));
    }



    //根据条件查询,只查询本级，不包含下级(list)

    @PostMapping(path="/ShowPoliceList",name="根据条件查询所有警员信息#enable")
    @ApiOperation("根据条件查询所有警员信息(list)")
    public ApiRes<List<PolicemanDto>> GetAllPolicemanList(@RequestBody PolicemanReq req){
        List<PolicemanDto> policemanDtos = policemanService.GetAllPolicemanList(req);
        return ApiRes.success(policemanDtos);
     }
    @PostMapping(path="/GetListByOrg",name="根据组织机构查询对应警员信息(警柜使用)#enable")
    @ApiOperation("根据组织机构查询对应警员信息(警柜使用)")
    public ApiRes<List<CabinetPoliceDto>> GetListByOrg(@RequestBody PolicemanReq req){
        List<CabinetPoliceDto> policemanDtos = policemanService.GetListByOrgAsync(req);
        return ApiRes.success(policemanDtos);
    }
    //根据组条件查询,只查询本级，不包含下级(page)--可以根据组织机构查询
    @PostMapping(path="/ShowPolicePage",name="根据条件查询所有警员信息#enable")
    @ApiOperation("根据条件查询所有警员信息(page)")
    public ApiRes<PageResult<PolicemanDto>> GetAllPolicemanPage(@RequestBody @Validated(ValidationApi.page.class) PolicemanReq req){
        PageResult<PolicemanDto> policemanDtoPageResult = policemanService.GetAllPolicemanPage(req);
        return ApiRes.success(policemanDtoPageResult);
    }


    /**
     * 通过警员id查询警员信息
     * @param req
     * @return
     */

    @PostMapping(path="/GetPoliceDetail",name="查询单个警员信息#enable")
    @ApiOperation("查询单个警员信息")
    public ApiRes<PolicemanDto> GetOnePolice(@RequestBody PolicemanReq req) {
        return ApiRes.success(policemanService.GetOnePolice(req));
    }


    /**
     * 根据单警柜id查询出单警柜下面箱门绑定的警员信息(list)
     * @param
     * @return
     */

    @PostMapping(path="/GetPoliceData",name="查询与单警柜绑定的警员信息#enable")
    @ApiOperation("查询与单警柜绑定的警员信息(list)")  //根据单警柜id查询
    public ApiRes<List<PolicemanDto>> GetPoliceData(@RequestBody CabinetReq req){
        //获取该单警柜下所有警员的id
        List<PolicemanDto> policemanDtos = policemanService.GetPoliceData(req);
        return ApiRes.success(policemanDtos);
    }

    @PostMapping(path="/GetPoliceDataPage",name="查询与单警柜绑定的警员信息#enable")
    @ApiOperation("查询与单警柜绑定的警员信息(page)")  //根据单警柜id查询
    public ApiRes<PageResult<PolicemanDto>> GetPoliceDataPage(@RequestBody CabinetReq req){
        //获取该单警柜下所有警员的id
        return ApiRes.success(policemanService.GetPoliceDataPage(req));
    }


    @PostMapping(path="/AddFaceInfo",name="添加警员面部信息#logType=30")
    @ApiOperation("添加警员面部信息")
    public ApiRes<CabinetPoliceDto> AddFaceInfo(@RequestBody @Validated(ValidationApi.detail.class) CabinetPolicemanReq req) {
        CabinetPoliceDto b = policemanService.AddFaceInfo(req);

        return ApiRes.success(b);
    }

    @PostMapping(path="/UpdateFaceInfo",name="修改、删除人脸信息#logType=30")
    @ApiOperation("修改、删除人脸信息")
    public ApiRes<CabinetPoliceDto> UpdateFaceInfo(@RequestBody @Validated(ValidationApi.delete.class) CabinetPolicemanReq req) {
        CabinetPoliceDto b = policemanService.UpdateFaceInfo(req);
        return ApiRes.success(b);
    }


    @PostMapping(path="/SearchFaceInfo",name="查找人脸信息#enable")
    @ApiOperation("查找人脸信息")
    public ApiRes<Policeman> SearchFaceInfo(@RequestBody CabinetPolicemanReq req) {
        Policeman policeman = policemanService.SearchFaceInfo(req);
        if (ObjectUtil.isEmpty(policeman)){
            return ApiRes.failure("未找到警员信息");
        }
        return ApiRes.success(policeman);
    }

    //查询组织机构下的用户以及角色信息(也可以根据别的查询条件查询)

    @PostMapping(path="/ShowUserRole",name="查询用户角色信息#enable")
    @ApiOperation("查询用户角色信息")
    public ApiRes<PageResult<SysUserDto>> ShowUserRole(@RequestBody SysUserReq req){
        return ApiRes.success( policemanService.ShowUserRole(req));
    }

    /**
     * 人员绑定箱门
     */
    @PostMapping(path="/PoliceBindBox",name="人员绑定箱门#logType=30")
    @ApiOperation("人员绑定箱门")
    public ApiRes<Boolean> PoliceBindBox(@RequestBody @Validated(ValidationApi.add.class) PolicemanReq req){
        return ApiRes.success(policemanService.PoliceBindBox(req));
    }

    @PostMapping(path="/PoliceBindBoxFormCab",name="人员绑定、解绑箱门(单警柜)#logType=30")
    @ApiOperation("人员绑定、解绑箱门(单警柜)")
    public ApiRes<CabinetPoliceDto> PoliceBindBoxFormCab(@RequestBody @Validated(ValidationApi.add.class) CabinetPolicemanReq req){
        return ApiRes.success(policemanService.PoliceBindBoxFormCab(req));
    }

    //人员解绑箱门
    @PostMapping(path = "PoliceUnBindBox",name = "人员解绑箱门#logType=30")
    @ApiOperation("人员解绑箱门")
    public ApiRes<Boolean> PoliceUnBindBox(@RequestBody @Validated(ValidationApi.add.class) PolicemanReq req){
        return ApiRes.success(policemanService.PoliceUnBindBox(req));
    }


    //删除账号的同时，将警员的信息进行更新
    @PostMapping(path="/UpdatePoliceInfo",name="删除警员账号，修改警员信息#logType=30")
    @ApiOperation("删除警员账号，修改警员信息")
    public ApiRes<Boolean> UpdatePoliceInfo (@RequestBody UserOrgReq req){
        return ApiRes.success(policemanService.UpdatePoliceInfo(req));
    }

    //将警员和user一一对应并且关系表也一一对应
    @PostMapping(path="/UpdatePoliceUser",name="将警员关系一一对应#logType=30")
    @ApiOperation("将警员关系一一对应")
    public ApiRes<Boolean> UpdatePoliceUser(){
        return ApiRes.success(policemanService.UpdatePoliceUser());
    }


    @PostMapping(path="/PoliceReassignment",name="警员调岗申请单#logType=30")
    @ApiOperation("警员调岗申请单")
    public ApiRes<String> PoliceReassignment(@RequestBody UpdateReassignmentReq req){
        return ApiRes.success(policemanService.PoliceReassignment(req));
    }

    //删除待提交的调岗单据
    @PostMapping(path = "/delToSubmitReassignment",name = "删除待提交的调岗单据#logType=30")
    @ApiOperation("删除待提交的调岗单据")
    public ApiRes<Boolean> delToSubmitReassignment(@RequestBody @Validated(ValidationApi.delete.class) UpdateReassignmentReq req){
        return ApiRes.success(policemanService.delToSubmitReassignment(req));
    }


    @PostMapping(path="/ShowReassignmentPage",name="查看警员调岗申请单#enable")
    @ApiOperation("查看警员调岗申请单")
    public ApiRes<PageResult<Reassignment>> ShowReassignmentPage(@RequestBody QueryReassignmentReq req){
        return ApiRes.success(policemanService.ShowReassignmentPage(req));
    }

    @PostMapping(path = "/showReassignmentById",name = "获取单个调岗单#enable")
    @ApiOperation("获取单个调岗单")
    public ApiRes<Reassignment> showReassignmentById(@RequestBody QueryReassignmentReq req){
        return ApiRes.success(policemanService.showReassignmentById(req));
    }

    @PostMapping(path="/ChangePoliceOrg",name="警员调岗（业务过程）#logType=30")
    @ApiOperation("警员调岗（业务过程）")
    public ApiRes<Boolean> changePoliceOrg(@RequestBody UpdateReassignmentReq req){
        return ApiRes.success(policemanService.changePoliceOrg(req));
    }


    //获取警员的装备信息

    @PostMapping(path="/GetPoliceEquipment",name="获取警员装备信息#enable")
    @ApiOperation("获取警员装备信息")
    public ApiRes<List<PoliceEquipment>> GetPoliceEqs(@RequestBody QueryReassignmentReq req){
        return ApiRes.success(policemanService.GetPoliceEqs(req));
    }

    //查询组织机构为null的警员

    @PostMapping(path="/getPoliceWithoutOrg",name="查询组织机构为空的警员#enable")
    @ApiOperation("查询组织机构为空的警员")
    public ApiRes<List<PolicemanDto>> getPoliceWithoutOrg(){
        return ApiRes.success(policemanService.getPoliceWithoutOrg());
    }


    @PostMapping(path="/alignPoliceInfo",name="同步更新警员信息#enable")
    @ApiOperation("同步更新警员信息")
    public ApiRes<List<PoliceDto>> alignPoliceInfo(@RequestBody PolicemanReq req){
        return ApiRes.success(policemanService.alignPoliceInfo(req));
    }

    @PostMapping(path="/alignUserInfo",name="同步更新用户信息#enable")
    @ApiOperation("同步更新用户信息")
    public ApiRes<List<UserDto>> alignUserInfo(@RequestBody PolicemanReq req){
        return ApiRes.success(policemanService.alignUserInfo(req));
    }

    @PostMapping(path = "/oneClickAccountCreation",name = "一键生成账号（运维，管理，审核）#logType=30")
    @ApiOperation("一键生成账号")
    public ApiRes<Boolean> oneClickAccountCreation(@RequestBody PubOrg pubOrg) throws IOException {
        return ApiRes.success(policemanService.oneClickAccountCreation1(pubOrg));
    }
    @PostMapping(path = "/CreateAccount",name = "一键生成全部账号（运维，管理，审核）#logType=30")
    @ApiOperation("一键生成全部账号")
    public ApiRes<Boolean> CreateAccount( ) throws InterruptedException, IOException {
        return ApiRes.success(policemanService.CreateSystemAccount());
    }

    @PostMapping(path = "/CreateAllUserId",name = "一键生成普通警员账号#logType=30")
    @ApiOperation("/一键生成普通警员账号")
    public ApiRes<Boolean> CreateAllUserId(){
        return ApiRes.success(policemanService.CreateAllUserId());
    }

    /**
     * 修改调准账号相同的数据
     * 解决方法：将相同的数据进行重新划分一个账号给普通警员，每个账号已经存在的人数确保只能为1个（存在系统账号）
     * 需要执行两次：将系统账号的普通警员抽离并且重新赋值普通警员的账号信心
     * 如果flag=true则执行第一种（相同警员抽取分离），flag=false 执行第二种（系统账号抽取分离）
     * @return
     */
    @PostMapping(path = "/changeErrorPoliceUserId",name="修改调准账号相同的数据#logType=30")
    public ApiRes<Boolean> changeErrorPoliceUserId(@RequestBody PolicemanReq req){
        return ApiRes.success(policemanService.changeErrorPoliceUserId(req));
    }

}
