package com.gree.mall.miniapp.bean.material.parts;

import cn.hutool.core.date.DateUtil;
import com.gree.mall.miniapp.enums.material.PartsRefTypeEnum;
import com.gree.mall.miniapp.plus.entity.WebsitPartsSalesItem;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

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

@Data
public class PartsRefundRecordDTO {

    @ApiModelProperty("返还明细id")
    private String itemId;

    @ApiModelProperty("申请单号")
    private String applyNo;

    @ApiModelProperty("销售单id")
    private String salesId;

    @ApiModelProperty("销售单明细id")
    private String salesItemId;

    @ApiModelProperty("配件编码")
    private String partsNumber;

    @ApiModelProperty("配件名称")
    private String partsName;

    @ApiModelProperty("销售价")
    private BigDecimal salesPrice;

    @ApiModelProperty("市场价")
    private BigDecimal marketPrice;

    @ApiModelProperty("优惠价2")
    private BigDecimal secondPrice;

    @ApiModelProperty("数量")
    private BigDecimal qty;

    @ApiModelProperty("总金额")
    private BigDecimal totalAmount;

    @ApiModelProperty("退款方式 WX=微信 CASH=现金")
    private String refundAmountMode;

    @ApiModelProperty("退款金额")
    private BigDecimal refundAmount;

    @ApiModelProperty("维修标识 INNER=保内 OUTSIDE=保外")
    private String repairFlag;

    @ApiModelProperty("工单编号")
    private String workOrderNo;

    @ApiModelProperty("总部工单派工id")
    private String pgId;

    @ApiModelProperty("用户名称")
    private String customerName;

    @ApiModelProperty("用户电话")
    private String customerTel;

    @ApiModelProperty("是否备用件 false=否 true=是")
    private Boolean isBackup;

    @ApiModelProperty("创建时间")
    private Date createTime;


    public void injectData(SalesItemRecordDTO itemRecordDTO, WebsitPartsSalesItem salesOrderItem, BigDecimal qty, String refType) {
        this.itemId = itemRecordDTO.getItemId();
        this.applyNo = itemRecordDTO.getApplyNo();
        this.salesId = salesOrderItem.getSalesId();
        this.salesItemId = salesOrderItem.getId();
        this.partsNumber = salesOrderItem.getPartsNumber();
        this.partsName = salesOrderItem.getPartsName();
        this.salesPrice = salesOrderItem.getSalesPrice();
        this.marketPrice = salesOrderItem.getMarketPrice();
        this.secondPrice = salesOrderItem.getSecondPrice();
        this.qty = qty;
        this.totalAmount = salesOrderItem.getSalesPrice().multiply(qty);
        this.repairFlag = itemRecordDTO.getRepairFlag();
        this.workOrderNo = itemRecordDTO.getWorkOrderNo();
        this.pgId = itemRecordDTO.getPgId();
        this.customerName = itemRecordDTO.getCustomerName();
        this.customerTel = itemRecordDTO.getCustomerTel();
        this.isBackup = itemRecordDTO.getIsBackup();
        this.createTime = DateUtil.date();
        this.refundAmountMode = salesOrderItem.getRefundAmountMode();
        if (refType.equals(PartsRefTypeEnum.OLD_REFUND.getRemark()) || refType.equals(PartsRefTypeEnum.NEW_REFUND.getRemark())) {
            // 旧件返还保内或新件返还 全额退还
            this.refundAmount = salesOrderItem.getSalesPrice().multiply(qty);
        } else if (refType.equals(PartsRefTypeEnum.OUTSIDE_TO_SALES.getRemark())) {
            // 保外新件转销售的市场价-优惠价2的差价退还（旧计算方式）
//            this.refundAmount = salesOrderItem.getMarketPrice().subtract(salesOrderItem.getSecondPrice()).multiply(qty);
            // 销售价-优惠价2*1.2倍  返还差价
            this.refundAmount = BigDecimal.ZERO;
            if (salesOrderItem.getSecondPrice().compareTo(BigDecimal.ZERO) > 0) {
                this.refundAmount = this.computeDiffAmount(salesOrderItem.getSalesPrice(), salesOrderItem.getSecondPrice(), qty);
            }

        }

        if (refType.equals(PartsRefTypeEnum.NEW_REFUND.toString())) {
            // 销售明细新件数量增加
            salesOrderItem.setNewRefundQty(salesOrderItem.getNewRefundQty().add(qty));
        } else {
            // 销售明细旧件数量增加
            salesOrderItem.setOldRefundQty(salesOrderItem.getOldRefundQty().add(qty));
        }
        // 销售明细数量减少
        salesOrderItem.setQty(salesOrderItem.getQty().subtract(qty));
    }

    public BigDecimal computeDiffAmount(BigDecimal salesPrice, BigDecimal secondPrice, BigDecimal qty) {
        return salesPrice.subtract(secondPrice.multiply(BigDecimal.valueOf(1.2))).multiply(qty);
    }
}
