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

import com.github.microservice.net.ResultContent;
import com.zhongshu.card.client.model.base.ProjectOidParam;
import com.zhongshu.card.client.model.projectAbout.ProjectWeChatInfoModel;
import com.zhongshu.card.client.model.projectAbout.ProjectWeChatInfoParam;
import com.zhongshu.card.client.model.xcx.ProjectXcxModel;
import com.zhongshu.card.server.core.dao.org.OrganizationDao;
import com.zhongshu.card.server.core.dao.projectAbout.ProjectWeChatInfoDao;
import com.zhongshu.card.server.core.domain.org.Organization;
import com.zhongshu.card.server.core.domain.projectAbout.ProjectIotInfo;
import com.zhongshu.card.server.core.domain.projectAbout.ProjectWeChatInfo;
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.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;

/**
 * 项目的 微信管理信息设置
 *
 * @author TRX
 * @date 2024/9/24
 */
@Slf4j
@Service
public class ProjectWeChatInfoService extends SuperService {

    @Autowired
    ProjectWeChatInfoDao projectWeChatInfoDao;

    @Autowired
    OrganizationDao organizationDao;

    /**
     * 保存信息
     *
     * @param param
     * @return
     */
    public ResultContent saveProjectWeChatInfo(ProjectWeChatInfoParam param) {
        String projectOid = param.getProjectOid();
        Organization projectInfo = organizationDao.findTopByOid(projectOid);
        if (ObjectUtils.isEmpty(projectInfo)) {
            return ResultContent.buildFail(String.format("项目oid不存在：%s", projectOid));
        }
        ProjectWeChatInfo projectWeChatInfo = projectWeChatInfoDao.findTopByProjectOid(projectOid);
        ProjectWeChatInfo appIdTemp = projectWeChatInfoDao.findTopByAppId(param.getAppId());
        if (ObjectUtils.isEmpty(projectWeChatInfo)) {
            projectWeChatInfo = new ProjectWeChatInfo();
            projectWeChatInfo.setIsDelete(Boolean.FALSE);
            initEntityNoOid(projectWeChatInfo);
        } else {
            initUpdateEntity(projectWeChatInfo);
        }
        if (ObjectUtils.isNotEmpty(appIdTemp)
                && !appIdTemp.getProjectOid().equals(projectOid)) {
            String _name = "";
            if (ObjectUtils.isNotEmpty(appIdTemp.getProjectInfo())) {
                _name = appIdTemp.getProjectInfo().getName();
            }
            return ResultContent.buildFail(String.format("appId已绑定项目：%s", _name));
        }

        BeanUtils.copyProperties(param, projectWeChatInfo);
        projectWeChatInfo.setProjectInfo(projectInfo);
        projectWeChatInfo.setProjectName(projectInfo.getName());
        projectWeChatInfo.setProjectCode(projectInfo.getCode());

        projectWeChatInfo.setOid(projectOid);
        projectWeChatInfo.setAboutOid(projectOid);
        projectWeChatInfo.setAboutAuthType(projectInfo.getAuthType());
        projectWeChatInfoDao.save(projectWeChatInfo);
        return ResultContent.buildSuccess();
    }

    /**
     * 得到项目设置
     *
     * @param projectOid
     * @return
     */
    public ResultContent<ProjectWeChatInfoModel> getProjectWeChatInfo(String projectOid) {
        ProjectWeChatInfo projectWeChatInfo = projectWeChatInfoDao.findTopByProjectOid(projectOid);
        ProjectWeChatInfoModel model = null;
        if (ObjectUtils.isNotEmpty(projectWeChatInfo)) {
            model = toModel(projectWeChatInfo);
        }
        return ResultContent.buildSuccess(model);
    }

    /**
     * 根据appId查询对应的项目信息 （有可能appId 对应对个配置）
     *
     * @param appId
     * @return
     */
    public ResultContent<ProjectWeChatInfoModel> getProjectWeChatInfoByAppId(String appId) {
        ProjectWeChatInfo projectWeChatInfo = projectWeChatInfoDao.findTopByAppId(appId);
        ProjectWeChatInfoModel model = null;
        if (ObjectUtils.isNotEmpty(projectWeChatInfo)) {
            model = toModel(projectWeChatInfo);
        }
        return ResultContent.buildSuccess(model);
    }

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

    /**
     * 更加当前的小程序appId 得到对应的项目oid （appId 存储在head里面）
     *
     * @return
     */
    public ResultContent<List<ProjectXcxModel>> getProjectOidForXcx() {
        List<ProjectXcxModel> models = new ArrayList<>();
        String appId = getHeadParam(new String[]{"appId", "appid"});
        if (StringUtils.isEmpty(appId)) {
            return ResultContent.buildFail("未取得appId");
        }

        List<ProjectWeChatInfo> list = projectWeChatInfoDao.findByAppIdOrderByUpdateTimeDesc(appId);
        if (ObjectUtils.isNotEmpty(list)) {
            models = list.stream().map(it -> {
                ProjectXcxModel model = new ProjectXcxModel();
                Organization projectInfo = it.getProjectInfo();
                if (ObjectUtils.isNotEmpty(projectInfo)) {
                    model.setProjectOid(projectInfo.getOid());
                    model.setProjectName(projectInfo.getName());
                }
                return model;
            }).collect(Collectors.toUnmodifiableList());
        }
        return ResultContent.buildSuccess(models);
    }

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

}
