package com.mosty.base.manager.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mosty.base.model.dto.jcgl.TbJcglXfllSearchQuery;
import com.mosty.base.model.dto.jcgl.TbJcglZyjzSearchDto;
import com.mosty.base.model.entity.lzother.TAlarmYjgj;
import com.mosty.base.model.entity.qwzx.TbQwZbbb;
import com.mosty.base.model.entity.rzzx.TbRzSpbf;
import com.mosty.base.model.query.jcgl.TbJcglXfllQueryAllDto;
import com.mosty.base.utils.DateUtils;
import com.mosty.base.utils.IDCardUtil;
import com.mosty.base.utils.PageUtils;
import com.mosty.base.model.vo.base.DeptInfoVo;
import com.mosty.base.model.vo.jcgl.TbJcglXfllBqVo;
import com.mosty.base.model.vo.jcgl.TbJcglZyjz;
import com.mosty.base.model.dto.jcgl.TbJcglXfllDto;
import com.mosty.base.model.entity.jcgl.TbJcglBq;
import com.mosty.base.model.entity.jcgl.TbJcglXfll;
import com.mosty.base.model.entity.jcgl.TbJcglXfllBq;
import com.mosty.base.model.query.jcgl.TbJcglXfllQuery;
import com.mosty.base.model.vo.jcgl.TbJcglXfllVo;
import com.mosty.base.manager.mapper.TbJcglBqMapper;
import com.mosty.base.manager.mapper.TbJcglXfllMapper;
import com.mosty.base.manager.mapper.TbJcglXfllbqMapper;
import com.mosty.base.manager.remote.TbBaseAdaptRemoteService;
import com.mosty.base.manager.service.TbJcglXfllService;
import com.mosty.base.utils.UUIDGenerator;
import com.mosty.common.base.exception.BusinessException;
import com.mosty.common.core.business.entity.SysUser;
import com.mosty.common.core.business.entity.vo.SysUserDeptVO;
import com.mosty.common.core.business.entity.vo.SysUserVO;
import com.mosty.common.core.business.service.SysRoleService;
import com.mosty.common.token.DeptInfo;
import com.mosty.common.token.UserInfo;
import com.mosty.common.token.UserInfoManager;
import com.mosty.common.util.PermissionsUtil;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.lang.reflect.Type;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 巡防力量表;(tb_jcgl_xfll)表服务实现类
 *
 * @author : Lhh
 * @date : 2022-6-4
 */
@AllArgsConstructor
@Service
public class TbJcglXfllServiceImpl extends ServiceImpl<TbJcglXfllMapper, TbJcglXfll> implements TbJcglXfllService {

    private final TbJcglXfllMapper tbJcglXfllMapper;
    private final TbJcglBqMapper tbJcglBqMapper;
    private final TbJcglXfllbqMapper tbJcglXfllbqMapper;
    private final TbBaseAdaptRemoteService tbBaseAdaptRemoteService;
    private final SysRoleService sysRoleService;


    @Override
    public IPage<TbJcglXfllVo> getList(TbJcglXfllQuery tbJcglXfll) {
//        UserInfo userInfo = UserInfoManager.get();
        tbJcglXfll.setSsbmdm(this.tbBaseAdaptRemoteService.getSsbm(tbJcglXfll.getSsbmdm(), null));
        IPage<TbJcglXfll> page = PageUtils.buildPage(tbJcglXfll.getPageSize(), tbJcglXfll.getPageCurrent());
        if (StringUtils.isEmpty(tbJcglXfll.getXtSjzt())) {
            tbJcglXfll.setXtSjzt("1");
        }
        return tbJcglXfllMapper.getList(page, tbJcglXfll);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(TbJcglXfllDto dto) {
        if (this.cheskXfll(dto.getSfzh())) {
            throw new BusinessException("身份证号已存在" + dto.getSfzh());
        }
        TbJcglXfll tbJcglXfll = new TbJcglXfll();
        BeanUtil.copyProperties(dto, tbJcglXfll);
        tbJcglXfll.setXtSjly("1");
        if (!StringUtils.isEmpty(dto.getSsbmdm())) {
            perfectDept(tbJcglXfll);
        }
        tbJcglXfllMapper.insert(tbJcglXfll);
        if (Objects.nonNull(dto.getBqId()) && dto.getBqId().size() > 0) {
            dto.getBqId().forEach(e -> {
                TbJcglXfllBq tbJcglXfllbq = new TbJcglXfllBq();
                tbJcglXfllbq.setXfllId(tbJcglXfll.getId());
                tbJcglXfllbq.setXtSjly("1");
                tbJcglXfllbq.setBqlx("SKL");
                tbJcglXfllbq.setBqId(e);
                tbJcglXfllbqMapper.insert(tbJcglXfllbq);
            });
        }
    }

    private void deleteSklbq(String id) {
        LambdaUpdateWrapper<TbJcglXfllBq> deleteWrapper = new LambdaUpdateWrapper<>();
        deleteWrapper.eq(TbJcglXfllBq::getBqlx, "SKL");
        deleteWrapper.eq(TbJcglXfllBq::getXfllId, id);
        tbJcglXfllbqMapper.delete(deleteWrapper);
    }

    /**
     * 验证巡防力量人员是否重复
     *
     * @param idCard
     * @return
     */
    @Override
    public boolean cheskXfll(String idCard) {
        LambdaQueryWrapper<TbJcglXfll> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TbJcglXfll::getSfzh, idCard);
        Integer integer = tbJcglXfllMapper.selectCount(lambdaQueryWrapper);
        if (integer > 0) {
            return true;
        }
        return false;
    }

    @Override
    public void lzList(List<String> list) {
        if (!CollectionUtils.isEmpty(list)) {
            for (String item : list) {
                TbJcglXfll tbJcglXfll = this.baseMapper.selectById(item);
                if (ObjectUtils.isNotEmpty(tbJcglXfll)) {
                    tbJcglXfll.setXtSjzt("0");
                    this.baseMapper.updateById(tbJcglXfll);
                }
            }
        }
    }


    @Override
    public Object deleteList(List<String> list) {
        if (list.size() > 0) {
            tbJcglXfllMapper.deleteList(list);
            for (String id : list) {
                LambdaQueryWrapper<TbJcglXfllBq> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(TbJcglXfllBq::getXfllId, id);
                List<TbJcglXfllBq> bqs = tbJcglXfllbqMapper.selectList(queryWrapper);
                if (!CollectionUtils.isEmpty(bqs)) {
                    List<Integer> ids = new ArrayList<>();
                    for (TbJcglXfllBq bq : bqs) {
                        ids.add(bq.getId());
                    }
                    tbJcglXfllbqMapper.deleteBatchIds(ids);
                }
            }

        }
        return true;
    }

    @Override
    public List<String> querySfzh() {
        LambdaQueryWrapper<TbJcglXfll> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TbJcglXfll::getXtScbz, "0");
        List<TbJcglXfll> list = tbJcglXfllMapper.selectList(queryWrapper);
        return list.stream().map(TbJcglXfll::getSfzh).collect(Collectors.toList());
    }

    @Override
    public Object selectAllPolice(TbJcglXfllDto tbJcglXfllDto) {
        tbJcglXfllDto.setSsbm(this.tbBaseAdaptRemoteService.getSsbm(tbJcglXfllDto.getSsbm(), null));

//        UserInfo userInfo = UserInfoManager.get();
        //所有警力
        Map<String, Integer> integerMap = tbJcglXfllMapper.selectAllPolice(tbJcglXfllDto);
        //民警所有警力,在岗警力
        Map<String, Integer> police = tbJcglXfllMapper.selectPolice(tbJcglXfllDto);
        //辅警所有警力,在岗警力
        Map<String, Integer> auxPolice = tbJcglXfllMapper.selectAuxPolice(tbJcglXfllDto);
        //可抽调-所有警力-(报备的警力+值班警力+请假警力)

        //街面警力

        //请休假

        integerMap.put("allPoilce", police.get("allPoilce"));
        integerMap.put("onAllPoilce", police.get("onAllPoilce"));
        integerMap.put("allAuxPoilce", auxPolice.get("allAuxPoilce"));
        integerMap.put("onAllAuxPoilce", auxPolice.get("onAllAuxPoilce"));
        return integerMap;
    }

    @Override
    public List<com.mosty.base.model.dto.jcgl.TbJcglXfllVo> getFjList(String deptid) {
        List<String> deptList = this.tbBaseAdaptRemoteService.getChildDept(deptid);
        List<TbJcglXfll> list = this.baseMapper.selectList(
                new LambdaQueryWrapper<TbJcglXfll>()
                        .eq(TbJcglXfll::getXtSjzt, "1")
                        .in(TbJcglXfll::getSsbmid, deptList)
                        .eq(TbJcglXfll::getFl, "02")
        );
        List<com.mosty.base.model.dto.jcgl.TbJcglXfllVo> rList = new ArrayList<>();
        list.forEach(item -> {
            com.mosty.base.model.dto.jcgl.TbJcglXfllVo vo =
                    new com.mosty.base.model.dto.jcgl.TbJcglXfllVo();
            BeanUtil.copyProperties(item, vo);
            rList.add(vo);
        });
        return rList;
    }

    @Override
    public IPage<TbJcglZyjz> getZyjzList(TbJcglZyjzSearchDto dto) {
        Map<String, Object> map = new HashMap<>();
        map.put("pageIndex", (dto.getPageCurrent() - 1) * dto.getPageSize());
        map.put("pageSize", dto.getPageSize());
        map.put("dto", dto);
        List<TbJcglZyjz> list = this.baseMapper.getxfllList(map);
        int count = this.baseMapper.getxfllCount(map);
        list.forEach(zyjz -> {
            // 查询标签
            List<TbJcglXfllBq> bqList = this.tbJcglXfllbqMapper.selectList(
                    new LambdaQueryWrapper<TbJcglXfllBq>()
                            .eq(TbJcglXfllBq::getXtSjzt, "1")
                            .eq(TbJcglXfllBq::getXfllId, zyjz.getId())
            );
            List<TbJcglXfllBqVo> voList = new ArrayList<>();
            bqList.forEach(bq -> {
                TbJcglXfllBqVo vo = new TbJcglXfllBqVo();
                BeanUtils.copyProperties(bq, vo);
                // 查询标签名称
                TbJcglBq q = this.tbJcglBqMapper.selectById(bq.getBqId());
                if (q != null) {
                    vo.setBqmc(q.getBqmc());
                }
                voList.add(vo);
            });
            zyjz.setBqList(voList);
        });
        IPage<TbJcglZyjz> page = new Page<>(dto.getPageCurrent(), dto.getPageSize());
        page.setTotal(count);
        page.setRecords(list);
        return page;
    }

    @Override
    public List<TbJcglXfllVo> getXfll(TbJcglXfllQueryAllDto dto) {
//        List<String> deptList = this.tbBaseAdaptRemoteService.getChildDept(dto.getDeptid());
        String ssbm = this.tbBaseAdaptRemoteService.getSsbm(dto.getSsbmdm(), null);
        List<TbJcglXfll> list = this.baseMapper.selectList(
                new LambdaQueryWrapper<TbJcglXfll>()
                        .eq(TbJcglXfll::getXtSjzt, "1")
                        .eq(!StringUtils.isEmpty(dto.getFl()), TbJcglXfll::getFl, dto.getFl())
                        .likeRight(!StringUtils.isEmpty(ssbm), TbJcglXfll::getSsbmdm, ssbm)
        );
        List<TbJcglXfllVo> rList = new ArrayList<>();
        list.forEach(item -> {
            TbJcglXfllVo vo = new TbJcglXfllVo();
            BeanUtil.copyProperties(item, vo);
            rList.add(vo);
        });
        return rList;
    }

    @Override
    public IPage<TbJcglXfllVo> getXfllList(TbJcglXfllSearchQuery dto) {
        UserInfo user = UserInfoManager.get();
        if ("1".equals(dto.getType())) {
            return this.getXfllByDeptId(dto, user.getDeptCode());
        } else {
            return this.getXfllByDeptIdAll(dto);
        }
    }

    @Override
    public TbJcglXfllDto getXfllBySfz(String sfzh) {
        TbJcglXfll xfll = this.tbJcglXfllMapper.selectOne(
                new LambdaQueryWrapper<TbJcglXfll>()
                        .eq(TbJcglXfll::getSfzh, sfzh)
                        .eq(TbJcglXfll::getXtSjzt, "1")
                        .last(" limit 1")
        );
        if (xfll != null) {
            TbJcglXfllDto vo = new TbJcglXfllDto();
            BeanUtils.copyProperties(xfll, vo);
            return vo;
        }
        return null;
    }

    @Override
    public IPage<TbJcglXfllVo> getXfllPage(TbJcglXfllQuery dto) {
        dto.setSsbmdm(this.tbBaseAdaptRemoteService.getSsbm(dto.getSsbmdm(), null));
        IPage<TbJcglXfll> page = PageUtils.buildPage(dto.getPageSize(), dto.getPageCurrent());
        if (StringUtils.isEmpty(dto.getXtSjzt())) {
            dto.setXtSjzt("1");
        }
        return tbJcglXfllMapper.getList(page, dto);
    }

    @Override
    public IPage<TbJcglXfll> getLzPage(TbJcglXfllQuery dto) {
        dto.setSsbmdm(this.tbBaseAdaptRemoteService.getSsbm(dto.getSsbmdm(), null));
        IPage<TbJcglXfll> page = this.tbJcglXfllMapper.selectPage(
                new Page<>(dto.getPageCurrent(), dto.getPageSize()),
                new LambdaQueryWrapper<TbJcglXfll>()
                        .eq(TbJcglXfll::getXtSjzt, "0")
                        .eq(!StringUtils.isEmpty(dto.getRyid()), TbJcglXfll::getRyid, dto.getRyid())
                        .eq(!StringUtils.isEmpty(dto.getFl()), TbJcglXfll::getFl, dto.getFl())
                        .eq(!StringUtils.isEmpty(dto.getLx()), TbJcglXfll::getLx, dto.getLx())
                        .like(!StringUtils.isEmpty(dto.getXm()), TbJcglXfll::getXm, dto.getXm())
                        .eq(!StringUtils.isEmpty(dto.getXbdm()), TbJcglXfll::getXbdm, dto.getXbdm())
                        .eq(!StringUtils.isEmpty(dto.getSfzh()), TbJcglXfll::getSfzh, dto.getSfzh())
                        .eq(!StringUtils.isEmpty(dto.getJh()), TbJcglXfll::getJh, dto.getJh())
                        .eq(!StringUtils.isEmpty(dto.getLxdh()), TbJcglXfll::getLxdh, dto.getLxdh())
                        .likeRight(!StringUtils.isEmpty(dto.getSsbmdm()), TbJcglXfll::getSsbmdm, dto.getSsbmdm())

        );
        return page;
    }

    @Override
    public Map<String, Object> getRytj(String ssbmdm) {
        Map<String, Object> map = new HashMap<>();
        ssbmdm = tbBaseAdaptRemoteService.getSsbm(ssbmdm, null);
        List<TbJcglXfll> xfllList = this.baseMapper.selectList(new LambdaQueryWrapper<TbJcglXfll>()
                .likeRight(TbJcglXfll::getSsbmdm, ssbmdm)
        );
        int mj = 0;
        int fj = 0;
        if (!CollectionUtils.isEmpty(xfllList)) {
            mj = xfllList.stream().filter(s -> s.getFl().equals("01")).collect(Collectors.toList()).size();
            fj = xfllList.stream().filter(s -> s.getFl().equals("02")).collect(Collectors.toList()).size();
        }
        map.put("mj", mj);
        map.put("fj", fj);
        return map;
    }


    @Override
    public List<TbJcglXfll> getBmAll() {
        List<TbJcglXfll> tbJcglXflls = this.baseMapper.selectList(new LambdaQueryWrapper<TbJcglXfll>().first("SELECT DISTINCT ssbmdm from ( ").last(" ) a "));
        return tbJcglXflls;
    }

    @Override
    public List<TbJcglXfll> export() {
        List<TbJcglXfll> tbJcglXflls = this.baseMapper.selectList(new LambdaQueryWrapper<TbJcglXfll>().last(" limit 10 "));
        return tbJcglXflls;
    }


    @Override
    public String importData(List<TbJcglXfll> userList, boolean updateSupport) {
        if (CollectionUtils.isEmpty(userList)) {
            throw new BusinessException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (TbJcglXfll xfll : userList) {
            try {
                if (StringUtils.isEmpty(xfll.getLxdh())
                        || StringUtils.isEmpty(xfll.getXm())
                        || StringUtils.isEmpty(xfll.getSsbmdm())
                        || StringUtils.isEmpty(xfll.getJh())
                        || StringUtils.isEmpty(xfll.getSfzh())
                ) {
                    failureNum++;
                    failureMsg.append("<br/>" + successNum + "、账号 " + xfll.getXm() + " 信息不全,请仔细核查！");
                    continue;
                }
                //验证是否存在这个用户
                TbJcglXfll u = this.baseMapper.selectOne(new LambdaQueryWrapper<TbJcglXfll>().eq(TbJcglXfll::getSfzh, xfll.getSfzh()).last(" limit 1"));
                DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByOrgcode(xfll.getSsbmdm());
                if (ObjectUtils.isNotEmpty(dept)) {
                    xfll.setSsbm(dept.getDeptname());
                    xfll.setSsbmid(dept.getDeptid());
                    xfll.setSsbmdm(dept.getDeptcode());
                    xfll.setSsxgajdm(dept.getFxjcode());
                    xfll.setSsxgajid(dept.getFxjid());
                    xfll.setSsxgaj(dept.getFxjname());
                    xfll.setSssgaj(dept.getDszname());
                    xfll.setSssgajdm(dept.getDszcode());
                    xfll.setSssgajid(dept.getDszid());
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + successNum + "、所属部门编码不存在 " + xfll.getSsbmdm() + " ,请仔细核查！");
                    continue;
                }
                xfll.setXtSjzt("1");
                xfll.setXtScbz("0");
                xfll.setFl("02");
                xfll.setLx("02");
                xfll.setXbdm(String.valueOf(IDCardUtil.getSexInteger(xfll.getSfzh())));

                if (ObjectUtils.isEmpty(u)) {
                    xfll.setCsrq(DateUtils.strToDate(IDCardUtil.getBirthday(xfll.getSfzh()), "01"));
                    xfll.setId(UUIDGenerator.getUUID());
                    this.baseMapper.insert(xfll);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + xfll.getXm() + " 导入成功");
                } else if (updateSupport) {
                    xfll.setId(u.getId());
                    this.baseMapper.updateById(xfll);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + xfll.getXm() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、账号 " + xfll.getXm() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、账号 " + xfll.getXm() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new BusinessException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public Integer getRysl(String ssbmdm) {
        QueryWrapper<TbJcglXfll> tb = new QueryWrapper<>();
//        PermissionsUtil.queryWrapperUtilByDeptInfo(tb, sysDeptLevel);
        ssbmdm = tbBaseAdaptRemoteService.getSsbm(ssbmdm, null);
        tb.lambda().likeRight(TbJcglXfll::getSsbmdm, ssbmdm);
        return this.baseMapper.selectCount(tb);
    }

    // 查询部门的所有的巡防力量
    private IPage<TbJcglXfllVo> getXfllByDeptIdAll(TbJcglXfllSearchQuery dto) {

        String ssbmdm = tbBaseAdaptRemoteService.getSsbm(null, null);

        IPage<TbJcglXfll> page = this.tbJcglXfllMapper.selectPage(
                new Page<>(dto.getPageCurrent(), dto.getPageSize()),
                new LambdaQueryWrapper<TbJcglXfll>()
                        .eq(TbJcglXfll::getXtSjzt, "1")
                        .eq(!StringUtils.isEmpty(dto.getJllx()), TbJcglXfll::getFl, dto.getJllx())
                        .likeRight(!StringUtils.isEmpty(ssbmdm), TbJcglXfll::getSsbmdm, ssbmdm)
                        .and(!StringUtils.isEmpty(dto.getKeyword()),
                                it -> it.like(TbJcglXfll::getXm, dto.getKeyword())
                                        .or()
                                        .like(TbJcglXfll::getSsbm, dto.getKeyword())
                        )
        );
        List<TbJcglXfllVo> voList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(page.getRecords())) {
            page.getRecords().forEach(item -> {
                TbJcglXfllVo vo = new TbJcglXfllVo();
                BeanUtils.copyProperties(item, vo);
                voList.add(vo);
            });
        }
        IPage<TbJcglXfllVo> voPage = new Page<>(dto.getPageCurrent(), dto.getPageSize());
        voPage.setRecords(voList);
        voPage.setTotal(page.getTotal());
        return voPage;
    }

    // 查询本部门的巡防力量
    private IPage<TbJcglXfllVo> getXfllByDeptId(TbJcglXfllSearchQuery dto, String ssbmdm) {
        // 查询本部门级下级部门
//        List<String> deptIds = this.tbBaseAdaptRemoteService.getChildDept(deptId);
        IPage<TbJcglXfll> page = this.tbJcglXfllMapper.selectPage(
                new Page<>(dto.getPageCurrent(), dto.getPageSize()),
                new LambdaQueryWrapper<TbJcglXfll>()
                        .eq(TbJcglXfll::getSsbmdm, ssbmdm)
                        .eq(TbJcglXfll::getXtSjzt, "1")
                        .like(!StringUtils.isEmpty(dto.getKeyword()), TbJcglXfll::getXm, dto.getKeyword())
                        .eq(!StringUtils.isEmpty(dto.getJllx()), TbJcglXfll::getFl, dto.getJllx())
        );
        IPage<TbJcglXfllVo> voPage = new Page<>(dto.getPageCurrent(), dto.getPageSize());
        List<TbJcglXfllVo> voList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(page.getRecords())) {
            page.getRecords().forEach(item -> {
                TbJcglXfllVo vo = new TbJcglXfllVo();
                BeanUtils.copyProperties(item, vo);
                voList.add(vo);
            });
        }
        voPage.setTotal(page.getTotal());
        voPage.setRecords(voList);
        return voPage;
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(TbJcglXfllDto tbJcglXfllDto) {
        try {
            TbJcglXfll tbJcglXfll = new TbJcglXfll();
            BeanUtil.copyProperties(tbJcglXfllDto, tbJcglXfll);
            if (!StringUtils.isEmpty(tbJcglXfll.getSsbmdm())) {
                perfectDept(tbJcglXfll);
            }
            tbJcglXfllMapper.updateById(tbJcglXfll);
            deleteSklbq(tbJcglXfllDto.getId());
            if (Objects.nonNull(tbJcglXfllDto.getBqId()) && tbJcglXfllDto.getBqId().size() > 0) {
                tbJcglXfllDto.getBqId().forEach(e -> {
                    TbJcglXfllBq tbJcglXfllbq = new TbJcglXfllBq();
                    tbJcglXfllbq.setXfllId(tbJcglXfll.getId());
                    tbJcglXfllbq.setBqlx("SKL");
                    tbJcglXfllbq.setXtSjly("1");
                    tbJcglXfllbq.setBqId(e);
                    tbJcglXfllbqMapper.insert(tbJcglXfllbq);
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public TbJcglXfllVo queryById(String id) {
        return tbJcglXfllMapper.queryById(id);
    }

    // 部门赋值
    public void perfectDept(TbJcglXfll xfll) {
        DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByOrgcode(xfll.getSsbmdm());
        if (dept != null) {
            xfll.setSsbm(dept.getDeptname());
            xfll.setSsbmid(dept.getDeptid());
            xfll.setSsbmdm(dept.getDeptcode());
            xfll.setSsxgajdm(dept.getFxjcode());
            xfll.setSsxgajid(dept.getFxjid());
            xfll.setSsxgaj(dept.getFxjname());
            xfll.setSssgaj(dept.getDszname());
            xfll.setSssgajdm(dept.getDszcode());
            xfll.setSssgajid(dept.getDszid());
        }
    }
}