package com.hai.openApi.config;

import com.hai.entity.*;
import com.hai.openApi.service.ApiOrderService;
import com.hai.order.type.OrderProductType;
import com.hai.service.ApiAmountRecordService;
import com.hai.service.ApiMerchantsService;
import com.hai.service.HighOrderService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;

@Component
public class PriceComputeConfig {

    @Resource
    private ApiMerchantsService apiMerchantsService;

    @Resource
    private ApiAmountRecordService apiAmountRecordService;

    @Resource
    private HighOrderService highOrderService;

    @Resource
    private ApiOrderService apiOrderService;

    public  Boolean mchBalance(ApiMerchants apiMerchants , BigDecimal price , String orderNo) {


        // 当前账号余额是否可以使用当前金额
        if (apiMerchants.getAmounts().compareTo(price) < 0) {
            return false;
        }

        ApiOrder apiOrder = apiOrderService.findByOrderNo(orderNo , null);

        HighOrder order = highOrderService.getOrderByOrderNo(apiOrder.getOrderNo());

        // 插入金额记录
        // 变更前金额
        BigDecimal beforeAmount = apiMerchants.getAmounts();
        // 计算金额
        apiMerchants.setAmounts(apiMerchants.getAmounts().subtract(price));
        // 变更后金额
        BigDecimal afterAmount = apiMerchants.getAmounts();

        apiMerchantsService.updateApiMerchants(apiMerchants);

        ApiAmountRecord apiAmountRecord = new ApiAmountRecord();

        apiAmountRecord.setCreateTime(new Date());
        apiAmountRecord.setUpdateTime(new Date());
        apiAmountRecord.setMchId(apiMerchants.getMchId());

        apiAmountRecord.setStatus(100);
        apiAmountRecord.setAmount(price);
        apiAmountRecord.setAfterAmount(afterAmount);
        apiAmountRecord.setBeforeAmount(beforeAmount);
        apiAmountRecord.setAmountType(2);
        apiAmountRecord.setSourceType(OrderProductType.getDataByType(order.getProductType()).getNumber());
        apiAmountRecord.setSourceOrderNo(orderNo);
        apiAmountRecord.setSourceId(apiMerchants.getId());
        apiAmountRecord.setSourceContent(apiMerchants.getMerchantName() + "|" + OrderProductType.getDataByType(order.getProductType()).getName() + "｜扣款" + price.setScale(2, BigDecimal.ROUND_HALF_DOWN));

        apiAmountRecordService.insertAmountRecord(apiAmountRecord);


        return true;
    }

    /**
     * @Author Sum1Dream
     * @Name computationPriceCost
     * @Description // 计算折扣价格
     * @Date 09:25 2023/3/13
     * @Param discount 折扣
     * @Param price 原价
     * @Return java.math.BigDecimal
     */
    public BigDecimal computationPriceCost(BigDecimal discount , BigDecimal price) {
        return price.multiply(discount).divide(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP);

    }

    /**
     * @Author Sum1Dream
     * @Name computationPriceCost
     * @Description // 计算成本价加上我们赚的利润
     * @Date 18:11 2023/4/3
     * @Param [discount, price, costPrice]
     * @Return java.math.BigDecimal
     */
    public BigDecimal computationPriceCost(BigDecimal discount , BigDecimal price , BigDecimal costPrice) {
        return  costPrice.add(price.multiply(discount.divide(new BigDecimal(100)))).setScale(2, RoundingMode.HALF_UP);

    }

    /**
     * @Author Sum1Dream
     * @Name getStarbucksCpuSizePriceMarket
     * @Description // 查询市场价 不同杯型不同的价格
     * @Date 18:48 2023/4/4
     * @Param starbucksProducts 星巴克对象
     * @Param cpuSize 杯型
     * @Return java.math.BigDecimal
     */
    public BigDecimal getStarbucksCpuSizePriceMarket(ApiStarbucksProducts starbucksProducts , String cpuSize) {
        switch (cpuSize) {
            case "中杯":
                return starbucksProducts.getMarketTallPrice();
            case "大杯":
                return starbucksProducts.getMarketGrandePrice();
            case "超大杯":
                return starbucksProducts.getMarketVentiPrice();
            default:
                return  null;
        }
    }

    /**
     * @Author Sum1Dream
     * @Name getStarbucksCpuSizePriceMarket
     * @Description // 查询销售价格 不同杯型不同的价格
     * @Date 18:48 2023/4/4
     * @Param starbucksProducts 星巴克对象
     * @Param cpuSize 杯型
     * @Return java.math.BigDecimal
     */
    public BigDecimal getStarbucksCpuSizePriceSales(ApiStarbucksProducts starbucksProducts , String cpuSize) {
        switch (cpuSize) {
            case "中杯":
                return starbucksProducts.getSalesTallPrice();
            case "大杯":
                return starbucksProducts.getSalesGrandePrice();
            case "超大杯":
                return starbucksProducts.getSalesVentiPrice();
            default:
                return  null;
        }
    }
}
