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

import com.gree.mall.miniapp.enums.material.DirectFlagEnum;
import com.gree.mall.miniapp.enums.material.PartsAttrEnum;
import com.gree.mall.miniapp.exception.RemoteServiceException;
import com.gree.mall.miniapp.plus.entity.WebsitStock;
import com.gree.mall.miniapp.plus.entity.WebsitStockAcc;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Objects;

@Data
@ApiModel
public class WebsitStockDTO {

    @ApiModelProperty("商户编号")
    @NotBlank(message = "商户编号不能空")
    private String companyWechatId;

    @ApiModelProperty("商户名称")
    @NotBlank(message = "商户名称不能空")
    private String companyWechatName;

    @ApiModelProperty("网点编号")
    @NotBlank(message = "网点编号不能空")
    private String websitId;

    @ApiModelProperty("网点名称")
    @NotBlank(message = "网点名称不能空")
    private String websitName;

    @ApiModelProperty("配件网点编号")
    private String partsWebsitId;

    @ApiModelProperty("发生时间")
    @NotNull(message = "发生时间不能空")
    private Date sdate;

    @ApiModelProperty("商品类型")
    @NotNull(message = "商品类型不能空")
    private String goodsType;

    @ApiModelProperty("配件属性 NEW=新件 OLD=旧件")
    private String partsAttr;

    @ApiModelProperty("商品编码")
    @NotBlank(message = "商品编码不能空")
    private String goodsId;

    @ApiModelProperty("商品名称")
    @NotBlank(message = "商品名称不能空")
    private String goodsName;

    @ApiModelProperty("发生单价")
    private BigDecimal price;

    @ApiModelProperty("相关单号")
    @NotBlank(message = "相关单号不能空")
    private String ref;

    @ApiModelProperty("相关单据类型")
    @NotBlank(message = "相关单据类型不能空")
    private String refType;

    @ApiModelProperty("变动数量")
    @NotNull(message = "变动数量不能空")
    private BigDecimal changeQty;

    @ApiModelProperty("发生方向 ADD=增加 SUB=减少")
    @NotBlank(message = "发生方向不能空")
    private String directFlag;

    @ApiModelProperty("备注")
    private String remark;

    @ApiModelProperty("业务对象")
    private String obj;

    @ApiModelProperty("操作人")
    @NotBlank(message = "操作人不能空")
    private String operateBy;

    @ApiModelProperty("操作时间")
    @NotNull(message = "操作时间不能空")
    private Date operateTime;

    @ApiModelProperty(value = "仓储id")
    @NotNull(message = "仓位不能空")
    private String storageId;

    @ApiModelProperty("采购订单号")
    private String purchaseId;

    @ApiModelProperty(value = "凭证号")
    private String voucherNo;

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

    @ApiModelProperty(value = "物料组名称")
    private String materialGroupName;

    @ApiModelProperty(value = "单位")
    private String goodsStockUnit;

    public WebsitStock createPartsStock() {
        WebsitStock websitStock = new WebsitStock();
        BeanUtils.copyProperties(this, websitStock);
        websitStock.setId(null)
                .setLastCost(BigDecimal.ZERO)
                .setGoodsType(this.getGoodsType())
                .setQty(BigDecimal.ZERO)
                .setOldQty(BigDecimal.ZERO)
                .setCreateBy(this.operateBy)
                .setCreateTime(this.operateTime);
        return websitStock;
    }

    public void computeWebsitStock(WebsitStock websitStock, WebsitStockAcc websitStockAcc) {
        BigDecimal qty = websitStock.getQty();
        if (StringUtils.isNotBlank(this.partsAttr) && this.partsAttr.equals(PartsAttrEnum.OLD.getKey())) {
            qty = websitStock.getOldQty();
        }
        if (Objects.isNull(this.directFlag)) {
            throw new RemoteServiceException("发生方向不符");
        }
        BigDecimal direct = this.directFlag.equals(DirectFlagEnum.ADD.getKey()) ? BigDecimal.ONE : new BigDecimal("-1");
        BigDecimal closeQty = qty.add(this.changeQty.multiply(direct));
        if (closeQty.compareTo(BigDecimal.ZERO) < 0 && direct.compareTo(BigDecimal.ZERO) < 0 && !this.getRefType().equals("销售订单")) {
            throw new RemoteServiceException("\""+ websitStock.getGoodsName() + "\"商品库存不允许为负");
        }
        // 注入三级帐
        BeanUtils.copyProperties(this, websitStockAcc);
        websitStockAcc.setWebsitId(this.websitId)
                .setWebsitName(this.websitName)
                .setPartsWebsitId(this.partsWebsitId)
                .setQty(qty)
                .setCloseQty(closeQty)
                .setOperateBy(this.operateBy)
                .setOperateTime(this.operateTime)
                .setStorageId(this.storageId);
        // 注入库存
        if (this.partsAttr.equals(PartsAttrEnum.NEW.getKey())) {
            websitStock.setQty(closeQty);
        } else if (this.partsAttr.equals(PartsAttrEnum.OLD.getKey())) {
            websitStock.setOldQty(closeQty);
        } else {
            throw new RemoteServiceException("配件属性异常");
        }
        websitStock.setUpdateBy(this.operateBy)
                .setUpdateTime(this.operateTime);
    }
}
