package com.djhk.uav.equ.service.impl;

import com.djhk.common.api.RemoteDeptService;
import com.djhk.common.api.RemoteDictDataService;
import com.djhk.common.api.RemoteUserService;
import com.djhk.common.constant.RedisConstant;
import com.djhk.common.constant.SecurityConstants;
import com.djhk.common.core.domain.R;
import com.djhk.common.core.domain.TreeSelect;
import com.djhk.common.core.domain.entity.SysDept;
import com.djhk.common.core.domain.entity.SysDictData;
import com.djhk.common.core.domain.entity.SysUser;
import com.djhk.common.core.domain.model.LoginUser;
import com.djhk.common.core.redis.RedisCache;
import com.djhk.common.exception.ServiceException;
import com.djhk.common.utils.DictUtils;
import com.djhk.common.utils.SecurityUtils;
import com.djhk.common.utils.StringUtils;
import com.djhk.common.utils.bean.BeanUtils;
import com.djhk.system.service.ISysDeptService;
import com.djhk.system.service.ISysUserService;
import com.djhk.system.service.impl.SysDictDataServiceImpl;
import com.djhk.uav.equ.domain.EquInfoEntity;
import com.djhk.uav.equ.domain.EquModelParam;
import com.djhk.uav.equ.domain.EquTranEntity;
import com.djhk.uav.equ.domain.EquType;
import com.djhk.uav.equ.domain.dto.EquInfoExportDto;
import com.djhk.uav.equ.domain.vo.EquInfoExportVo;
import com.djhk.uav.equ.mapper.EquInfoMapper;
import com.djhk.uav.equ.mapper.EquModelMapper;
import com.djhk.uav.equ.mapper.EquTranMapper;
import com.djhk.uav.equ.mapper.EquTypeMapper;
import com.djhk.uav.equ.service.EquInfoService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 设备台账
 *
 * @author gzm
 * @since 1.0.0 2023-06-07
 */
@Service
public class EquInfoServiceImpl implements EquInfoService {
    private static final Logger log = LoggerFactory.getLogger(EquInfoServiceImpl.class);
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    @Autowired
    private EquInfoMapper equInfoMapper;

    @Autowired
    private EquTranMapper equTranMapper;

//    @Autowired
//    private RemoteUserService userService;

//    @Autowired
//    private RemoteDeptService sysDeptService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private RedisCache redisService;

    @Autowired
    private EquModelMapper equModelMapper;

    @Autowired
    private EquTypeMapper equTypeMapper;

    @Autowired
    private SysDictDataServiceImpl sysDictDataService;

    @Autowired
    private RemoteDeptService remoteDeptService;

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private RemoteDictDataService remoteDictDataService;

    @Override
    public List<EquInfoEntity> getEquInfoList(EquInfoEntity query) {
        List<EquInfoEntity> equInfoEntities = equInfoMapper.selectEquInfoList(query);

        if (equInfoEntities != null) {
            try {
//                List<SysDictData> sysDictDataList = sysDictDataService.getDictDataByType("equ_model_info").getData();
//                List<SysDictData> sysDictDataList = remoteDictDataService.getDictDataByType("equ_model_info", "inner").getData();
                List<SysDictData> sysDictDataList = DictUtils.getDictDataByType("equ_model_info");

                for (EquInfoEntity equInfoEntity : equInfoEntities) {
                    // modelId转
                    if (sysDictDataList != null) {
                        for (SysDictData sysDictData : sysDictDataList) {
                            if (StringUtils.equals(equInfoEntity.getModelId(), String.valueOf(sysDictData.getDictValue()))) {
                                equInfoEntity.setModelId(sysDictData.getDictLabel());
                                break;
                            }
                        }
                    }

                    // 设备保险期限30天倒计时
                    if (!StringUtils.isEmpty(equInfoEntity.getMtnDate())) {
                        Date endDate = dateFormat.parse(equInfoEntity.getMtnDate());//结束时间
                        long msNum = endDate.getTime() - new Date().getTime();//时间戳相差的毫秒数
                        long dayNum = msNum / (24 * 60 * 60 * 1000);//除以一天的毫秒数，得到相差天数
                        if (dayNum > 90)  //
                        {
                            equInfoEntity.setMtnDateState(1);
                        } else if (dayNum > 0 && dayNum < 90) {
                            equInfoEntity.setMtnDateState(3);// 黄
                        } else {
                            equInfoEntity.setMtnDateState(2); // 红
                        }
                        if (msNum > 0){
                            equInfoEntity.setInsuranceSts("在保");
                        }else {
                            equInfoEntity.setInsuranceSts("过期");
                        }
                    }
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }

        }
        return equInfoEntities;
    }

    @Override
    public List<EquInfoEntity> getEquInfoLists(EquInfoEntity query) {
        List<EquInfoEntity> equInfoEntities = equInfoMapper.selectEquInfoList(query);
        if (equInfoEntities != null) {
            try {
                SysDictData sysDictData1 = new SysDictData();
                sysDictData1.setDictType("equ_model_info");
                List<SysDictData> sysDictDataList = sysDictDataService.selectDictDataList(sysDictData1);

                for (EquInfoEntity equInfoEntity : equInfoEntities) {
                    // modelId转
                    if (sysDictDataList != null) {
                        for (SysDictData sysDictData : sysDictDataList) {
                            if (StringUtils.equals(equInfoEntity.getModelId(), String.valueOf(sysDictData.getDictValue()))) {
                                equInfoEntity.setModelId(sysDictData.getDictLabel());
                                break;
                            }
                        }
                    }

                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }

        }
        return equInfoEntities;
    }

    @Override
    public List<EquInfoEntity> getEquInfos(EquInfoEntity query) {
        log.info("query=============================={}", query);
        log.info("query.getModelId()=============================={}", query.getModelId());
        List<EquInfoEntity> equInfoEntities = equInfoMapper.selectEquInfoList(query);
        log.info("equInfoEntities=========================={}", equInfoEntities);
        return equInfoEntities;
    }

    @Override
    public EquInfoEntity selectEquInfoById(String equId) {
        return equInfoMapper.selectEquInfoById(equId);
    }

    @Override
    public int saves(EquInfoEntity vo) {

        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (null == loginUser) {
            throw new ServiceException("登录失效，请重新登录！", 500);
        }
//        Long deptId = loginUser.getSysUser().getDeptId();
        Long deptId = SecurityUtils.getDeptId();
        EquInfoEntity equInfoEntities = equInfoMapper.selectEquInfoByTypeId(vo.getTypeId(), String.valueOf(deptId));
//        SysDept dept = remoteDeptService.getDeptInfo(deptId, "inner").getData();
        SysDept dept = deptService.selectDeptById(deptId);
//        List<SysDictData> sysDictDataList = sysDictDataService.getDictDataByType("jhbh_code").getData();
//        List<SysDictData> sysDictDataList = remoteDictDataService.getDictDataByType("jhbh_code", "inner").getData();
        List<SysDictData> sysDictDataList = DictUtils.getDictDataByType("jhbh_code");
//        log.info("查询字典信息：{}", sysDictDataList);
        List<String> collect = sysDictDataList.stream().map(SysDictData::getDictLabel).collect(Collectors.toList());
        boolean b = false;
        for (String s : collect) {
            if (dept.getAdminCode() ==null){
                break;
            }
            if (s.contains(dept.getAdminCode().substring(0, 6).trim())) {
                b = true;
                break;
            }
        }
        if (b) {
            for (SysDictData sysDictData : sysDictDataList) {
                if (StringUtils.equals(sysDictData.getDictLabel(), dept.getAdminCode())){
                    EquInfoEntity equInfo = new EquInfoEntity();
                    equInfo.setEquCode("50" + sysDictData.getDictValue());
                    equInfo.setTypeId("10");
                    log.info("equInfo：{},{}", equInfo.getEquCode(),equInfo.getTypeId());
                    List<EquInfoEntity> entities = equInfoMapper.selectEquInfoList(equInfo);
                    if (entities.size() > 0 ) {
                        log.info("entities数量：{}", entities.size());
                        vo.setEquCode(String.valueOf(Integer.parseInt(entities.get(0).getEquCode()) + 1));
                        log.info("初始生成警航编号：{}", Integer.parseInt(entities.get(0).getEquCode()) + 1);
                    }
                    else {
                        vo.setEquCode("50" + sysDictData.getDictValue() + "001");
                        log.info("按序生成警航编号：{}", "50" + sysDictData.getDictValue() + "001");
                    }
                    break;
                }

                else if (StringUtils.equals(sysDictData.getDictLabel().substring(0, 8), dept.getAdminCode().substring(0, 8))) {
                    log.info("编码与字典值比较：{}，{}", sysDictData.getDictLabel(), dept.getAdminCode());
                    EquInfoEntity equInfo = new EquInfoEntity();
                    equInfo.setEquCode("50" + sysDictData.getDictValue());
                    equInfo.setTypeId("10");
                    log.info("equInfo：{},{}", equInfo.getEquCode(),equInfo.getTypeId());
                    List<EquInfoEntity> entities = equInfoMapper.selectEquInfoList(equInfo);
                    if (entities.size() > 0 ) {
                        log.info("entities数量：{}", entities.size());
                        vo.setEquCode(String.valueOf(Integer.parseInt(entities.get(0).getEquCode()) + 1));
                        log.info("初始生成警航编号：{}", Integer.parseInt(entities.get(0).getEquCode()) + 1);
                    } else {
                        vo.setEquCode("50" + sysDictData.getDictValue() + "001");
                        log.info("按序生成警航编号：{}", "50" + sysDictData.getDictValue() + "001");
                    }
                    break;
                }
            }
        } else {
            vo.setEquCode("");
            log.info("未在编号内部门警航编号不做生成");
        }
//        }
        return equInfoMapper.insertEquInfo(vo);
    }


    @Override
    public int update(EquInfoEntity vo) {
        return equInfoMapper.updateEquInfo(vo);
    }

    @Override
    public int delete(String[] equIds) {
        String equIdsStr = "";
        for (String str : equIds
        ) {
            equIdsStr += str + ",";
        }
        return equInfoMapper.deleteEquInfoByIds(equIds);
//        return equInfoMapper.deleteEquInfoByIds(equIdsStr.isEmpty()?equIdsStr:(equIdsStr.substring(0,equIdsStr.length()-1)));
    }

    @Override
    public int updateEquInfoByIds(String[] equIds) {
        String equIdsStr = "";
        for (String str : equIds
        ) {
            equIdsStr += str + ",";
        }
        return equInfoMapper.updateEquInfoByIds(equIds);

    }


    //查询资产转移记录
    @Override
    public List<EquTranEntity> getEquTranList(EquTranEntity query) {
        List<EquTranEntity> equTranEntities = equTranMapper.selectEquTranList(query);
        // 判断当前用户是否为接收人
        for (EquTranEntity tran : equTranEntities) {
//            SysUser sendUser = userService.getUserInfoByUserId(Long.parseLong(tran.getSendBy()), SecurityConstants.INNER).getData();
            SysUser sendUser = userService.selectUserById(SecurityUtils.getUserId());
            tran.setSendUserName(sendUser != null ? sendUser.getNickName() : tran.getSendBy());
//            SysUser recvUser = userService.getUserInfoByUserId(Long.parseLong(tran.getRecvBy()), SecurityConstants.INNER).getData();
            SysUser recvUser = userService.selectUserById(SecurityUtils.getUserId());
            tran.setRecvUserName(recvUser != null ? recvUser.getNickName() : tran.getRecvBy());
            if (StringUtils.isNotBlank(tran.getTranSts()) && tran.getTranSts().equals("0")) {
                String userId = SecurityUtils.getUserId().toString();
                //当前用户是否为发起人或接收人
                if (userId.equals(tran.getRecvBy())) {
                    tran.setIsRecvUser("Recv");
                } else if (userId.equals(tran.getSendBy())) {
                    tran.setIsRecvUser("Send");
                }
            }
        }
        return equTranEntities;
    }

    @Override
    public int saveTran(EquTranEntity vo) {
        vo.setSendBy(SecurityUtils.getUserId().toString());
        Long userId = SecurityUtils.getUserId();
//        SysUser sysUser = userService.getUserInfoByUserId(userId, SecurityConstants.INNER).getData();
        SysUser sysUser = userService.selectUserById(SecurityUtils.getUserId());
        vo.setSendOrgan(sysUser.getDeptId());
        return equTranMapper.insertEquTran(vo);
    }

    @Override
    public List<EquTranEntity> getTranStsByEquId(String equId) {
        return equTranMapper.getTranSts(equId);
    }

    @Override
    public int reciveTranMethod(Map vo) {
        if (vo.get("methods").equals("receiveTran")) {
            EquTranEntity equTranEntity = equTranMapper.selectEquTranById(vo.get("tranSeq").toString());
            equTranEntity.setTranSts("1");
            // 修改设备所属机构
            EquInfoEntity equInfoEntity = equInfoMapper.selectEquInfoById(equTranEntity.getEquId().toString());
            equInfoEntity.setOrganId(equTranEntity.getRecvOrgan());
            equInfoMapper.updateEquInfo(equInfoEntity);

            return equTranMapper.updateEquTran(equTranEntity);
        } else {
            EquTranEntity equTranEntity = equTranMapper.selectEquTranById(vo.get("tranSeq").toString());
            equTranEntity.setTranSts("2");
            equTranEntity.setCacelBy(SecurityUtils.getUserId().toString());
            return equTranMapper.updateEquTran(equTranEntity);
        }
    }

    @Override
    public List<Object> getTjTree() {
//        SysUser sysUser = userService.getUserInfoByUserId(SecurityUtils.getUserId(), SecurityConstants.INNER).getData();
        SysUser sysUser = userService.selectUserById(SecurityUtils.getUserId());
        List<Map<String, Object>> resulItemtList = new ArrayList<>();
        List<Object> resultList = new ArrayList<>();

        SysDept sysDept2 = new SysDept();
        //获取组织树
//        List<Object> treeSelects = sysDeptService.selectDeptTreeList(sysDept2);
        List<TreeSelect> treeSelects = sysDeptService.selectDeptTreeList(sysDept2);
        if (treeSelects == null) {
            return resultList;
        } else {
            //根节点
            // 创建ObjectMapper对象
            ObjectMapper objectMapper = new ObjectMapper();

            TreeSelect treeSelect0 = objectMapper.convertValue(treeSelects.get(0), TreeSelect.class);
            List<Map<String, Object>> equTj = equInfoMapper.getEquTj(treeSelect0.getId().toString());
            Map<String, Object> stringObjectMap = doTreeMethods(equTj, treeSelect0.getLabel());
            resulItemtList.add(stringObjectMap);
            resultList.add(resulItemtList);
            // 子节点
            if (treeSelect0.getChildren() != null) {
                List<TreeSelect> childrenList = treeSelect0.getChildren();
                for (TreeSelect item : childrenList) {
                    List<Map<String, Object>> resulChildList = new ArrayList<>();
                    List<Map<String, Object>> equTjItem = equInfoMapper.getEquTj(item.getId().toString());
                    Map<String, Object> stringObjectMapItem = doTreeMethods(equTjItem, item.getLabel());
                    resulChildList.add(stringObjectMapItem);
                    resultList.add(resulChildList);
                    if (item.getChildren() != null && item.getChildren().size() > 0) {
                        for (TreeSelect item2 : item.getChildren()) {
                            List<Map<String, Object>> resulChildList2 = new ArrayList<>();
                            List<Map<String, Object>> equTjItem2 = equInfoMapper.getEquTj(item2.getId().toString());
                            Map<String, Object> stringObjectMapItem2 = doTreeMethods(equTjItem2, item2.getLabel());
                            resulChildList2.add(stringObjectMapItem2);
                            resultList.add(resulChildList2);
                        }
                    }
                }
            }
        }
        return resultList;
    }


    @Override
    public List<TreeSelect> getTjTreeForDept(SysDept dept) {
        /*SysUser sysUser = userService.getUserInfoByUserId(SecurityUtils.getUserId(),SecurityConstants.INNER).getData();
//        List<Map<String, Object>> equTj = equInfoMapper.getEquTjForDept(sysUser.getDeptId().toString());
        String scop = " AND (d.dept_id IN ( SELECT dept_id FROM sys_dept WHERE dept_id = "+sysUser.getDeptId()+" or find_in_set( "+sysUser.getDeptId()+" , ancestors ) ))";
        dept.getParams().put("dataScope",scop);*/

//        List<Object> treeSelects = sysDeptService.selectDeptTreeList(dept, SecurityConstants.INNER).getData();
        List<TreeSelect> treeSelects = sysDeptService.selectDeptTreeList(dept);

//        List<TreeSelect> bianli = bianli(treeSelects, equTj);
        return treeSelects;
    }

    private List<TreeSelect> bianli(List<TreeSelect> treeSelects, List<Map<String, Object>> equTj) {
        for (TreeSelect item : treeSelects) {
            for (Map map : equTj) {
                if (item.getId().toString().equals(map.get("deptId").toString())) {
                    item.setLabel(item.getLabel() + "(" + map.get("value") + ")");
                }
            }
            if (item.getChildren().size() > 0) {
                bianli(item.getChildren(), equTj);
            }
        }
        return treeSelects;
    }


    private Map<String, Object> doTreeMethods(List<Map<String, Object>> equTj, String sysDeptName) {
        Map<String, Object> parentMap = new HashMap();
        Map<String, Integer> typeCountList = new HashMap<>();
        Integer allEqu = 0;
        for (Map item : equTj) {

            String type = item.get("type").toString();
            String model = item.get("model").toString();
            String count = item.get("count").toString();
            allEqu += Integer.valueOf(count);

            String typeName = item.get("typeName").toString();
            String modelName = item.get("modelName").toString();

            Map parentItem = new HashMap();
            if (parentMap.get(item.get("type").toString()) == null) {
                // type节点还没有加入
                List<Map> chlidList = new ArrayList<>();
                parentItem.put("id", type);
                parentItem.put("label", typeName);
                parentItem.put("value", 0);

                Map childItem = new HashMap();
                childItem.put("id", model);
                childItem.put("label", modelName);
                childItem.put("value", count);
                childItem.put("typeId", type);
                chlidList.add(childItem);

                //统计type总数
                typeCountList.put(type, Integer.valueOf(count));
                parentItem.put("value", typeCountList.get(type));
                parentItem.put("children", chlidList);
                parentMap.put(type, parentItem);
            } else {
                Map childItem = new HashMap();
                childItem.put("id", model);
                childItem.put("value", count);
                childItem.put("label", modelName);
                childItem.put("typeId", type);

                //统计type总数
                typeCountList.put(type, typeCountList.get(type) + Integer.valueOf(count));
                parentItem.put("value", typeCountList.get(type));
                Map o = (Map) parentMap.get(type);
                List child = (List) o.get("children");
                child.add(childItem);
                Map o1 = (Map) parentMap.get(type);
                o1.put("value", typeCountList.get(type));
                parentMap.put(type, o1);
            }
        }

        Map resultMap = new HashMap();
        resultMap.put("id", 1);
        resultMap.put("label", sysDeptName);
        resultMap.put("value", allEqu);
        resultMap.put("children", parentMap.values());

        return resultMap;
    }


    @Override
    public List<EquInfoEntity> selectMonitorEquList(String[] typeIds) {
        List<EquInfoEntity> list = equInfoMapper.selectMonitorEquList(typeIds);
        list.stream().forEach(x -> {
            Map<String, String> map = x.getModelParamList().stream().collect(Collectors.toMap(EquModelParam::getParamCode, EquModelParam::getParamValue));
            x.setOnlineSts("1");
            x.setIsCounter("0");
            if (!map.isEmpty()) {
                if (map.get("DFD_IS_SEND").equals("1")) {

                    String key = RedisConstant.COUNTER_PREFIX + x.getEquCode();
                    if (!redisService.hasKey(key)) {
                        x.setOnlineSts("-1");
                    }
                }
                if (map.get("DFD_IS_DIST_FZ").equals("1")) {
                    x.setIsCounter("1");
                }
            }

        });
        return list;
    }

    @Override
    public EquInfoEntity selectEquInfoByEquSn(EquInfoEntity vo) {

        return equInfoMapper.selectEquInfoByEquSn(vo);
    }

    @Override
    public List<EquInfoEntity> selectEquInfoExportList(EquInfoEntity equInfoEntity) {
        List<EquInfoEntity> equInfoExportDtos = equInfoMapper.selectEquInfoExportList(equInfoEntity);
        for (EquInfoEntity equInfoExportDto : equInfoExportDtos) {

//            R<SysDept> data = sysDeptService.getDeptInfo(Long.valueOf(equInfoExportDto.getOrganId()), "inner");
//            SysDept sysDept = data.getData();
            SysDept sysDept = sysDeptService.selectDeptById(Long.valueOf(equInfoExportDto.getOrganId()));
            if (sysDept != null) {
                equInfoExportDto.setDeptName(sysDept.getDeptName());
            } else {
                equInfoExportDto.setDeptName(null);
            }

            EquType equType = equTypeMapper.selectEquTypeByTypeId(equInfoExportDto.getTypeId());
            if (equType != null) {
                equInfoExportDto.setTypeId(equType.getTypeName());
            } else {
                equInfoExportDto.setTypeId(null);
            }
//            List<SysDictData> sysDictDataList = sysDictDataService.getDictDataByType("equ_model_info").getData();
//            List<SysDictData> sysDictDataList = remoteDictDataService.getDictDataByType("equ_model_info", "inner").getData();
            List<SysDictData> sysDictDataList = DictUtils.getDictDataByType("equ_model_info");

            if (sysDictDataList != null) {
                for (SysDictData sysDictData : sysDictDataList) {
                    if (StringUtils.equals(equInfoExportDto.getModelId(), String.valueOf(sysDictData.getDictValue()))) {
                        equInfoExportDto.setModelId(sysDictData.getDictLabel());
                        break;
                    } else {
                        equInfoExportDto.setModelId(equInfoExportDto.getModelId());
                    }
                }
            }
        }
        return equInfoExportDtos;
    }

    @Override
    public String importData(List<EquInfoExportDto> userList, Boolean isUpdateSupport) {
        if (userList.size() == 0) {
            throw new ServiceException("导入用户数据不能为空！", 500);
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (null == loginUser) {
            throw new ServiceException("登录失效，请重新登录！", 500);
        }
//        Long deptId = loginUser.getSysUser().getDeptId();
        Long deptId = SecurityUtils.getDeptId();
        for (int i = 0; i < userList.size(); i++) {

            try {
                // 验证是否存在数据
                EquInfoEntity target = new EquInfoEntity();
                BeanUtils.copyBeanProp(target, userList.get(i));
                EquInfoEntity equInfoEntity = equInfoMapper.selectEquInfoByEquSn(target);
                if (equInfoEntity == null) {

                    target.setOrganId(deptId);
                    EquType equType = equTypeMapper.selectEquTypeByTypeName(userList.get(i).getTypeId());
                    if (equType != null) {
                        target.setTypeId(equType.getTypeId());
                    } else {
//                        target.setTypeId(null);
                        failureNum++;
                        failureMsg.append("第" + (i + 1) + "条数据类别标识【" + equType.getTypeId() + "】不存在，请联系管理员！, ");
                        continue;
                    }

//                    List<SysDictData> sysDictDataList = sysDictDataService.getDictDataByType("equ_model_info").getData();
//                    List<SysDictData> sysDictDataList = remoteDictDataService.getDictDataByType("equ_model_info", "inner").getData();
                    List<SysDictData> sysDictDataList = DictUtils.getDictDataByType("equ_model_info");

                    if (null != sysDictDataList) {
                        List<String> collect = sysDictDataList.stream().map(SysDictData::getDictLabel).map(String::trim).collect(Collectors.toList());
                        if (collect.contains(userList.get(i).getModelId().trim())) {
                            for (SysDictData sysDictData : sysDictDataList) {
                                if (StringUtils.equals(sysDictData.getDictLabel().trim(), userList.get(i).getModelId().trim())) {
                                    target.setModelId(sysDictData.getDictValue());
                                    break;
                                }
                            }
                        }
                    }
                    EquInfoEntity equInfoEntities = equInfoMapper.selectEquInfoByTypeId(equType.getTypeId(), String.valueOf(deptId));
//                    SysDept dept = remoteDeptService.getDeptInfo(deptId, "inner").getData();
                    SysDept dept = deptService.selectDeptById(deptId);
//                    List<SysDictData> jhbhCodeList = sysDictDataService.getDictDataByType("jhbh_code").getData();
//                    List<SysDictData> jhbhCodeList = remoteDictDataService.getDictDataByType("jhbh_code", "inner").getData();
                    List<SysDictData> jhbhCodeList = DictUtils.getDictDataByType("jhbh_code");

                    if (null != jhbhCodeList) {
                        List<String> collect = jhbhCodeList.stream().map(SysDictData::getDictLabel).collect(Collectors.toList());
                        if (collect.contains(dept.getAdminCode())) {
                            for (SysDictData sysDictData : jhbhCodeList) {
                                if (StringUtils.equals(sysDictData.getDictLabel(), dept.getAdminCode())) {
                                    if (null != equInfoEntities) {
                                        target.setEquCode(String.valueOf(Integer.parseInt(equInfoEntities.getEquCode()) + 1));
                                    } else {
                                        target.setEquCode("50" + sysDictData.getDictValue() + "001");
                                    }
                                    break;
                                }
                            }
                        } else {
                            target.setEquCode("");// todo: 未在编号内部门警航编号不做生成
                        }
                    }
                    equInfoMapper.insertEquInfo(target);
                    successNum++;


                } else {
                    failureNum++;
                    failureMsg.append("第" + (i + 1) + "条数据" + "已存在, ");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "第" + (i + 1) + "条数据" + "导入失败:";
                failureMsg.append(msg + e.getMessage() + ",");
            }
        }

        if (failureNum > 0) {
            failureMsg.insert(0, "数据导入失败！共 " + failureNum + "条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString(), 10002);
        } else {
            successMsg.insert(0, "数据已全部导入成功！共 " + successNum + " 条");
        }
        return successMsg.toString();
    }


}
