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

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.djhk.common.core.domain.model.LoginUser;
import com.djhk.common.exception.ServiceException;
import com.djhk.common.utils.DateUtils;
import com.djhk.common.utils.SecurityUtils;
import com.djhk.common.utils.bean.BeanUtils;
import com.djhk.system.service.ISysDeptService;
import com.djhk.system.service.ISysUserService;
import com.djhk.uav.uav.mapper.UavPilotMapper;
import com.djhk.uav.uav.model.UavPilot;
import com.djhk.uav.uav.service.IUavPilotService;
import com.djhk.uav.uav.mapper.UavPilotMapper;
import com.djhk.uav.uav.model.UavPilot;
import com.djhk.uav.uav.service.IUavPilotService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 飞手管理Service业务层处理
 *
 * @author jiachengshuai
 * @date 2022-08-30
 */
@Service
@Slf4j
public class UavPilotServiceImpl extends ServiceImpl<UavPilotMapper, UavPilot>  implements IUavPilotService
{

    @Autowired
    private UavPilotMapper uavPilotMapper;

    @Autowired
    private UavInfoServiceImpl uavInfoService;



    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysDeptService deptService;

    /**
     * 查询飞手管理
     *
     * @param id 飞手管理主键
     * @return 飞手管理
     */
    @Override
    public UavPilot selectUavPilotById(Long id)
    {
        UavPilot uavPilot = uavPilotMapper.selectUavPilotById(id);
        return uavPilot;
    }


    /**
     * 查询飞手管理列表
     *
     * @param uavPilot 飞手管理
     * @return 飞手管理
     */
    @Override
    public List<UavPilot> selectUavPilotList(UavPilot uavPilot)
    {
        return  uavPilotMapper.selectUavPilotList(uavPilot);
    }

    /**
     * 新增飞手管理
     *
     * @param uavPilot 飞手管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertUavPilot(UavPilot uavPilot)
    {
        uavPilot.setCreateBy(SecurityUtils.getUsername());
        uavPilot.setCreateTime(DateUtils.getNowDate());
        return uavPilotMapper.insertUavPilot(uavPilot);
    }

    /**
     * 修改飞手管理
     *
     * @param uavPilot 飞手管理
     * @return 结果
     */
    @Override
    public int updateUavPilot(UavPilot uavPilot)
    {
        uavPilot.setUpdateTime(DateUtils.getNowDate());
        uavPilot.setUpdateBy(SecurityUtils.getUsername());
        return uavPilotMapper.updateUavPilot(uavPilot);
    }

    /**
     * 批量删除飞手管理
     *
     * @param ids 需要删除的飞手管理主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteUavPilotByIds(Long[] ids)
    {
        return uavPilotMapper.deleteUavPilotByIds(ids);
    }

    /**
     * 删除飞手管理信息
     *
     * @param id 飞手管理主键
     * @return 结果
     */
    @Override
    public int deleteUavPilotById(Long id)
    {
        return uavPilotMapper.deleteUavPilotById(id);
    }

//    @Override
//    public List<UavPilot> selectUavPilotExportList(UavPilotExportVo uavPilotExportVo)
//    {
//        return  uavPilotMapper.selectUavPilotExportList(uavPilotExportVo);
//    }

    @Override
    public String importData(List<UavPilot> 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 = SecurityUtils.getDeptId();
        for (int i = 0; i < userList.size(); i++) {

            try {
                // 验证是否存在数据
                UavPilot target = new UavPilot();
                BeanUtils.copyBeanProp(target, userList.get(i));
                target.setCreateTime(DateUtils.getNowDate());
                uavPilotMapper.insertUavPilot(target);
                successNum++;
//                EquInfoEntity equInfoEntity = equInfoMapper.selectEquInfoByEquSn(target);
//                if (equInfoEntity == null) {
//
//                    target.setOrganId(loginUser.getSysUser().getDeptId());
//                    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();
//
//                    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 (org.apache.commons.lang.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();
////                    List<SysDictData> jhbhCodeList = sysDictDataService.getDictDataByType("jhbh_code").getData();
//                    List<SysDictData> jhbhCodeList = remoteDictDataService.getDictDataByType("jhbh_code", "inner").getData();
//
//                    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();
    }

}
