package com.zhongshu.card.server.core.service.payment;

import com.github.microservice.http.APIResponseModel;
import com.github.microservice.models.hxz.base.IotBaseResult;
import com.github.microservice.net.ResultContent;
import com.zhongshu.card.client.model.payment.RequestInfoSaveParam;
import com.zhongshu.card.client.type.payment.RequestType;
import com.zhongshu.card.server.core.dao.org.UserCountDao;
import com.zhongshu.card.server.core.dao.payment.RequestInfoDao;
import com.zhongshu.card.server.core.domain.org.UserAccount;
import com.zhongshu.card.server.core.domain.payment.RequestInfo;
import com.zhongshu.card.client.utils.DateUtils;
import com.zhongshu.card.server.core.util.BeanUtils;
import com.zhongshu.card.server.core.util.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.concurrent.CompletableFuture;

/**
 * @author TRX
 * @date 2024/6/25
 */
@Slf4j
@Service
public class RequestInfoService {

    @Autowired
    RequestInfoDao requestInfoDao;

    @Autowired
    UserCountDao userCountDao;

    private static Long ttlTime = 30 * 24 * 60 * 60 * 1000L;

    /**
     * 保存请求数据
     *
     * @param param
     * @return
     */
    @Async
    public ResultContent saveRequestInfo(RequestInfoSaveParam param) {
        RequestInfo requestInfo = new RequestInfo();
        BeanUtils.copyProperties(param, requestInfo);
        requestInfo.setTimeStr(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.FORMAT_LONG));
        Long ttl = param.getTTL();
        if (CommonUtil.longIsEmpty(ttl)) {
            ttl = ttlTime;
        }
        requestInfo.setTTL(new Date(System.currentTimeMillis() + ttlTime));
        if (StringUtils.isNotEmpty(param.getUserId())) {
            UserAccount userAccount = userCountDao.findTopByUserId(param.getUserId());
            if (userAccount != null) {
                requestInfo.setUserName(userAccount.getName());
            }
        }

        requestInfoDao.save(requestInfo);
        return ResultContent.buildSuccess();
    }

    /**
     * 添加 HXZ 请求日志
     *
     * @param param
     * @param response
     * @return
     */
    public boolean addRequestInfo(Object param, IotBaseResult response) {
        CompletableFuture.runAsync(() -> {
            RequestInfo requestInfo = new RequestInfo();
            requestInfo.setParam(param);
            requestInfo.setResponse(response);
            requestInfo.setIsSuccess(response.isSuccess());
            requestInfo.setMsg(response.getMsg());
            requestInfo.setTimeStr(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.FORMAT_LONG));
            requestInfo.setRequestType(RequestType.HXZ);
            requestInfo.setTTL(new Date(System.currentTimeMillis() + ttlTime));
            requestInfoDao.save(requestInfo);
        });
        return true;
    }

    public boolean addRequestInfo(Object param, IotBaseResult response, RequestType requestType) {
        CompletableFuture.runAsync(() -> {
            RequestInfo requestInfo = new RequestInfo();
            requestInfo.setParam(param);
            requestInfo.setResponse(response);
            requestInfo.setIsSuccess(response.isSuccess());
            requestInfo.setMsg(response.getMsg());
            requestInfo.setTimeStr(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.FORMAT_LONG));
            requestInfo.setRequestType(requestType);
            requestInfo.setTTL(new Date(System.currentTimeMillis() + ttlTime));
            requestInfoDao.save(requestInfo);
        });
        return true;
    }

    /**
     * 添加IOT平台访问日志
     *
     * @param param
     * @param response
     * @return
     */
    public boolean addIotRequestInfo(Object param, APIResponseModel response) {
        RequestInfo requestInfo = new RequestInfo();
        requestInfo.setParam(param);
        requestInfo.setApiParam(response.getParam());
        requestInfo.setResponse(response);
        requestInfo.setIsSuccess(response.isSuccess());
        requestInfo.setMsg(response.getMsg());
        requestInfo.setTimeStr(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.FORMAT_LONG));
        requestInfo.setRequestType(RequestType.IotCenter);
        requestInfo.setTTL(new Date(System.currentTimeMillis() + ttlTime));
        requestInfo.setMillis(response.getMillis());
        requestInfoDao.save(requestInfo);
        return true;
    }
}
