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.web.domain.overhaulplan.OverhaulPlan;
import com.cpp.web.domain.overhaulplan.OverhaulPlanRecords;
import com.cpp.web.domain.station.ElectricField;
import com.cpp.web.mapper.overhaulplan.OverhaulPlanMapper;
import com.cpp.web.service.overhaulplan.OverhaulPlanRecordsService;
import com.cpp.web.service.overhaulplan.OverhaulPlanService;
import com.cpp.web.service.station.ElectricFieldService;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * cpp_overhaul_plan
 *
 * @author fxy
 * @date 2024-10-08 9:47:33
 */
@Service
@RequiredArgsConstructor
public class OverhaulPlanServiceImpl extends ServiceImpl<OverhaulPlanMapper, OverhaulPlan> implements OverhaulPlanService {

    private final OverhaulPlanMapper overhaulPlanMapper;

    private final ElectricFieldService electricFieldService;

    private final OverhaulPlanRecordsService overhaulPlanRecordsService;

    /**
     * 查询所有检修计划
     *
     * @return
     */
    @Override
    public QueryWrapper<OverhaulPlan> getList() {
        QueryWrapper<OverhaulPlan> wrapper = new QueryWrapper<>();
        return wrapper;
    }

    /**
     * 根据场站编号查询检修计划信息
     *
     * @param stationCode
     * @return
     */
    @Override
    public QueryWrapper<OverhaulPlan> getByStationCode(String stationCode, Integer status) {
        QueryWrapper<OverhaulPlan> wrapper = new QueryWrapper<>();
        if (null != status) {
            if (!"".equals(stationCode)) {
                wrapper.eq("station_code", stationCode);
                wrapper.eq("status", status);
            }
        } else {
            if (!"".equals(stationCode)) {
                wrapper.eq("station_code", stationCode);
            }
        }
        return wrapper;
    }

    @Override
    public List<OverhaulPlan> findByStationCode(String stationCode) {
        List<OverhaulPlan> overhaulPlanList = overhaulPlanMapper.findBysStationCode(stationCode);
        return overhaulPlanList;
    }

    @Override
    public List<OverhaulPlan> findByStationCodeAndTimeBetween(Date date, String stationCode) {
        QueryWrapper<OverhaulPlan> wrapper = new QueryWrapper<>();
        if (null != date) {
            wrapper.ge("start_time", date);
            wrapper.le("end_time", date);
        }
        if (null != stationCode && !"".equals(stationCode)) {
            wrapper.eq("station_code", stationCode);
        }
        wrapper.eq("status", 1);
        return overhaulPlanMapper.selectList(wrapper);
    }

    /**
     *  返回修正后数据
     * @param list
     * @return
     * @param <T>
     */
    @Override
    public <T> List<T> getCorrectionData(List<T> list) {
        List<T> resultList = new ArrayList<>();
        BigDecimal electricFieldCapacity = BigDecimal.ZERO;
        for (T tList : list) {
            try {
                Class<?> childClass = tList.getClass();
                Class<?> superClass = childClass.getSuperclass();
                // 时间
                Field startTimeField = childClass.getDeclaredField("time");
                startTimeField.setAccessible(true);
                Date startTime = (Date) startTimeField.get(tList);
                // 场站编号
                Field stationCodeField = superClass.getDeclaredField("stationCode");
                stationCodeField.setAccessible(true);
                String stationCode = (String) stationCodeField.get(tList);
                // 获取场站装机
                ElectricField electricField = electricFieldService.findByStationCode(stationCode);
                if (null != electricField) {
                    electricFieldCapacity = electricField.getCapacity();
                }
                // 装机
                Field capacityField = childClass.getDeclaredField("capacity");
                capacityField.setAccessible(true);
                capacityField.set(tList, electricFieldCapacity);
                // 开机
                Field openCapcityField = childClass.getDeclaredField("openCapacity");
                openCapcityField.setAccessible(true);
                // 预测模型
                Field forecastModelField = childClass.getDeclaredField("forecastModel");
                forecastModelField.setAccessible(true);
                String forecastModel = (String)forecastModelField.get(tList);
                // 根据时间查询检修计划
                List<OverhaulPlan> overhaulPlanList = this.findByStationCodeAndTimeBetween(startTime, stationCode);
                if (overhaulPlanList.size() > 0) {
                    BigDecimal overCapacity = overhaulPlanList.get(0).getOverhaulCapacity();
                    openCapcityField.set(tList, electricFieldCapacity.subtract(overCapacity));
                    // 预测数据
                    Field fpValueFiled = childClass.getDeclaredField("fpValue");
                    fpValueFiled.setAccessible(true);
                    BigDecimal fpValue = (BigDecimal) fpValueFiled.get(tList);
                    // 开机/装机
                    BigDecimal ratio = (electricFieldCapacity.subtract(overCapacity)).divide(electricFieldCapacity, 2, RoundingMode.HALF_UP);
                    fpValueFiled.set(tList, fpValue.multiply(ratio));
                    // 查询检修记录
                    OverhaulPlanRecords overhaulPlanRecord = overhaulPlanRecordsService.findByStationCodeAndForecastModel(stationCode,forecastModel,overhaulPlanList.get(0).getStartTime(),overhaulPlanList.get(0).getEndTime());
                    if (null != overhaulPlanRecord) {
                        overhaulPlanRecord.setUseNum(overhaulPlanRecord.getUseNum() + 1);
                        overhaulPlanRecord.setUpdateTime(new Date());
                        overhaulPlanRecordsService.saveOrUpdate(overhaulPlanRecord);
                        // 使用次数 +1
                    }else {
                        OverhaulPlanRecords overhaulPlanRecords = new OverhaulPlanRecords();
                        overhaulPlanRecords.setOverhaulCapacity(overhaulPlanList.get(0).getOverhaulCapacity());
                        overhaulPlanRecords.setStartTime(overhaulPlanList.get(0).getStartTime());
                        overhaulPlanRecords.setEndTime(overhaulPlanList.get(0).getEndTime());
                        overhaulPlanRecords.setUseNum(overhaulPlanRecords.getUseNum() + 1);
                        overhaulPlanRecords.setStationCode(stationCode);
                        overhaulPlanRecords.setForecastModel(forecastModel);
                        overhaulPlanRecords.setCreateTime(new Date());
                        overhaulPlanRecordsService.saveOrUpdate(overhaulPlanRecords);
                    }

                }else {
                    // 设置开机为装机
                    openCapcityField.set(tList,electricFieldCapacity);
                }
                resultList.add(tList);
            } catch (NoSuchFieldException | IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
        return resultList;
    }
}
