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

import com.github.microservice.net.ResultContent;
import com.zhongshu.card.client.model.projectAbout.ProjectIotInfoModel;
import com.zhongshu.card.client.model.projectAbout.ProjectIotInfoParam;
import com.zhongshu.card.server.core.dao.org.OrganizationDao;
import com.zhongshu.card.server.core.dao.projectAbout.ProjectIotInfoDao;
import com.zhongshu.card.server.core.domain.org.Organization;
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 ProjectIotInfoService extends SuperService {

    @Autowired
    OrganizationDao organizationDao;

    @Autowired
    ProjectIotInfoDao projectIotInfoDao;

    /**
     * 保存信息
     *
     * @param param
     * @return
     */
    public ResultContent saveProjectIotInfo(ProjectIotInfoParam param) {
        String projectOid = param.getProjectOid();
        Organization projectInfo = organizationDao.findTopByOid(projectOid);
        if (ObjectUtils.isEmpty(projectInfo)) {
            return ResultContent.buildFail(String.format("项目oid不存在：%s", projectOid));
        }
        ProjectIotInfo entity = projectIotInfoDao.findTopByProjectOid(projectOid);
        if (ObjectUtils.isEmpty(entity)) {
            entity = new ProjectIotInfo();
            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());
        projectIotInfoDao.save(entity);
        return ResultContent.buildSuccess();
    }

    /**
     * 得到项目设置
     *
     * @param projectOid
     * @return
     */
    public ResultContent<ProjectIotInfoModel> getProjectIotInfo(String projectOid) {
        ProjectIotInfo ProjectIotInfo = projectIotInfoDao.findTopByProjectOid(projectOid);
        ProjectIotInfoModel model = null;
        if (ObjectUtils.isNotEmpty(ProjectIotInfo)) {
            model = toModel(ProjectIotInfo);
        }
        return ResultContent.buildSuccess(model);
    }

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

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

}
