package com.base.biz.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.base.api.client.PointRpcService;
import com.base.api.model.PointRpcDTO;
import com.base.api.model.PointTypeEnums;
import com.base.api.model.request.PointListRpcReqDTO;
import com.base.api.model.request.TaskStateNotice;
import com.base.biz.mapper.FzTaskInfoMapper;
import com.base.biz.model.dto.FzTaskDTO;
import com.base.biz.model.dto.PointDTO;
import com.base.biz.model.dto.request.FzTaskInfoReqDTO;
import com.base.biz.model.dto.request.FzTaskInfoUpdateDTO;
import com.base.biz.model.entity.FzTaskInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class FzTaskService extends ServiceImpl<FzTaskInfoMapper, FzTaskInfo> {

    @Resource
    FzTaskInfoMapper fzTaskInfoMapper;
    @Resource
    PointRpcService pointRpcService;

    public List<FzTaskDTO> getFzTaskDTOList(FzTaskInfoReqDTO req) {
        List<FzTaskInfo> fzTaskInfoList = fzTaskInfoMapper.getFzTaskInfoList(req);
        return formatFzTaskDTO(fzTaskInfoList);
    }

    public void updateFzTask(FzTaskInfoUpdateDTO fzTaskInfoUpdateDTO) {
        fzTaskInfoMapper.updateTaskInfo(fzTaskInfoUpdateDTO);
    }

    public void updateTaskStepState(TaskStateNotice taskStateNotice) {
        fzTaskInfoMapper.updateTaskStepState(taskStateNotice);
    }

    public void deleteTaskStepState(TaskStateNotice taskStateNotice) {
        QueryWrapper<FzTaskInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("req_id",taskStateNotice.getBizTaskId());
        fzTaskInfoMapper.delete(queryWrapper);
    }


    private List<FzTaskDTO> formatFzTaskDTO(List<FzTaskInfo> fzTaskInfoList) {

        List<FzTaskDTO> list = new ArrayList<>();
        for (FzTaskInfo fzTaskInfo : fzTaskInfoList) {
            FzTaskDTO fzTaskDTO = new FzTaskDTO();
            fzTaskDTO.setId(fzTaskInfo.getId());
            fzTaskDTO.setReqId(fzTaskInfo.getReqId());
            fzTaskDTO.setTargetName(fzTaskInfo.getTargetName());
            fzTaskDTO.setTargetCode(fzTaskInfo.getTargetId());
            List<String> targetVO = Arrays.asList(fzTaskDTO.getTargetCode().split(","));
            fzTaskDTO.setTargetNum(targetVO.size());
            fzTaskDTO.setStartTime(fzTaskInfo.getReqStartTime());
            fzTaskDTO.setEndTime(fzTaskInfo.getReqEndTime());
            fzTaskDTO.setPriority(fzTaskInfo.getPriority());
            fzTaskDTO.setEmulationMakeTime(fzTaskInfo.getReqTime());
            fzTaskDTO.setFzTaskStep(fzTaskInfo.getFzTaskStep());
            fzTaskDTO.setFzTaskState(fzTaskInfo.getFzTaskState());
            PointListRpcReqDTO pointListRpcReqDTO = new PointListRpcReqDTO();
            pointListRpcReqDTO.setPointType(PointTypeEnums.TARGET.toString());
            pointListRpcReqDTO.setParentIds(Arrays.asList(fzTaskDTO.getTargetCode().split(",")));
            List<PointRpcDTO> list1 = pointRpcService.getPointDTOList(pointListRpcReqDTO);
            fzTaskDTO.setPointDTOList(list1);
            fzTaskDTO.setTargetCodes(targetVO);
            List<List<PointRpcDTO>> pointDTOLists = new ArrayList<>();
            Map<String, List<PointRpcDTO>> PointRpcDTOmap = list1.stream().collect(Collectors.groupingBy(PointRpcDTO::getParentId));
            PointRpcDTOmap.forEach((key, value) -> {
                pointDTOLists.add(value);
            });
            fzTaskDTO.setPointDTOLists(pointDTOLists);
            fzTaskDTO.setPayloadIdList(Arrays.asList(fzTaskInfo.getPayloadIds().split(",")));
            fzTaskDTO.setTaskId(fzTaskInfo.getTaskId());
            fzTaskDTO.setWorkMode(fzTaskInfo.getWorkMode());
            list.add(fzTaskDTO);
        }
        return list;
    }

    private List<PointDTO> formatPointDTOList(String centerLocation) {
        List<String> list = Arrays.asList(centerLocation.split(";"));
        List<PointDTO> pointDTOList = new ArrayList<>();
        list.forEach(e -> {
            PointDTO pointDTO = new PointDTO();
            String[] post = e.replace("(", "").replace(")", "").split(",");
            pointDTO.setLongitude(post[0]);
            pointDTO.setLatitude(post[1]);
            pointDTOList.add(pointDTO);
        });
        return pointDTOList;
    }
}