package cn.pluss.platform.ryx.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.jiguang.common.utils.StringUtils;
import cn.pluss.platform.channel.BaseMerchantAuditService;
import cn.pluss.platform.dto.AccountDTO;
import cn.pluss.platform.entity.*;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.mapper.BankCodeSxfMapper;
import cn.pluss.platform.mcc.MccReflectService;
import cn.pluss.platform.merchantImage.MerchantImageService;
import cn.pluss.platform.ryx.RyxConstants;
import cn.pluss.platform.ryx.RyxService;
import cn.pluss.platform.task.RyxMerAuditHandler;
import cn.pluss.platform.task.SxfMerAuditHandler;
import cn.pluss.platform.userApp.UserAppService;
import cn.pluss.platform.util.ByteArrayImg;
import cn.pluss.platform.util.FileUtil;
import cn.pluss.platform.util.ParametersUtil;
import cn.pluss.platform.util.RyxConfig;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;

/**
 * @author DJH
 * 瑞银信进件服务类
 */
@Slf4j
@Service("ryxMerchantAuditService")
public class RyxMerchantAuditServiceImpl extends BaseMerchantAuditService {

    @Autowired
    private MccReflectService mccReflectService;

    @Autowired
    private MerchantImageService miService;

    @Autowired
    private RyxService ryxService;

    @Autowired
    private RyxMerAuditHandler ryxMerAuditHandler;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired @Lazy
    private UserAppService userAppService;

    @Resource
    private BankCodeSxfMapper bankCodeSxfMapper;

    @Autowired @Lazy
    private SxfMerAuditHandler sxfMerAuditHandler;

    private static final List<String> NOT_ALLOWED_INCOMING_PROVINCE = Arrays.asList("西藏自治区", "新疆维吾尔自治区", "内蒙古自治区", "宁夏回族自治区", "安徽省", "上海市");
    private static final List<String> NOT_ALLOWED_INCOMING_CITY = Arrays.asList("大连市", "宁波市", "临沧市", "德宏州", "西双版纳傣族自治州", "保山市", "文山市");
    private static final List<String> NOT_ALLOWED_INCOMING_DISTRICT = Arrays.asList("余干县", "宾阳县");

    @Override
    public void imageUpload(MerchantImage merchantImage) {
        MsgException.throwException("瑞银行不能单独上传文件");
    }

    @Override
    public void imageUpload(String url, String picType) {
        MsgException.throwException("瑞银行不能单独上传文件");
    }

    @Override
    public void merchantAudit(String userId, boolean isFailCheck) {
        MerchantBaseInfo mbi = merchantBaseInfoService.getMerchantBaseInfoByUserId(userId);

//        checkAuditArea(mbi);

        // 进件参数拼接
        MerchantChannelStatus channelStatus = mcsService.getByMerchantCode(mbi.getMerchantCode(), 3);

        Map<String, Object> paramMap = getParams(mbi);
        if (StringUtils.isNotEmpty(channelStatus.getMerchantId())) {
            String merInId = channelStatus.getExtra().getString(RyxConstants.MER_IN_ID);
            if (!StringUtils.isEmpty(merInId)) {
                paramMap.put(RyxConstants.MER_IN_ID, merInId);
            }
        }

        log.info("{}瑞银信进件参数{}", userId, JSON.toJSONString(paramMap));
        RyxAccessResp resp = auditReq(mbi, paramMap);

        Map<String, Object> result = resp.getResult();

        String applicationId = ((String) result.get("orderId")).replace("-", "");

        channelStatus.setApplicationId(applicationId);
        channelStatus.setStatus(MerchantChannelStatus.AUDIT_STATUS_EXAMINING);
        channelStatus.setRemark("瑞银信审核中");
        channelStatus.setMerchantCode(mbi.getMerchantCode());
        channelStatus.setChannel(3);
        channelStatus.setThirdStatus("");
        channelStatus.setCallbackStatus("0");
        mcsService.saveOrUpdate(channelStatus);

        QueryWrapper<MerchantAuditRecord> queryWrapper = new QueryWrapper<MerchantAuditRecord>().eq("applicationId", applicationId);
        MerchantAuditRecord maRecord = ObjectUtil.defaultIfNull(
                merchAuditRecService.getOne(queryWrapper),
                new MerchantAuditRecord());
        maRecord.setStatus("1");
        maRecord.setMerchantCode(mbi.getMerchantCode());
        maRecord.setChannel(3);
        maRecord.setThirdStatus("");
        maRecord.setMerchantId(applicationId);
        merchAuditRecService.saveOrUpdate(maRecord);
    }

    /**
     * @description:检查当前地区是否支持进件
     * @date: 2021/9/10 18:51
     */
    private void checkAuditArea(MerchantBaseInfo merchantBaseInfo) {
        String province = merchantBaseInfo.getProvince();
        String city = merchantBaseInfo.getCity();
        String district = merchantBaseInfo.getDistrict();
        String template = "瑞银信地区【%s】禁止进件";
        if (NOT_ALLOWED_INCOMING_PROVINCE.contains(province)) {
            MsgException.throwException(String.format(template, province));
        }
        if (NOT_ALLOWED_INCOMING_CITY.contains(city)) {
            MsgException.throwException(String.format(template, city));
        }
        if (NOT_ALLOWED_INCOMING_DISTRICT.contains(district)) {
            MsgException.throwException(String.format(template, district));
        }
    }

    /**
     * 进件请求
     *
     * @param merchantBaseInfo 商户基本信息
     * @return
     */
    public RyxAccessResp auditReq(MerchantBaseInfo merchantBaseInfo, Map<String, Object> paramMap) {
        List<ByteArrayResource> files = getFiles(merchantBaseInfo.getUserId(), merchantBaseInfo.getMerchantType(), "1".equals(paramMap.get(RyxConstants.PACT_TYPE)));
        return ryxService.reqCommon(paramMap, files, RyxConfig.merchantIncome);
    }

    /**
     * 商服宝进件请求
     *
     * @param merchantBaseInfo 商户基本信息
     * @return
     */
    public RyxAccessResp subAuditReq(MerchantBaseInfo merchantBaseInfo, Map<String, Object> paramMap, MerchantPlugin merchantPlugin) {
        // 修改机构号为商服宝的机构号
        paramMap.put(RyxConstants.ORG_ID, "OX0000000002984");
        // 修改商户id为插件的商户id
        paramMap.put(RyxConstants.ACCESS_ID, RyxConfig.subAccessId);

        if (merchantPlugin.getExtra() != null) {
            String merInId = merchantPlugin.getExtra().getString(RyxConstants.MER_IN_ID);
            if (merInId != null) {
                paramMap.put(RyxConstants.MER_IN_ID, merInId);
            } else {
                paramMap.remove(RyxConstants.MER_IN_ID);
            }
        } else {
            paramMap.remove(RyxConstants.MER_IN_ID);
        }

        List<ByteArrayResource> files = getFiles(merchantBaseInfo.getUserId(), merchantBaseInfo.getMerchantType(), "1".equals(paramMap.get(RyxConstants.PACT_TYPE)));
        RyxAccessModel ryxAccessModel = ryxService.accessCombineEncrypt(paramMap, files, RyxConfig.subAccessId, RyxConfig.subAccessPubKey);

        MultiValueMap<String, Object> map = RyxAccessModel.toMap(ryxAccessModel);
        HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(map);

        RyxAccessModel ryxResult = restTemplate.postForObject(RyxConfig.merchantIncome, request, RyxAccessModel.class);
        MsgException.checkNull(ryxResult, "未知请求结果");
        return ryxService.accessResultDecrypt(ryxResult, RyxAccessResp.class);
    }

    @Override
    public void editMerchantAudit(String userId) {
        MerchantBaseInfo merchantBaseInfo = merchantBaseInfoService.getMerchantBaseInfoByUserId(userId);
        // 进件参数拼接
        Map<String, Object> paramMap = new HashMap<>();

        MerchantChannelStatus mcs = mcsService.getByMerchantCode(merchantBaseInfo.getMerchantCode(), 3);
        MsgException.checkUnequals(3, mcs.getChannel(), "修改通道选择错误");

        paramMap.put(RyxConstants.MER_IN_ID, mcs.getExtra().getString(RyxConstants.MER_IN_ID));

        IdCard idCard = idCardService.getAccountIdCard(userId, Account.CHANNEL_TYPE_D1);
        Account account = new Account().setUserId(userId).setChannelType(Account.CHANNEL_TYPE_D1);
        account = accountService.getOne(new QueryWrapper<>(account));
        JSONObject bak = account.getBak();

        paramMap.put(RyxConstants.BANK_CARD, bak.get("bankCardNo"));
        paramMap.put(RyxConstants.BANK_CARD_NAME, bak.get("bankHolder"));
        paramMap.put(RyxConstants.SERVICE_TYPE, "04");
        // 小微的结算方式只有非法人
        paramMap.put(RyxConstants.SETTLE_TYPE, "2");
        paramMap.put(RyxConstants.SETTLE_CRED_TYPE, "0105");
        paramMap.put(RyxConstants.SETTLE_CRED_NO, idCard.getCertNo());

        String branchProvince = (String) bak.get("branchProvince");
        String branchCity = (String) bak.get("branchCity");
        String branchArea = (String) bak.get("branchArea");

        RegionCodeRyx regionCodeRyx = regionReflectService.getRyxRegionCode(branchProvince, branchCity, branchArea);
        paramMap.put(RyxConstants.BANK_CITY_AREA, regionCodeRyx.getAreaCode());

        QueryWrapper<BankCodeSxf> queryWrapper = new QueryWrapper<BankCodeSxf>().eq("cnaps_code", bak.get("contactLine"));
        BankCodeSxf bankCodeSxf = bankCodeSxfMapper.selectOne(queryWrapper);
        paramMap.put(RyxConstants.HEAD_BANK_NO, bankCodeSxf.getBankCode());
        paramMap.put(RyxConstants.OPEN_BANK_CHILD, bak.get("contactLine"));

        byte[] bytes = new byte[0];
        try {
            bytes = FileUtil.url2Bytes(bak.getString("imgUrl"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        ByteArrayResource pic = new ByteArrayResource(bytes) {
            @Override
            public String getFilename() {
                return "pic.png";
            }
        };

        log.info("瑞银信请求参数: {}", JSON.toJSONString(paramMap));
        RyxAccessModel ryxAccessModel = ryxService.accessCombineEncrypt(paramMap, null, RyxConfig.accessId, RyxConfig.accessPubKey);
        MultiValueMap<String, Object> params = RyxAccessModel.toMap(ryxAccessModel);
        params.put(RyxConstants.CARD_PIC, Collections.singletonList(pic));
        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(params);
        RyxAccessModel respModel = restTemplate.postForObject(RyxConfig.updateSettleCard, httpEntity, RyxAccessModel.class);
        RyxAccessResp result = ryxService.accessResultDecrypt(respModel, RyxAccessResp.class);
        if (RyxAccessResp.RESULT_ERROR.equals(result.getCode())) {
            String errMsg = result.getMsg();
            log.error(errMsg);
            MsgException.throwException(errMsg);
        } else {
            log.info(result.getMsg());

            // 修改商户状态
            mcs.setStatus(MerchantChannelStatus.AUDIT_STATUS_DATA_EDIT);
            mcsService.updateById(mcs);
        }
    }

    @Override
    public JSONObject merchantAuditResult(Map<String, Object> result) throws Exception {
        RyxAccessResp ryxAccessResp = ryxService.reqCommon(result, null, RyxConfig.auditResult);
        return ryxAccessResp.getResult();
    }

    @Override
    public JSONObject merchantAuditCallback(JSONObject auditResult) throws Exception {
        // 异步处理回调处理，减少幂等处理次数
        try {
            ryxMerAuditHandler.auditHandler(auditResult);
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
        }

        return RyxConfig.ok();
    }

    @Override
    public JSONObject editMerchantCallback(JSONObject auditResult) throws Exception {
        // 瑞银信修改结算账户是直接返回结果，暂不需要做回调操作
        return new JSONObject();
    }

    @Override
    public String imgTypeTransform(String imageType) {
        switch (imageType) {
            case MerchantImage.ImageType.IDCARD_FRONT:
                return ImageType.IDCARD_FRONT;
            case MerchantImage.ImageType.IDCARD_BACK:
                return ImageType.IDCARD_BACK;
            case MerchantImage.ImageType.SETTLE_IDCARD_FRONT:
                return ImageType.SETTLE_IDCARD_FRONT;
            case MerchantImage.ImageType.SETTLE_IDCARD_BACK:
                return ImageType.SETTLE_IDCARD_BACK;
            case MerchantImage.ImageType.BANK_CARD_FRONT:
                return ImageType.BANK_CARD_FRONT;
            case MerchantImage.ImageType.BUSINESS_LICENSE:
                return ImageType.BUSINESS_LICENSE;
            case MerchantImage.ImageType.ACCOUNT_PERMITS:
                return ImageType.ACCOUNT_PERMITS;
            case MerchantImage.ImageType.NON_LEG_SETTLE_AUTH:
                return ImageType.NON_LEG_SETTLE_AUTH;
            case MerchantImage.ImageType.SHOP_FRONT_DOOR:
                return ImageType.SHOP_FRONT_DOOR;
            case MerchantImage.ImageType.CASH_DESK_PHOTO:
                return ImageType.CASH_DESK_PHOTO;
            case MerchantImage.ImageType.STORE_INTERIOR_PHOTO:
                return ImageType.STORE_INTERIOR_PHOTO;
            case MerchantImage.ImageType.MERCHANT_PROTOCOL:
                return ImageType.MERCHANT_PROTOCOL;
            case MerchantImage.ImageType.MERCHANT_SUB_PROTOCOL:
                return ImageType.MERCHANT_SUB_PROTOCOL;
            case MerchantImage.ImageType.HAND_IDCARD_PHOTO:
                return ImageType.HAND_IDCARD_PHOTO;
            default:
                return ImageType.OTHER;
        }
    }

    @Override
    public void checkSign(JSONObject receiveData) throws Exception {
        MsgException.throwException("校验方法请使用RyxService中的方法");
    }

    /**
     * 组装进件参数
     *
     * @return 进件参数
     */
    public Map<String, Object> getParams(MerchantBaseInfo merchantBaseInfo) {
        Map<String, Object> param = new HashMap<>(32);

        MerchantChannelStatus mcs = mcsService.getByMerchantCode(merchantBaseInfo.getMerchantCode(), 3);

        param.put(RyxConstants.MER_PROP, getMerchantType(merchantBaseInfo.getMerchantType()));
        param.put(RyxConstants.ACCESS_MODE, "1");

        AccountDTO account = accountService.getAccount(merchantBaseInfo.getUserId());

        if (MerchantBaseInfo.MERCH_TYPE_MICRO.equals(merchantBaseInfo.getMerchantType())) {
            param.put(RyxConstants.ORG_MER_NAME, "商户_" + account.getIdcard().getCertName());
            param.put(RyxConstants.MER_NAME, "商户_" + account.getIdcard().getCertName());
        } else {
            param.put(RyxConstants.ORG_MER_NAME, merchantBaseInfo.getBussAuthName());
            param.put(RyxConstants.MER_NAME, merchantBaseInfo.getBussAuthName());
        }

        param.put(RyxConstants.MER_NAME_ABBR, merchantBaseInfo.getAlias());

        RegionCodeRyx regionCodeRyx = regionReflectService.getRyxRegionCode(merchantBaseInfo.getProvince(), merchantBaseInfo.getCity(), merchantBaseInfo.getDistrict());
        param.put(RyxConstants.CITY_AREA, regionCodeRyx.getAreaCode());

        BankCard certBankCard = new BankCard();
        certBankCard.setUserId(merchantBaseInfo.getUserId());
        certBankCard.setAccountType(BankCard.ACCOUNT_TYPE_CERT);

        IdCard leaderIdCard = idCardService.getLegalIdCard(merchantBaseInfo.getUserId());

        if (MerchantBaseInfo.MERCH_TYPE_MICRO.equals(merchantBaseInfo.getMerchantType())) {
            leaderIdCard = account.getIdcard();
        } else {
            // 非小微商户需要的信息
            param.put(RyxConstants.LIC_TYPE, "2389");
            param.put(RyxConstants.LIC_NUM, merchantBaseInfo.getBussAuthNum());
            param.put(RyxConstants.LIC_NUM_START_TIME, merchantBaseInfo.getBussAuthStartTime().replace("-", ""));
            param.put(RyxConstants.LIC_NUM_TIME, merchantBaseInfo.getBussAuthEndTime().replace("-", ""));
            param.put(RyxConstants.CRED_PEOPLE, leaderIdCard.getCertName());
            param.put(RyxConstants.CRED_TYPE, "0105");
            param.put(RyxConstants.CRED_NO, leaderIdCard.getCertNo());
            param.put(RyxConstants.CRED_START_TIME, leaderIdCard.getCertStartTime().replace("-", ""));

            if (!"长期".equals(leaderIdCard.getCertEndTime())) {
                param.put(RyxConstants.CRED_TIME, leaderIdCard.getCertEndTime().replace("-", ""));
            } else {
                param.put(RyxConstants.CRED_TIME, "");
            }

            param.put(RyxConstants.MANA_SCOP, merchantBaseInfo.getMccName());
        }

        // 使用实名信息作为商户实名认证的材料
        param.put(RyxConstants.CONTACT_PERSON, leaderIdCard.getCertName());
        param.put(RyxConstants.CONTACT_PHONE, merchantBaseInfo.getContactMobile());
        // 电子协议短信接收人及手机号
        param.put(RyxConstants.AGREEMENT_NAME, leaderIdCard.getCertName());
        param.put(RyxConstants.AGREEMENT_PHONE, merchantBaseInfo.getContactMobile());

        param.put(RyxConstants.SUB_APP_ID, ParametersUtil.APPLETS_APPID);

        IdCard idcard = account.getIdcard();
        BankCard bankCard = account.getBankCard();

        param.put(RyxConstants.BANK_CARD, bankCard.getBankCardNo());
        String accountProp = bankCard.getAccountType().equals(BankCard.ACCOUNT_TYPE_PRIVATE) ? "01" : "00";
        param.put(RyxConstants.ACCOUNT_PROP, accountProp);

        String settleType = getSettleType(account.getSettleType());

        param.put(RyxConstants.SETTLE_TYPE, settleType);
        param.put(RyxConstants.BANK_CARD_NAME, bankCard.getBankHolder());
        param.put(RyxConstants.SETTLE_CRED_TYPE, "0105");
        param.put(RyxConstants.SETTLE_CRED_NO, idcard.getCertNo());
        param.put(RyxConstants.SETTLE_CRED_START_TIME, idcard.getCertStartTime().replace("-", ""));
        if (!"长期".equals(idcard.getCertEndTime())) {
            param.put(RyxConstants.SETTLE_CRED_TIME, idcard.getCertEndTime().replace("-", ""));
        } else {
            param.put(RyxConstants.SETTLE_CRED_TIME, "");
        }

        MccReflect ryxMccReflect = mccReflectService.getRyxMccReflect(merchantBaseInfo.getMcc());
        param.put(RyxConstants.MCC, ryxMccReflect.getMccCode());

        String address = String.format("%s-%s-%s-%s", merchantBaseInfo.getProvince(), merchantBaseInfo.getCity(), merchantBaseInfo.getDistrict(), merchantBaseInfo.getAddress());
        param.put(RyxConstants.OPERATE_ADDR, address);
        param.put(RyxConstants.MODEL_TYPE, "2");

        // ----- 设置费率信息
        param.put(RyxConstants.CREDIT_RATE, "0.615");
        param.put(RyxConstants.DEBIT_RATE, "0.615");
        param.put(RyxConstants.DEBIT_TOP, "20");
        param.put(RyxConstants.QR_DEBIT_RATE, "0.40");
        param.put(RyxConstants.QR_CREDIT_RATE, "0.40");
        param.put(RyxConstants.WX_DEBIT_RATE, "0.38");
        param.put(RyxConstants.ZFB_DEBIT_RATE, "0.38");
        // ----- 设置费率信息结束
        regionCodeRyx = regionReflectService.getRyxRegionCode(bankCard.getBranchProvince(), bankCard.getBranchCity(), bankCard.getBranchArea());

        param.put(RyxConstants.BANK_PROVINCE, regionCodeRyx.getProvinceCode());
        param.put(RyxConstants.BANK_CITY, regionCodeRyx.getCityCode());
        param.put(RyxConstants.BANK_CITY_AREA, regionCodeRyx.getAreaCode());
        param.put(RyxConstants.OPEN_BANK_CHILD, bankCard.getContactLine());

        QueryWrapper<BankCodeSxf> queryWrapper = new QueryWrapper<BankCodeSxf>().eq("cnaps_code", bankCard.getContactLine());
        BankCodeSxf bankCodeSxf = bankCodeSxfMapper.selectOne(queryWrapper);
        param.put(RyxConstants.HEAD_BANK_NO, bankCodeSxf.getBankCode());
        param.put(RyxConstants.TERM_NUM, "1");
        param.put(RyxConstants.SERVICE_TYPE, "04");
        param.put(RyxConstants.TRADE_TYPE_REIMBURSE, "0");
        param.put(RyxConstants.ORG_ID, "OX0000000002983");
        // 目前只能用纸质协议
        param.put(RyxConstants.PACT_TYPE, "1");
        param.put(RyxConstants.CERTIFICATION_TYPE, "1");
        param.put(RyxConstants.CONTACT_PERSON_CRED, leaderIdCard.getCertNo());

        return param;
    }

    /**
     * 获取文件列表
     *
     * @param userId       用户id
     * @param merchantType 商户类型
     * @return 文件列表
     */
    private List<ByteArrayResource> getFiles(String userId, String merchantType, boolean isOnlineProtocol) {
        ArrayList<ByteArrayResource> result = new ArrayList<>();

        List<MerchantImage> merchantImages = miService.getListOfMerch(userId);

        for (MerchantImage image : merchantImages) {
            if (image.getPhotoType().equals(MerchantImage.ImageType.BUSINESS_LICENSE) && MerchantBaseInfo.MERCH_TYPE_MICRO.equals(merchantType)) {
                // 小微商户不能上传营业执照
                continue;
            }

            // 获取瑞银信对应的图片类型code
            String imgTypeCode = imgTypeTransform(image.getPhotoType());
            String imgUrl = image.getPicUrl();

            if (MerchantBaseInfo.MERCH_TYPE_MICRO.equals(merchantType) && ImageType.STORE_INTERIOR_PHOTO.equals(imgTypeCode)) {
                // 将营业场景一同样传到营业场景二中
                addFile(result, imgUrl, "pic" + ImageType.STORE_INTERIOR_PHOTO2 + ".png");
            }

            addFile(result, imgUrl, "pic" + imgTypeCode + ".png");
        }

        if (!isOnlineProtocol) {
            String imgUrl = "https://www.shouyinbei.net/resources/images/upload/blank.png";
            // 弄一个空白图片作为商户协议上传
            addFile(result, imgUrl, "pic" + ImageType.MERCHANT_SUB_PROTOCOL + ".png");
            addFile(result, imgUrl, "pic" + ImageType.MERCHANT_PROTOCOL + ".png");
        }

        return result;
    }

    /**
     * 读取图片内容并添加到列表中
     *
     * @param imgBytesList 图片列表
     * @param url          图片url
     * @param picName      图片名称
     */
    private void addFile(ArrayList<ByteArrayResource> imgBytesList, String url, String picName) {
        try {
            byte[] bytes = FileUtil.url2Bytes(url);
            ByteArrayResource byteArrayResource = new ByteArrayImg(bytes, picName);

            imgBytesList.add(byteArrayResource);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 商户类型转换，将自身的商户类型转换为瑞银信的商户类型
     *
     * @param merchantType 商户类型
     * @return 随行付商户类型
     */
    private String getMerchantType(String merchantType) {
        switch (merchantType) {
            case "1":
                return "7";
            case "2":
                return "1";
            case "3":
                return "2";
            default:
                MsgException.throwException("未知的商户类型");
        }

        return merchantType;
    }

    /**
     * 根据账户结算方式获取结算方式
     *
     * @param accountType 账户结算方式
     * @return 瑞银信结算方式
     */
    private String getSettleType(String accountType) {
        switch (accountType) {
            // 对私法人
            case Account.SETTLE_TYPE_MICRO_STORE:
            case Account.SETTLE_TYPE_PRIVATE_LEGAL:
            case Account.LEGAL_PRIVATE_LEGAL:
            case Account.ILLEGAL_PRIVATE_LEGAL:
                accountType = "1";
                break;
            // 对私非法人
            case Account.SETTLE_TYPE_PRIVATE_ILLEGAL:
            case Account.LEGAL_PRIVATE_ILLEGAL:
            case Account.ILLEGAL_PRIVATE_ILLEGAL:
                accountType = "2";
                break;
            case Account.SETTLE_TYPE_CORPORATE:
                accountType = "3";
                break;
            default:
                MsgException.throwException("未知的结算方式");
        }

        return accountType;
    }

    public static class ImageType {
        /**
         * 商户协议
         */
        public static final String MERCHANT_PROTOCOL = "0101";
        /**
         * 商户附属协议
         */
        public static final String MERCHANT_SUB_PROTOCOL = "0130";
        /**
         * 手持身份证，需要与结算人身份信息一致，非法人结算与小微商户需要
         */
        public static final String HAND_IDCARD_PHOTO = "0105";
        /**
         * 入账人/法人身份证正面
         */
        public static final String IDCARD_FRONT = "0111";
        /**
         * 入账人/法人身份证反面
         */
        public static final String IDCARD_BACK = "0112";
        /**
         * 银行卡卡号面
         */
        public static final String BANK_CARD_FRONT = "0206";
        /**
         * 门头照
         */
        public static final String SHOP_FRONT_DOOR = "0211";
        /**
         * 门店内景/营业场景图片一
         */
        public static final String STORE_INTERIOR_PHOTO = "0117";
        /**
         * 营业执照
         */
        public static final String BUSINESS_LICENSE = "0103";
        /**
         * 开户许可证
         */
        public static final String ACCOUNT_PERMITS = "0109";
        /**
         * 门店内景二/营业场景图片二
         */
        public static final String STORE_INTERIOR_PHOTO2 = "0118";
        /**
         * 结算人身份证正面
         */
        public static final String SETTLE_IDCARD_FRONT = "0119";
        /**
         * 结算人身份证反面
         */
        public static final String SETTLE_IDCARD_BACK = "0120";
        /**
         * 收银台照片
         */
        public static final String CASH_DESK_PHOTO = "0217";
        /**
         * 非法人授权函
         */
        public static final String NON_LEG_SETTLE_AUTH = "0205";
        /**
         * 其他
         */
        public static final String OTHER = "0401";

        private ImageType() {
        }
    }
}