package com.yueqian.xingyun.sc.bo.purchase.receive;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.lframework.starter.common.constants.StringPool;
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.StoreCenter;
import com.yueqian.xingyun.basedata.entity.Supplier;
import com.yueqian.xingyun.basedata.service.storecenter.StoreCenterService;
import com.yueqian.xingyun.basedata.service.supplier.SupplierService;
import com.yueqian.xingyun.sc.entity.PurchaseOrder;
import com.yueqian.xingyun.sc.entity.ReceiveSheet;
import com.yueqian.xingyun.sc.service.purchase.PurchaseOrderService;
import com.lframework.starter.web.inner.service.system.SysUserService;
import io.swagger.v3.oas.annotations.media.Schema;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import lombok.Data;

@Data
public class QueryReceiveSheetBo extends BaseBo<ReceiveSheet> {

    /**
     * ID
     */
    @Schema(description = "ID")
    private String id;

    /**
     * 单号
     */
    @Schema(description = "单号")
    private String code;

    /**
     * 仓库编号
     */
    @Schema(description = "仓库编号")
    private String scCode;

    /**
     * 仓库名称
     */
    @Schema(description = "仓库名称")
    private String scName;

    /**
     * 供应商编号
     */
    @Schema(description = "供应商编号")
    private String supplierCode;

    /**
     * 供应商名称
     */
    @Schema(description = "供应商名称")
    private String supplierName;

    /**
     * 采购员姓名
     */
    @Schema(description = "采购员姓名")
    private String purchaserName;

    /**
     * 到货日期
     */
    @Schema(description = "到货日期")
    @JsonFormat(pattern = StringPool.DATE_PATTERN)
    private LocalDate receiveDate;

    /**
     * 采购订单ID
     */
    @Schema(description = "采购订单ID")
    private String purchaseOrderId;

    /**
     * 采购订单号
     */
    @Schema(description = "采购订单号")
    private String purchaseOrderCode;

    /**
     * 采购数量
     */
    @Schema(description = "采购数量")
    private BigDecimal totalNum;

    /**
     * 赠品数量
     */
    @Schema(description = "赠品数量")
    private BigDecimal totalGiftNum;

    /**
     * 采购金额
     */
    @Schema(description = "采购金额")
    private BigDecimal totalAmount;

    /**
     * 备注
     */
    @Schema(description = "备注")
    private String description;

    /**
     * 创建人
     */
    @Schema(description = "创建人")
    private String createBy;

    /**
     * 创建时间
     */
    @Schema(description = "创建时间")
    @JsonFormat(pattern = StringPool.DATE_TIME_PATTERN)
    private LocalDateTime createTime;

    /**
     * 审核人
     */
    @Schema(description = "审核人")
    private String approveBy;

    /**
     * 审核时间
     */
    @Schema(description = "审核时间")
    @JsonFormat(pattern = StringPool.DATE_TIME_PATTERN)
    private LocalDateTime approveTime;

    /**
     * 状态
     */
    @Schema(description = "状态")
    private Integer status;

    /**
     * 拒绝原因
     */
    @Schema(description = "拒绝原因")
    private String refuseReason;

    /**
     * 结算状态
     */
    @Schema(description = "结算状态")
    private Integer settleStatus;

    public QueryReceiveSheetBo(ReceiveSheet dto) {

        super(dto);
    }

    @Override
    public BaseBo<ReceiveSheet> convert(ReceiveSheet dto) {

        return super.convert(dto, QueryReceiveSheetBo::getStatus, QueryReceiveSheetBo::getSettleStatus);
    }

    @Override
    protected void afterInit(ReceiveSheet dto) {

        StoreCenterService storeCenterService = ApplicationUtil.getBean(StoreCenterService.class);
        StoreCenter sc = storeCenterService.findById(dto.getScId());
        this.scCode = sc.getCode();
        this.scName = sc.getName();

        SupplierService supplierService = ApplicationUtil.getBean(SupplierService.class);
        Supplier supplier = supplierService.findById(dto.getSupplierId());
        this.supplierCode = supplier.getCode();
        this.supplierName = supplier.getName();

        SysUserService userService = ApplicationUtil.getBean(SysUserService.class);
        if (!StringUtil.isBlank(dto.getPurchaserId())) {
            this.purchaserName = userService.findById(dto.getPurchaserId()).getName();
        }

        if (!StringUtil.isBlank(dto.getApproveBy())) {
            this.approveBy = userService.findById(dto.getApproveBy()).getName();
        }

        this.status = dto.getStatus().getCode();
        this.settleStatus = dto.getSettleStatus().getCode();

        if (!StringUtil.isBlank(dto.getPurchaseOrderId())) {
            PurchaseOrderService purchaseOrderService = ApplicationUtil.getBean(
                PurchaseOrderService.class);
            PurchaseOrder purchaseOrder = purchaseOrderService.getById(dto.getPurchaseOrderId());
            this.purchaseOrderCode = purchaseOrder.getCode();
        }
    }
}
