package com.yueqian.xingyun.sc.excel.sale;

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.format.DateTimeFormat;
import com.lframework.starter.common.constants.StringPool;
import com.lframework.starter.common.utils.CollectionUtil;
import com.lframework.starter.common.utils.DateUtil;
import com.lframework.starter.common.utils.StringUtil;
import com.lframework.starter.web.core.bo.BaseBo;
import com.lframework.starter.web.core.components.excel.ExcelModel;
import com.lframework.starter.web.core.utils.ApplicationUtil;
import com.yueqian.xingyun.basedata.entity.Customer;
import com.yueqian.xingyun.basedata.entity.PayType;
import com.yueqian.xingyun.basedata.entity.StoreCenter;
import com.yueqian.xingyun.basedata.service.customer.CustomerService;
import com.yueqian.xingyun.basedata.service.paytype.PayTypeService;
import com.yueqian.xingyun.basedata.service.storecenter.StoreCenterService;
import com.lframework.starter.web.inner.entity.SysUser;
import com.yueqian.xingyun.sc.entity.OrderPayType;
import com.yueqian.xingyun.sc.entity.SaleOrder;
import com.yueqian.xingyun.sc.service.paytype.OrderPayTypeService;
import com.lframework.starter.web.inner.service.system.SysUserService;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import lombok.Data;

@Data
public class SaleOrderExportModel extends BaseBo<SaleOrder> implements ExcelModel {

  /**
   * 单号
   */
  @ExcelProperty("业务单据号")
  private String code;

  /**
   * 仓库编号
   */
  @ExcelProperty("仓库编号")
  private String scOde;

  /**
   * 仓库名称
   */
  @ExcelProperty("仓库名称")
  private String scName;

  /**
   * 客户编号
   */
  @ExcelProperty("客户编号")
  private String customerCode;

  /**
   * 客户名称
   */
  @ExcelProperty("客户名称")
  private String customerName;

  /**
   * 销售员姓名
   */
  @ExcelProperty("销售员")
  private String salerName;

  /**
   * 单据总金额
   */
  @ExcelProperty("单据总金额")
  private BigDecimal totalAmount;

  /**
   * 约定支付
   */
  @ExcelProperty("约定支付")
  private String payTypeStr;

  /**
   * 商品数量
   */
  @ExcelProperty("商品数量")
  private BigDecimal totalNum;

  /**
   * 赠品数量
   */
  @ExcelProperty("赠品数量")
  private BigDecimal giftNum;

  /**
   * 操作时间
   */
  @ExcelProperty("操作时间")
  @DateTimeFormat(StringPool.DATE_TIME_PATTERN)
  private Date createTime;

  /**
   * 操作人
   */
  @ExcelProperty("操作人")
  private String createBy;

  /**
   * 审核状态
   */
  @ExcelProperty("审核状态")
  private String status;

  /**
   * 审核时间
   */
  @ExcelProperty("审核时间")
  @DateTimeFormat(StringPool.DATE_TIME_PATTERN)
  private Date approveTime;

  /**
   * 审核人
   */
  @ExcelProperty("审核人")
  private String approveBy;

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

  public SaleOrderExportModel() {

  }

  public SaleOrderExportModel(SaleOrder dto) {

    super(dto);
  }

  @Override
  public <A> BaseBo<SaleOrder> convert(SaleOrder dto) {

    return this;
  }

  @Override
  protected void afterInit(SaleOrder dto) {

    StoreCenterService storeCenterService = ApplicationUtil.getBean(StoreCenterService.class);
    StoreCenter sc = storeCenterService.findById(dto.getScId());

    CustomerService customerService = ApplicationUtil.getBean(CustomerService.class);
    Customer customer = customerService.findById(dto.getCustomerId());

    SysUserService userService = ApplicationUtil.getBean(SysUserService.class);
    SysUser saler = userService.findById(dto.getSalerId());
    SysUser approveBy = null;
    if (!StringUtil.isBlank(dto.getApproveBy())) {
      approveBy = userService.findById(dto.getApproveBy());
    }

    this.setCode(dto.getCode());
    this.setScOde(sc.getCode());
    this.setScName(sc.getName());
    this.setCustomerCode(customer.getCode());
    this.setCustomerName(customer.getName());
    if (saler != null) {
      this.setSalerName(saler.getName());
    }
    this.setTotalNum(dto.getTotalNum());
    this.setGiftNum(dto.getTotalGiftNum());
    this.setTotalAmount(dto.getTotalAmount());
    this.setDescription(dto.getDescription());
    this.setCreateTime(DateUtil.toDate(dto.getCreateTime()));
    if (approveBy != null) {
      this.setApproveBy(approveBy.getName());
    }

    if (dto.getApproveTime() != null) {
      this.setApproveTime(DateUtil.toDate(dto.getApproveTime()));
    }

    this.setStatus(dto.getStatus().getDesc());

    OrderPayTypeService orderPayTypeService = ApplicationUtil.getBean(OrderPayTypeService.class);
    List<OrderPayType> orderPayTypes = orderPayTypeService.findByOrderId(dto.getId());

    PayTypeService payTypeService = ApplicationUtil.getBean(PayTypeService.class);

    if (CollectionUtil.isNotEmpty(orderPayTypes)) {
      this.payTypeStr = orderPayTypes.stream().map(orderPayType -> {
        PayType payType = payTypeService.findById(orderPayType.getPayTypeId());
        return StringUtil.format("{}：{}元", payType.getName(), orderPayType.getPayAmount());
      }).collect(Collectors.joining("；"));
    }
  }
}
