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

import com.github.microservice.net.ResultContent;
import com.zhongshu.card.client.model.org.role.RoleModel;
import com.zhongshu.card.client.model.projectAbout.projectCommon.ProjectCommonConfigModel;
import com.zhongshu.card.client.model.projectAbout.projectCommon.ProjectCommonConfigParam;
import com.zhongshu.card.client.model.projectAbout.projectCommon.ProjectCommonSuperParam;
import com.zhongshu.card.server.core.dao.org.OrganizationDao;
import com.zhongshu.card.server.core.dao.org.RoleDao;
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.org.Role;
import com.zhongshu.card.server.core.domain.projectAbout.ProjectCommonConfig;
import com.zhongshu.card.server.core.service.base.SuperService;
import com.zhongshu.card.server.core.service.user.RoleServiceImpl;
import com.zhongshu.card.server.core.util.BeanUtils;
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;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

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

    @Autowired
    private OrganizationDao organizationDao;

    @Autowired
    private ProjectCommonConfigDao commonConfigDao;

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private RoleServiceImpl roleService;

    /**
     * 保存配置信息
     *
     * @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 projectOid
     * @return
     */
    public ResultContent<ProjectCommonConfigModel> getInfo(String projectOid) {
        return ResultContent.buildSuccess(getInfoByOid(projectOid));
    }

    public ResultContent<ProjectCommonConfigModel> getXcxInfo(String projectOid) {
        if (StringUtils.isEmpty(projectOid)) {
            projectOid = getCurrentProjectOid();
        }
        return ResultContent.buildSuccess(getInfoByOid(projectOid));
    }

    public ResultContent<ProjectCommonConfigModel> getConfigInfoAndCheck(String projectOid) {
        if (StringUtils.isEmpty(projectOid)) {
            projectOid = getCurrentProjectOid();
        }
        ProjectCommonConfigModel model = getInfoByOid(projectOid);
        if (ObjectUtils.isEmpty(model)) {
            return ResultContent.buildFail("项目未配置人脸可信度");
        }
        return ResultContent.buildSuccess(model);
    }

    public ProjectCommonConfigModel getInfoByOid(String projectOid) {
        if (StringUtils.isEmpty(projectOid)) {
            return null;
        }
        ProjectCommonConfig entity = commonConfigDao.findTopByProjectOid(projectOid);
        ProjectCommonConfigModel model = null;
        if (ObjectUtils.isNotEmpty(entity)) {
            model = toModel(entity);
        }
        return model;
    }

    /**
     * 保存通用配置信息
     *
     * @param param
     * @return
     */
    public ResultContent saveCommonConfigInfo(ProjectCommonSuperParam 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
     * @param model
     * @return
     */
    public ResultContent getCommonConfigInfo(String projectOid, ProjectCommonSuperParam model) {
        ProjectCommonConfig entity = commonConfigDao.findTopByProjectOid(projectOid);
        if (ObjectUtils.isNotEmpty(entity) && ObjectUtils.isNotEmpty(model)) {
            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);

            // 可选择角色
            List<RoleModel> roleModels = new ArrayList<RoleModel>();
            List<String> roleIds = new ArrayList<>();
            if (entity.getIsAllowSelectRole() != null && entity.getIsAllowSelectRole()) {
                roleIds = entity.getRoleIds();
                if (ObjectUtils.isNotEmpty(entity.getRoleIds())) {
                    List<Role> roles = roleDao.findByIdIn(entity.getRoleIds());
                    model.setRoles(roleService.toModels(roles));
                }
            } else {
                // 查询项目的访客角色
                List<Role> roles = roleService.getVisitorRoles(entity.getProjectOid());
                if (ObjectUtils.isNotEmpty(roles)) {
                    roleModels = roleService.toModels(roles);
                    roleIds = roles.stream().map(Role::getId).collect(Collectors.toList());
                }
            }
            model.setRoles(roleModels);
            model.setRoleIds(roleIds);
        }
        return model;
    }

}
