package com.base.biz.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.base.biz.mapper.InstanceInfoMapper;
import com.base.biz.mapper.PayloadInfoMapper;
import com.base.biz.mapper.SatelliteInfoMapper;
import com.base.biz.model.dto.SatelliteInfoDTO;
import com.base.biz.model.dto.request.QueryPayloadAddDTO;
import com.base.biz.model.dto.request.QuerySatelliteAddDTO;
import com.base.biz.model.dto.request.SatelliteInfoDTOListReq;
import com.base.biz.model.dto.request.UpdateSatelliteDTOReq;
import com.base.biz.model.dto.response.SatellitePayloadDTO;
import com.base.biz.model.dto.response.SatelliteTypeListDTO;
import com.base.biz.model.entity.InstanceInfo;
import com.base.biz.model.entity.PayloadInfo;
import com.base.biz.model.entity.SatelliteInfo;
import com.base.biz.model.entity.TypeListBiz;
import com.base.biz.model.enums.*;
import com.base.orbit.utils.BizUtils;
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 javax.annotation.Resource;
import java.util.*;

@Service
public class SatelliteService extends ServiceImpl<SatelliteInfoMapper, SatelliteInfo> {

    @Resource
    SatelliteInfoMapper satelliteInfoMapper;
    @Resource
    PayloadInfoMapper payloadInfoMapper;

    public List<SatelliteInfoDTO> getSatelliteInfoDTOList(SatelliteInfoDTOListReq req) {
        List<SatelliteInfo> satelliteInfoDTOList = satelliteInfoMapper.getSatelliteInfoDTOList(req);
        return BizUtils.copyEntityToDto(SatelliteInfoDTO.class, satelliteInfoDTOList);
    }

    public void updateSatelliteInfo() {

    }

    public List<SatelliteInfoDTO> listByIds(List<String> satelliteCodes) {
        List<SatelliteInfo> list = satelliteInfoMapper.listByIds(satelliteCodes);
        List<SatelliteInfoDTO> res = new ArrayList<>();
        list.forEach(e -> {
            SatelliteInfoDTO satelliteInfoDTO = new SatelliteInfoDTO();
            BeanUtils.copyProperties(e, satelliteInfoDTO);
            res.add(satelliteInfoDTO);
        });
        return res;
    }

    public List<SatelliteInfoDTO> queryAllSatelliteByIds(List<String> satelliteids) {
        List<SatelliteInfo> list = satelliteInfoMapper.queryAllSatelliteByIds(satelliteids);
        List<SatelliteInfoDTO> res = new ArrayList<>();
        list.forEach(e -> {
            SatelliteInfoDTO satelliteInfoDTO = new SatelliteInfoDTO();
            BeanUtils.copyProperties(e, satelliteInfoDTO);
            res.add(satelliteInfoDTO);
        });
        return res;
    }

    public SatelliteInfo getSatelliteInfoByCode(String satelliteCode) {
        return satelliteInfoMapper.getSatelliteInfoByCode(satelliteCode);
    }

    public void update(UpdateSatelliteDTOReq updateSatelliteDTOReq) {
        SatelliteInfo satelliteInfo = new SatelliteInfo();
        BeanUtils.copyProperties(updateSatelliteDTOReq, satelliteInfo);
        satelliteInfoMapper.updateById(satelliteInfo);
    }

    public SatelliteTypeListDTO querySatelliteTypeListDTO() {
        SatelliteTypeListDTO satelliteTypeListDTO = new SatelliteTypeListDTO();
        try {
            satelliteTypeListDTO.setSatelliteUserType(TypeListBiz.getTypeList(SatelliteUserTypeEnums.values()));
            satelliteTypeListDTO.setFunctionUseType(TypeListBiz.getTypeList(SatelliteFunctionEnums.values()));
            satelliteTypeListDTO.setState(TypeListBiz.getTypeList(SatelliteStateEnums.values()));
            satelliteTypeListDTO.setOrbitType(TypeListBiz.getTypeList(OrbitTypeEnums.values()));
            satelliteTypeListDTO.setSatelliteType(TypeListBiz.getTypeList(SatelliteTypeEnums.values()));
            satelliteTypeListDTO.setStatus(TypeListBiz.getTypeList(SatelliteStatusEnums.values()));
            satelliteTypeListDTO.setZczjlx(TypeListBiz.getTypeList(RelayTypeEnums.values()));
            satelliteTypeListDTO.setControlPolarityMethod(TypeListBiz.getTypeList(ControlPolarityMethodEnums.values()));
            satelliteTypeListDTO.setKpmode(TypeListBiz.getTypeList(KpModeEnums.values()));
            satelliteTypeListDTO.setReceivePolarityMethod(TypeListBiz.getTypeList(ReceivePolarityMethodEnums.values()));
            satelliteTypeListDTO.setZhlx(TypeListBiz.getTypeList(SatelliteLoadTypeEnums.values()));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }

        return satelliteTypeListDTO;
    }

    @Transactional
    public void addSatellite(QuerySatelliteAddDTO querySatelliteAddDTO) {
        //新增卫星
        SatelliteInfo satelliteInfo = new SatelliteInfo();
        BeanUtils.copyProperties(querySatelliteAddDTO, satelliteInfo);
        String satelliteId = UUID.randomUUID().toString();
        satelliteInfo.setId(satelliteId);
        satelliteInfo.setCreateTime(new Date());
        satelliteInfoMapper.insert(satelliteInfo);
        //新增载荷
        List<QueryPayloadAddDTO> payloadDTOList = querySatelliteAddDTO.getPayloadAddDTOList();
        if (CollectionUtils.isEmpty(payloadDTOList)) {
            return;
        }
        List<PayloadInfo> list = new ArrayList<>();
        for (QueryPayloadAddDTO satellitePayloadDTO : payloadDTOList) {
            PayloadInfo payloadInfo = new PayloadInfo();
            BeanUtils.copyProperties(satellitePayloadDTO, payloadInfo);
            payloadInfo.setId(UUID.randomUUID().toString());
            payloadInfo.setSatelliteId(satelliteId);
            payloadInfo.setOverLap(0.0);
            payloadInfo.setCreateTime(new Date());
            list.add(payloadInfo);
        }
        //payloadInfoMapper.batch(list);
        for (PayloadInfo payloadInfo : list) {
            payloadInfoMapper.insert(payloadInfo);
        }

    }

    public void updateSatellite(SatelliteInfoDTO param) {
        SatelliteInfo satelliteInfo = new SatelliteInfo();
        BeanUtils.copyProperties(param, satelliteInfo);
        satelliteInfoMapper.updateById(satelliteInfo);
    }

    public List<SatellitePayloadDTO> getSatellitePayloadDTOList(String type) {
        List<String> list = Arrays.asList(type.split(","));
        if (list.size() == 0 || StringUtils.isEmpty(type)) {
            list = null;
        }

        return satelliteInfoMapper.selectSatellitePayloadDTOList(list);
    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteSatellite(List<String> ids) {
        satelliteInfoMapper.deleteBatchIds(ids);
        payloadInfoMapper.deleteBySatelliteId(ids);
    }
}
