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

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.djhk.common.api.domain.SysFile;
import com.djhk.common.core.domain.R;
import com.djhk.common.utils.DateUtils;
import com.djhk.common.utils.SecurityUtils;
import com.djhk.common.utils.StringUtils;
import com.djhk.system.service.ISysDeptService;
import com.djhk.system.service.ISysUserService;
import com.djhk.uav.airspace.domain.UavOperator;
import com.djhk.uav.uav.domain.UavOperatorExportVo;
import com.djhk.uav.uav.mapper.UavOperatorMapper;
import com.djhk.uav.uav.model.UavOperatorAbility;
import com.djhk.uav.uav.service.IUavOperatorService;
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.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 飞手管理Service业务层处理
 *
 * @author jiachengshuai
 * @date 2022-08-30
 */
@Service
@Slf4j
public class UavOperatorServiceImpl extends ServiceImpl<UavOperatorMapper, UavOperator>  implements IUavOperatorService
{

    @Autowired
    private UavOperatorMapper uavOperatorMapper;

    @Autowired
    private UavInfoServiceImpl uavInfoService;

    @Autowired
    private UavOperatorAbilityServiceImpl uavOperatorAbilityService;

//    @Autowired
//    private RemoteUserService userService;
//
//    @Autowired
//    private RemoteDeptService deptService;

    @Autowired
    private ISysUserService userService;


    @Autowired
    private ISysDeptService deptService;


    /**
     * 查询飞手管理
     *
     * @param id 飞手管理主键
     * @return 飞手管理
     */
    @Override
    public UavOperator selectUavOperatorById(Long id)
    {
        UavOperator uavOperator = uavOperatorMapper.selectUavOperatorById(id);
//        List<UavOperatorAbility> operatorAbilityList = uavOperator.getOperatorAbilityList();
//        List<String> abilityNameList = new ArrayList<>();
//        operatorAbilityList.forEach(x -> {
//            abilityNameList.add(x.getAbilityName());
//        });
//        uavOperator.setAbilityNameList(abilityNameList);

        List<Long> idList = new ArrayList<>();
        if(uavOperator != null && StringUtils.isNotEmpty(uavOperator.getCertificateFileId())){
            idList.add(Long.parseLong(uavOperator.getCertificateFileId()));
        }
//        if(CollUtil.isNotEmpty(idList)){
//            Long[] ids = idList.toArray(new Long[idList.size()]);
//            R<List<SysFile>> fileListR = deptService.getFileList(ids);
//            if(StringUtils.isNotNull(fileListR) || StringUtils.isNotNull(fileListR.getData())) {
//                List<SysFile> sysFiles = fileListR.getData();
//                for(SysFile sysFile : sysFiles){
//                    if(sysFile.getId().toString().equals(uavOperator.getCertificateFileId().toString())){
//                        Map<String,String> map = new HashMap<>();
//                        map.put("name",sysFile.getFileName());
//                        map.put("url",sysFile.getPresignedURL());
//                        uavOperator.setCertificateMap(map);
//                    }
//                }
//            }
//        }
        return uavOperator;
    }

    public UavOperator selectOperatorById(Long id)
    {
        UavOperator uavOperator = uavOperatorMapper.selectUavOperatorById(id);
//        List<UavOperatorAbility> operatorAbilityList = uavOperator.getOperatorAbilityList();
//        List<String> abilityNameList = new ArrayList<>();
//        operatorAbilityList.forEach(x -> {
//            abilityNameList.add(x.getAbilityName());
//        });
//        uavOperator.setAbilityNameList(abilityNameList);

//        List<Long> idList = new ArrayList<>();
//        if(StringUtils.isNotEmpty(uavOperator.getCertificateFileId())){
//            idList.add(Long.parseLong(uavOperator.getCertificateFileId()));
//        }
//        if(CollUtil.isNotEmpty(idList)){
//            Long[] ids = idList.toArray(new Long[idList.size()]);
//            R<List<SysFile>> fileListR = deptService.getFileList(ids);
//            if(StringUtils.isNotNull(fileListR) || StringUtils.isNotNull(fileListR.getData())) {
//                List<SysFile> sysFiles = fileListR.getData();
//                for(SysFile sysFile : sysFiles){
//                    if(sysFile.getId().toString().equals(uavOperator.getCertificateFileId().toString())){
//                        Map<String,String> map = new HashMap<>();
//                        map.put("name",sysFile.getFileName());
//                        map.put("url",sysFile.getPresignedURL());
//                        uavOperator.setCertificateMap(map);
//                    }
//                }
//            }
//        }
        return uavOperator;
    }

    /**
     * 查询飞手管理列表
     *
     * @param uavOperator 飞手管理
     * @return 飞手管理
     */
    @Override
    public List<UavOperator> selectUavOperatorList(UavOperator uavOperator)
    {
        return  uavOperatorMapper.selectUavOperatorList(uavOperator);
    }

    /**
     * 新增飞手管理
     *
     * @param uavOperator 飞手管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertUavOperator(UavOperator uavOperator)
    {
//        QueryWrapper<UavOperator> wrapper = new QueryWrapper<>();
//        wrapper.eq("del_flag","0");
//        wrapper.eq("user_id",uavOperator.getUserId());
//        wrapper.eq("police_no",uavOperator.getPoliceNo());
//        wrapper.and(wrapper1 -> wrapper1.eq("user_id",uavOperator.getUserId()).or().eq("uav_num",uavOperator.getUavNum()));
//        List<UavOperator> operatorList = uavOperatorMapper.selectList(wrapper);

        UavOperator uavOperator1 = new UavOperator();
        uavOperator1.setDelFlag("0");
        uavOperator1.setPoliceNo(uavOperator.getPoliceNo());
        List<UavOperator> operatorList = uavOperatorMapper.selectLists(uavOperator1);
        if(operatorList.size() > 0){
            return -1;
        }

        uavOperator.setCreateBy(SecurityUtils.getUsername());
        uavOperator.setCreateTime(DateUtils.getNowDate());
        int result =  uavOperatorMapper.insertUavOperator(uavOperator);
        Long uavOperatorId = uavOperator.getId();

        //新增飞手特长表
        String abilityName = uavOperator.getAbilityName();
        if(StringUtils.isNotEmpty(abilityName)){
            String[] abilityNameArray = abilityName.trim().split(",");
            List<UavOperatorAbility> abilityList = new ArrayList<>();
            for(int i = 0;i < abilityNameArray.length; i++){
                UavOperatorAbility ability = new UavOperatorAbility();
                ability.setUavOperatorId(uavOperatorId);
                ability.setAbilityName(abilityNameArray[i]);
                ability.setCreateBy(SecurityUtils.getUsername());
                ability.setCreateTime(DateUtils.getNowDate());
                ability.setDelFlag("0");
                abilityList.add(ability);
            }
            uavOperatorAbilityService.insertUavOperatorAbilityList(abilityList);
        }
        return result;
    }

    /**
     * 修改飞手管理
     *
     * @param uavOperator 飞手管理
     * @return 结果
     */
    @Override
    public int updateUavOperator(UavOperator uavOperator)
    {
        uavOperator.setUpdateTime(DateUtils.getNowDate());
        uavOperator.setUpdateBy(SecurityUtils.getUsername());
        return uavOperatorMapper.updateUavOperator(uavOperator);
    }

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

        return uavOperatorMapper.deleteUavOperatorByIds(ids);

    }

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

    @Override
    public List<UavOperator> selectUavOperatorExportList(UavOperatorExportVo uavOperatorExportVo)
    {
        return  uavOperatorMapper.selectUavOperatorExportList(uavOperatorExportVo);
    }

}
