package com.pusong.business.domain.vo.home;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.pusong.business.domain.vo.PsCustomPriceVo;
import lombok.Data;

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

@Data
public class ChannelAmountVo {

    /**
     * 当前时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm")
    private Date currentDate = new Date();
    /**
     * 统计范围
     */
    private String dateStr;
    /**
     * 成交总金额
     */
    private BigDecimal all;

    /**
     * 各渠道成交金额
     */
    private List<DataAmountVo> channelList;


    public void cellPer(){
        //计算总金额
        this.all = this.all == null? BigDecimal.ZERO: this.all;
        for (DataAmountVo str : channelList){
            all = all.add(str.getMoney());
        }
        int num = 0;
        BigDecimal payMoney = BigDecimal.ZERO;
        BigDecimal unpayMoney = BigDecimal.ZERO;
        //计算占比
        if(0 == this.all.compareTo(BigDecimal.ZERO)) {
            channelList.forEach((v)->{v.setPer("0%");});
        }else {
            for (DataAmountVo str : channelList){
                num = num + Integer.parseInt(str.getNum().replace("单",""));
                payMoney = payMoney.add(str.getPayMoney());
                unpayMoney = unpayMoney.add(str.getUnPayMoney());
                str.setPer(str.getMoney().multiply(new BigDecimal("100.0")).divide(this.all,2, RoundingMode.HALF_UP).toPlainString()+"%");
            }
        }
        DataAmountVo dataAmountVo = new DataAmountVo();
        dataAmountVo.setType("总计");
        dataAmountVo.setPer("100%");
        dataAmountVo.setNum(num+"单");
        dataAmountVo.setPayMoney(payMoney);
        dataAmountVo.setUnPayMoney(unpayMoney);
        dataAmountVo.setMoney(all);
        channelList = channelList.stream().sorted(Comparator.comparing(DataAmountVo::getMoney).reversed()).collect(Collectors.toList());
        channelList.add(dataAmountVo);
    }

}
