package com.yd.csf.service.vo;

import cn.hutool.core.convert.Convert;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.format.DateTimeFormat;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.alibaba.excel.annotation.write.style.HeadRowHeight;
import com.yd.csf.feign.response.expectedfortune.ApiExpectedFortunePageResponse;
import com.yd.csf.service.enums.FortuneStatusEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;

@Data
@ContentRowHeight(15)
@HeadRowHeight(20)
@ColumnWidth(20)
public class ExpectedFortuneExportDTO {

    @ExcelProperty("预计发佣业务ID")
    private String expectedFortuneBizId;

    @ExcelProperty("应付款编号")
    private String payableNo;

    @ExcelProperty("应付款类型")
    private String fortuneBizType;

    @ExcelProperty("保单号")
    private String policyNo;

    @ExcelProperty("类型")
    private String type;

    @ExcelProperty("出账状态")
    private String status;

    @ExcelProperty("转介人名称")
    private String broker;

    @ExcelProperty("转介人等级")
    private String brokerGradeName;

    @ExcelProperty("团队名称")
    private String team;

    @ExcelProperty("出账币种")
    private String currency;

    @ExcelProperty("出账期数")
    private Integer fortunePeriod;

    @ExcelProperty("出账总期数")
    private Integer fortuneTotalPeriod;

    @ExcelProperty("出账项目")
    private String fortuneName;

    @ExcelProperty("关联人员")
    private String amountSourceName;

    @ExcelProperty("出账年月（估）")
    @DateTimeFormat("yyyy-MM")
    @ColumnWidth(25)
    private Date payoutDate;

    @ExcelProperty("出账年月（实）")
    @DateTimeFormat("yyyy-MM")
    @ColumnWidth(25)
    private Date actualPayoutDate;

    @ExcelProperty("出账金额（估）")
    private BigDecimal amount;

    @ExcelProperty("已出账比例(%)")
    private BigDecimal paidRatio;

    @ExcelProperty("已出账金额")
    private BigDecimal paidAmount;

    @ExcelProperty("待出账比例(%)")
    private BigDecimal unpaidRatio;

    @ExcelProperty("本次发放保单记账金额")
    private BigDecimal currentPaymentRuleAmount;

    @ExcelProperty("本次发放金额")
    private BigDecimal currentPayoutAmount;

    @ExcelProperty("本次发放折合港币金额")
    private BigDecimal currentPaymentHkdAmount;

    @ExcelProperty("本次发放比例")
    private BigDecimal currentPaymentRatio;

    @ExcelProperty("累积已发放保单记账金额")
    private BigDecimal paidRuleAmount;

    @ExcelProperty("剩余保单记账金额")
    private BigDecimal unpaidRuleAmount;

    @ExcelProperty("剩余发放折合港币金额")
    private BigDecimal unpaidRuleAmounthHkd;

    @ExcelProperty("持有比例")
    private String brokerRatio;

    @ExcelProperty("产品计划")
    private String productName;

    @ExcelProperty("期交保费")
    private BigDecimal premium;

    @ExcelProperty("备注")
    @ColumnWidth(30)
    private String remark;

    @ExcelProperty("创建人")
    private String creatorName;

    @ExcelProperty("创建时间")
    @DateTimeFormat("yyyy-MM-dd HH:mm:ss")
    @ColumnWidth(25)
    private LocalDateTime createTime;

    @ExcelProperty("更新时间")
    @DateTimeFormat("yyyy-MM-dd HH:mm:ss")
    @ColumnWidth(25)
    private LocalDateTime updateTime;

    /**
     * 将对象转换为导出DTO
     */
    public static ExpectedFortuneExportDTO convertToExportDTO(ApiExpectedFortunePageResponse vo) {
        ExpectedFortuneExportDTO dto = new ExpectedFortuneExportDTO();
        dto.setExpectedFortuneBizId(vo.getExpectedFortuneBizId());
        dto.setPayableNo(vo.getPayableNo());
        dto.setFortuneBizType("R".equals(vo.getFortuneBizType()) ? "关联保单应付款" : "非关联保单应付款");
        dto.setPolicyNo(vo.getPolicyNo());
        dto.setType(vo.getType() != null ? (vo.getType() == 1 ? "预计出账" : "实际出账") : null);
        dto.setPremium(vo.getPremium());
        dto.setFortunePeriod(vo.getFortunePeriod());
        dto.setFortuneTotalPeriod(vo.getFortuneTotalPeriod());
        dto.setBroker(vo.getBroker());
        dto.setTeam(vo.getTeam());
        dto.setFortuneName(vo.getFortuneName());
        dto.setAmountSourceName(vo.getAmountSourceName());
        dto.setBrokerRatio(vo.getBrokerRatio());
        dto.setAmount(vo.getHkdAmount());
        dto.setCurrency("港币");
        dto.setStatus(getStatusText(vo.getStatus()));
        dto.setProductName(vo.getProductName());
        dto.setPayoutDate(Convert.toDate(vo.getPayoutDate()));
        dto.setActualPayoutDate(Convert.toDate(vo.getActualPayoutDate()));
        dto.setPaidAmount(vo.getPaidAmount());
        dto.setPaidRatio(vo.getPaidRatio());
        dto.setUnpaidRatio(vo.getUnpaidRatio());
        dto.setCurrentPaymentRuleAmount(vo.getCurrentPaymentRuleAmount());
        dto.setCurrentPayoutAmount(vo.getCurrentPayoutAmount());
        dto.setCurrentPaymentHkdAmount(vo.getCurrentPaymentHkdAmount());
        dto.setCurrentPaymentRatio(vo.getCurrentPaymentRatio());
        dto.setPaidRuleAmount(vo.getPaidRuleAmount());
        dto.setUnpaidRuleAmount(vo.getUnpaidRuleAmount());
        dto.setUnpaidRuleAmounthHkd(vo.getUnpaidRuleAmounthHkd());
        dto.setRemark(vo.getRemark());
        dto.setCreatorName(vo.getCreatorName());
        dto.setCreateTime(vo.getCreateTime());
        dto.setUpdateTime(vo.getUpdateTime());
        dto.setBrokerGradeName(vo.getBrokerGradeName());
        return dto;
    }

    /**
     * 获取状态文本
     */
    public static String getStatusText(String status) {
        if (status == null) return "未知";
        for (FortuneStatusEnum enumItem : FortuneStatusEnum.values()) {
            if (enumItem.getItemValue().equals(status)) {
                return enumItem.getItemLabel();
            }
        }
        return "未知";
    }
}
