package com.hzs.sale.order.controller.api;

import cn.hutool.core.date.DateUtil;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.sale.order.vo.ProgressQueryVo;
import com.hzs.sale.order.service.ISaOrderService;
import com.hzs.sale.order.vo.TargetPerformanceVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @BelongsProject: hzs_cloud
 * @BelongsPackage: com.hzs.sale.order.controller.api
 * @Author: yh
 * @CreateTime: 2023-04-12  18:25
 * @Description: TODO
 * @Version: 1.0
 */
@RestController
@RequestMapping("/api/target-performance")
@Slf4j
public class ApiTargetPerformanceController {

    @Autowired
    private ISaOrderService orderService;

    /**
     * 目标业绩进度
     */
    @GetMapping("target-performance-progress")
    public AjaxResult targetPerformanceProgress() {
        /**
         * 当前业绩
         */
        Date startPayDate = DateUtils.getStartTime(DateUtils.currentDate());
        Date endPayDate = DateUtils.getEndTime(DateUtils.currentDate());
        Long memberId = SecurityUtils.getUserId();
        BigDecimal targetPerformance = orderService.selectBySumPv(startPayDate, endPayDate, memberId);
        /**
         * 目标基数 TODO 目标基数后台配置
         */
        List<TargetPerformanceVo> resultList = new ArrayList<>();
        int baseNumber = 200000;
        // 取值余数
        BigDecimal remainder = new BigDecimal(targetPerformance.doubleValue() / baseNumber);
        // 余数
        if (remainder.compareTo(new BigDecimal(0)) == 0) {
            // 表示没有余数
            for (int i = 0; i < 5; i++) {
                TargetPerformanceVo targetPerformanceVo = new TargetPerformanceVo();
                if (i == 0) {
                    targetPerformanceVo.setTargetPerformance(targetPerformance);
                    targetPerformanceVo.setTargetPerformanceLeaveOut(decimalToStr(targetPerformance));
                    targetPerformanceVo.setPercentage("100%");
                    resultList.add(targetPerformanceVo);
                } else {
                    BigDecimal rd = remainder.add(new BigDecimal(i));
                    BigDecimal remainderBase = targetPerformance.add(new BigDecimal(baseNumber).multiply(rd));
                    BigDecimal divide = targetPerformance.multiply(new BigDecimal(100)).divide(remainderBase, 2, BigDecimal.ROUND_HALF_UP);
                    targetPerformanceVo.setTargetPerformance(remainderBase);
                    targetPerformanceVo.setTargetPerformanceLeaveOut(decimalToStr(targetPerformanceVo.getTargetPerformance()));
                    targetPerformanceVo.setPercentage(divide + "%");
                    resultList.add(targetPerformanceVo);
                }
            }
        } else {
            // 有余数
            for (int i = 0; i < 5; i++) {
                TargetPerformanceVo targetPerformanceVo = new TargetPerformanceVo();
                if (i == 0) {
                    targetPerformanceVo.setTargetPerformance(new BigDecimal(baseNumber).multiply(remainder));
                    targetPerformanceVo.setTargetPerformanceLeaveOut(decimalToStr(targetPerformanceVo.getTargetPerformance()));
                    targetPerformanceVo.setPercentage("100%");
                    resultList.add(targetPerformanceVo);
                } else {
                    BigDecimal rd = remainder.add(new BigDecimal(i));
                    BigDecimal remainderBase = new BigDecimal(baseNumber).multiply(rd);
                    BigDecimal multiply = targetPerformance.multiply(new BigDecimal(100));
                    BigDecimal divide = multiply.divide(remainderBase, 2, BigDecimal.ROUND_HALF_UP);
                    targetPerformanceVo.setTargetPerformance(remainderBase);
                    targetPerformanceVo.setTargetPerformanceLeaveOut(decimalToStr(targetPerformanceVo.getTargetPerformance()));
                    targetPerformanceVo.setPercentage(divide + "%");
                    resultList.add(targetPerformanceVo);
                }
            }
        }
        ProgressQueryVo progressQueryVo = new ProgressQueryVo();

        progressQueryVo.setStartDate(DateUtil.formatDate(startPayDate));
        progressQueryVo.setEndDate(DateUtil.formatDate(endPayDate));
        progressQueryVo.setTargetPerformanceList(resultList);
        return AjaxResult.success(progressQueryVo);
    }

    /**
     * 目标金额进度
     *
     * @return
     */
    @GetMapping("target-performance-amount-progress")
    public AjaxResult targetPerformanceAmountProgress() {
        /**
         * 当前业绩
         */
        Date startPayDate = DateUtils.getStartTime(DateUtils.currentDate());
        Date endPayDate = DateUtils.getEndTime(DateUtils.currentDate());
        Long memberId = SecurityUtils.getUserId();
        BigDecimal targetPerformance = orderService.selectBySumAmount(startPayDate, endPayDate, memberId);
        /**
         * 目标基数 TODO 目标基数后台配置
         */
        List<TargetPerformanceVo> resultList = new ArrayList<>();
        int baseNumber = 200000;
        // 取值余数
        BigDecimal remainder = new BigDecimal(targetPerformance.doubleValue() / baseNumber);
        // 余数
        if (remainder.compareTo(new BigDecimal(0)) == 0) {
            // 表示没有余数
            for (int i = 0; i < 5; i++) {
                TargetPerformanceVo targetPerformanceVo = new TargetPerformanceVo();
                if (i == 0) {
                    targetPerformanceVo.setTargetPerformance(targetPerformance);
                    targetPerformanceVo.setTargetPerformanceLeaveOut(decimalToStr(targetPerformance));
                    targetPerformanceVo.setPercentage("100%");
                    resultList.add(targetPerformanceVo);
                } else {
                    BigDecimal rd = remainder.add(new BigDecimal(i));
                    BigDecimal remainderBase = targetPerformance.add(new BigDecimal(baseNumber).multiply(rd));
                    BigDecimal divide = targetPerformance.multiply(new BigDecimal(100)).divide(remainderBase, 2, BigDecimal.ROUND_HALF_UP);
                    targetPerformanceVo.setTargetPerformance(remainderBase);
                    targetPerformanceVo.setTargetPerformanceLeaveOut(decimalToStr(targetPerformanceVo.getTargetPerformance()));
                    targetPerformanceVo.setPercentage(divide + "%");
                    resultList.add(targetPerformanceVo);
                }
            }
        } else {
            // 有余数
            for (int i = 0; i < 5; i++) {
                TargetPerformanceVo targetPerformanceVo = new TargetPerformanceVo();
                if (i == 0) {
                    targetPerformanceVo.setTargetPerformance(new BigDecimal(baseNumber).multiply(remainder));
                    targetPerformanceVo.setTargetPerformanceLeaveOut(decimalToStr(targetPerformanceVo.getTargetPerformance()));
                    targetPerformanceVo.setPercentage("100%");
                    resultList.add(targetPerformanceVo);
                } else {
                    BigDecimal rd = remainder.add(new BigDecimal(i));
                    BigDecimal remainderBase = new BigDecimal(baseNumber).multiply(rd);
                    BigDecimal multiply = targetPerformance.multiply(new BigDecimal(100));
                    BigDecimal divide = multiply.divide(remainderBase, 2, BigDecimal.ROUND_HALF_UP);

                    targetPerformanceVo.setTargetPerformance(remainderBase);
                    targetPerformanceVo.setTargetPerformanceLeaveOut(decimalToStr(targetPerformanceVo.getTargetPerformance()));
                    targetPerformanceVo.setPercentage(divide + "%");
                    resultList.add(targetPerformanceVo);
                }
            }
        }
        ProgressQueryVo progressQueryVo = new ProgressQueryVo();
        progressQueryVo.setStartDate(DateUtil.formatDate(startPayDate));
        progressQueryVo.setEndDate(DateUtil.formatDate(endPayDate));
        progressQueryVo.setTargetPerformanceList(resultList);
        return AjaxResult.success(progressQueryVo);
    }

    /**
     * decimal转str 0000转万
     *
     * @param pv
     * @return
     */
    public String decimalToStr(BigDecimal pv) {
        return pv.divide(new BigDecimal(10000)) + "万";
    }

}
