package com.base.track.task.handler;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.base.api.client.InstanceRpcService;
import com.base.api.client.SatelliteRpcService;
import com.base.api.model.InstanceRpcDTO;
import com.base.api.model.SatelliteAndInstanceRpcDTO;
import com.base.api.model.SatelliteRpcDTO;
import com.base.api.model.YxInfoBasisSatelliteRpcDTO;
import com.base.orbit.utils.FileUtil;
import com.base.orbit.utils.IdGenerator;
import com.base.orbit.utils.StringUtil;
import com.base.task.handler.TaskHandler;
import com.base.task.model.Enums.TaskTypeEnum;
import com.base.task.model.Mapping;
import com.base.task.model.Task;
import com.base.track.config.sharding.ShardingAlgorithmTool;
import com.base.track.config.sharding.enums.ShardingTableCacheEnum;
import com.base.track.model.entity.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Mapping(taskTypeEnum = TaskTypeEnum.INSTANCE)
@Service
@Slf4j
public class InstanceTaskHandler implements TaskHandler {
    @Autowired
    private InstanceRpcService instanceRpcService;


    @Override
    public void taskExecute(Task task) {
        try {
            log.info("推算瞬根入库");
            String startTime=task.getStartTime();
            String endTime=task.getEndTime();

                DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

                FileInputStream fileInputStream = new FileInputStream(task.getResultDataSavePath()+task.getResultDataSaveName());
                InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                System.out.println("===========读取到瞬根数据===========");
                // 每行数据
                String result;
                // 执行的行数
                int line = 0;

                // 遍历结果文件数据
            InstanceRpcDTO instanceRpcDTO= new InstanceRpcDTO();
            instanceRpcDTO.setId(String.valueOf(IdGenerator.getId()));
            instanceRpcDTO.setSatelliteCode(task.getSatelliteCode());
                while ((result = bufferedReader.readLine()) != null) {
                    // 数据转换
                    List<String> r = FileUtil.getLineArray(result);

                    if (line==0){
                        String dateStr = r.get(0) + " " + r.get(1);
                        Date date = sdf.parse(dateStr);
                        instanceRpcDTO.setEpochTime( new Date(date.getTime()+8*3600*1000));
                    }
                    if (line==1){
                        instanceRpcDTO.setOrbitNum(r.get(0));
                    }
                    if (line==2){
                        instanceRpcDTO.setSemiMajorAxis(r.get(0));
                    }
                    if (line==3){
                        instanceRpcDTO.setEccentricity(r.get(0));
                    }
                    if (line==4){
                        instanceRpcDTO.setInclination(r.get(0));
                    }
                    if (line==5){
                        instanceRpcDTO.setRaan(r.get(0));
                    }
                    if (line==6){
                        instanceRpcDTO.setArgumentOfPerigee(r.get(0));
                    }
                    if (line==7){
                        instanceRpcDTO.setMeanAnoMaly(r.get(0));
                    }
                    line++;
                }
                String codemin=task.getSatelliteCode();
                List<String> satelliteCodes=new ArrayList<>();
            satelliteCodes.add(codemin);
            List<InstanceRpcDTO> instanceRpcDTOS= instanceRpcService.getListBySatelliteCode(satelliteCodes);
            if (!CollectionUtils.isEmpty(instanceRpcDTOS)){
                instanceRpcService.deleteBySatelliteCode(satelliteCodes.get(0));
            }
            instanceRpcService.save(instanceRpcDTO);
                //发送消息
                log.info("计算完成");
                /*task.getCountDownLatch().countDown();*/
                System.out.println(task);


        }catch (Exception e){
            log.error(e.getMessage(),e);
        }finally {
            task.getCountDownLatch().countDown();
        }

    }
}
