package com.jeequan.jeepay.db.entity;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler;
import com.jeequan.jeepay.core.entity.AccountOperate;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.model.BaseModel;
import lombok.*;
import lombok.experimental.Accessors;

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

/**
 * <p>
 *
 * </p>
 * 账户资金明细
 * @author [mybatis plus generator]
 * @since 2024-03-28
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName(value = "t_account_fund_info",autoResultMap = true)
@NoArgsConstructor
@AllArgsConstructor
public class AccountFundInfo extends BaseModel {

    /**
     * 打款类型 手动
     */
    public static final byte MANUAL = 0;

    /**
     * 打款类型 灵工
     */
    public static final byte FLEXIBLE = 1;

    private static final long serialVersionUID=1L;

    public static LambdaQueryWrapper<AccountFundInfo> gw(AccountFundInfo info) {
        return new LambdaQueryWrapper<>(info);
    }

    public static LambdaQueryWrapper<AccountFundInfo> gw() {
        return new LambdaQueryWrapper<>();
    }

    /**
     * 状态 0：订单生成 -1：失败 1：审核中 2：打款中 3：成功
     */

    public static final  byte INIT = 0;

    public static final  byte FAIL = -1;

    public static final  byte AUDIT = 1;

    public static final  byte PAYMENT = 2;

    public static final  byte SUCCESS = 3;

    /**
     *  付款方式 WECHAT：微信  ALIPAY：支付宝   BANK 银行卡
     */
    public static final String WECHAT = "WECHAT";

    public static final String ALIPAY = "ALIPAY";

    public static final String BANK = "BANK";

    /**
     * 订单号
     */
    @TableId(type = IdType.INPUT)
    @TableField(whereStrategy = FieldStrategy.NOT_EMPTY)
    private String orderId;

    /**
     * 账户编号
     */
    @TableField(whereStrategy = FieldStrategy.NOT_EMPTY)
    private String accountNo;

    /**
     * 渠道流水号
     */
    @TableField(whereStrategy = FieldStrategy.NOT_EMPTY)
    private String channelOrderId;

    /**
     * 用户号
     */
    @TableField(whereStrategy = FieldStrategy.NOT_EMPTY)
    private String mchNo;

    /**
     * 账户类型 0：退款账户 1：营销账户 2：虚拟银行 3：钱包 取值 ACCOUNT_INFO 的枚举类
     */
    @TableField(whereStrategy = FieldStrategy.NOT_NULL)
    private Byte accountType;

    /**
     * 变动类型 0：充值 1：提现 2：转账 3：退款
     */
    @TableField(whereStrategy = FieldStrategy.NOT_NULL)
    private Byte changeType;

    /**
     * 变动方式 0：扫码充值 1：对公转账 2：账户提现 3:平台户退款 -1:提现驳回
     */
    @TableField(whereStrategy = FieldStrategy.NOT_NULL)
    private Byte changeMethod;

    /**
     * 金额
     */
    private Long amount;

    /**
     * 实际金额
     */
    private Long finalAmount;

    /**
     * 手续费
     */
    private Long fee;

    /**
     * 状态 0：订单生成 -1：失败 1：审核中 2：打款中 3：成功
     */
    @TableField(whereStrategy = FieldStrategy.NOT_NULL)
    private Byte state;

    /**
     * 渠道
     */
    @TableField(whereStrategy = FieldStrategy.NOT_EMPTY)
    private String ifCode;

    /**
     * 备注
     */
    private String remark;

    /**
     * 创建时间
     */
    private Date createdAt;

    /**
     * 更新时间
     */
    private Date updatedAt;

    /**
     * 扩展参数
     */
    @TableField(typeHandler = FastjsonTypeHandler.class)
    private JSONObject extra;

    /**
     * 渠道返回参数
     */
    @TableField(typeHandler = FastjsonTypeHandler.class)
    private JSONObject channelExtra;

    /**
     * 付款方式 WECHAT：微信  ALIPAY：支付宝   BANK 银行卡
     */
    private String payMethod;

    /**
     * 收款商户号
     */
    private String mchExtNo;

    /**
     * 变动金额
     */
    @TableField(exist = false)
    private Long changeAmt;

    /**
     * 0:手动打款 1：灵工打款
     */
    @TableField(exist = false)
    private Byte mark;

    @TableField(exist = false)
    private String accountTypeDesc;

    @TableField(exist = false)
    private String changeTypeDesc;

    @TableField(exist = false)
    private String changeMethodDesc;

    @TableField(exist = false)
    private String stateDesc;

    @Getter
    @AllArgsConstructor
    public enum ChangeType{

        RECHARGE((byte)0,"充值"),

        CASH((byte)1,"提现"),

        TRANSFER((byte)2,"转账"),

        REFUND((byte)3,"退款"),

        DEDUCT((byte)4,"抵扣")
        ;

        private final Byte value;

        private final String desc;

        public static ChangeType getVal(byte value){
            ChangeType[] values = values();
            for (ChangeType val:values) {
                if(val.getValue() == value){
                    return val;
                }
            }
            throw new BizException("充值类型非法");
        }
    }

    @Getter
    @AllArgsConstructor
    public enum ChangeMethod{

        FAIL((byte)-1,"失败"),

        SCAN((byte)0,"扫码充值"),

        PUBIC((byte)1,"对公转账"),

        ACCOUNT_CASH((byte)2,"账户提现"),

        YBH_REFUND((byte)3,"一般户退款"),

        ZFT((byte)4,"直付通"),

        DF((byte)5,"代发");

        private final Byte value;

        private final String desc;

        public static ChangeMethod getVal(byte value){
            ChangeMethod[] values = values();
            for (ChangeMethod val:values) {
                if(val.getValue() == value){
                    return val;
                }
            }
            throw new BizException("变动类型非法");
        }
    }

    /**
     * 构建账户资金明细
     * @param operate
     */
    public AccountFundInfo(AccountOperate operate,AccountInfo account,String payMethod) {
        this.orderId = operate.getOrderId();
        this.accountNo = operate.getAccountNo();
        this.mchNo = account.getInfoId();
        this.accountType = account.getType();
        this.changeType = operate.getChangeType();
        this.changeMethod = operate.getChangeMethod();
        this.amount = operate.getAmount();
        this.finalAmount = operate.getFinalAmt();
        this.fee = BigDecimal.valueOf(operate.getAmount()).subtract(BigDecimal.valueOf(operate.getFinalAmt())).longValue();
        if(operate.getState() != null){
            this.state = operate.getState();
        }else{
            if(operate.getIsUpdateAccount()){
                this.state = SUCCESS;
            }else{
                this.state = INIT;
            }
        }
        this.ifCode = operate.getIfCode();
        this.remark = operate.getRemark();
        this.mchExtNo = operate.getMchExtNo();
        this.payMethod = payMethod;
        this.changeAmt = operate.getChangeAmt();
        this.extra = operate.getExtra();
    }

    public AccountFundInfo(AccountOperate operate,AccountInfo account) {
        this(operate,account,BANK);
    }


    public String getAccountTypeDesc() {
        if(this.getAccountType() == null){
            return "";
        }
        return AccountInfo.Type.getVal(this.getAccountType()).getDesc();
    }

    public String getChangeTypeDesc() {
        if(this.getChangeType() == null){
            return "";
        }
        return ChangeType.getVal(this.getChangeType()).getDesc();
    }

    public String getChangeMethodDesc() {
        if(this.getChangeMethod() == null){
            return "";
        }
        return ChangeMethod.getVal(this.getChangeMethod()).getDesc();
    }

    /**
     *  0：待处理 -1：失败 1：审核中 2：打款中 3：成功
     * @return
     */
    public String getStateDesc() {
        if(this.getState() == null){
            return "";
        }
        if(this.getState() == FAIL){
            return "失败";
        }
        if(this.getState() == 0){
            return "初始化";
        }
        if(this.getState() == AUDIT){
            return "审核中";
        }

        if(this.getState() == PAYMENT){
            return "打款中";
        }

        if(this.getState() == SUCCESS){
            return "成功";
        }

        return "未知";
    }
}



