package com.yiqi.core.domain.dto;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.yiqi.common.annotation.Excel;
import com.yiqi.common.core.domain.AuthHolder;
import com.yiqi.common.core.domain.BaseEntity;
import com.yiqi.common.enums.OrderSourceType;
import com.yiqi.common.enums.SourceType;
import com.yiqi.common.enums.SysBoolType;
import com.yiqi.common.utils.DateUtils;
import com.yiqi.common.utils.SecurityUtils;
import com.yiqi.core.domain.GoodsSkuStockRecord;
import com.yiqi.order.domain.OrderGoods;
import com.yiqi.order.domain.OrderGoodsSku;
import com.yiqi.order.domain.OrderPurchaseGoods;
import com.yiqi.order.domain.OrderPurchaseGoodsSku;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品库存变更记录对象 goods_sku_stock_record
 *
 * @author ruoyi
 * @date 2023-11-29
 */
@ApiModel("商品库存变更记录")
@Data
public class GoodsSkuStockRecordDTO extends BaseEntity {
    private static final long serialVersionUID = 1L;

    /**
     * 主键ID
     */
    @ApiModelProperty("主键ID")
    @TableId(type = IdType.AUTO)

    private Long id;

    /**
     * skuID
     */
    @Excel(name = "skuID")
    @ApiModelProperty("skuID")

    private Long goodsSkuId;

    /**
     * 组织ID
     */
    @Excel(name = "组织ID")
    @ApiModelProperty("组织ID")

    private Long orgId;

    /**
     * 来源类型
     */
    @Excel(name = "来源类型")
    @ApiModelProperty("来源类型")
    private String sourceType;

    /**
     * 组织ID
     */
    @Excel(name = "组织ID")
    @ApiModelProperty("组织ID")

    private Long buyOrgId;

    /**
     * 来源类型
     */
    @Excel(name = "来源类型")
    @ApiModelProperty("来源类型")
    private String buySourceType;

    /**
     * 增减数量
     */
    @Excel(name = "增减数量")
    @ApiModelProperty("增减数量")
    private Integer num;

    /**
     * 是否增加
     */
    @Excel(name = "是否增加")
    @ApiModelProperty("是否增加")
    private String isAdd;

    /**
     * 卖方组织ID
     */
    @Excel(name = "卖方组织ID")
    @ApiModelProperty("卖方组织ID")

    private Long sellOrgId;

    /**
     * 卖方来源类型
     */
    @Excel(name = "卖方来源类型")
    @ApiModelProperty("卖方来源类型")
    private String sellSourceType;

    /**
     * 订单ID
     */
    @Excel(name = "订单ID")
    @ApiModelProperty("订单ID")

    private Long orderId;

    /**
     * 订单编号
     */
    @Excel(name = "订单编号")
    @ApiModelProperty("订单编号")
    private String orderNo;

    /**
     * 商品ID
     */
    @Excel(name = "商品ID")
    @ApiModelProperty("商品ID")

    private Long goodsId;

    /**
     * 商品类型
     */
    @Excel(name = "商品类型")
    @ApiModelProperty("商品类型")
    private String goodsType;

    /**
     * 是否退款
     */
    @Excel(name = "是否退款")
    @ApiModelProperty("是否退款")
    private String isRefund;

    /**
     * 备注
     */
    @Excel(name = "备注")
    @ApiModelProperty("备注")
    private String remark;


    /**
     * 进出货订单 写入库存记录
     */
    public static List<GoodsSkuStockRecord> buildStockByOrderPurchaseGoods(OrderPurchaseGoods orderPurchaseGoods,
                                                                           List<OrderPurchaseGoodsSku> orderPurchaseGoodsSkus,
                                                                           Boolean isAdd,
                                                                           Boolean isRefund) {
        List<GoodsSkuStockRecord> goodsStockRecordList = new ArrayList<>();
        for (OrderPurchaseGoodsSku goodsSkus : orderPurchaseGoodsSkus) {
            GoodsSkuStockRecord goodsSkuStockRecord = new GoodsSkuStockRecord();
            goodsSkuStockRecord.buildCreateData();
            goodsSkuStockRecord.setGoodsSkuId(goodsSkus.getSkuId());
            goodsSkuStockRecord.setNum(goodsSkus.getBuyNum());
            goodsSkuStockRecord.setOrderId(orderPurchaseGoods.getId());
            goodsSkuStockRecord.setOrderNo(orderPurchaseGoods.getOrderNo());
            goodsSkuStockRecord.setGoodsId(goodsSkus.getGoodsId());
            goodsSkuStockRecord.setGoodsType(orderPurchaseGoods.getGoodsType());
            goodsSkuStockRecord.setIsAdd(isAdd ? SysBoolType.YES.getCode() : SysBoolType.NO.getCode());
            goodsSkuStockRecord.setIsRefund(isRefund ? SysBoolType.YES.getCode() : SysBoolType.NO.getCode());
            goodsSkuStockRecord.setSourceType(SecurityUtils.getLoginUser().getSourceType());
            goodsSkuStockRecord.setBuySourceType(orderPurchaseGoods.getSourceType());
            goodsSkuStockRecord.setBuyOrgId(orderPurchaseGoods.getOrgId());
            // 进货
            if (isAdd) {
                // 总部进货
                if (SourceType.MANAGER.getCode().equals(SecurityUtils.getLoginUser().getSourceType())) {
                    goodsSkuStockRecord.setRemark("总部进货");
                } else {
                    // 门店进货
                    goodsSkuStockRecord.setOrgId(SecurityUtils.getLoginUser().getStoreId());
                    goodsSkuStockRecord.setRemark("门店进货");
                }
            } else {
                // 出货
                goodsSkuStockRecord.setRemark("总部出货");
            }
            goodsStockRecordList.add(goodsSkuStockRecord);
        }
        return goodsStockRecordList;
    }

    /**
     * 零售订单 写入出库记录
     */
    public static List<GoodsSkuStockRecord> buildStockRecordByOrderRetailGoods(OrderGoods orderGoods,
                                                                               List<OrderGoodsSku> orderGoodsSkus,
                                                                               Boolean isAdd,
                                                                               Boolean isRefund) {
        List<GoodsSkuStockRecord> goodsStockRecordList = new ArrayList<>();
        for (OrderGoodsSku goodsSkus : orderGoodsSkus) {
            GoodsSkuStockRecord goodsSkuStockRecord = new GoodsSkuStockRecord();
            if (OrderSourceType.APP.getCode().equals(orderGoods.getOrderSource())) {
                goodsSkuStockRecord.setSourceType(SourceType.MANAGER.getCode());
            } else {
                goodsSkuStockRecord.setSourceType(SecurityUtils.getLoginUser().getSourceType());
            }
            goodsSkuStockRecord.buildCreateData();
            goodsSkuStockRecord.setGoodsSkuId(goodsSkus.getSkuId());
            goodsSkuStockRecord.setNum(goodsSkus.getBuyNum());
            goodsSkuStockRecord.setOrderId(orderGoods.getId());
            goodsSkuStockRecord.setOrderNo(orderGoods.getOrderNo());
            goodsSkuStockRecord.setGoodsId(goodsSkus.getGoodsId());
            goodsSkuStockRecord.setGoodsType(orderGoods.getGoodsType());
            goodsSkuStockRecord.setIsAdd(isAdd ? SysBoolType.YES.getCode() : SysBoolType.NO.getCode());
            goodsSkuStockRecord.setIsRefund(isRefund ? SysBoolType.YES.getCode() : SysBoolType.NO.getCode());

            // 客户退货 = 加货 只有总部会退货 所以都是总部
            if (isAdd) {
                if (SecurityUtils.getLoginUser() != null && SourceType.STORE.getCode().equals(SecurityUtils.getLoginUser().getSourceType())) {
                    //门店退货
                    goodsSkuStockRecord.setSourceType(SecurityUtils.getLoginUser().getSourceType());
                    goodsSkuStockRecord.setOrgId(SecurityUtils.getLoginUser().getOrgId());
                    goodsSkuStockRecord.setRemark("线下门店退货");
                } else {
                    goodsSkuStockRecord.setSourceType(SourceType.MANAGER.getCode());
                    goodsSkuStockRecord.setRemark("线上零售退货");
                }
            } else {
                if (OrderSourceType.APP.getCode().equals(orderGoods.getOrderSource())) {
                    // 总部出货
                    goodsSkuStockRecord.setSourceType(SourceType.MANAGER.getCode());
                    goodsSkuStockRecord.setRemark("线上零售出货");
                } else {
                    // 总部出货
                    if (SecurityUtils.getLoginUser() != null && SourceType.MANAGER.getCode().equals(SecurityUtils.getLoginUser().getSourceType())) {

                    } else {
                        // 门店零售出货
                        goodsSkuStockRecord.setSourceType(SecurityUtils.getLoginUser().getSourceType());
                        goodsSkuStockRecord.setOrgId(SecurityUtils.getLoginUser().getOrgId());
                        goodsSkuStockRecord.setRemark("线下门店出货");
                    }
                }
            }
            goodsSkuStockRecord.setBuySourceType(SourceType.CUSTOM.getCode());
            goodsSkuStockRecord.setBuyOrgId(orderGoods.getAppUserId());
//            goodsSkuStockRecord.setSellSourceType(orderGoods.getSourceType());
//            goodsSkuStockRecord.setSellOrgId(orderGoods.getOrgId());
            goodsStockRecordList.add(goodsSkuStockRecord);
        }
        return goodsStockRecordList;
    }
}
