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

import com.github.microservice.net.ResultContent;
import com.zhongshu.card.client.model.projectAbout.*;
import com.zhongshu.card.server.core.dao.org.OrganizationDao;
import com.zhongshu.card.server.core.dao.projectAbout.ProjectCommonConfigDao;
import com.zhongshu.card.server.core.domain.org.Organization;
import com.zhongshu.card.server.core.domain.projectAbout.ProjectCommonConfig;
import com.zhongshu.card.server.core.domain.projectAbout.ProjectIotInfo;
import com.zhongshu.card.server.core.service.base.SuperService;
import com.zhongshu.card.server.core.util.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 项目的 关联的物联网 ak sk 设置
 *
 * @author TRX
 * @date 2024/9/24
 */
@Slf4j
@Service
public class ProjectCommonConfigService extends SuperService {

    @Autowired
    OrganizationDao organizationDao;

    @Autowired
    ProjectCommonConfigDao commonConfigDao;

    /**
     * 保存配置信息
     *
     * @param param
     * @return
     */
    public ResultContent saveInfo(ProjectCommonConfigParam param) {
        String projectOid = param.getProjectOid();
        Organization projectInfo = organizationDao.findTopByOid(projectOid);
        if (ObjectUtils.isEmpty(projectInfo)) {
            return ResultContent.buildFail(String.format("项目oid不存在：%s", projectOid));
        }
        ProjectCommonConfig entity = commonConfigDao.findTopByProjectOid(projectOid);
        if (ObjectUtils.isEmpty(entity)) {
            entity = new ProjectCommonConfig();
            entity.setIsDelete(Boolean.FALSE);
            initEntityNoOid(entity);
        } else {
            initUpdateEntity(entity);
        }
        BeanUtils.copyProperties(param, entity);
        entity.setProjectInfo(projectInfo);
        entity.setProjectName(projectInfo.getName());
        entity.setProjectCode(projectInfo.getCode());

        entity.setOid(projectOid);
        entity.setAboutOid(projectOid);
        entity.setAboutAuthType(projectInfo.getAuthType());
        commonConfigDao.save(entity);
        return ResultContent.buildSuccess();
    }

    /**
     * 保存配置信息
     *
     * @param param
     * @return
     */
    public ResultContent saveAgreementInfo(ProjectAgreementConfigParam param) {
        String projectOid = param.getProjectOid();
        Organization projectInfo = organizationDao.findTopByOid(projectOid);
        if (ObjectUtils.isEmpty(projectInfo)) {
            return ResultContent.buildFail(String.format("项目oid不存在：%s", projectOid));
        }
        ProjectCommonConfig entity = commonConfigDao.findTopByProjectOid(projectOid);
        if (ObjectUtils.isEmpty(entity)) {
            entity = new ProjectCommonConfig();
            entity.setIsDelete(Boolean.FALSE);
            initEntityNoOid(entity);
        } else {
            initUpdateEntity(entity);
        }
        BeanUtils.copyProperties(param, entity);
        entity.setProjectInfo(projectInfo);
        entity.setProjectName(projectInfo.getName());
        entity.setProjectCode(projectInfo.getCode());
        entity.setOid(projectOid);
        entity.setAboutOid(projectOid);
        entity.setAboutAuthType(projectInfo.getAuthType());
        commonConfigDao.save(entity);
        return ResultContent.buildSuccess();
    }

    /**
     * 得到项目
     *
     * @param projectOid
     * @return
     */
    public ResultContent<ProjectCommonConfigModel> getInfo(String projectOid) {
        ProjectCommonConfig entity = commonConfigDao.findTopByProjectOid(projectOid);
        ProjectCommonConfigModel model = null;
        if (ObjectUtils.isNotEmpty(entity)) {
            model = toModel(entity);
        }
        return ResultContent.buildSuccess(model);
    }

    public ResultContent<ProjectAgreementConfigParam> getAgreementInfo(String projectOid) {
        ProjectCommonConfig entity = commonConfigDao.findTopByProjectOid(projectOid);
        ProjectAgreementConfigParam model = null;
        if (ObjectUtils.isNotEmpty(entity)) {
            model = new ProjectAgreementConfigParam();
            org.springframework.beans.BeanUtils.copyProperties(entity, model);
        }
        return ResultContent.buildSuccess(model);
    }

    /**
     * 项目是否完成配置
     *
     * @param projectOid
     * @return
     */
    public boolean isFinishConfig(String projectOid) {
        ProjectCommonConfig entity = commonConfigDao.findTopByProjectOid(projectOid);
        if (ObjectUtils.isNotEmpty(entity)) {
            return true;
        }
        return false;
    }

    public ProjectCommonConfigModel toModel(ProjectCommonConfig entity) {
        ProjectCommonConfigModel model = null;
        if (ObjectUtils.isNotEmpty(entity)) {
            model = new ProjectCommonConfigModel();
            BeanUtils.copyProperties(entity, model);
        }
        return model;
    }

}
