package com.base.biz.mq.handler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.base.biz.model.dto.request.BaseDmzBaseMsgDTO;
import com.base.biz.model.dto.request.BaseInstanceBaseMsgDTO;
import com.base.biz.model.dto.request.BaseTxBaseMsgDTO;
import com.base.biz.model.dto.request.BaseWxBaseMsgDTO;
import com.base.biz.model.entity.AntennaInfo;
import com.base.biz.model.entity.InstanceInfo;
import com.base.biz.model.entity.SatelliteInfo;
import com.base.biz.model.entity.StationInfo;
import com.base.biz.mq.factory.HandlerMqFactory;
import com.base.biz.service.AntennaService;
import com.base.biz.service.InstanceService;
import com.base.biz.service.SatelliteService;
import com.base.biz.service.StationService;
import com.base.orbit.log.ServerNameEnum;
import com.base.orbit.mq.model.BaseHeadMqDTO;
import com.base.orbit.mq.topics.TopicConst;
import com.base.orbit.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author xj
 * @date 2023/5/22 11:24
 * @Description: 基础服务列表数据
 */
@Component
@Slf4j
public class BaseMqProducerHandler extends AbstractMqInfoHandler {

    @Resource
    private AntennaService antennaService;
    @Resource
    private StationService stationService;
    @Resource
    private InstanceService instanceService;
    @Resource
    private SatelliteService satelliteService;

    private final static String ONE = "1";
    private final static String TWO = "2";
    private final static String THREE = "3";

    @Override
    public void process(List<String> var1) {
        String message = var1.get(0);
        log.info("receive baseInfo message: " + message, ServerNameEnum.BASE_SERVER);
        //入库
        JSONObject obj = JSONObject.parseObject(message);
        JSONObject bodyObj = obj.getJSONObject("messageBody");
        if (Objects.nonNull(bodyObj)) {
            buildTxBodyDTO(bodyObj);
            buildDmzBodyDTO(bodyObj);
        }
    }

    /**
     * 构建请求头信息
     *
     * @param headObj 传入报文
     * @throws Exception
     */
    public static BaseHeadMqDTO buildHeadDTO(JSONObject headObj) {
        BaseHeadMqDTO baseHeadMqDTO = new BaseHeadMqDTO();
        baseHeadMqDTO.setMessageType(headObj.getString("messageType"));
        baseHeadMqDTO.setCreationTime(headObj.getString("creationTime"));
        baseHeadMqDTO.setRecipient(headObj.getString("recipient"));
        baseHeadMqDTO.setMessageId(headObj.getString("messageId"));
        baseHeadMqDTO.setOriginator(headObj.getString("originator"));
        return baseHeadMqDTO;
    }

    /**
     * 构建天线信息
     *
     * @param bodyObj 传入报文
     * @return
     */
    public void buildTxBodyDTO(JSONObject bodyObj) {
        String txBaseMsgs = bodyObj.getString("txBaseMsg");
        if (StringUtils.isNotEmpty(txBaseMsgs)) {
            List<BaseTxBaseMsgDTO> baseTxBaseMsgDTOS = JSON.parseArray(txBaseMsgs, BaseTxBaseMsgDTO.class);
            baseTxBaseMsgDTOS.stream().forEach(baseTxBaseMsgDTO -> {
                AntennaInfo antennaInfo = new AntennaInfo();
                BeanUtils.copyProperties(baseTxBaseMsgDTO, antennaInfo);
                antennaInfo.setDmzdh(baseTxBaseMsgDTO.getSczdh());
                antennaInfo.setJssl(baseTxBaseMsgDTO.getMsl());
                antennaInfo.setCreateTime(DateUtils.getNowDate());
                antennaInfo.setUpdateTime(DateUtils.getNowDate());
                if(StringUtils.isEmpty(baseTxBaseMsgDTO.getOperaType()) || baseTxBaseMsgDTO.getOperaType().equals(THREE)){
                    antennaInfo.setCreateTime(DateUtils.getNowDate());
                    antennaInfo.setUpdateTime(DateUtils.getNowDate());
                    antennaService.save(antennaInfo);
                }else {
                    if (baseTxBaseMsgDTO.getOperaType().equals(ONE)) {
                        antennaService.removeById(antennaInfo.getId());
                    }
                    if (baseTxBaseMsgDTO.getOperaType().equals(TWO)) {
                        antennaInfo.setUpdateTime(DateUtils.getNowDate());
                        antennaService.updateById(antennaInfo);
                    }
                }
            });
        }
    }

    /**
     * 构建地面站信息
     *
     * @throws Exception
     */
    public void buildDmzBodyDTO(JSONObject bodyObj) {
        String dmzBaseMsg = bodyObj.getString("dmzBaseMsg");
        if (StringUtils.isNotEmpty(dmzBaseMsg)) {
            List<BaseDmzBaseMsgDTO> baseDmzBaseMsgDTOS = JSON.parseArray(dmzBaseMsg, BaseDmzBaseMsgDTO.class);
            baseDmzBaseMsgDTOS.stream().forEach(baseDmzBaseMsgDTO -> {
                StationInfo stationInfo = new StationInfo();
                stationInfo.setId(baseDmzBaseMsgDTO.getId());
                stationInfo.setStationCode(baseDmzBaseMsgDTO.getDmzdh());
                stationInfo.setStationName(baseDmzBaseMsgDTO.getDmzmc());
                stationInfo.setResourceType(baseDmzBaseMsgDTO.getZylx());
                stationInfo.setMissionType(baseDmzBaseMsgDTO.getRwlx());
                stationInfo.setLongitude(baseDmzBaseMsgDTO.getLon());
                stationInfo.setLatitude(baseDmzBaseMsgDTO.getLat());
                stationInfo.setAltitude(baseDmzBaseMsgDTO.getAlt());
                stationInfo.setMinTraceElevation(baseDmzBaseMsgDTO.getZxgzyj());
                stationInfo.setMinWorkElevation(baseDmzBaseMsgDTO.getZxjsyj());
                stationInfo.setBasebandType(baseDmzBaseMsgDTO.getJdlx());
                stationInfo.setReceivedRate(baseDmzBaseMsgDTO.getScsl());
                stationInfo.setSundialCorer(baseDmzBaseMsgDTO.getRlj());
                stationInfo.setRelayType(Integer.valueOf(baseDmzBaseMsgDTO.getZjxnl()));
                if(StringUtils.isEmpty(baseDmzBaseMsgDTO.getOperaType()) || baseDmzBaseMsgDTO.getOperaType().equals(THREE)){
                    stationInfo.setCreateTime(DateUtils.getNowDate());
                    stationInfo.setUpdateTime(DateUtils.getNowDate());
                    stationService.save(stationInfo);
                }else {
                    if (baseDmzBaseMsgDTO.getOperaType().equals(ONE)) {
                        stationService.removeById(stationInfo.getId());
                    }
                    if (baseDmzBaseMsgDTO.getOperaType().equals(TWO)) {
                        stationInfo.setUpdateTime(DateUtils.getNowDate());
                        stationService.updateById(stationInfo);
                    }
                }
            });
        }
    }

    /**
     * 构建wx瞬根信息
     *
     * @param bodyObj 传入报文
     * @return
     */
    public void buildWxInstanceBodyDTO(JSONObject bodyObj) {
        String wxInstanceBaseMsgs = bodyObj.getString("wxInstanceBaseMsg");
        if (StringUtils.isNotEmpty(wxInstanceBaseMsgs)) {
            List<BaseInstanceBaseMsgDTO> baseWxInstanceMsgDTOS = JSON.parseArray(wxInstanceBaseMsgs, BaseInstanceBaseMsgDTO.class);
            baseWxInstanceMsgDTOS.stream().forEach(baseInstanceBaseMsgDTO -> {
                InstanceInfo instanceInfo = new InstanceInfo();
                instanceInfo.setId(baseInstanceBaseMsgDTO.getId());
                instanceInfo.setSatelliteCode(baseInstanceBaseMsgDTO.getWxdh());
                instanceInfo.setOrbitNum(baseInstanceBaseMsgDTO.getQh());
                instanceInfo.setEpochTime(DateUtils.string2LocalDate(DateUtils.TimeStampCastToStrDate(Long.valueOf(baseInstanceBaseMsgDTO.getLysk())), DateUtils.YYYY_MM_DD_HH_MM_SS));
                instanceInfo.setSemiMajorAxis(baseInstanceBaseMsgDTO.getGdbj());
                instanceInfo.setEccentricity(baseInstanceBaseMsgDTO.getPxl());
                instanceInfo.setInclination(baseInstanceBaseMsgDTO.getGdqj());
                instanceInfo.setArgumentOfPerigee(baseInstanceBaseMsgDTO.getJddfj());
                instanceInfo.setMeanAnoMaly(baseInstanceBaseMsgDTO.getPjdj());
                instanceInfo.setRaan(baseInstanceBaseMsgDTO.getSjdcj());
                instanceInfo.setCreateTime(DateUtils.strCastToDate(baseInstanceBaseMsgDTO.getCreatTime()));
                instanceInfo.setUpdateTime(DateUtils.strCastToDate(baseInstanceBaseMsgDTO.getUpdateTime()));
                if (baseInstanceBaseMsgDTO.getOperaType().equals(ONE)) {
                    instanceService.removeById(instanceInfo.getId());
                }
                if (baseInstanceBaseMsgDTO.getOperaType().equals(TWO)) {
                    instanceService.updateById(instanceInfo);
                }
                if (baseInstanceBaseMsgDTO.getOperaType().equals(THREE)) {
                    instanceService.save(instanceInfo);
                }
            });
        }
    }

    /**
     * 构建wx信息
     *
     * @param bodyObj 传入报文
     * @return
     */
    public void buildWxInfoBodyDTO(JSONObject bodyObj) {
        String wxBaseMsgs = bodyObj.getString("wxBaseMsg");
        if (StringUtils.isNotEmpty(wxBaseMsgs)) {
            List<BaseWxBaseMsgDTO> baseWxBaseMsgDTOS = JSON.parseArray(wxBaseMsgs, BaseWxBaseMsgDTO.class);
            baseWxBaseMsgDTOS.stream().forEach(baseWxBaseMsgDTO -> {
                SatelliteInfo satelliteInfo = new SatelliteInfo();
                satelliteInfo.setId(baseWxBaseMsgDTO.getId());
                satelliteInfo.setSatelliteCode(baseWxBaseMsgDTO.getWxdh());
                satelliteInfo.setSatelliteName(baseWxBaseMsgDTO.getWxzwmc());
                satelliteInfo.setLoadType(baseWxBaseMsgDTO.getZhlx());
                satelliteInfo.setAltitude(baseWxBaseMsgDTO.getGdgd());
                satelliteInfo.setMass(baseWxBaseMsgDTO.getWxzl());
                satelliteInfo.setGroundResolution(baseWxBaseMsgDTO.getFbl());
                satelliteInfo.setSwathWidth(baseWxBaseMsgDTO.getFk());
                satelliteInfo.setWindArea(baseWxBaseMsgDTO.getYfmj());
                satelliteInfo.setShineArea(baseWxBaseMsgDTO.getGymj());
                satelliteInfo.setFixedVolume(baseWxBaseMsgDTO.getGcrl());
                satelliteInfo.setReceiveRelayType(baseWxBaseMsgDTO.getZczjlx());
                satelliteInfo.setEarthGravityFieldModeOrde1(baseWxBaseMsgDTO.getDqzlcmxjs1());
                satelliteInfo.setEarthGravityFieldModeOrde2(baseWxBaseMsgDTO.getDqzlcmxjs2());
                satelliteInfo.setMaxRollAngle(baseWxBaseMsgDTO.getZzcbj());
                satelliteInfo.setMinRollAngle(baseWxBaseMsgDTO.getZxcbj());
                satelliteInfo.setMaxPitchAngle(baseWxBaseMsgDTO.getZdfyj());
                satelliteInfo.setMinPitchAngle(baseWxBaseMsgDTO.getZxfyj());
                satelliteInfo.setDataPassSpeed(baseWxBaseMsgDTO.getScsl());
                satelliteInfo.setEirp(baseWxBaseMsgDTO.getEirp());
                satelliteInfo.setXxsl(baseWxBaseMsgDTO.getXxsl());
                satelliteInfo.setCreateTime(DateUtils.strCastToDate(baseWxBaseMsgDTO.getCreatTime()));
                satelliteInfo.setUpdateTime(DateUtils.strCastToDate(baseWxBaseMsgDTO.getUpdateTime()));
                if (baseWxBaseMsgDTO.getOperaType().equals(ONE)) {
                    satelliteService.removeById(satelliteInfo.getId());
                }
                if (baseWxBaseMsgDTO.getOperaType().equals(TWO)) {
                    satelliteService.updateById(satelliteInfo);
                }
                if (baseWxBaseMsgDTO.getOperaType().equals(THREE)) {
                    satelliteService.save(satelliteInfo);
                }
            });
        }
    }


    @Override
    public void afterPropertiesSet() throws Exception {
        HandlerMqFactory.register(TopicConst.BASE_TOPIC, this);
    }
}
