package com.cool.store.request.close.refund;

import com.cool.store.annotation.DictField;
import com.cool.store.dto.close.CloseStoreMkUserDTO;
import com.cool.store.entity.StoreDO;
import com.cool.store.enums.master.StoreCloseNatureEnum;
import com.cool.store.enums.master.StoreCloseReasonEnum;
import com.cool.store.request.xgj.XgjReimburseFeeRequest;
import com.cool.store.request.xgj.XgjReimburseRequest;
import com.cool.store.utils.CoolDateUtils;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 闭店退款新增Request
 * </p>
 *
 * @author wangff
 * @since 2025/10/13
 */
@Data
public class CloseStoreRefundAddRequest {
    @ApiModelProperty("闭店申请id")
    @NotNull(message = "闭店申请id不能为空")
    private Long closeStoreId;

    @ApiModelProperty("退款类型")
    @NotNull(message = "退款类型不能为空")
    private Integer type;

    @ApiModelProperty("公司抬头，字典表close_store_invoice_title")
    @NotBlank(message = "公司抬头")
    private String invoiceTitle;

    @ApiModelProperty(value = "公司抬头", hidden = true)
    @DictField(sourceField = "invoiceTitle")
    private String legalEntity;

    @ApiModelProperty("提交人每刻工号")
    private String submitterId;

    @ApiModelProperty("提交人名称")
    private String submitterName;

    @ApiModelProperty("退款事由")
    @NotBlank(message = "退款事由")
    private String refundReason;

    @ApiModelProperty("退款类型，字典表编码close_store_refund_type")
    @NotBlank(message = "退款类型不能为空")
    private String refundType;

    @ApiModelProperty(value = "退款类型", hidden = true)
    @DictField(sourceField = "refundType")
    private String cf175;

    @ApiModelProperty("其他说明")
    private String remark;

    @ApiModelProperty("退款加盟商编码")
    @NotBlank(message = "退款加盟商编码不能为空")
    private String businessCode;

    @ApiModelProperty("退款加盟商名称")
    @NotBlank(message = "退款加盟商名称不能为空")
    private String businessName;

    @ApiModelProperty("退款加盟商收款账户")
    @NotBlank(message = "退款加盟商收款账户不能为空")
    private String businessAccount;

    @ApiModelProperty("退款项目，type=30固定为'加盟关店费用'，type=40固定为'加盟关店贷款退款'")
    @NotBlank(message = "退款项目不能为空")
    private String refundProject;

    @ApiModelProperty("费用申请日期")
    @NotNull(message = "费用申请日期不能为空")
    private Date expenseApplyTime;

    @ApiModelProperty("费用类型code，测试环境目前写死'04.006'")
    @NotBlank(message = "费用类型code不能为空")
    private String expenseTypeCode;

    @ApiModelProperty("应退金额")
    @NotNull(message = "应退金额不能为空")
    private BigDecimal expectedAmount;

    @ApiModelProperty("实退金额")
    @NotNull(message = "实退金额不能为空")
    private BigDecimal actualAmount;

    @ApiModelProperty("扣款金额")
    @NotNull(message = "扣款金额不能为空")
    private BigDecimal deductAmount;

    @ApiModelProperty("扣款原因")
    private String deductReason;

    @ApiModelProperty("银行账户")
    @NotBlank(message = "银行账户不能为空")
    private String accountNumber;

    @ApiModelProperty("开户行")
    @NotBlank(message = "开户行不能为空")
    private String bankName;

    @ApiModelProperty("联行号")
    @NotBlank(message = "联行号不能为空")
    private String bankBranchNo;

    @ApiModelProperty("往来单位性质")
    private List<String> partnerProperty;

    public XgjReimburseRequest convertToXgjRequest(CloseStoreMkUserDTO mkUserDTO, Integer closeReason, StoreDO storeDO) {
        StoreCloseReasonEnum closeReasonEnum = StoreCloseReasonEnum.getByCode(closeReason);
        return XgjReimburseRequest.builder()
                .legalEntity(legalEntity)
                .coverUser(mkUserDTO.getJobnumber())
                .reimburseName(refundReason)
                .cf175(cf175)
                .cf176(refundProject)
                .cf333(closeReasonEnum.getCloseNature().getDesc())
                .cf334(StoreCloseNatureEnum.REQUIRE_CLOSE.equals(closeReasonEnum.getCloseNature()) ? closeReasonEnum.getDesc() : "")
                .cf335(StoreCloseNatureEnum.ONESELF_CLOSE.equals(closeReasonEnum.getCloseNature()) ? closeReasonEnum.getDesc() : "")
                .cf177(storeDO.getStoreNum())
                .cf178(storeDO.getStoreName())
                .comments(remark)
                .tradingPartner(businessCode)
                .cf242("375|其他合法收入——资金退还")
                .cf243("")
                .bankAcctName(businessName)
                .bankAcctNumber(accountNumber)
                .bankName(bankName)
                .accountType("CORP")
                .bankBranchNo(bankBranchNo)
                .expenseList(Collections.singletonList(
                        XgjReimburseFeeRequest.builder()
                                .expenseTypeCode(expenseTypeCode)
                                .consumeAmount(actualAmount)
                                .refundAmount(expectedAmount)
                                .deductionAmount(deductAmount)
                                .consumeTime(CoolDateUtils.DateFormat(expenseApplyTime, "yyyy-MM-dd HH:mm:ss"))
                                .build()
                ))
                .build();
    }
}
