package com.sckw.order.serivce;

import com.sckw.core.common.enums.NumberConstant;
import com.sckw.core.exception.BusinessException;
import com.sckw.core.utils.DateUtils;
import com.sckw.core.web.context.LoginUserHolder;
import com.sckw.order.model.dto.WbTOrderExcelStsReqDTO;
import com.sckw.order.model.dto.WbTOrderExcelStsResDTO;
import com.sckw.order.model.vo.req.WbTOrderExcelStsParam;
import com.sckw.order.model.vo.res.WbTOrderExcelStsResVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @desc: 贸易订单统计service
 * @author: yzc
 * @date: 2023-09-13 8:36
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class KwoTradeOrderStatisticsService {

    private final KwoTradeOrderService kwoTradeOrderService;

    /**
     * @desc: 工作台贸易订单表格统计
     * @author: yzc
     * @date: 2023-09-13 15:46
     * @Param params:
     * @return: java.util.List<com.sckw.order.model.vo.res.WbTOrderExcelStsResVO>
     */
    public List<WbTOrderExcelStsResVO> workbenchExcel(WbTOrderExcelStsParam params) {
        Integer dateType = params.getDateType();
        Date startTime;
        if (Objects.equals(1, dateType)) {
            startTime = DateUtils.getStartTime(3, LocalDateTime.now());
        } else if (Objects.equals(3, dateType)) {
            startTime = DateUtils.getYearStartTime();
        } else {
            throw new BusinessException("Invalid date type");
        }
        List<WbTOrderExcelStsResVO> res = buildFormatDate(dateType, startTime);
        WbTOrderExcelStsReqDTO reqDTO = new WbTOrderExcelStsReqDTO();
        reqDTO.setUnitType(String.valueOf(params.getOrderType()))
                .setDateFormat(DateUtils.getDateFormat(dateType))
                .setStartTime(startTime)
                .setTopEntId(LoginUserHolder.getEntId())
                .setStsFactorsOne(params.getStsFactorsOne())
                .setStsFactorsTwo(params.getStsFactorsTwo());
        Map<String, WbTOrderExcelStsResDTO> map = kwoTradeOrderService.workbenchExcel(reqDTO).stream()
                .collect(Collectors.toMap(WbTOrderExcelStsResDTO::getFormatDate, e -> e, (k1, k2) -> k1));
        res.forEach(e -> {
            WbTOrderExcelStsResDTO dto = map.get(e.getFormatDate());
            if (Objects.nonNull(dto)) {
                BigDecimal one = dto.getStsFactorsOneValue();
                if (Objects.equals(params.getStsFactorsOne(), 1)) {
                    one = one.divide(NumberConstant.TEN_THOUSAND, 2, RoundingMode.HALF_UP);
                }
                BigDecimal two = dto.getStsFactorsTwoValue();
                if (Objects.equals(params.getStsFactorsTwo(), 1)) {
                    two = two.divide(NumberConstant.TEN_THOUSAND, 2, RoundingMode.HALF_UP);
                }
                e.setStsFactorsOneValue(one);
                e.setStsFactorsTwoValue(two);
            }
        });
        return res;
    }

    /**
     * @desc: 构建时间转换格式
     * @author: yzc
     * @date: 2023-09-13 15:45
     * @Param dateType:
     * @Param startTime:
     * @return: java.util.List<com.sckw.order.model.vo.res.WbTOrderExcelStsResVO>
     */
    private List<WbTOrderExcelStsResVO> buildFormatDate(Integer dateType, Date startTime) {
        LocalDate start = DateUtils.dateToLocalDate(startTime);
        LocalDate end = LocalDate.now();
        List<WbTOrderExcelStsResVO> res = new ArrayList<>();
        if (dateType == 1) {
            while (start.isBefore(end)) {
                WbTOrderExcelStsResVO vo = new WbTOrderExcelStsResVO();
                vo.setFormatDate(start.toString());
                start = start.plusDays(1);
                res.add(vo);
            }
            WbTOrderExcelStsResVO vo = new WbTOrderExcelStsResVO();
            vo.setFormatDate(end.toString());
            res.add(vo);
        } else {
            //当月第一天
            end = end.with(TemporalAdjusters.firstDayOfMonth());
            while (start.isBefore(end)) {
                WbTOrderExcelStsResVO vo = new WbTOrderExcelStsResVO();
                vo.setFormatDate(DateUtils.localDateToString(start, "yyyy-MM"));
                start = start.plusMonths(1);
                res.add(vo);
            }
            WbTOrderExcelStsResVO vo = new WbTOrderExcelStsResVO();
            vo.setFormatDate(DateUtils.localDateToString(end, "yyyy-MM"));
            res.add(vo);
        }
        return res;
    }
}

