package com.jeequan.jeepay.db.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.model.openapi.biz.RefundResultResp;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;

/**
 * <p>
 * 退款订单表
 * </p>
 *
 * @author [mybatis plus generator]
 * @since 2021-04-27
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("t_refund_order")
public class RefundOrder extends com.jeequan.jeepay.core.entity.RefundOrder {

    public static final byte STATE_INIT = 0; //订单生成
    public static final byte STATE_ING = 1; //退款中
    public static final byte STATE_SUCCESS = 2; //退款成功
    public static final byte STATE_FAIL = 3; //退款失败
    public static final byte STATE_CLOSED = 4; //退款任务关闭

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

    private static final long serialVersionUID=1L;

    /**
     * 退款订单号（支付系统生成订单号）
     */
    @TableId
    private String refundOrderId;

    /**
     * 支付订单号（与t_pay_order对应）
     */
    private String payOrderId;

    /**
     * 渠道支付单号（与t_pay_order channel_order_no对应）
     */
    private String channelPayOrderNo;

    /**
     * 商户号（更改为用户号）
     */
    private String mchNo;

    /**
     * 商户号，也就是现在的申请单编号
     */
    private String mchExtNo;

    /**
     * 服务商号
     */
    private String isvNo;

    /**
     * 服务商号
     */
    private String agentNo;

    /**
     * 顶级服务商号
     */
    private String topAgentNo;

    /**
     * 商户门店ID
     */
    private String storeId;

    /**
     * 门店名称
     */
    private String storeName;

    /**
     * 应用ID
     */
    private String appId;

    /**
     * 商户名称
     */
    private String mchName;

    /**
     * 类型: 1-普通商户, 2-特约商户(服务商模式)
     */
    private Byte mchType;

    /**
     * 设备类型：qr_code-码牌，scan_pos-扫码POS，auto_pos-智能POS，other-其他
     */
    private String deviceType;

    /**
     * 设备号
     */
    private String deviceNo;

    /**
     * 设备厂商，参考商户门店设备表
     */
    private String deviceProvider;

    /**
     * 商户退款单号（商户系统的订单号）
     */
    private String mchRefundNo;

    /**
     * 支付方式代码
     */
    private String wayCode;

    /**
     * 支付方式类型
     */
    private String wayCodeType;

    /**
     * 支付接口代码
     */
    private String ifCode;

    /**
     * 支付金额,单位分
     */
    private Long payAmount;

    /**
     * 退款金额,单位分
     */
    private Long refundAmount;

    /**
     * 手续费退还金额, 单位：分
     */
    private Long refundFeeAmount;

    /**
     * 三位货币代码,人民币:cny
     */
    private String currency;

    /**
     * 退款状态:0-订单生成,1-退款中,2-退款成功,3-退款失败,4-退款任务关闭
     */
    private Byte state;

    /**
     * 客户端IP
     */
    private String clientIp;

    /**
     * 退款原因
     */
    private String refundReason;

    /**
     * 渠道订单号
     */
    private String channelOrderNo;

    /**
     * 渠道特殊业务数据，各渠道业务自处理数据
     */
    private String channelBizData;

    /**
     * 渠道错误码
     */
    private String errCode;

    /**
     * 渠道错误描述
     */
    private String errMsg;

    /**
     * 特定渠道发起时额外参数
     */
    private String channelExtra;

    /**
     * 通知地址
     */
    private String notifyUrl;

    /**
     * 扩展参数 暂时扩展为退款模式 “1”收款商户号退款,"2"平台一般户退款
     */
    private String extParam;

    /**
     * 门店收银员ID
     */
    private Long storeUserId;

    /**
     * 订单退款成功时间
     */
    private Date successTime;

    /**
     * 退款失效时间（失效后系统更改为退款任务关闭状态）
     */
    private Date expiredTime;

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

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

    /**
     * 退款类型 1：全额退款 2：部分退款
     */
    private Byte refundType;

    /**
     * 绑定的应用名称
     */
    @TableField(exist = false)
    private String appName;

    /**
     * 渠道名称
     */
    @TableField(exist = false)
    private String isvName;

    /**
     * 渠道简称
     */
    @TableField(exist = false)
    private String isvShortName;

    /**
     * 服务商名称
     */
    @TableField(exist = false)
    private String agentName;

    @TableField(exist = false)
    private String agentContactTel;

    @TableField(exist = false)
    private String mchUserName;

    @TableField(exist = false)
    private String mchUserPhone;

    public RefundResultResp bulidResult() {
        RefundResultResp resp = new RefundResultResp();
        resp.setRefundOrderId(this.getRefundOrderId());
        resp.setRefundReason(this.getRefundReason());
        resp.setMchRefundNo(this.getMchRefundNo());
        resp.setRefundAmt(this.getRefundAmount());
        resp.setIfCode(this.getIfCode());
        resp.setOriPayOrderId(this.getPayOrderId());
        resp.setMercNo(this.getMchExtNo());
        CS.OpenRefundState val = CS.OpenRefundState.getVal(this.getState());
        resp.setState(val.getCode());
        String note = StringUtils.isNotEmpty(this.getErrMsg()) ? this.getErrMsg() : val.getDesc();
        resp.setNote(note);
        resp.setOriAmount(this.getPayAmount());
        resp.setMercNo(this.getMchExtNo());
        resp.setRefundType(this.getRefundType());
        resp.setPayType(this.getWayCodeType());
        resp.setRefundTime(this.getSuccessTime());
        resp.setExtParam(this.getExtParam());
//        resp.setOriMchOrderNo(mchOrderNo);
        return resp;
    }
}
