package com.jjt.ws.service.impl;

import com.jjt.calc.domain.TwinCalcDayYhj;
import com.jjt.calc.service.ITwinCalcDayYhjService;
import com.jjt.common.utils.DateUtils;
import com.jjt.elec.domain.ElecPrice;
import com.jjt.elec.service.IElecPriceService;
import com.jjt.ws.domain.TwinRzCalcMonth;
import com.jjt.ws.domain.TwinWorkshopCalc;
import com.jjt.ws.mapper.TwinRzCalcMonthMapper;
import com.jjt.ws.service.ITwinRzCalcMonthService;
import com.jjt.ws.vo.MonthCalcVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 染整月统计Service业务层处理
 *
 * @author wukai
 * @date 2025-04-23
 */
@Service
public class TwinRzCalcMonthServiceImpl implements ITwinRzCalcMonthService {
    @Resource
    private TwinRzCalcMonthMapper twinRzCalcMonthMapper;
    @Resource
    private IElecPriceService elecPriceService;
    @Resource
    private ITwinCalcDayYhjService yhjService;

    /**
     * 查询染整月统计
     *
     * @param calcId 染整月统计主键
     * @return 染整月统计
     */
    @Override
    public TwinRzCalcMonth selectTwinRzCalcMonthByCalcId(Long calcId) {
        return twinRzCalcMonthMapper.selectTwinRzCalcMonthByCalcId(calcId);
    }

    /**
     * 查询染整月统计列表
     *
     * @param twinRzCalcMonth 染整月统计
     * @return 染整月统计
     */
    @Override
    public List<TwinRzCalcMonth> selectTwinRzCalcMonthList(TwinRzCalcMonth twinRzCalcMonth) {
        return twinRzCalcMonthMapper.selectTwinRzCalcMonthList(twinRzCalcMonth);
    }

    /**
     * 新增染整月统计
     *
     * @param twinRzCalcMonth 染整月统计
     * @return 结果
     */
    @Override
    public int insertTwinRzCalcMonth(TwinRzCalcMonth twinRzCalcMonth) {
        return twinRzCalcMonthMapper.insertTwinRzCalcMonth(twinRzCalcMonth);
    }

    /**
     * 修改染整月统计
     *
     * @param twinRzCalcMonth 染整月统计
     * @return 结果
     */
    @Override
    public int updateTwinRzCalcMonth(TwinRzCalcMonth twinRzCalcMonth) {
        LocalDate date = DateUtils.toLocalDate(twinRzCalcMonth.getDataDate());
        int year = date.getYear();
        int month = date.getMonthValue();
        ElecPrice elecPrice = elecPriceService.selectElecPriceByMonth(year, month);
        if (twinRzCalcMonth.getQMidUse() != null) {
            twinRzCalcMonth.setQMidPrice(twinRzCalcMonth.getQMidUse().multiply(elecPrice.getMidStreamPrice()));
        }
        twinRzCalcMonth.setPrice();
        if (twinRzCalcMonth.getLength() != null) {
            BigDecimal lp = twinRzCalcMonth.getTotalPrice().divide(twinRzCalcMonth.getLength(), 6, RoundingMode.HALF_UP);
            twinRzCalcMonth.setLengthPrice(lp);
        }
        if (twinRzCalcMonth.getWeight() != null) {
            BigDecimal wp = twinRzCalcMonth.getTotalPrice().divide(twinRzCalcMonth.getWeight(), 6, RoundingMode.HALF_UP);
            twinRzCalcMonth.setWeightPrice(wp);
        }
        return twinRzCalcMonthMapper.updateTwinRzCalcMonth(twinRzCalcMonth);
    }

    /**
     * 批量删除染整月统计
     *
     * @param calcIds 需要删除的染整月统计主键
     * @return 结果
     */
    @Override
    public int deleteTwinRzCalcMonthByCalcIds(Long[] calcIds) {
        return twinRzCalcMonthMapper.deleteTwinRzCalcMonthByCalcIds(calcIds);
    }

    /**
     * 删除染整月统计信息
     *
     * @param calcId 染整月统计主键
     * @return 结果
     */
    @Override
    public int deleteTwinRzCalcMonthByCalcId(Long calcId) {
        return twinRzCalcMonthMapper.deleteTwinRzCalcMonthByCalcId(calcId);
    }

    private TwinRzCalcMonth selectExists(Date date) {
        TwinRzCalcMonth search = new TwinRzCalcMonth();
        search.setDataDate(date);
        List<TwinRzCalcMonth> list = selectTwinRzCalcMonthList(search);
        if (list.size() > 0) {
            return list.get(0);
        } else {
            return search;
        }
    }

    /**
     * 染整线月统计
     *
     * @param date     时间
     * @param calcList 统计数据
     */
    @Override
    public void calc(Date date, List<TwinWorkshopCalc> calcList) {
        TwinCalcDayYhj search = new TwinCalcDayYhj();
        Map<String, Object> params = new HashMap<>();
        params.put("beginTime", DateUtils.dateTime(date));
        params.put("endTime", DateUtils.dateTime(date));
        search.setParams(params);
        List<TwinCalcDayYhj> yhjList = yhjService.selectTwinCalcDayYhjList(search);
        Integer totalLength = yhjList.stream()
                .map(TwinCalcDayYhj::getLength)
                .filter(java.util.Objects::nonNull)
                .reduce(0, Integer::sum);
        TwinRzCalcMonth vo = selectExists(date);
        for (TwinWorkshopCalc calc : calcList) {
            switch (calc.getWsCode()) {
                case "WS03-D-QZ":
                    vo.setQzUse(calc.getTotalValue());
                    vo.setQzPrice(calc.getTotalPrice());
                    vo.setQzFUse(calc.getPeakValue());
                    vo.setQzPUse(calc.getFlatValue());
                    vo.setQzGUse(calc.getValleyValue());
                    vo.setQzJfUse(calc.getSuperPeakValue());
                    break;
                case "WS03-D-HZ":
                    vo.setHzUse(calc.getTotalValue());
                    vo.setHzPrice(calc.getTotalPrice());
                    vo.setHzFUse(calc.getPeakValue());
                    vo.setHzPUse(calc.getFlatValue());
                    vo.setHzGUse(calc.getValleyValue());
                    vo.setHzJfUse(calc.getSuperPeakValue());
                    break;
                case "WS03-D-YR":
                    vo.setYrUse(calc.getTotalValue());
                    vo.setYrPrice(calc.getTotalPrice());
                    vo.setYrFUse(calc.getPeakValue());
                    vo.setYrPUse(calc.getFlatValue());
                    vo.setYrGUse(calc.getValleyValue());
                    vo.setYrJfUse(calc.getSuperPeakValue());
                    break;
                case "WS03-S-YR":
                    vo.setSPrice(calc.getTotalPrice());
                    vo.setSUse(calc.getTotalValue());
                    break;
                case "WS03-Q-DY":
                    vo.setQLowPrice(calc.getTotalPrice());
                    vo.setQLowUse(calc.getTotalValue());
                    break;
                case "WS03-Q-ZY":
//                    if (vo.getQMidUse() != null && vo.getQMidUse().intValue() != 0) {
                    vo.setQMidPrice(calc.getTotalPrice());
                    vo.setQMidUse(calc.getTotalValue());
//                    }
                    break;
                default:
            }

        }
        vo.setPrice();
        if (vo.getDPrice().compareTo(BigDecimal.ZERO) != 0) {
//            BigDecimal baseD = BigDecimal.valueOf(45000);
//            BigDecimal baseL = BigDecimal.valueOf(120000).multiply(BigDecimal.valueOf(0.95 + (Math.random()) * 0.1));
//            BigDecimal length = vo.getDPrice().multiply(baseL).divide(baseD, 2, RoundingMode.HALF_UP);
            BigDecimal length = BigDecimal.valueOf(totalLength);
            BigDecimal weight = length.divide(BigDecimal.valueOf(1000), 2, RoundingMode.HALF_UP);
            vo.setLength(length);
            vo.setWeight(weight);
            BigDecimal lp = vo.getTotalPrice().divide(vo.getLength(), 6, RoundingMode.HALF_UP);
            vo.setLengthPrice(lp);
            BigDecimal wp = vo.getTotalPrice().divide(vo.getWeight(), 6, RoundingMode.HALF_UP);
            vo.setWeightPrice(wp);
        }
        if (vo.getCalcId() != null) {
            updateTwinRzCalcMonth(vo);
        } else {
            insertTwinRzCalcMonth(vo);
        }
    }

    private void setWorkshopData(MonthCalcVO.D.DD dd, TwinWorkshopCalc calc) {
        dd.setUse(calc.getTotalValue());
        dd.setPrice(calc.getTotalPrice());
        dd.setFPrice(calc.getPeakUPrice());
        dd.setFUse(calc.getPeakValue());
        dd.setPPrice(calc.getFlatUPrice());
        dd.setPUse(calc.getFlatValue());
        dd.setGPrice(calc.getValleyUPrice());
        dd.setGUse(calc.getValleyValue());
        dd.setJfPrice(calc.getSuperPeakUPrice());
        dd.setJfUse(calc.getSuperPeakValue());
    }

    private void setQuarterData(MonthCalcVO.Q q, TwinWorkshopCalc calc, BigDecimal unitPrice, String type) {
        switch (type) {
            case "low":
                q.setLowPrice(calc.getTotalPrice());
                q.setLowUse(calc.getTotalValue());
                q.setLowUnitPrice(unitPrice);
                break;
            case "mid":
                q.setMidPrice(calc.getTotalPrice());
                q.setMidUse(calc.getTotalValue());
                q.setMidUnitPrice(unitPrice);
                break;
            default:
        }
    }

}
