package com.yd.csf.service.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.yd.csf.service.model.Commission;
import com.yd.csf.service.model.CommissionExpected;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Data
@Schema(description = "预计来佣响应")
public class CommissionExpectedVO implements Serializable {

    /**
     * 预计来佣id
     */
    @Schema(description = "预计来佣id")
    private Long id;

    /**
     * 预计来佣业务id
     */
    @Schema(description = "预计来佣业务id")
    private String commissionExpectedBizId;

    /**
     * 应收账款编号
     */
    @Schema(description = "应收账款编号")
    private String receivableNo;

    /**
     * 应收款类型 R=关联保单应收单 U=非关联保单应收单
     */
    @Schema(description = "应收款类型 R=关联保单应收单 U=非关联保单应收单")
    private String commissionBizType;

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

    /**
     * 期交保费
     */
    @Schema(description = "期交保费")
    private Object premium;

    /**
     * 产品计划名称
     */
    @Schema(description = "产品计划名称")
    private String productName;

    /**
     * 保险公司业务ID
     */
    @Schema(description = "保险公司业务ID")
    private String insuranceCompanyBizId;

    /**
     * 保险公司
     */
    @Schema(description = "保险公司")
    private String insuranceCompany;

    /**
     * 产品上架信息表唯一业务ID
     */
    @Schema(description = "产品计划: 产品上架信息表唯一业务ID")
    private String productLaunchBizId;

    /**
     * 对账公司
     */
    @Schema(description = "对账公司")
    private String reconciliationCompany;

    /**
     * 对账公司业务ID
     */
    @Schema(description = "对账公司业务ID")
    private String reconciliationCompanyBizId;

    /**
     * 佣金期数(1=第一年; 2=第二年; 3=第三年; 4=第四年; 5=第五年)
     */
    @Schema(description = "佣金期数(1=第一年; 2=第二年; 3=第三年; 4=第四年; 5=第五年)")
    private Integer commissionPeriod;

    /**
     * 总来佣期数
     */
    @Schema(description = "总来佣期数")
    private Integer totalPeriod;

    /**
     * 来佣名称(1:销售拥金;2:销售奖金;3:销售津贴;4:续期佣金;5:服务津贴;等等)
     */
    @Schema(description = "来佣名称(1:销售拥金;2:销售奖金;3:销售津贴;4:续期佣金;5:服务津贴;等等)")
    private String commissionName;

    /**
     * 来佣类型(1:销售拥金;2:销售奖金;3:销售津贴;4:续期佣金;5:服务津贴;等等)
     */
    @Schema(description = "来佣类型(1:销售拥金;2:销售奖金;3:销售津贴;4:续期佣金;5:服务津贴;等等)")
    private String commissionType;

    /**
     * 预计入账比例 (%)
     */
    @Schema(description = "预计入账比例 (%)")
    private BigDecimal commissionRatio;

    /**
     * 预计入账金额
     */
    @Schema(description = "预计入账金额 HKD（如果不关联保单，就没有预计入账比例，此时记录预计入账金额）")
    private BigDecimal amount;

    /**
     * 来佣币种
     */
    @Schema(description = "来佣币种")
    private String currency;

    /**
     * 预计来佣日期
     */
    @Schema(description = "预计来佣日期")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    private Date commissionDate;

    /**
     * 入账状态 0=待入账 1=完成入账 2=部分入账 3=已失效
     */
    @Schema(description = "入账状态 0=待入账 1=完成入账 2=部分入账 3=已失效 字典值: csf_expected_commission_status")
    private String status;

    /**
     * 设置入账状态-修改理由
     */
    @Schema(description = "设置入账状态-修改理由")
    private String statusDesc;

    /**
     * 预计入账金额（HKD）
     */
    @Schema(description = "预计入账金额（HKD）")
    private BigDecimal expectedAmount;

    /**
     * 结算汇率（估）
     */
    @Schema(description = "结算汇率（估）")
    private BigDecimal defaultExchangeRate;

    /**
     * 已入账来佣金额（HKD）
     */
    @Schema(description = "已入账来佣金额（HKD）")
    private BigDecimal paidAmount;

    /**
     * 已入账来佣比例 (%)
     */
    @Schema(description = "已入账来佣比例 (%)")
    private BigDecimal paidRatio;

    /**
     * 待入账金额
     */
    @Schema(description = "待入账金额")
    private BigDecimal pendingAmount;

    /**
     * 待入账比例
     */
    @Schema(description = "待入账比例")
    private BigDecimal pendingRatio;

    /**
     * 已入账来佣列表
     */
    @Schema(description = "已入账来佣列表")
    private List<Commission> commissionList;

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

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

    /**
     * 更新人ID
     */
    @Schema(description = "更新人ID")
    private String updaterId;

    /**
     * 创建时间
     */
    @Schema(description = "创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createTime;

    /**
     * 更新时间
     */
    @Schema(description = "更新时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date updateTime;

    private static final long serialVersionUID = 1L;

    /**
     * 对象转封装类
     *
     * @param commissionExpected
     * @return
     */
    public static CommissionExpectedVO objToVo(CommissionExpected commissionExpected) {
        if (commissionExpected == null) {
            return null;
        }
        CommissionExpectedVO commissionVO = new CommissionExpectedVO();
        BeanUtils.copyProperties(commissionExpected, commissionVO);

        BigDecimal paidAmount1 = ObjectUtils.defaultIfNull(commissionExpected.getPaidAmount(), BigDecimal.ZERO);
        BigDecimal paidRatio1 = ObjectUtils.defaultIfNull(commissionExpected.getPaidRatio(), BigDecimal.ZERO);

        // 待入账比例、待入账金额
        if (commissionExpected.getCommissionRatio() != null) {
            commissionVO.setPaidRatio(paidRatio1);
            commissionVO.setPendingRatio(commissionExpected.getCommissionRatio().subtract(paidRatio1));
            if (commissionExpected.getExpectedAmount() != null) {
                commissionVO.setPaidAmount(paidAmount1);
                commissionVO.setPendingAmount(commissionExpected.getExpectedAmount().subtract(paidAmount1));
            }
            return commissionVO;
        } else {
            commissionVO.setPaidAmount(paidAmount1);
            commissionVO.setPendingAmount(commissionExpected.getAmount().subtract(paidAmount1));

            return commissionVO;
        }
    }
}
