package com.yueqian.xingyun.sc.bo.stock.transfer;

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.service.storecenter.StoreCenterService;
import com.yueqian.xingyun.sc.entity.ScTransferOrder;
import com.lframework.starter.web.inner.service.system.SysUserService;
import io.swagger.v3.oas.annotations.media.Schema;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import lombok.Data;

/**
 * <p>
 * 仓库调拨单 QueryBo
 * </p>
 *
 * @author zmj
 */
@Data
public class QueryScTransferOrderBo extends BaseBo<ScTransferOrder> {

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

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

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

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

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

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

  /**
   * 调拨数量
   */
  @Schema(description = "调拨数量")
  private BigDecimal totalNum;

  /**
   * 调拨成本金额
   */
  @Schema(description = "调拨成本金额")
  private BigDecimal totalAmount;

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

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

  /**
   * 修改人
   */
  @Schema(description = "修改人")
  private String updateBy;

  /**
   * 修改时间
   */
  @Schema(description = "修改时间")
  @JsonFormat(pattern = StringPool.DATE_TIME_PATTERN)
  private LocalDateTime updateTime;

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

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

  public QueryScTransferOrderBo() {

  }

  public QueryScTransferOrderBo(ScTransferOrder dto) {

    super(dto);
  }

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

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

  @Override
  protected void afterInit(ScTransferOrder dto) {

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

    StoreCenterService storeCenterService = ApplicationUtil.getBean(StoreCenterService.class);
    StoreCenter sourceSc = storeCenterService.findById(dto.getSourceScId());
    this.sourceScCode = sourceSc.getCode();
    this.sourceScName = sourceSc.getName();

    StoreCenter targetSc = storeCenterService.findById(dto.getTargetScId());
    this.targetScCode = targetSc.getCode();
    this.targetScName = targetSc.getName();

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