package cn.pluss.platform.sxf.audit;

import cn.hutool.json.JSONUtil;
import cn.pluss.platform.entity.MerchantChannelStatus;
import cn.pluss.platform.klk.LaKaLaUtility;
import cn.pluss.platform.klk.LakalaConfig;
import cn.pluss.platform.task.LklMerAuditHandler;
import cn.pluss.platform.task.SxfMerAuditHandler;
import com.alibaba.fastjson.JSONArray;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

@Slf4j
@Service
public class LklMerAuditServiceImpl {



    @Lazy
    @Autowired
    private LklMerAuditHandler lklMerAuditHandler;

    @Lazy
    @Autowired
    private LakalaConfig lakalaConfig;


    public String merchantAuditCallback(Map<String,Object> map){

        log.info("拉卡拉进件回调:{}", JSONUtil.toJsonStr(map));
        try {
            String data = LaKaLaUtility.decrypt(lakalaConfig.getPubKey(),map.get("data").toString());
            Map arryMap = (Map) JSONArray.parse(data);
            // 异步处理
            lklMerAuditHandler.auditHandler(arryMap);
            return "{\n" +
                    "\t\"code\": \"SUCCESS\",\n" +
                    "\t\"msg\": \"成功\"\n" +
                    "}";
        } catch (Exception e) {
            log.error("入驻回调异步任务启动失败!");
            e.printStackTrace();
        }

        return "{\n" +
                "\t\"code\": \"FAIL\",\n" +
                "\t\"msg\": \"失败\"\n" +
                "}";
    }




    public String auditEditHander(Map<String,Object> map){
        log.info("拉卡拉修改回调:{}", JSONUtil.toJsonStr(map));
        try {
            String data = LaKaLaUtility.decrypt(lakalaConfig.getPubKey(),map.get("data").toString());
            Map arryMap = (Map) JSONArray.parse(data);
            // 异步处理
            lklMerAuditHandler.auditEditHandler(arryMap);
            return "{\n" +
                    "\t\"code\": \"SUCCESS\",\n" +
                    "\t\"msg\": \"成功\"\n" +
                    "}";
        } catch (Exception e) {
            log.error("入驻回调异步任务启动失败!");
            e.printStackTrace();
        }

        return "{\n" +
                "\t\"code\": \"FAIL\",\n" +
                "\t\"msg\": \"失败\"\n" +
                "}";
    }



    public MerchantChannelStatus queryMerchantChannelStatus(String customerNo,String externalCustomerNo){
        return lklMerAuditHandler.queryMerchatChannelstatus(customerNo, externalCustomerNo);
    }






}
