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

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.format.DateTimeFormat;
import com.fasterxml.jackson.annotation.JsonFormat;
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.bo.BaseBo;
import com.lframework.starter.web.common.utils.ApplicationUtil;
import com.lframework.starter.web.components.excel.ExcelModel;
import com.lframework.xingyun.basedata.entity.Customer;
import com.lframework.xingyun.basedata.entity.PayType;
import com.lframework.xingyun.basedata.entity.StoreCenter;
import com.lframework.xingyun.basedata.service.customer.CustomerService;
import com.lframework.xingyun.basedata.service.paytype.PayTypeService;
import com.lframework.xingyun.basedata.service.storecenter.StoreCenterService;
import com.lframework.xingyun.sc.bo.sale.QuerySaleOrderBo;
import com.lframework.xingyun.sc.entity.OrderPayType;
import com.lframework.xingyun.sc.entity.SaleOrder;
import com.lframework.xingyun.sc.enums.SaleOrderStatus;
import com.lframework.xingyun.sc.service.paytype.OrderPayTypeService;
import com.lframework.xingyun.template.core.dto.UserDto;
import com.lframework.xingyun.template.core.service.UserService;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import lombok.Data;

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

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

    @ExcelProperty(value = "商品信息")
    private String productName;

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

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

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

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

    /**
     *  todo 成本  采购总价totalPurchasePrice + 运费shippingFee + 客户费用customMoney
     */
    @ExcelProperty("成本")
    private BigDecimal cost;

    /**
     * 客户费用
     */
    @ExcelProperty("客户费用")
    private BigDecimal customMoney;

    /**
     * 本单利润
     */
    @ExcelProperty("利润")
    private BigDecimal thisOrderProfit;

    /**
     * 毛利率 仅做计算，不做存储
     * 毛利率 = 本单利润/含税总金额* 100%
     */
    @ExcelProperty("毛利率")
    private String grossMargin;


    /**
     * 销售数量
     */
    @ExcelProperty("商品数量")
    private Integer totalNum;

    /**
     * 赠品数量
     */
    @ExcelProperty("赠品数量")
    private Integer totalGiftNum;

    /**
     * 是否租赁订单
     */
    @ExcelProperty("是否租赁订单")
    private String isLease;

    /**
     * todo 租赁时间
     */
    @ExcelProperty("租赁时间")
    private String leaseTime;

    /**
     * 创建时间
     */
    @ExcelProperty("操作时间")
    @JsonFormat(pattern = StringPool.DATE_TIME_PATTERN)
    private String createTime;

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

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

    /**
     * 审核时间
     */
    @ExcelProperty("审核时间")
    @JsonFormat(pattern = StringPool.DATE_TIME_PATTERN)
    private String approveTime;

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

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

    public SaleOrderExportModel() {

    }

    public SaleOrderExportModel(QuerySaleOrderBo bo) {

        super(bo);
    }

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

        return this;
    }

    @Override
    protected void afterInit(QuerySaleOrderBo dto) {
        //采购总价totalPurchasePrice + 运费shippingFee + 客户费用customMoney
        // 推荐写法：链式调用 + 空值安全处理（若参数可能为null）
        this.setCost(
                Optional.ofNullable(dto.getTotalPurchasePrice()).orElse(BigDecimal.ZERO)
                        .add(Optional.ofNullable(dto.getShippingFee()).orElse(BigDecimal.ZERO))
                        .add(Optional.ofNullable(dto.getCustomMoney()).orElse(BigDecimal.ZERO))
                        .setScale(3, RoundingMode.HALF_UP)
        );
        this.setCode(dto.getCode());
        this.setProductName(dto.getProductName());
        this.setCustomerCode(dto.getCustomerCode());
        this.setCustomerName(dto.getCustomerName());
        this.setSalerName(dto.getSalerName());
        this.setTotalAmount(dto.getTotalAmount());
        this.setCustomMoney(dto.getCustomMoney());
        this.setThisOrderProfit(dto.getThisOrderProfit());
        this.setGrossMargin(dto.getGrossMargin());
        this.setTotalNum(dto.getTotalNum());
        this.setTotalGiftNum(dto.getTotalGiftNum());
        this.setIsLease(dto.getIsLease() == 1 ? "是" : "否");
        // 步骤1：定义日期格式（按需调整格式）
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        String leaseStartTime = Optional.ofNullable(dto.getLeaseStartTime())
                .map(formatter::format)
                .orElse("");  // 空值默认替换为空字符串（或改为 "N/A"）
        String leaseEndTime = Optional.ofNullable(dto.getLeaseEndTime())
                .map(formatter::format)
                .orElse("");

        this.setLeaseTime(leaseStartTime + "-" + leaseEndTime);

        String createTime = Optional.ofNullable(dto.getCreateTime())
                .map(formatter::format)
                .orElse("");

        this.setCreateTime(createTime);
        this.setCreateBy(dto.getCreateBy());
        this.setStatus(SaleOrderStatus.valueOfCode(dto.getStatus()));
        String approveTime = Optional.ofNullable(dto.getApproveTime())
                .map(formatter::format)
                .orElse("");
        this.setApproveTime(approveTime);
        this.setApproveBy(dto.getApproveBy());
        this.setDescription(dto.getDescription());
    }
}

