package com.sckw.payment.service;

import com.sckw.core.model.constant.NumberConstant;
import com.sckw.core.web.context.LoginUserHolder;
import com.sckw.payment.model.vo.SettlementRecord;
import com.sckw.payment.model.vo.req.FinanceCount;
import com.sckw.payment.model.vo.res.EChartsVo;
import com.sckw.core.utils.DateTimeUtil;
import com.sckw.payment.utils.DecimalUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 工作台
 *
 * @author xucaiqin
 * @date 2023-08-28 15:38:53
 */
@Slf4j
@Service
public class WorkbenchService {

    @Resource
    private KwpSettlementRecordService kwpSettlementRecordService;
    private final Map<String, Function<SettlementRecord, String>> map = new HashMap<>(NumberConstant.FOUR);

    public WorkbenchService() {
        DecimalFormat df = new DecimalFormat("0.00");
        map.put("1", (d) -> {
            if (Objects.isNull(d.getReceivedMoney())) {
                return "0.00";
            }
            return df.format(d.getReceivedMoney().divide(new BigDecimal("10000.0"), 2, RoundingMode.HALF_UP));
        });
        map.put("2", (d) -> {
            if (Objects.isNull(d.getExactMoney())) {
                return "0.00";
            }
            return df.format(d.getExactMoney().divide(new BigDecimal("10000.0"), 2, RoundingMode.HALF_UP));
        });
        map.put("3", (d) -> {
            if (Objects.isNull(d.getOverdueExactMoney())) {
                return "0.00";
            }
            return df.format(d.getOverdueExactMoney().divide(new BigDecimal("10000.0"), 2, RoundingMode.HALF_UP));
        });
        map.put("4", (d) -> {
            BigDecimal overdueExactMoney = d.getOverdueExactMoney();
            //总应收金额
            BigDecimal totalReceiveMoney = d.getTotalReceiveMoney();
             return DecimalUtils.financePercent(overdueExactMoney, totalReceiveMoney);
        });
    }

    public Object financeCount(FinanceCount financeCount) {
        //周期维度 1-天 2-周 3-月
        Integer circle = financeCount.getCircle();
        List<String> time = new ArrayList<>();

        //时间参数为空，按照默认维度统计
        if (StringUtils.isBlank(financeCount.getStartTime()) && StringUtils.isBlank(financeCount.getEndTime())) {
            //按照周期维度生成时间段2
            if (circle == 1) {
                //从今天倒推15天
                time.add(DateTimeUtil.getComputeDaysStr(-14));
                time.add(DateTimeUtil.getComputeDaysStr(0));
            } else if (circle == 3) {
                //从当月倒推12月
                time.add(DateTimeUtil.getComputeMonthStartDayStr(-12));
                time.add(DateTimeUtil.getComputeMonthEndDayStr(0));
            }
        } else {
            //按照给定的时间参数进行统计
            if (circle == 1) {
                time.add(financeCount.getStartTime());
                time.add(financeCount.getEndTime());
            } else if (circle == 3) {
                time.add(DateTimeUtil.getComputeMonthStartDayStr(financeCount.getStartTime()));
                time.add(DateTimeUtil.getComputeMonthEndDayStr(financeCount.getEndTime()));
            }
        }
        Long entId = LoginUserHolder.getEntId();
        //获取数据，按照统计因子进行处理，并转换成万元
        List<SettlementRecord> settlementRecords = kwpSettlementRecordService.financeCount(time, entId, circle);
        return settlementRecords.stream().map(a -> {
            EChartsVo eChartsVo = new EChartsVo();
            eChartsVo.setCreateTime(a.getCreateTime());
            eChartsVo.setFactorOne(map.get(financeCount.getFactorOne()).apply(a));
            eChartsVo.setFactorTwo(map.get(financeCount.getFactorTwo()).apply(a));
            return eChartsVo;
        }).collect(Collectors.toList());
    }
}
