package com.yueqian.xingyun.sc.bo.stock.take.pre;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.lframework.starter.common.utils.StringUtil;
import com.lframework.starter.web.core.bo.BaseBo;
import com.lframework.starter.web.core.utils.ApplicationUtil;
import com.yueqian.xingyun.basedata.entity.Product;
import com.yueqian.xingyun.basedata.entity.ProductBrand;
import com.yueqian.xingyun.basedata.entity.ProductCategory;
import com.yueqian.xingyun.basedata.service.product.ProductBrandService;
import com.yueqian.xingyun.basedata.service.product.ProductCategoryService;
import com.yueqian.xingyun.basedata.service.product.ProductService;
import com.yueqian.xingyun.sc.dto.stock.take.pre.QueryPreTakeStockSheetProductDto;
import com.yueqian.xingyun.sc.entity.ProductStock;
import com.yueqian.xingyun.sc.entity.TakeStockConfig;
import com.yueqian.xingyun.sc.enums.PreTakeStockSheetStatus;
import com.yueqian.xingyun.sc.service.stock.ProductStockService;
import com.yueqian.xingyun.sc.service.stock.take.TakeStockConfigService;
import io.swagger.v3.oas.annotations.media.Schema;
import java.math.BigDecimal;
import lombok.Data;

@Data
public class QueryPreTakeStockSheetProductBo extends BaseBo<QueryPreTakeStockSheetProductDto> {

    /**
     * 仓库ID
     */
    @Schema(description = "仓库ID", hidden = true)
    @JsonIgnore
    private String scId;

    /**
     * 商品ID
     */
    @Schema(description = "商品ID")
    private String productId;

    /**
     * 商品编号
     */
    @Schema(description = "商品编号")
    private String productCode;

    /**
     * 商品名称
     */
    @Schema(description = "商品名称")
    private String productName;

    /**
     * 分类名称
     */
    @Schema(description = "分类名称")
    private String categoryName;

    /**
     * 品牌名称
     */
    @Schema(description = "品牌名称")
    private String brandName;

    /**
     * 规格
     */
    @Schema(description = "规格")
    private String spec;

    /**
     * 单位
     */
    @Schema(description = "单位")
    private String unit;

    /**
     * 库存数量
     */
    @Schema(description = "库存数量")
    private BigDecimal stockNum;

    /**
     * 盘点数量
     */
    @Schema(description = "盘点数量")
    private BigDecimal takeNum;

    public QueryPreTakeStockSheetProductBo(QueryPreTakeStockSheetProductDto dto, String scId) {

        this.scId = scId;

        this.init(dto);
    }

    @Override
    protected void afterInit(QueryPreTakeStockSheetProductDto dto) {

        ProductService productService = ApplicationUtil.getBean(ProductService.class);
        Product product = productService.findById(dto.getProductId());

        ProductCategoryService productCategoryService = ApplicationUtil.getBean(
            ProductCategoryService.class);
        ProductCategory productCategory = productCategoryService.findById(product.getCategoryId());

        if(StringUtil.isNotBlank(product.getBrandId())) {
            ProductBrandService productBrandService = ApplicationUtil.getBean(ProductBrandService.class);
            ProductBrand productBrand = productBrandService.findById(product.getBrandId());
            this.brandName = productBrand.getName();
        }

        this.productCode = product.getCode();
        this.productName = product.getName();
        this.categoryName = productCategory.getName();
        this.spec = product.getSpec();
        this.unit = product.getUnit();

        if (dto.getTakeStatus() == PreTakeStockSheetStatus.FIRST_TAKE) {
            this.takeNum = dto.getFirstNum();
        } else if (dto.getTakeStatus() == PreTakeStockSheetStatus.SECOND_TAKE) {
            this.takeNum = dto.getSecondNum();
        } else if (dto.getTakeStatus() == PreTakeStockSheetStatus.RAND_TAKE) {
            this.takeNum = dto.getRandNum();
        }

        TakeStockConfigService takeStockConfigService = ApplicationUtil.getBean(
            TakeStockConfigService.class);
        TakeStockConfig config = takeStockConfigService.get();
        if (config.getShowStock()) {
            ProductStockService productStockService = ApplicationUtil.getBean(ProductStockService.class);
            ProductStock productStock = productStockService.getByProductIdAndScId(this.productId, this.scId);
            this.stockNum = productStock == null ? BigDecimal.ZERO : productStock.getStockNum();
        }
    }
}
