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

import com.gree.mall.manager.enums.material.DirectFlagEnum;
import com.gree.mall.manager.enums.material.PartsAttrEnum;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.plus.entity.WorkerStock;
import com.gree.mall.manager.plus.entity.WorkerStockAcc;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
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
public class WorkerStockDTO {

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

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

    @ApiModelProperty("师傅身份证")
    @NotBlank(message = "师傅身份证不能空")
    private String identity;

    @ApiModelProperty("师傅编号")
    @NotBlank(message = "师傅编号不能空")
    private String workerId;

    @ApiModelProperty("师傅名称")
    @NotBlank(message = "师傅名称不能空")
    private String workerName;

    @ApiModelProperty("师傅网点编号")
//    @NotBlank(message = "师傅网点编号不能空")
    private String workerWebsitId;

    @ApiModelProperty("师傅网点名称")
//    @NotBlank(message = "师傅网点名称不能空")
    private String workerWebsitName;

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

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

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

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

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

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

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

    @ApiModelProperty("网点商品编码")
    private String websitGoodsId;

    @ApiModelProperty("网点商品名称")
    private String websitGoodsName;

    @ApiModelProperty("网点商品转换系数")
    private BigDecimal goodsSalesConvertQty;

    @ApiModelProperty("网点规格型号")
    private String goodsSpecification;

    @ApiModelProperty("网点销售数量")
    private BigDecimal websitGoodsQty;

    @ApiModelProperty("网点销售价")
    private BigDecimal websitGoodsPrice;

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

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

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

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

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

    @ApiModelProperty("配件属性 NEW=新件 OLD=旧件 WAY=在途 TEMPNEW=临占新件 TEMPOLD=临占旧件")
    @NotBlank(message = "配件属性不能空")
    private String partsAttr;

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

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

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

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

    @ApiModelProperty("状态 USED=已使用 REFUND=退货 INIT=初始化 REFUND_NEW=返新件 REFUND_OLD=返旧件")
    private String flag;

    @ApiModelProperty("工单号")
    private String workerOrderNo;

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

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

    public WorkerStock createWorkerStock() {
        WorkerStock workerStock = new WorkerStock();
        BeanUtils.copyProperties(this, workerStock);
        workerStock.setId(null)
                .setWebsitId(this.websitId)
                .setWebsitName(this.websitName)
                .setQty(BigDecimal.ZERO)
                .setOldQty(BigDecimal.ZERO)
                .setTempNewQty(BigDecimal.ZERO)
                .setTempOldQty(BigDecimal.ZERO)
                .setWayQty(BigDecimal.ZERO)
                .setChangeSalesQty(BigDecimal.ZERO)
                .setOperBy(this.operateBy)
                .setOperTime(this.operateTime);
        return workerStock;
    }

    public void computeWorkerStock(WorkerStock workerStock, WorkerStockAcc workerStockAcc) throws RemoteServiceException {
        BigDecimal qty;
        if (this.partsAttr.equals(PartsAttrEnum.NEW.getKey())) {
            qty = workerStock.getQty();
        } else if (this.partsAttr.equals(PartsAttrEnum.OLD.getKey())) {
            qty = workerStock.getOldQty();
        } else if (this.partsAttr.equals(PartsAttrEnum.TEMP_NEW.getKey())) {
            qty = workerStock.getTempNewQty();
        } else if (this.partsAttr.equals(PartsAttrEnum.TEMP_OLD.getKey())) {
            qty = workerStock.getTempOldQty();
        } else if (this.partsAttr.equals(PartsAttrEnum.WAY.getKey())) {
            qty = workerStock.getWayQty();
        } else if (this.partsAttr.equals(PartsAttrEnum.CHANGE_NEW.getKey())) {
            qty = workerStock.getChangeSalesQty();
        } else {
            throw new RemoteServiceException("配件库存属性不匹配：" + this.partsAttr);
        }
        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) {
//            throw new RemoteServiceException(workerStock.getGoodsName() + "：" + PartsAttrEnum.valueOf(this.partsAttr).getRemark() + "结存库存不能少于0");
//        }
        // 注入三级帐
        BeanUtils.copyProperties(this, workerStockAcc);
        workerStockAcc.setQty(qty)
                .setCloseQty(closeQty)
                .setOperBy(this.operateBy)
                .setOperTime(this.operateTime);
        // 注入库存
        if (this.partsAttr.equals(PartsAttrEnum.NEW.getKey())) {
            workerStock.setQty(closeQty);
        } else if (this.partsAttr.equals(PartsAttrEnum.OLD.getKey())) {
            workerStock.setOldQty(closeQty);
        } else if (this.partsAttr.equals(PartsAttrEnum.TEMP_NEW.getKey())) {
            workerStock.setTempNewQty(closeQty);
        } else if (this.partsAttr.equals(PartsAttrEnum.TEMP_OLD.getKey())) {
            workerStock.setTempOldQty(closeQty);
        } else if (this.partsAttr.equals(PartsAttrEnum.WAY.getKey())) {
            workerStock.setWayQty(closeQty);
        } else if (this.partsAttr.equals(PartsAttrEnum.CHANGE_NEW.getKey())) {
            workerStock.setChangeSalesQty(closeQty);
        }
        workerStock.setOperBy(this.operateBy)
                .setOperTime(this.operateTime);
    }

}
