package cn.pluss.platform.entity;

import cn.pluss.platform.constants.Constant;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.util.MD5Util;
import cn.pluss.platform.util.SignUtils;
import cn.pluss.platform.util.StringUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serializable;
import java.util.Date;
import java.util.Objects;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class MerchantBaseInfo implements Serializable {

    /** 商户类型-小微 */
    public static final String MERCH_TYPE_MICRO = "1";

    /** 商户类型-个体户 */
    public static final String MERCH_TYPE_INDIVIDUAL = "2";

    /** 商户类型-企业 */
    public static final String MERCH_TYPE_COMPANY = "3";

    /** 不支持信用卡交易 **/
    public static final String LIMIT_PAY_TYPE_NO_CREDIT = "1";

    /** 不限制信用卡交易 **/
    public static final String LIMIT_PAY_TYPE_ALL = "0";

    /** 不分账 **/
    public static final Integer SPLIT_FLAG_NO = 1;

    /** 分账 **/
    public static final Integer SPLIT_FLAG_YES = 2;

    @TableId
    private Long id;
    private String bindingCode;
    private String userId;
    private String aliAccount;
    private String merchantCode;
    private String merchantName;
    private String merchantType;
    private String productDesc;
    private String aliMcc;
    private String mcc;
    private String mccName;
    private String alias;
    private String contactMobile;
    private String contactName;
    private String addressNo;
    private String province;
    private String city;
    private String district;
    private String address;
    private String email;
    /**
     * 营业执照法人信息
     */
    private String principalMobile;
    private String principalCertType;
    private String principalCertNo;
    private String principalPerson;
    /**
     * 营业执照信息
     */
    private String bussAuthName;
    private String bussAuthNum;
    private String bussAuthAddress;
    private String bussAuthStartTime;
    private String bussAuthEndTime;

    /**
     * 是否支持银联单笔1000以上的交易
     * 0：不支持；1支持
     */
    private String allowBankLarge;


    /**
     * 是否已经完成微信认证
     * 0：未完成；1：已完成
     */
    private String wxCertStatus;

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd hh:mm:ss")
    private Date createDt;
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd hh:mm:ss")
    private Date updateDt;

    private String buslicType;
    private String isVoice;

    /**
     * 是否推送微信消息0：不推送 1：推送
     */
    @TableField("isPushWxMessage")
    private String isPushWxMessage;

    /**
     * 是否开启银联支付0：不开启 1：开启
     */
    @TableField("isUnionPay")
    private String isUnionPay;

    /**
     * @description:Md5Key
     * @date: 2021/10/28 12:53
     */
    @TableField("md5Key")
    private String md5Key;

    /**
     * 是否限制信用卡0：不限制 1：限制
     * @date: 2022/1/5 10:09
     */
    @TableField("limitPay")
    private String limitPay;

    @TableField("subAppId")
    private String subAppId;

    @TableField("appid")
    private String appid;

    @TableField(exist = false)
    private String status;
    @TableField(exist = false)
    private String merchantAddress;
    //门头照
    @TableField(exist = false)
    private String storeHeadPic;
    //门店内景照
    @TableField(exist = false)
    private String storeInsidePic;
    //收银台照
    @TableField(exist = false)
    private String cashPic;
    //结算人与收银台合照
    @TableField(exist = false)
    private String literalPic;
    //非法人授权函
    @TableField(exist = false)
    private String warrantyPic;
    //商户协议
    @TableField(exist = false)
    private String dealPic;
    //商户附属协议
    @TableField(exist = false)
    private String affilatePic;
    //结算人手持身份证照
    @TableField(exist = false)
    private String handPic;

    @TableField(exist = false)
    private String merchantAuditStatus;

    @TableField(exist = false)
    private String startTime;
    @TableField(exist = false)
    private String endTime;
    @TableField(exist = false)
    private String imgUrl;
    @TableField(exist = false)
    private String loginName;
    @TableField(exist = false)
    private String password;

    /**
     * logo
     */
    @TableField(exist = false)
    private String logo;

    /**
     * 推广者的手机号
     */
    @TableField(exist = false)
    private String userPhone;

    /**
     * 推广者的名称
     */
    @TableField(exist = false)
    private String userName;

    /**
     * 营业执照图片地址
     */
    @TableField(exist = false)
    private String businessLicensePic;

    @TableField(exist = false)
    private String location;

    @TableField("firstTradeTime")
    private Date firstTradeTime;

    @TableField("lastTradeTime")
    private Date lastTradeTime;

    /**
     * 是否分账 1：不分账 2：分账
     */
    @TableField("splitFlag")
    private Integer splitFlag;

    /**
     * 有效商户标识 1：有效 -1 其他
     */
    @TableField("validFlag")
    private Integer validFlag;



    public static void setFirstAndLastTradeTime(MerchantBaseInfo mbi, Date date) {
        if (mbi.getFirstTradeTime() == null) {
            mbi.setFirstTradeTime(date);
        }

        mbi.setLastTradeTime(date);
    }

    public static void cpImgUrl(MerchantBaseInfo mbi, MerchantImage mi) {
        if (mi.getPhotoType().equals(MerchantImage.ImageType.SHOP_FRONT_DOOR)) {
            // 门头照
            mbi.setStoreHeadPic(mi.getPicUrl());
        }

        if (mi.getPhotoType().equals(MerchantImage.ImageType.CASH_DESK_PHOTO)) {
            // 收银台
            mbi.setCashPic(mi.getPicUrl());
        }

        if (mi.getPhotoType().equals(MerchantImage.ImageType.STORE_INTERIOR_PHOTO)) {
            // 门店内景
            mbi.setStoreInsidePic(mi.getPicUrl());
        }

        if (mi.getPhotoType().equals(MerchantImage.ImageType.CASH_DESK_WITH_PERSON)) {
            //商户负责人与收银台合照
            mbi.setLiteralPic(mi.getPicUrl());
        }

        if (mi.getPhotoType().equals(MerchantImage.ImageType.BUSINESS_LICENSE)) {
            // 营业执照
            mbi.setBusinessLicensePic(mi.getPicUrl());
        }

        if (mi.getPhotoType().equals(MerchantImage.ImageType.NON_LEG_SETTLE_AUTH)) {
            //非法人授权函
            mbi.setWarrantyPic(mi.getPicUrl());
        }

        if (mi.getPhotoType().equals(MerchantImage.ImageType.MERCHANT_PROTOCOL)) {
            //商户协议
            mbi.setDealPic(mi.getPicUrl());
        }

        if (mi.getPhotoType().equals(MerchantImage.ImageType.MERCHANT_SUB_PROTOCOL)) {
            //商户附属协议
            mbi.setAffilatePic(mi.getPicUrl());
        }

        if (mi.getPhotoType().equals(MerchantImage.ImageType.HAND_IDCARD_PHOTO)) {
            //结算人手持身份证照
            mbi.setHandPic(mi.getPicUrl());
        }
    }
    /**
     * 设置通道交易卡范围
     * @date: 2022/1/5 10:38
     * @param reqData:
     * @param channel:
     */
    public void setChannelLimitPay(JSONObject reqData, Integer channel) {
        switch (channel){
            case 1:
                //随行付
                if(LIMIT_PAY_TYPE_NO_CREDIT.equals(this.getLimitPay())){
                    //不支持信用卡支付
                    reqData.put("limitPay","01");
                }else{
                    reqData.put("limitPay","00");
                }
                break;
            case 3:
                //瑞银信
                reqData.put("limitPay",this.getLimitPay());
            case 4:
                reqData.put("limit_credit_pay",this.getLimitPay());
        }
    }

    /**
     * 校验签名
     * @param req
     */
    public void chekSign(Object req) {
        String md5Key = StringUtil.isEmpty(this.getMd5Key()) ? Constant.DEFAULT_MD5_KEY : this.getMd5Key();
        JSONObject reqData = JSON.parseObject(JSONObject.toJSONString(req));
        String sign = reqData.getString("sign");
        String checkSign = SignUtils.wapGetSign(reqData,md5Key);
        MsgException.check(!checkSign.equals(sign),"签名校验失败");
    }

    public String getPlatformSign(Object req) {
        String md5Key = StringUtil.isEmpty(this.getMd5Key()) ? Constant.DEFAULT_MD5_KEY : this.getMd5Key();
        JSONObject reqData = JSON.parseObject(JSONObject.toJSONString(req));
        return SignUtils.wapGetSign(reqData,md5Key);
    }
}

