package cn.pluss.platform.ryx.impl;

//import cn.hutool.core.util.ObjectUtil;
//import cn.pluss.platform.MerchantAuditRecordService;
//import cn.pluss.platform.channel.MerchantAuditService;
//import cn.pluss.platform.channel.PluginService;
//import cn.pluss.platform.entity.*;
//import cn.pluss.platform.exception.MsgException;
//import cn.pluss.platform.mapper.UserAppMapper;
//import cn.pluss.platform.merchant.MerchantPluginService;
//import cn.pluss.platform.merchant.MerchantBaseInfoService;
//import cn.pluss.platform.ryx.RyxConstants;
//import cn.pluss.platform.ryx.RyxService;
//import cn.pluss.platform.task.SxfMerAuditHandler;
//import cn.pluss.platform.util.RyxConfig;
//import cn.pluss.platform.wx.WxTalkService;
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.JSONObject;
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import lombok.Setter;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.springframework.util.MultiValueMap;
//import org.springframework.web.client.RestTemplate;
//
//import java.util.HashMap;
//import java.util.Map;
//
///**
// * 瑞银信商服宝插件进件操作类
// * @author DJH
// */
//@Slf4j
//@Service("ryxSfbPluginServiceImpl")
//public class RyxSfbPluginServiceImpl implements PluginService {
//
//    public static final int SFB_PLUGIN_CHANNEL = 31;
//
//    @Setter(onMethod_ = {@Autowired})
//    private MerchantPluginService merchantPluginService;
//    @Setter(onMethod_ = {@Autowired})
//    private MerchantBaseInfoService merchantBaseInfoService;
//    @Setter(onMethod_ = {@Autowired})
//    private MerchantAuditService ryxMerchantAuditService;
//    @Setter(onMethod_ = {@Autowired})
//    private MerchantAuditRecordService auditRecordService;
//    @Setter(onMethod_ = {@Autowired})
//    private RyxService ryxService;
//    @Setter(onMethod_ = {@Autowired})
//    private RestTemplate restTemplate;
//    @Setter(onMethod_ = {@Autowired})
//    private WxTalkService wxTalkService;
//
//    @Setter(onMethod_ = {@Autowired})
//    private UserAppMapper userAppMapper;
//
//    @Setter(onMethod_ = {@Autowired})
//    private SxfMerAuditHandler sxfMerAuditHandler;
//
//    @Override
//    public void audit(String userId) {
//        MerchantBaseInfo merchantBaseInfo = merchantBaseInfoService.getMerchantBaseInfoByUserId(userId);
//
//        MerchantPlugin merchantPlugin = merchantPluginService.getPluginInfo(userId);
//
//        if (merchantPlugin == null) {
//            merchantPlugin = new MerchantPlugin();
//        } else {
//            String status = merchantPlugin.getStatus();
//            String thirdStatus = merchantPlugin.getThirdStatus();
//
//            if (!MerchantChannelStatus.AUDIT_STATUS_EXAMINING.equals(status) || !MerchantChannelStatus.AUDIT_THIRD_STATUS_WAITING.equals(thirdStatus)) {
//                throw new MsgException("状态非法");
//            }
//        }
//
//        merchantPlugin.setUserId(Integer.parseInt(merchantBaseInfo.getUserId()));
//        merchantPlugin.setChannel(SFB_PLUGIN_CHANNEL);
//
//        // 进件参数拼接
//        Map<String, Object> paramMap = ((RyxMerchantAuditServiceImpl)ryxMerchantAuditService).getParams(merchantBaseInfo);
//        paramMap.put(RyxConstants.CREDIT_RATE, "0.6");
//        paramMap.put(RyxConstants.DEBIT_RATE, "0.6");
//        paramMap.put(RyxConstants.QR_DEBIT_RATE, "0.30");
//        paramMap.put(RyxConstants.QR_CREDIT_RATE, "0.30");
//        paramMap.put(RyxConstants.WX_DEBIT_RATE, "0.30");
//        paramMap.put(RyxConstants.ZFB_DEBIT_RATE, "0.30");
//
////        log.info("请求参数：{}", JSON.toJSONString(paramMap));
//        RyxAccessResp resp = ((RyxMerchantAuditServiceImpl) ryxMerchantAuditService).subAuditReq(merchantBaseInfo, paramMap, merchantPlugin);
//        log.info("请求参数：{}", JSON.toJSONString(paramMap));
//        Map<String, Object> result = resp.getResult();
//
//        String applicationId = ((String) result.get("orderId")).replace("-", "");
//        merchantPlugin.setApplicationId(applicationId);
//        merchantPlugin.setStatus(MerchantChannelStatus.AUDIT_STATUS_EXAMINING);
//        merchantPlugin.setThirdStatus("");
//        merchantPlugin.setRemark("瑞银信审核中");
//        merchantPluginService.saveOrUpdate(merchantPlugin);
//
//        QueryWrapper<MerchantAuditRecord> queryWrapper = new QueryWrapper<MerchantAuditRecord>().eq("applicationId", applicationId);
//        MerchantAuditRecord record = ObjectUtil.defaultIfNull(
//                auditRecordService.getOne(queryWrapper),
//                new MerchantAuditRecord());
//        record.setStatus("1");
//        record.setMerchantCode(merchantBaseInfo.getMerchantCode());
//        record.setChannel(SFB_PLUGIN_CHANNEL);
//        record.setThirdStatus("");
//        record.setMerchantId(applicationId);
//        auditRecordService.saveOrUpdate(record);
//        wxTalkService.sendPluginAuditResultAlert(merchantBaseInfo.getUserId());
//    }
//
//    @Override
//    public void auditCallback(JSONObject jsonObject) {
//        RyxAccessModel ryxAccessModel = JSON.parseObject(jsonObject.toJSONString(), RyxAccessModel.class);
//
//        JSONObject result = ryxService.accessResultDecrypt(ryxAccessModel, JSONObject.class);
//
//        String applicationId = result.getString("orderId").replace("-", "");
//
//        MerchantPlugin merchantPlugin = new MerchantPlugin()
//                .setChannel(SFB_PLUGIN_CHANNEL)
//                .setApplicationId(applicationId);
//        merchantPlugin = merchantPluginService.getOne(new QueryWrapper<>(merchantPlugin));
//
//        if (merchantPlugin == null) {
//            return;
//        }
//
//        if (MerchantChannelStatus.AUDIT_STATUS_SUCCESS.equals(merchantPlugin.getStatus())) {
//            // 已经进件通过, 不再处理后续
//            return;
//        }
//
//        String taskStatus = result.getString("status");
//        String remark = result.getString("memo");
//        merchantPlugin.setThirdStatus(taskStatus);
//
//        UserApp userApp = userAppMapper.selectByUserId(merchantPlugin.getUserId());
//        // 暂时只处理成功和失败的状态
//
//        if ("3".equals(taskStatus)) {
//            // 进件成功
//            merchantPlugin.setStatus(MerchantChannelStatus.AUDIT_STATUS_SUCCESS);
//            merchantPlugin.setPluginId(result.getString("onlMerId"));
//            merchantPlugin.setExtra(result);
//            String posMerId = result.getString("posMerId");
//            merchantPlugin.setPosMerId(posMerId);
//            merchantPluginService.updateById(merchantPlugin);
//            sxfMerAuditHandler.sendNotice(userApp, "收银呗审核通知", "商户插件审核通过", applicationId + "_" + ryxAccessModel.getReqTime());
//        }
//
//        if ("4".equals(taskStatus)) {
//            // 进件失败
//            merchantPlugin.setStatus(MerchantChannelStatus.AUDIT_STATUS_REJECT);
//            merchantPlugin.setRemark(remark);
//            merchantPluginService.updateById(merchantPlugin);
//            sxfMerAuditHandler.sendNotice(userApp, "收银呗审核通知", remark, applicationId + "_" + ryxAccessModel.getReqTime());
//        }
//
//        MerchantAuditRecord lastRecord = auditRecordService.getLastRecord(userApp.getMerchantCode());
//        if (lastRecord != null) {
//            lastRecord.setStatus(merchantPlugin.getStatus());
//            lastRecord.setThirdStatus(merchantPlugin.getThirdStatus());
//            lastRecord.setRemark(result.getString("memo"));
//            auditRecordService.updateById(lastRecord);
//        }
//    }
//
//    @Override
//    public JSONObject termList(String posMerId) {
//        Map<String, Object> paramMap = new HashMap<>();
//        paramMap.put("s_merId", posMerId);
//        paramMap.put("page", "1");
//        paramMap.put("rows", "10");
//
//        RyxAccessModel ryxAccessModel = ryxService.accessCombineEncrypt(paramMap, null, RyxConfig.subAccessId, RyxConfig.subAccessPubKey);
//        MultiValueMap<String, Object> multiValueMap = RyxAccessModel.toMap(ryxAccessModel);
//        RyxAccessModel result = restTemplate.postForObject(RyxConfig.termList, multiValueMap, RyxAccessModel.class);
//        RyxAccessResp resp = ryxService.accessResultDecrypt(result, RyxAccessResp.class);
//
//        return resp.getResult();
//    }
//}
