package com.jwipc.job.logic.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.jwipc.log.logic.ThirdApiLogLogic;
import com.jwipc.sys.entity.LogThirdPartyCall;
import com.jwipc.sys.entity.LogThirdPartyInterface;
import com.jwipc.sys.service.ILogThirdPartyCallService;
import com.jwipc.sys.service.ILogThirdPartyInterfaceService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author shenyang.ou
 * @date 2024/3/18 15:59
 * @desc
 */
@Service
@RequiredArgsConstructor
public class ThirdApiLogLogicImpl implements ThirdApiLogLogic{
    private final ILogThirdPartyInterfaceService logThirdPartyInterfaceService;
    private final ILogThirdPartyCallService logThirdPartyCallService;

    @Override
    public void saveLog(String code, String name, Integer Type, String infRequestParams, String infResponse, String infInfo, LocalDateTime beginDateTime) {
        LogThirdPartyInterface logThirdPartyInterface = new LogThirdPartyInterface();
        logThirdPartyInterface.setInfCode(code);
        logThirdPartyInterface.setInfName(name);
        logThirdPartyInterface.setInfType(Type);
        if ("createShortagePlan".equals(code)){
            try {
                JSONObject jsonObject = JSON.parseObject(infRequestParams);
                JSONArray jsonArray = JSON.parseArray(jsonObject.getString("IT_DATA"));
                logThirdPartyInterface.setInfRequestParams(StrUtil.format("传入{}条",jsonArray.size()));
            }catch (Exception e){
                logThirdPartyInterface.setInfRequestParams(infRequestParams);
            }
        }else {
            logThirdPartyInterface.setInfRequestParams(infRequestParams);
        }
        logThirdPartyInterface.setInfResponse(infResponse);
        logThirdPartyInterface.setInfInfo(infInfo);
        logThirdPartyInterface.setInfResult(infResponse.contains("\"S\"")||infResponse.contains("\"code\":\"0\"")||infResponse.contains("\"errcode\":\"0000\"")||infResponse.contains("\"errCode\":\"0000\"")?1:0);
        logThirdPartyInterface.setInfCallTime(beginDateTime);
        logThirdPartyInterfaceService.save(logThirdPartyInterface);
    }

    /**
     * 调用方日志
     */
    @Override
    public void saveCallLog(String code, String name, Integer Type, String infRequestParams, String infResponse, String infInfo, LocalDateTime beginDateTime) {
        LogThirdPartyCall logThirdPartyCall = new LogThirdPartyCall();
        logThirdPartyCall.setInfCode(code);
        logThirdPartyCall.setInfName(name);
        logThirdPartyCall.setInfType(Type);
        logThirdPartyCall.setInfRequestParams(infRequestParams);
        logThirdPartyCall.setInfResponse(infResponse);
        logThirdPartyCall.setInfInfo(infInfo);
        logThirdPartyCall.setInfResult(infResponse.contains("\"S\"")||infResponse.contains("\"成功\"")||infResponse.contains(":true")||infResponse.contains("\"code\":\"0\"")||infResponse.contains("\"errcode\":\"0000\"")||infResponse.contains("\"errCode\":\"0000\"")?1:0);
        logThirdPartyCall.setInfCallTime(beginDateTime);
        logThirdPartyCallService.save(logThirdPartyCall);
    }
}
