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

import com.github.microservice.net.ResultContent;
import com.zhongshu.card.client.model.config.ProjectUserConfigModel;
import com.zhongshu.card.client.model.config.ProjectUserConfigParam;
import com.zhongshu.card.server.core.dao.config.ProjectUserConfigDao;
import com.zhongshu.card.server.core.domain.config.ProjectUserConfig;
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;

/**
 * @author TRX
 * @date 2024/11/26
 */
@Slf4j
@Service
public class ProjectUserConfigService extends SuperService {

    @Autowired
    private ProjectUserConfigDao projectUserConfigDao;

    /**
     * 保存小程序用户配置
     *
     * @param param
     * @return
     */
    public ResultContent xcxSaveUserConfig(ProjectUserConfigParam param) {
        if (StringUtils.isEmpty(param.getUserId())) {
            param.setUserId(getCurrentUserId());
        }
        if (StringUtils.isEmpty(param.getProjectOid())) {
            param.setProjectOid(getCurrentProjectOid());
        }
        return saveUserConfig(param);
    }

    private ResultContent saveUserConfig(ProjectUserConfigParam param) {
        if (StringUtils.isEmpty(param.getUserId())) {
            return ResultContent.buildFail("userId为空");
        }
        if (StringUtils.isEmpty(param.getProjectOid())) {
            return ResultContent.buildFail("projectOid为空");
        }
        if (StringUtils.isEmpty(param.getKey())) {
            return ResultContent.buildFail("key为空");
        }
        ProjectUserConfig entity = projectUserConfigDao.findTopByProjectOidAndUserIdAndKey(param.getProjectOid(), param.getUserId(), param.getKey());
        if (ObjectUtils.isEmpty(entity)) {
            entity = new ProjectUserConfig();
        }
        entity.setProjectOid(param.getProjectOid());
        entity.setUserId(param.getUserId());
        entity.setKey(param.getKey());
        entity.setData(param.getData());
        projectUserConfigDao.save(entity);
        return ResultContent.buildSuccess();
    }

    /**
     * 得到小程序配置 （主要是从header得到当前用户、项目oid信息）
     *
     * @param param
     * @return
     */
    public ResultContent<ProjectUserConfigModel> xcxGetUserConfig(ProjectUserConfigParam param) {
        if (StringUtils.isEmpty(param.getUserId())) {
            param.setUserId(getCurrentUserId());
        }
        if (StringUtils.isEmpty(param.getProjectOid())) {
            param.setProjectOid(getCurrentProjectOid());
        }
        return getUserConfig(param);
    }

    public ResultContent<ProjectUserConfigModel> getUserConfig(ProjectUserConfigParam param) {
        if (StringUtils.isEmpty(param.getUserId())) {
            return ResultContent.buildFail("userId为空");
        }
        if (StringUtils.isEmpty(param.getProjectOid())) {
            return ResultContent.buildFail("projectOid为空");
        }
        if (StringUtils.isEmpty(param.getKey())) {
            return ResultContent.buildFail("key为空");
        }
        ProjectUserConfig entity = projectUserConfigDao.findTopByProjectOidAndUserIdAndKey(param.getProjectOid(), param.getUserId(), param.getKey());
        ProjectUserConfigModel model = toModel(entity);
        if (StringUtils.isEmpty(model.getKey())) {
            if (ObjectUtils.isEmpty(model)) {
                model = new ProjectUserConfigModel();
            }
            model.setKey(param.getKey());
        }
        return ResultContent.buildSuccess(model);
    }

    private ProjectUserConfigModel toModel(ProjectUserConfig entity) {
        ProjectUserConfigModel model = new ProjectUserConfigModel();
        if (ObjectUtils.isNotEmpty(entity)) {
            BeanUtils.copyProperties(entity, model);
        }
        return model;
    }
}
