package com.dcsoft.system.controller;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dcsoft.common.core.constant.Constants;
import com.dcsoft.common.core.domain.R;
import com.dcsoft.common.core.enums.CompanyType;
import com.dcsoft.common.core.exception.ServiceException;
import com.dcsoft.common.core.utils.poi.ExcelUtil;
import com.dcsoft.common.core.utils.uuid.IdUtils;
import com.dcsoft.common.core.web.controller.BaseController;
import com.dcsoft.common.core.web.domain.AjaxResult;
import com.dcsoft.common.core.web.page.TableDataInfo;
import com.dcsoft.common.log.annotation.Log;
import com.dcsoft.common.log.enums.BusinessType;
import com.dcsoft.common.security.annotation.RequiresPermissions;
import com.dcsoft.common.security.utils.SecurityUtils;
import com.dcsoft.system.api.RemoteFileService;
import com.dcsoft.system.api.domain.SysFile;
import com.dcsoft.system.api.domain.SysUser;
import com.dcsoft.system.domain.*;
import com.dcsoft.system.domain.vo.JjserInfoVo;
import com.dcsoft.system.service.*;
import com.dcsoft.system.uniubi.domain.Person;
import com.dcsoft.system.uniubi.service.ISysApiService;
import com.dcsoft.system.uniubi.service.ISysSdkService;
import com.dcsoft.system.utils.PictureUtils;
import com.dcsoft.system.visitor.service.IVisitorService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;

/**
 * 人员管理Controller
 *
 * @author nichun
 * @date 2023-03-08
 */
@RestController
@RequestMapping("/sysPeople")
@Slf4j
public class SysPeopleController extends BaseController
{
    @Autowired
    private ISysPeopleService sysPeopleService;

    @Autowired
    private ISysApiService sysApiService;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    protected ISysSdkService sdkService;

    @Autowired
    private ISysEquipmentService equipmentService;

    @Autowired
    private RemoteFileService fileService;

    @Autowired
    protected ISysPeopleEquipmentService peopleEquipmentService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysBranchService sysBranchService;

    @Autowired
    private ISysRuleService sysRuleService;

    @Autowired
    private ISysPeopleRuleService sysPeopleRuleService;

    @Autowired
    private ISysSyncRuleService sysSyncRuleService;

    @Autowired
    private IVisitorService visitorService;

    @Value("${park.tdGuid}")
    public String tdGuid;

    public static ExecutorService service = Executors.newFixedThreadPool(10);


    /**
     * 查询人员管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysPeople sysPeople)
    {
        List<SysPeople> list = sysPeopleService.selectSysPeopleList(sysPeople);
        return getDataTable(list);
    }

    @GetMapping("/queryPeopleList")
    public AjaxResult queryPeopleList(SysPeople sysPeople) {
        List<SysPeople> list = sysPeopleService.queryPeopleList(sysPeople);
        return success(list);
    }

    /**
     * 导出人员管理列表
     */
    @RequiresPermissions("system:sysPeople:export")
    @Log(title = "人员管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysPeople sysPeople)
    {
        List<SysPeople> list = sysPeopleService.selectSysPeopleLists(sysPeople);
        ExcelUtil<SysPeople> util = new ExcelUtil<SysPeople>(SysPeople.class);
        util.exportExcel(response, list, "人员管理数据");
    }

    /**
     * 获取人员管理详细信息
     */
    @RequiresPermissions("system:sysPeople:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysPeopleService.selectSysPeopleById(id));
    }

    /**
     * 新增人员管理
     */
    @RequiresPermissions("system:sysPeople:add")
    @Log(title = "人员管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysPeople sysPeople) throws ExecutionException, InterruptedException {
        SysPeople people = sysPeopleService.selectSysPeopleByGh(sysPeople.getGh());
        if(people != null) {
            return error("该工号已存在");
        }
        if(StringUtils.isNotEmpty(sysPeople.getDoorNo())) {
            SysPeople doorNo = sysPeopleService.selectSysPeopleByDoorNo(sysPeople.getDoorNo());
            if(doorNo != null) {
                return error("该门禁号已存在");
            }
        }

        //判断设备是否上云
        String cloud = configService.selectConfigByKey("sys.equipment.cloud");
        int i=0;
        if("true".equals(cloud)){
            String data=sysApiService.admitCreate(sysPeople);
            JSONObject json= JSONObject.parseObject(data);
            if("1".equals(json.get("result")+"")){
                JSONObject obj= (JSONObject) json.get("data");
                String admitGuid=obj.get("admitGuid")+"";
                sysPeople.setGuid(admitGuid);
                //人像注册，如果注册失败将人像置空
                String datas=sysApiService.faceRegister(sysPeople);
                JSONObject jsons= JSONObject.parseObject(datas);
                if("1".equals(jsons.get("result")+"")){
                    JSONObject objs= (JSONObject) jsons.get("data");
                    String faceGuid=objs.get("faceGuid")+"";
                    sysPeople.setFaceGuid(faceGuid);
                }
            }else{
                return error(json.get("msg")+"");
            }
            i=sysPeopleService.insertSysPeople(sysPeople);
        }else{
            //注册设备进行注册
            if(StringUtils.isNotEmpty(sysPeople.getAvatar())) {//是否上传头像，检测照片是否合格
                SysEquipment equipment=new SysEquipment();
                equipment.setIsCj("1");
                equipment.setFlag("0");
                List<SysEquipment> list=equipmentService.selectSysEquipmentList(equipment);
                if(list.size()>0){
                    //人员是否注册
                    String ip=list.get(0).getIp();
                    String pass=list.get(0).getPassword();
                    if(StringUtils.isEmpty(sysPeople.getGuid())){
                        Person person=new Person();
                        person.setName(sysPeople.getName());
                        person.setiDNumber(sysPeople.getIdcard());
                        person.setPhone(sysPeople.getPhone());
                        String data=sdkService.personCreate(person,ip,pass);
                        JSONObject json= JSONObject.parseObject(data);
                        if("1".equals(json.get("result")+"")) {
                            JSONObject obj= (JSONObject) json.get("data");
                            String admitGuid=obj.get("id")+"";
                            sysPeople.setGuid(admitGuid);
                        }else{
                            return error(json.get("msg")+"");
                        }
                    }else{//更新人员信息
                        sdkService.personDelete(sysPeople.getGuid(),ip,pass);
                        Person person=new Person();
                        person.setId(sysPeople.getGuid());
                        person.setName(sysPeople.getName());
                        person.setiDNumber(sysPeople.getIdcard());
                        person.setPhone(sysPeople.getPhone());
                        String data=sdkService.personCreate(person,ip,pass);
                        JSONObject json= JSONObject.parseObject(data);
                        if("1".equals(json.get("result")+"")) {
                        }else{
                            return error(json.get("msg")+"");
                        }
                    }
                    //人像是否注册
                    String datas = sdkService.imageCreateUrl(sysPeople.getGuid(), sysPeople.getAvatar(), ip, pass);
                    JSONObject jsons = JSONObject.parseObject(datas);
                    if ("1".equals(jsons.get("result")+"")) {
                        if(jsons.get("data")!=null){
                            String faceGuid = jsons.get("data")+"";
                            sysPeople.setFaceGuid(faceGuid);
                        }
                    }else{
                        return error(jsons.get("msg")+"");
                    }
                    //照片检测后删除注册设备人员
                    sdkService.personDelete(sysPeople.getGuid(),ip,pass);
                }else{
                    Long userId=SecurityUtils.getUserId();
                    SysUser user=userService.selectUserById(userId);
                    if(user.getEquipmentId()!=null){
                        SysEquipment equipments = equipmentService.selectSysEquipmentById(user.getEquipmentId());
                        String ip = equipments.getIp();
                        String pass = equipments.getPassword();
                        String flag = equipments.getFlag();
                        if("0".equals(flag)){
                            Person person=new Person();
                            person.setName(sysPeople.getName());
                            person.setiDNumber(sysPeople.getIdcard());
                            person.setPhone(sysPeople.getPhone());
                            String data=sdkService.personCreate(person,ip,pass);
                            JSONObject json= JSONObject.parseObject(data);
                            if("1".equals(json.get("result")+"")) {
                                JSONObject obj= (JSONObject) json.get("data");
                                String admitGuid=obj.get("id")+"";
                                sysPeople.setGuid(admitGuid);
                            }else{
                                return error(json.get("msg")+"");
                            }
                            String datas = sdkService.imageCreateUrl(sysPeople.getGuid(), sysPeople.getAvatar(), ip, pass);
                            JSONObject jsons = JSONObject.parseObject(datas);
                            if ("1".equals(jsons.get("result")+"")) {
                                if(jsons.get("data")!=null){
                                    String faceGuid = jsons.get("data")+"";
                                    sysPeople.setFaceGuid(faceGuid);
                                }
                            }else{
                                return error(jsons.get("msg")+"");
                            }
                            //照片检测后删除注册设备人员
                            sdkService.personDelete(sysPeople.getGuid(),ip,pass);
                        }else {
                            return error("默认设备设备已离线");
                        }
                    }
                }
            }else{
                sysPeople.setGuid(IdUtils.simpleUUID());
            }

            //下发设备
            if(sysPeople.getBranchId()!=null){
                SysBranch branch=sysBranchService.selectSysBranchById(sysPeople.getBranchId());
                if (branch.getRuleId()!= null) {
                    //添加人员授权信息
                    SysRule rule=sysRuleService.selectSysRuleById(branch.getRuleId());
                    String data=sdkService.authDeviceNew(rule,sysPeople);
                    if("".equals(data)){
                        return AjaxResult.error("该规则未绑定设备!");
                    }
                    sysPeople.setDown(1L);
                }
            }

            i=sysPeopleService.insertSysPeople(sysPeople);

        }
        return toAjax(i);
    }


    /**
     * 修改人员管理
     */
    @RequiresPermissions("system:sysPeople:edit")
    @Log(title = "人员管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysPeople sysPeople)
    {
        //判断设备是否上云
        String cloud = configService.selectConfigByKey("sys.equipment.cloud");
        if("true".equals(cloud)) {
            if (StringUtils.isEmpty(sysPeople.getGuid())) {
                    String data = sysApiService.admitUpdate(sysPeople);
                    JSONObject json = JSONObject.parseObject(data);
                    if ("1".equals(json.get("result") + "")) {
                        //人像注册，如果注册失败将人像置空
                        if (StringUtils.isEmpty(sysPeople.getFaceGuid())) {
                            String datas = sysApiService.faceRegister(sysPeople);
                            JSONObject jsons = JSONObject.parseObject(datas);
                            if ("1".equals(jsons.get("result") + "")) {
                                JSONObject objs = (JSONObject) jsons.get("data");
                                String faceGuid = objs.get("faceGuid") + "";
                                sysPeople.setFaceGuid(faceGuid);
                            }
                        }
                    } else {
                        return error(json.get("msg") + "");
                    }
            }else {
                //人像注册，如果注册失败将人像置空
                if (StringUtils.isEmpty(sysPeople.getFaceGuid())) {
                    String datas = sysApiService.faceRegister(sysPeople);
                    JSONObject jsons = JSONObject.parseObject(datas);
                    if ("1".equals(jsons.get("result") + "")) {
                        JSONObject objs = (JSONObject) jsons.get("data");
                        String faceGuid = objs.get("faceGuid") + "";
                        sysPeople.setFaceGuid(faceGuid);
                    }
                }
            }
        }else{
            //注册设备进行注册
            if(StringUtils.isNotEmpty(sysPeople.getAvatar())) {//是否上传头像，检测照片是否合格
                SysEquipment equipment=new SysEquipment();
                equipment.setIsCj("1");
                equipment.setFlag("0");
                List<SysEquipment> list=equipmentService.selectSysEquipmentList(equipment);
                if(list.size()>0){
                    //人员是否注册
                    String ip=list.get(0).getIp();
                    String pass=list.get(0).getPassword();
                    if(StringUtils.isEmpty(sysPeople.getGuid())){
                        Person person=new Person();
                        person.setName(sysPeople.getName());
                        person.setiDNumber(sysPeople.getIdcard());
                        person.setPhone(sysPeople.getPhone());
                        String data=sdkService.personCreate(person,ip,pass);
                        JSONObject json= JSONObject.parseObject(data);
                        log.info("人员修改-personCreate-result:{}", json.toJSONString());
                        if("1".equals(json.get("result")+"")) {
                            JSONObject obj= (JSONObject) json.get("data");
                            String admitGuid=obj.get("id")+"";
                            sysPeople.setGuid(admitGuid);
                        }else{
                            return error(json.get("msg")+"");
                        }
                    }else{//更新人员信息
                        sdkService.personDelete(sysPeople.getGuid(),ip,pass);
                        Person person=new Person();
                        person.setId(sysPeople.getGuid());
                        person.setName(sysPeople.getName());
                        person.setiDNumber(sysPeople.getIdcard());
                        person.setPhone(sysPeople.getPhone());
                        String data=sdkService.personCreate(person,ip,pass);
                        JSONObject json= JSONObject.parseObject(data);
                        log.info("人员修改-personCreate2-result:{}", json.toJSONString());
                        if("1".equals(json.get("result")+"")) {
                        }else{
                            return error(json.get("msg")+"");
                        }
                    }

                    //人像是否注册
                    String datas = sdkService.imageCreateUrl(sysPeople.getGuid(), sysPeople.getAvatar(), ip, pass);
                    JSONObject jsons = JSONObject.parseObject(datas);
                    log.info("人员修改-imageCreateUrl-result:{}", jsons.toJSONString());
                    if ("1".equals(jsons.get("result")+"")) {
                        if(jsons.get("data")!=null){
                            String faceGuid = jsons.get("data")+"";
                            sysPeople.setFaceGuid(faceGuid);
                        }
                    }else{
                        return error(jsons.get("msg")+"");
                    }
                    //照片检测后删除注册设备人员
                    sdkService.personDelete(sysPeople.getGuid(),ip,pass);
                }else{
                    Long userId=SecurityUtils.getUserId();
                    SysUser user=userService.selectUserById(userId);
                    if(user.getEquipmentId()!=null){
                        SysEquipment equipments = equipmentService.selectSysEquipmentById(user.getEquipmentId());
                        String ip = equipments.getIp();
                        String pass = equipments.getPassword();
                        String flag = equipments.getFlag();
                        if("0".equals(flag)){
                            if(StringUtils.isEmpty(sysPeople.getGuid())){
                                Person person=new Person();
                                person.setName(sysPeople.getName());
                                person.setiDNumber(sysPeople.getIdcard());
                                person.setPhone(sysPeople.getPhone());
                                String data=sdkService.personCreate(person,ip,pass);
                                JSONObject json= JSONObject.parseObject(data);
                                if("1".equals(json.get("result")+"")) {
                                    JSONObject obj= (JSONObject) json.get("data");
                                    String admitGuid=obj.get("id")+"";
                                    sysPeople.setGuid(admitGuid);
                                }else{
                                    return error(json.get("msg")+"");
                                }
                                if(StringUtils.isNotEmpty(sysPeople.getAvatar())){
                                    String datas = sdkService.imageCreateUrl(sysPeople.getGuid(), sysPeople.getAvatar(), ip, pass);
                                    JSONObject jsons = JSONObject.parseObject(datas);
                                    if ("true".equals(jsons.get("success") + "")) {
                                        if(jsons.get("data")!=null){
                                            String faceGuid = jsons.get("data")+"";
                                            sysPeople.setFaceGuid(faceGuid);
                                        }
                                    }else{
                                        return error(jsons.get("msg")+"");
                                    }
                                }
                                //照片检测后删除注册设备人员
                                sdkService.personDelete(sysPeople.getGuid(),ip,pass);
                            }else{
                                if(StringUtils.isEmpty(sysPeople.getFaceGuid())){
                                    String datas = sdkService.imageCreateUrl(sysPeople.getGuid(), sysPeople.getAvatar(), ip, pass);
                                    JSONObject jsons = JSONObject.parseObject(datas);
                                    logger.info("进入无jsons==============="+jsons);
                                    if ("true".equals(jsons.get("success") + "")) {
                                        if(jsons.get("data")!=null){
                                            String faceGuid = jsons.get("data")+"";
                                            logger.info("进入无faceguid==============="+faceGuid);
                                            sysPeople.setFaceGuid(faceGuid);
                                        }
                                    }
                                    //照片检测后删除注册设备人员
                                    sdkService.personDelete(sysPeople.getGuid(),ip,pass);
                                }
                            }

                        }else {
                            return error("默认设备设备已离线");
                        }
                    }
                }
            }
        }
        //下发设备
        if(sysPeople.getBranchId()!=null){
            SysBranch branch=sysBranchService.selectSysBranchById(sysPeople.getBranchId());
            if (branch.getRuleId()!= null) {
                 //添加人员授权信息
                SysRule rule=sysRuleService.selectSysRuleById(branch.getRuleId());
                String data=sdkService.authDeviceNew(rule,sysPeople);
                log.info("人员修改-authDeviceNew-result:{}", data);
                sysPeople.setDown(1L);
                if("".equals(data)){
                    return AjaxResult.error("该规则未绑定设备!");
                }
            }
        }

        SysPeople peopleInfo = sysPeopleService.selectSysPeopleById(sysPeople.getId());
        return toAjax(sysPeopleService.updateSysPeople(sysPeople));
    }



    @Log(title = "手机端人员管理", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public AjaxResult update(@RequestBody SysPeople sysPeople)
    {
        //判断设备是否上云
        String cloud = configService.selectConfigByKey("sys.equipment.cloud");
        if("true".equals(cloud)) {
            if (StringUtils.isEmpty(sysPeople.getGuid())) {
                String data = sysApiService.admitUpdate(sysPeople);
                JSONObject json = JSONObject.parseObject(data);
                if ("1".equals(json.get("result") + "")) {
                    //人像注册，如果注册失败将人像置空
                    if (StringUtils.isEmpty(sysPeople.getFaceGuid())) {
                        String datas = sysApiService.faceRegister(sysPeople);
                        JSONObject jsons = JSONObject.parseObject(datas);
                        if ("1".equals(jsons.get("result") + "")) {
                            JSONObject objs = (JSONObject) jsons.get("data");
                            String faceGuid = objs.get("faceGuid") + "";
                            sysPeople.setFaceGuid(faceGuid);
                        }
                    }
                } else {
                    return error(json.get("msg") + "");
                }
            }else {
                //人像注册，如果注册失败将人像置空
                if (StringUtils.isEmpty(sysPeople.getFaceGuid())) {
                    String datas = sysApiService.faceRegister(sysPeople);
                    JSONObject jsons = JSONObject.parseObject(datas);
                    if ("true".equals(jsons.get("success") + "")) {
                        JSONObject objs = (JSONObject) jsons.get("data");
                        String faceGuid = objs.get("faceGuid") + "";
                        sysPeople.setFaceGuid(faceGuid);
                    }else{
                        error(jsons.get("msg")+"");
                    }
                }
            }
        }else{
            //注册设备进行注册
            if(StringUtils.isNotEmpty(sysPeople.getAvatar())) {//是否上传头像，检测照片是否合格
                SysEquipment equipment=new SysEquipment();
                equipment.setIsCj("1");
                equipment.setFlag("0");
                List<SysEquipment> list=equipmentService.selectSysEquipmentList(equipment);
                if(list.size()>0){
                    //人员是否注册
                    String ip=list.get(0).getIp();
                    String pass=list.get(0).getPassword();
                    if(StringUtils.isEmpty(sysPeople.getGuid())){
                        Person person=new Person();
                        person.setName(sysPeople.getName());
                        person.setiDNumber(sysPeople.getIdcard());
                        person.setPhone(sysPeople.getPhone());
                        String data=sdkService.personCreate(person,ip,pass);
                        JSONObject json= JSONObject.parseObject(data);
                        if("1".equals(json.get("result")+"")) {
                            JSONObject obj= (JSONObject) json.get("data");
                            String admitGuid=obj.get("id")+"";
                            sysPeople.setGuid(admitGuid);
                        }else{
                            return error(json.get("msg")+"");
                        }
                    }else{//更新人员信息
                        sdkService.personDelete(sysPeople.getGuid(),ip,pass);
                        Person person=new Person();
                        person.setId(sysPeople.getGuid());
                        person.setName(sysPeople.getName());
                        person.setiDNumber(sysPeople.getIdcard());
                        person.setPhone(sysPeople.getPhone());
                        String data=sdkService.personCreate(person,ip,pass);
                        JSONObject json= JSONObject.parseObject(data);
                        if("1".equals(json.get("result")+"")) {
                        }else{
                            return error(json.get("msg")+"");
                        }
                    }

                    //查看是否有底图，有底图就注册
                    SysPeople sysPeoples=sysPeopleService.selectSysPeopleById(sysPeople.getId());
                    if(StringUtils.isNotEmpty(sysPeoples.getAvatar())){
                        sdkService.imageCreateUrl(sysPeople.getGuid(), sysPeoples.getAvatar(), ip, pass);
                    }

                    //人像是否注册
                    String datas = sdkService.imageCreateUrl(sysPeople.getGuid(), sysPeople.getAvatar(), ip, pass);
                    JSONObject jsons = JSONObject.parseObject(datas);
                    if ("true".equals(jsons.get("success")+"")) {
                        if(jsons.get("data")!=null){
                            String faceGuid = jsons.get("data")+"";
                            sysPeople.setFaceGuid(faceGuid);
                            //照片检测后删除注册设备人员
                            sdkService.personDelete(sysPeople.getGuid(),ip,pass);
                        }
                    }else{
                        //照片检测后删除注册设备人员
                        sdkService.personDelete(sysPeople.getGuid(),ip,pass);
                        return error(jsons.get("msg")+"");
                    }
                }
            }
        }
        //下发设备
        if(sysPeople.getBranchId()!=null){
            SysBranch branch=sysBranchService.selectSysBranchById(sysPeople.getBranchId());
            if (branch.getRuleId()!= null) {
                //添加人员授权信息
                SysRule rule=sysRuleService.selectSysRuleById(branch.getRuleId());
                String data=sdkService.authDeviceNew(rule,sysPeople);
                sysPeople.setDown(1L);
                if("".equals(data)){
                    return AjaxResult.error("该规则未绑定设备!");
                }
            }
        }
        return toAjax(sysPeopleService.updateSysPeople(sysPeople));
    }


    /**
     * 删除人员管理
     */
    @RequiresPermissions("system:sysPeople:remove")
    @Log(title = "人员管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        String cloud = configService.selectConfigByKey("sys.equipment.cloud");
        if("true".equals(cloud)) {
            String admitGuids = "";
            for (Long id : ids) {
                SysPeople people = sysPeopleService.selectSysPeopleById(id);
                admitGuids += people.getGuid() + ",";
            }
            if (!"".equals(admitGuids)) {
                admitGuids = admitGuids.substring(0, admitGuids.length() - 1);
                if (!"null".equals(admitGuids)) {
                    String data = sysApiService.admitDelete(admitGuids);
                    JSONObject json = JSONObject.parseObject(data);
                    if ("1".equals(json.get("result") + "")) {
                    } else {
                        // return error(json.get("msg")+"");
                    }
                }
            }
        }else{
            String admitGuids = "";
            for (Long id : ids) {
                SysPeople people = sysPeopleService.selectSysPeopleById(id);
                admitGuids += people.getGuid() + ",";
            }
            if (!"".equals(admitGuids)) {
                admitGuids = admitGuids.substring(0, admitGuids.length() - 1);
                if (!"null".equals(admitGuids)) {
                    SysEquipment equipment=new SysEquipment();
                    equipment.setFlag("0");
                    equipment.setProductId(3L);
                    List<SysEquipment> list = equipmentService.selectSysEquipmentList(equipment);
                    for(SysEquipment e:list){
                        sdkService.personDelete(admitGuids,e.getIp(),e.getPassword());
                    }
                }
            }
        }
        //暂未处理离线设备人员数据删除
        return toAjax(sysPeopleService.deleteSysPeopleByIds(ids));
    }

    /**
     * 获取人员管理详细信息
     */
    @GetMapping(value = "/getUser/{userId}")
    public AjaxResult getUser(@PathVariable("userId") Long userId)
    {
        return success(sysPeopleService.selectSysPeopleByUserId(userId));
    }

    /**
     * 获取人员管理详细信息
     */
    @GetMapping(value = "/queryPeopleById")
    public AjaxResult queryPeopleById(String id)
    {
        return success(sysPeopleService.queryPeopleById(id));
    }

    /**
     * 获取人员管理详细信息
     */
    @GetMapping(value = "/getUserName/{branchName}/{userName}")
    public AjaxResult getUserName(@PathVariable("branchName") String branchName,@PathVariable("userName") String userName)
    {
        return success(sysPeopleService.selectSysPeopleByBranchUser(branchName,userName));
    }

    /**
     * 指纹采集
     */
    @PutMapping(value = "/dactylogram")
    public AjaxResult dactylogram(@RequestBody SysPeople sysPeople) {
        SysEquipment equipment=new SysEquipment();
        equipment.setIsCj("1");
        List<SysEquipment> list = equipmentService.selectSysEquipmentList(equipment);
        if (list.size() > 0) {
            String ip = list.get(0).getIp();
            String pass = list.get(0).getPassword();
            String flag = list.get(0).getFlag();
            if("0".equals(flag)){
                if (StringUtils.isEmpty(sysPeople.getGuid())) {
                    //判断设备人员是否能注册上设备后再删除掉
                    Person person = new Person();
                    person.setName(sysPeople.getName());
                    person.setiDNumber(sysPeople.getIdcard());
                    person.setPhone(sysPeople.getPhone());
                    String data = sdkService.personCreate(person, ip, pass);
                    JSONObject json = JSONObject.parseObject(data);
                    if ("1".equals(json.get("result") + "")) {
                        JSONObject obj = (JSONObject) json.get("data");
                        String admitGuid = obj.get("id") + "";
                        sysPeople.setGuid(admitGuid);
                        //注册指纹
                        String datas = sdkService.fingerRegist(admitGuid, ip, pass);
                    } else {
                        return error(json.get("msg") + "");
                    }
                }else {//只注册指纹
                    String data = sdkService.fingerRegist(sysPeople.getGuid(), ip, pass);
                }
            }else {
                return error("采集设备已离线");
            }
        }

        return success(sysPeople);
    }

    /**
     * 人脸采集
     */
    @PutMapping(value = "/faceCollect")
    public AjaxResult faceCollect(@RequestBody SysPeople sysPeople) {
        //查询当前用户默认的采集端
        Long userId=SecurityUtils.getUserId();
        SysUser user=userService.selectUserById(userId);
        if(user.getEquipmentId()!=null){
            SysEquipment equipment = equipmentService.selectSysEquipmentById(user.getEquipmentId());
            String ip = equipment.getIp();
            String pass = equipment.getPassword();
            String flag = equipment.getFlag();
            if("0".equals(flag)){
                if (StringUtils.isEmpty(sysPeople.getGuid())) {
                    //判断设备人员是否能注册上设备后再删除掉
                    Person person = new Person();
                    person.setName(sysPeople.getName());
                    person.setiDNumber(sysPeople.getIdcard());
                    person.setPhone(sysPeople.getPhone());
                    String data = sdkService.personCreate(person, ip, pass);
                    JSONObject json = JSONObject.parseObject(data);
                    if ("1".equals(json.get("result") + "")) {
                        JSONObject obj = (JSONObject) json.get("data");
                        String admitGuid = obj.get("id") + "";
                        sysPeople.setGuid(admitGuid);
                        //注册人脸
                        String datas = sdkService.takeImg(admitGuid, ip, pass);
                    } else {
                        return error(json.get("msg") + "");
                    }
                }else {//只注册人脸
                    //删除采集设备已有人脸
                    Person person = new Person();
                    person.setName(sysPeople.getName());
                    person.setiDNumber(sysPeople.getIdcard());
                    person.setPhone(sysPeople.getPhone());
                    person.setId(sysPeople.getGuid());
                    sdkService.personCreate(person, ip, pass);
                    try{
                        SysPeople people = sysPeopleService.selectByGuid(sysPeople);
                        if(StringUtils.isNotEmpty(people.getFaceGuid())){
                            sdkService.imageDeletel(people.getFaceGuid(),ip,pass);
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }

                    String data = sdkService.takeImg(sysPeople.getGuid(), ip, pass);
                }
            }else {
                return error("采集设备已离线");
            }
        }else{
            SysEquipment equipment=new SysEquipment();
            equipment.setIsCj("1");
            List<SysEquipment> list = equipmentService.selectSysEquipmentList(equipment);
            if (list.size() > 0) {
                String ip = list.get(0).getIp();
                String pass = list.get(0).getPassword();
                String flag = list.get(0).getFlag();
                if("0".equals(flag)){
                    if (StringUtils.isEmpty(sysPeople.getGuid())) {
                        //判断设备人员是否能注册上设备后再删除掉
                        Person person = new Person();
                        person.setName(sysPeople.getName());
                        person.setiDNumber(sysPeople.getIdcard());
                        person.setPhone(sysPeople.getPhone());
                        String data = sdkService.personCreate(person, ip, pass);
                        JSONObject json = JSONObject.parseObject(data);
                        if ("1".equals(json.get("result") + "")) {
                            JSONObject obj = (JSONObject) json.get("data");
                            String admitGuid = obj.get("id") + "";
                            sysPeople.setGuid(admitGuid);
                            //注册人脸
                            String datas = sdkService.takeImg(admitGuid, ip, pass);
                        } else {
                            return error(json.get("msg") + "");
                        }
                    }else {//只注册人脸
                        //删除采集设备已有人脸
                        try{
                            SysPeople people = sysPeopleService.selectByGuid(sysPeople);
                            sdkService.imageDeletel(people.getFaceGuid(),ip,pass);
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                        String data = sdkService.takeImg(sysPeople.getGuid(), ip, pass);
                    }
                }else {
                    return error("采集设备已离线");
                }
            }
        }


        return success(sysPeople);
    }

    /**
     * 注册卡号
     */
    @PutMapping(value = "/icCardRegist")
    public AjaxResult icCardRegist(@RequestBody SysPeople sysPeople) {
        Long userId=SecurityUtils.getUserId();
        SysUser user=userService.selectUserById(userId);
        if(user.getEquipmentId()!=null){
            SysEquipment equipment = equipmentService.selectSysEquipmentById(user.getEquipmentId());
            String ip = equipment.getIp();
            String pass = equipment.getPassword();
            String flag = equipment.getFlag();
            if("0".equals(flag)){
                if (StringUtils.isEmpty(sysPeople.getGuid())) {
                    //判断设备人员是否能注册上设备后再删除掉
                    Person person = new Person();
                    person.setName(sysPeople.getName());
                    person.setiDNumber(sysPeople.getIdcard());
                    person.setPhone(sysPeople.getPhone());
                    String data = sdkService.personCreate(person, ip, pass);
                    JSONObject json = JSONObject.parseObject(data);
                    if ("1".equals(json.get("result") + "")) {
                        JSONObject obj = (JSONObject) json.get("data");
                        String admitGuid = obj.get("id") + "";
                        sysPeople.setGuid(admitGuid);
                        //注册门卡
                        String datas = sdkService.icCardRegist(admitGuid, ip, pass);
                    } else {
                        return error(json.get("msg") + "");
                    }
                }else {//注册门卡
                    sdkService.personDelete(sysPeople.getGuid(), ip, pass);
                    Person person = new Person();
                    person.setName(sysPeople.getName());
                    person.setiDNumber(sysPeople.getIdcard());
                    person.setPhone(sysPeople.getPhone());
                    person.setId(sysPeople.getGuid());
                    person.setIdcardNum("");
                    String data = sdkService.personCreate(person, ip, pass);
                    JSONObject json = JSONObject.parseObject(data);
                    if ("1".equals(json.get("result") + "")) {
                        JSONObject obj = (JSONObject) json.get("data");
                        String admitGuid = obj.get("id") + "";
                        sysPeople.setGuid(admitGuid);
                        //注册门卡
                        String datas = sdkService.icCardRegist(admitGuid, ip, pass);
                    }
                }
            }else {
                return error("采集设备已离线");
            }
        }else{
            SysEquipment equipment=new SysEquipment();
            equipment.setIsCj("1");
            List<SysEquipment> list = equipmentService.selectSysEquipmentList(equipment);
            if (list.size() > 0) {
                String ip = list.get(0).getIp();
                String pass = list.get(0).getPassword();
                String flag = list.get(0).getFlag();
                if("0".equals(flag)){
                    if (StringUtils.isEmpty(sysPeople.getGuid())) {
                        //判断设备人员是否能注册上设备后再删除掉
                        Person person = new Person();
                        person.setName(sysPeople.getName());
                        person.setiDNumber(sysPeople.getIdcard());
                        person.setPhone(sysPeople.getPhone());
                        String data = sdkService.personCreate(person, ip, pass);
                        JSONObject json = JSONObject.parseObject(data);
                        if ("1".equals(json.get("result") + "")) {
                            JSONObject obj = (JSONObject) json.get("data");
                            String admitGuid = obj.get("id") + "";
                            sysPeople.setGuid(admitGuid);
                            //注册门卡
                            String datas = sdkService.icCardRegist(admitGuid, ip, pass);
                        } else {
                            return error(json.get("msg") + "");
                        }
                    }else {//注册门卡
                        //删除门卡
                        Person person = new Person();
                        person.setName(sysPeople.getName());
                        person.setiDNumber(sysPeople.getIdcard());
                        person.setPhone(sysPeople.getPhone());
                        person.setId(sysPeople.getGuid());
                        person.setIdcardNum("");
                        sdkService.personCreate(person, ip, pass);

                        String data = sdkService.icCardRegist(sysPeople.getGuid(), ip, pass);
                    }
                }else {
                    return error("采集设备已离线");
                }
            }
        }
        return success(sysPeople);
    }

    /**
     * 人脸采集
     */
    @PutMapping(value = "/featureInfo")
    public AjaxResult featureInfo(@RequestBody SysPeople sysPeople) {
        SysEquipment equipment=new SysEquipment();
        equipment.setIsCj("1");
        List<SysEquipment> list = equipmentService.selectSysEquipmentList(equipment);
        try{
            if (list.size() > 0) {
                String ip = list.get(0).getIp();
                String pass = list.get(0).getPassword();
                //指纹查询
                String data = sdkService.featureInfo(sysPeople.getGuid(), ip, pass);
                JSONObject json = JSONObject.parseObject(data);
                if (StringUtils.isNotEmpty(json.get("data") + "")) {
                    JSONArray obj = (JSONArray) json.get("data");
                    if(obj!=null){
                        if(obj.get(0)!=null){
                            JSONObject objs = (JSONObject) obj.get(0);
                            String feature=objs.get("feature")+"";
                            sysPeople.setFingerprint(feature);
                        }
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return success(sysPeople);
    }



    /*
       * 人脸获取
     */
    @PutMapping(value = "/faceFind")
    public AjaxResult faceFind(@RequestBody SysPeople sysPeople) {
        Long userId=SecurityUtils.getUserId();
        SysUser user=userService.selectUserById(userId);
        if(user.getEquipmentId()!=null){
            SysEquipment equipment = equipmentService.selectSysEquipmentById(user.getEquipmentId());
            String ip = equipment.getIp();
            String pass = equipment.getPassword();
            //人脸查询
            String data = sdkService.faceFind(sysPeople.getGuid(), ip, pass);
            JSONObject json = JSONObject.parseObject(data);
            if (StringUtils.isNotEmpty(json.get("data")+"")) {
                JSONArray obj = (JSONArray) json.get("data");
                if(obj!=null){
                    if(StringUtils.isNotEmpty(obj.get(0)+"")){
                        JSONObject objs = (JSONObject) obj.get(0);
                        String path=objs.get("path")+"";
                        String facleId=objs.get("faceId")+"";
                        if(StringUtils.isNotEmpty(path)){
                            //String paths=getImg(path,ip,pass);
                            //http://192.168.1.30:8090/download/image?pass=123123aa&filename=/data/record/RegisterPhoto//2023-07-28/17/173420_310.jpg
                            String urls="http://"+ip+":8090/download/image?pass="+pass+"&filename="+path;
                            MultipartFile file= PictureUtils.createMultipartFile(urls,facleId+".jpg");
                            R<SysFile> datas= fileService.uploadMinio(file,facleId);
                            sysPeople.setAvatar(datas.getData().getUrl());
                            sysPeople.setFaceGuid(facleId);
                        }
                    }
                }
            }
        }else{
            SysEquipment equipment=new SysEquipment();
            equipment.setIsCj("1");
            List<SysEquipment> list = equipmentService.selectSysEquipmentList(equipment);
            try{
                if (list.size() > 0) {
                    String ip = list.get(0).getIp();
                    String pass = list.get(0).getPassword();
                    //人脸查询
                    String data = sdkService.faceFind(sysPeople.getGuid(), ip, pass);
                    JSONObject json = JSONObject.parseObject(data);
                    if (StringUtils.isNotEmpty(json.get("data")+"")) {
                        JSONArray obj = (JSONArray) json.get("data");
                        if(obj!=null){
                            if(StringUtils.isNotEmpty(obj.get(0)+"")){
                                JSONObject objs = (JSONObject) obj.get(0);
                                String path=objs.get("path")+"";
                                String facleId=objs.get("faceId")+"";
                                if(StringUtils.isNotEmpty(path)){
                                    //String paths=getImg(path,ip,pass);
                                    //http://192.168.1.30:8090/download/image?pass=123123aa&filename=/data/record/RegisterPhoto//2023-07-28/17/173420_310.jpg
                                    String urls="http://"+ip+":8090/download/image?pass="+pass+"&filename="+path;
                                    MultipartFile file= PictureUtils.createMultipartFile(urls,facleId+".jpg");
                                    R<SysFile> datas= fileService.uploadMinio(file,facleId);
                                    sysPeople.setAvatar(datas.getData().getUrl());
                                    sysPeople.setFaceGuid(facleId);
                                }
                            }
                        }
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return success(sysPeople);
    }

    /*
     * 人脸获取
     */
    @PutMapping(value = "/personFind")
    public AjaxResult personFind(@RequestBody SysPeople sysPeople) {
        Long userId=SecurityUtils.getUserId();
        SysUser user=userService.selectUserById(userId);
        if(user.getEquipmentId()!=null){
            SysEquipment equipment = equipmentService.selectSysEquipmentById(user.getEquipmentId());
            String ip = equipment.getIp();
            String pass = equipment.getPassword();
            //人脸查询
            String data = sdkService.personFind(sysPeople.getGuid(), ip, pass);
            JSONObject json = JSONObject.parseObject(data);
            if ("1".equals(json.get("result") + "")) {
                JSONArray obj = (JSONArray) json.get("data");
                if(obj!=null){
                    if(StringUtils.isNotEmpty(obj.get(0)+"")){
                        JSONObject objs = (JSONObject) obj.get(0);
                        String idcardNum=objs.get("idcardNum")+"";
                        sysPeople.setDoorNo(idcardNum.replaceFirst("^0*",""));
                    }
                }
            }
        }else{
            SysEquipment equipment=new SysEquipment();
            equipment.setIsCj("1");
            List<SysEquipment> list = equipmentService.selectSysEquipmentList(equipment);
            try{
                if (list.size() > 0) {
                    String ip = list.get(0).getIp();
                    String pass = list.get(0).getPassword();
                    //人脸查询
                    String data = sdkService.personFind(sysPeople.getGuid(), ip, pass);
                    JSONObject json = JSONObject.parseObject(data);
                    if ("1".equals(json.get("result") + "")) {
                        JSONArray obj = (JSONArray) json.get("data");
                        if(obj!=null){
                            if(StringUtils.isNotEmpty(obj.get(0)+"")){
                                JSONObject objs = (JSONObject) obj.get(0);
                                String idcardNum=objs.get("idcardNum")+"";
                                sysPeople.setDoorNo(idcardNum.replaceFirst("^0*",""));
                            }
                        }
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }


        return success(sysPeople);
    }


    @GetMapping("/down/{ids}")
    public AjaxResult down(@PathVariable Long[] ids)
    {

        SysEquipment equipment=new SysEquipment();
        equipment.setFlag("0");
        List<SysEquipment> list = equipmentService.selectSysEquipmentList(equipment);
        if(list.size()>0){
            for(Long id:ids){//下发到设备上
                SysPeople people=sysPeopleService.selectSysPeopleById(id);
                //下发设备
                if(people.getBranchId()!=null){
                    SysBranch branch=sysBranchService.selectSysBranchById(people.getBranchId());
                    if (branch.getRuleId()!= null) {
                        //添加人员授权信息
                        SysRule rule=sysRuleService.selectSysRuleById(branch.getRuleId());
                        String data=sdkService.authDeviceNew(rule,people);
                        people.setDown(1L);
                        if("".equals(data)){
                            return AjaxResult.error("该规则未绑定设备!");
                        }
                    }
                }
                sysPeopleService.updateSysPeople(people);
            }
        }
        return toAjax(1);
    }


    @Log(title = "人员信息", businessType = BusinessType.IMPORT)
    @RequiresPermissions("system:sysPeople:import")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<SysPeople> util = new ExcelUtil<SysPeople>(SysPeople.class);
        List<SysPeople> userList = util.importExcel(file.getInputStream());
        String operName = SecurityUtils.getUsername();
        String message = sysPeopleService.importPeople(userList, updateSupport, operName);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException
    {
        ExcelUtil<SysPeople> util = new ExcelUtil<SysPeople>(SysPeople.class);
        util.importTemplateExcel(response, "人员信息数据");
    }

    /**
     * 定时批量下发
     */
    @PostMapping("/batchDistribute")
    public void batchDistribute() {
        List<SysPeople> ids = sysPeopleService.queryPeopleInfo();
        if(!CollectionUtils.isEmpty(ids)) {
            Long[] array = (Long[]) ids.stream().map(SysPeople::getId).toArray();
            down(array);
        }
    }


    /**
     * 同步人员（锦江）
     */
    @PostMapping("/saveUserInfo")
    public void saveUserInfo(SysBranch space) {
        HttpResponse response;
        if(Constants.ZERO.equals(space.getType())) {
            response = HttpUtil.createGet("http://192.168.255.135:5566/api/ulYcRBiE/dhr/getAccount")
                    .header("access_token", "f23c34a3200f1994d85de4daf1f15d")
                    .execute();
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            response = HttpUtil.createGet("http://192.168.255.135:5566/api/ulYcRBiE/dhr/getAccount?ts=" + sdf.format(new Date()))
                    .header("access_token", "f23c34a3200f1994d85de4daf1f15d")
                    .execute();
        }
        String body = response.body();
        JSONObject object = JSONObject.parseObject(body);
        if(!Constants.CODE.equals(object.get("code").toString())) {
            throw new ServiceException("人员接口请求异常");
        }
        List<JjserInfoVo> data = JSONObject.parseArray(object.get("data").toString(), JjserInfoVo.class);
        if(CollectionUtils.isEmpty(data)) {
            return;
        }
        List<JjserInfoVo> list = new ArrayList<>();
        for (JjserInfoVo datum : data) {
            datum.setDelFlag(Constants.ZERO);
            if(Constants.FIVE.equals(datum.getStatus())) {
                JjserInfoVo infoVo = new JjserInfoVo();
                infoVo.setWorkcode(datum.getWorkcode());
                list.add(infoVo);
            }
        }
        if(!CollectionUtils.isEmpty(list)) {
            List<String> collectStr = list.stream().map(JjserInfoVo::getWorkcode).collect(Collectors.toList());
            List<SysPeople> ids = sysPeopleService.queryPeople(collectStr);
            if(!CollectionUtils.isEmpty(ids)) {
                Long[] collect = ids.stream().map(SysPeople::getId).toArray(Long[]::new);
                remove(collect);
            }
        }
        List<JjserInfoVo> collect = data.stream().filter(e -> !Constants.FIVE.equals(e.getStatus())).collect(Collectors.toList());
        sysBranchService.saveUserInfo(collect);
    }


    /**
     * 定时批量下发
     */
    @PostMapping("/batchDown")
    public void batchDown() {
        startPage();
        SysPeopleRule sysPeopleRule= new SysPeopleRule();
        sysPeopleRule.setSync(0L);
        List<SysPeopleRule>  List=sysPeopleRuleService.selectSysPeopleRuleList(sysPeopleRule);
        for(SysPeopleRule p:List){
            SysPeople people=sysPeopleService.selectSysPeopleById(p.getPeopleId());
            if(p.getEquipmentId()==null){
                SysRule rule=sysRuleService.selectSysRuleById(p.getRuleId());
                sdkService.authDeviceNews(rule,people);//规则授权
            }else{
                SysEquipment equipment=equipmentService.selectSysEquipmentById(p.getEquipmentId());
                sdkService.authDevices(people,equipment);//设备人员授权恢复
            }
            p.setSync(1L);
            people.setDown(1L);
            sysPeopleService.updateSysPeople(people);
            sysPeopleRuleService.updateSysPeopleRule(p);
        }
    }

    /**
     * 删除离职人员（测试用得）
     */
    @PostMapping("/getAccount")
    public void getAccount() {
        String body = HttpUtil.createGet("http://192.168.255.135:5566/api/ulYcRBiE/dhr/getAccount")
                .header("access_token", "f23c34a3200f1994d85de4daf1f15d")
                .execute().body();
        JSONObject object = JSONObject.parseObject(body);
        List<JjserInfoVo> data = JSONObject.parseArray(object.get("data").toString(), JjserInfoVo.class);
        List<SysPeople> sysPeople = sysPeopleService.queryPeople(new ArrayList<>());
        List<JjserInfoVo> collect = data.stream().filter(e -> Constants.FIVE.equals(e.getStatus())).collect(Collectors.toList());
        for (JjserInfoVo datum : collect) {
            for (SysPeople sysPerson : sysPeople) {
                if(datum.getWorkcode().equals(sysPerson.getGh())) {
                    System.out.println(datum.getWorkcode());
                    SysPeople people = new SysPeople();
                    people.setGh(datum.getWorkcode());
                    sysPeopleService.updatePeople(people);
                }
            }
        }

    }

    /**
     * 通过手机号查找姓名
     * @param phone
     * @return
     */
    @PostMapping("/queryPeopleName")
    public AjaxResult queryPeopleName(String phone, String deptType) {
        List<SysBranch> branch = visitorService.querySysBranch(deptType);
        List<String> collect = branch.stream().map(SysBranch::getId).collect(Collectors.toList());
        SysPeople people = sysPeopleService.queryPeopleName(phone, collect);
        if(people == null) {
            return success();
        }
        return success(people);
    }

    /**
     * 通过职位标签查询人员信息
     * @param position
     * @return
     */
    @PostMapping("/queryPeopleByPosition")
    public AjaxResult queryPeopleByPosition(String position) {
        List<SysPeople> sysPeople = sysPeopleService.queryPeopleByPosition(position);
        return success(sysPeople);
    }

    /**
     * 小程序通过手机号查找姓名
     *
     * @param js
     * @return
     */
    @PostMapping("/app/queryPeopleName")
    public AjaxResult queryAppPeopleName(@RequestBody JSONObject js) {
        if (null == js) {
            throw new ServiceException("请输入查询参数");
        }
        String phone = js.getString("phone");
        String deptType = js.getString("deptType");
        if (StringUtils.isBlank(phone)) {
            throw new ServiceException("手机号不能为空");
        }
        if (StringUtils.isBlank(deptType)) {
            throw new ServiceException("公司/部门不能为空");
        }

        List<SysBranch> branch = sysBranchService.queryJuniorBranch(deptType);
        List<String> collect = branch.stream().map(SysBranch::getId).collect(Collectors.toList());
        collect.add(deptType);
        SysPeople people = sysPeopleService.queryPeopleName(phone, collect);
        if (people == null) {
            return success();
        }
        return success(people);
    }

}
