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

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.github.microservice.dataConfig.IotIdentifierConfig;
import com.github.microservice.models.iot.IotSendParam;
import com.github.microservice.models.project.ProjectConfigQueryParam;
import com.github.microservice.models.project.ProjectConfigResultModel;
import com.github.microservice.net.ResultContent;
import com.zhongshu.card.server.core.dao.org.OrganizationDao;
import com.zhongshu.card.server.core.domain.org.Organization;
import com.zhongshu.card.server.core.service.openAPI.OpenApiRequestService;
import com.zhongshu.card.server.core.service.payment.ExpenseFlowServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 项目配置信息 (得到)
 *
 * @author TRX
 * @date 2025/1/3
 */
@Slf4j
@Service
public class ProjectConfigService {

    @Autowired
    private ExpenseFlowServiceImpl expenseFlowService;

    @Autowired
    private OrganizationDao organizationDao;

    @Autowired
    private OpenApiRequestService openApiRequestService;

    /**
     * 注册网关是查询项目的配置
     *
     * @param param
     * @return
     */
    public ResultContent iotQueryProjectConfig(ProjectConfigQueryParam param) {
        ProjectConfigResultModel model = new ProjectConfigResultModel();
        String projectInfoCode = param.getProjectInfoCode();
        boolean isVerify = true;
        if (StringUtils.isEmpty(projectInfoCode)) {
            isVerify = false;
            model.setFailed("projectInfoCode 不能为空");
        }
        if (isVerify) {
            // 下发查询项目相关的配置
            sendProjectConfig(projectInfoCode);
        }
        return ResultContent.buildSuccess(expenseFlowService.buildPayResultStr(model));
    }

    /**
     * 下发项目信息
     *
     * @param projectInfoCode
     */
    public void sendProjectConfig(String projectInfoCode) {
        IotSendParam param = new IotSendParam();
        param.setIdentifier(IotIdentifierConfig.projectConfig);
        param.setProjectInfoCode(projectInfoCode);
        String projectOid = "";

        ProjectConfigResultModel model = new ProjectConfigResultModel();
        model.setProjectInfoCode(projectInfoCode);
        Organization organization = organizationDao.findTopByCode(projectInfoCode);
        if (ObjectUtils.isNotEmpty(organization)) {
            projectOid = organization.getOid();
            model.setSuccess();
        } else {
            model.setFailed("项目信息不存在");
        }
        JSONObject data = JSONUtil.parseObj(model);
        param.setData(data);
        ResultContent resultContent = openApiRequestService.sendIotMessage(param, projectOid);
        if (resultContent.isSuccess()) {
            log.info("项目配置信息下发成功");
        } else {
            log.error("项目配置信息下发失败 {}", resultContent.getMsg());
        }
    }

}
