package com.cpp.web.service.overhaulplan.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cpp.common.utils.spring.SpringUtils;
import com.cpp.web.domain.BaseForecastEntity;
import com.cpp.web.domain.datafactory.enums.FileTypeEnum;
import com.cpp.web.domain.overhaulplan.OverhaulPlan;
import com.cpp.web.domain.overhaulplan.OverhaulPlanRecords;
import com.cpp.web.mapper.overhaulplan.OverhaulPlanRecordsMapper;
import com.cpp.web.service.overhaulplan.OverhaulPlanRecordsService;
import com.cpp.web.service.overhaulplan.OverhaulPlanService;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * cpp_overhaul_plan_records
 *
 * @author fxy
 * @date 2024-10-08 9:47:33
 */
@Service
@RequiredArgsConstructor
public class OverhaulPlanRecordsServiceImpl extends ServiceImpl<OverhaulPlanRecordsMapper, OverhaulPlanRecords> implements OverhaulPlanRecordsService {

    private final OverhaulPlanRecordsMapper overhaulPlanRecordsMapper;

    @Override
    public QueryWrapper<OverhaulPlanRecords> getByStationCode(String stationCode) {
        QueryWrapper<OverhaulPlanRecords> wrapper = new QueryWrapper<>();
        if (null != stationCode && !"".equals(stationCode)) {
            wrapper.eq("station_code", stationCode);
        }
        return wrapper;
    }

    @Override
    public List<PlanAndRecordDto> findByOpId(Long opId) {
        QueryWrapper<OverhaulPlanRecords> wrapper = new QueryWrapper<>();
        if (null != opId) {
            wrapper.eq("op_id", opId);
        }
        List<OverhaulPlan> overhaulPlanList = SpringUtils.getBean(OverhaulPlanService.class).findById(opId);

        List<OverhaulPlanRecords> overhaulPlanRecordsList = list(wrapper);

        return needList(overhaulPlanList, overhaulPlanRecordsList);
    }

    @Override
    public List<PlanAndRecordDto> findByStationCodeAndCreatTimeBetween(String stationCode, Date startTime, Date endTime) {
        QueryWrapper<OverhaulPlanRecords> wrapper = new QueryWrapper<>();
        if (null != stationCode && !"".equals(stationCode)) {
            wrapper.eq("station_code", stationCode);
        }
        if (null != startTime && !"".equals(startTime) && null != endTime && !"".equals(endTime)) {
            wrapper.between("create_time", startTime, endTime);
        }
        List<OverhaulPlanRecords> overhaulPlanRecordsList = list(wrapper);
        List<Long> list = overhaulPlanRecordsList.stream().map(OverhaulPlanRecords::getOpId).collect(Collectors.toList());

        List<OverhaulPlan> overhaulPlanList = SpringUtils.getBean(OverhaulPlanService.class).findByAllId(list);

        return needList(overhaulPlanList, list(wrapper));
    }


    @Data
    public class PlanAndRecordDto {

        private BigDecimal overPlanCapacity;

        private String name;

        private String stationCode;

        private String forecastModel;

        private FileTypeEnum forecastType;

        private String details;

        private Date createTime;
    }

    public List<PlanAndRecordDto> needList(List<OverhaulPlan> overhaulPlanList, List<OverhaulPlanRecords> overhaulPlanRecordsList) {
        List<PlanAndRecordDto> planAndRecordDtos = new ArrayList<>();
        for (OverhaulPlanRecords overhaulPlanRecords : overhaulPlanRecordsList) {
            PlanAndRecordDto planAndRecordDto = new PlanAndRecordDto();
            planAndRecordDto.setStationCode(overhaulPlanRecords.getStationCode());
            planAndRecordDto.setForecastModel(overhaulPlanRecords.getForecastModel());
            planAndRecordDto.setForecastType(overhaulPlanRecords.getForecastType());
            planAndRecordDto.setDetails(overhaulPlanRecords.getDetails());
            planAndRecordDto.setCreateTime(overhaulPlanRecords.getCreateTime());
            for (OverhaulPlan overhaulPlan : overhaulPlanList) {
                if (overhaulPlan.getId() == overhaulPlanRecords.getOpId()) {
                    planAndRecordDto.setName(overhaulPlan.getName());
                    planAndRecordDto.setOverPlanCapacity(overhaulPlan.getOverhaulCapacity());
                }
            }
            planAndRecordDtos.add(planAndRecordDto);
        }
        return planAndRecordDtos;
    }
}
