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.dataConfig.UserProjectTaskConfig;
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 com.zhongshu.card.server.core.util.CommonUtil;
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.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用户在项目的配置 管理服务
 *
 * @author TRX
 * @date 2024/11/26
 */
@Slf4j
@Service
public class ProjectUserConfigService extends SuperService {

    @Autowired
    private ProjectUserConfigDao projectUserConfigDao;

    /**
     * 得到当前用户在项目的 使用必要配置情况，如：是否完成 绑卡操作、是否开头无感支付
     *
     * @return
     */
    public ResultContent getCurrentUserProjectConfigs() {
        String userId = getCurrentUserId();
        String projectId = getCurrentProjectOid();
        Map<String, Object> map = getUserProjectConfigs(userId, projectId);
        return ResultContent.buildSuccess(map);
    }

    /**
     * 得到项目中用户所有的配置
     *
     * @param userId
     * @param projectId
     * @return
     */
    public Map<String, Object> getUserProjectConfigs(String userId, String projectId) {
        if (StringUtils.isEmpty(userId)) {
            userId = getCurrentUserId();
        }
        if (StringUtils.isEmpty(projectId)) {
            projectId = getCurrentProjectOid();
        }
        Map<String, Object> map = new HashMap<String, Object>();
        List<String> keys = CommonUtil.getClassAllStringStatic(UserProjectTaskConfig.class);
        List<ProjectUserConfig> list = getUserProjectConfigs(userId, projectId, keys);
        Map<String, Object> tempMap = new HashMap<>();
        if (ObjectUtils.isNotEmpty(list)) {
            tempMap = list.stream().collect(Collectors.toMap(it -> it.getKey(), it -> it.getData(), (v1, v2) -> v1));
        }
        for (String key : keys) {
            if (tempMap.containsKey(key)) {
                map.put(key, tempMap.get(key));
            } else {
                map.put(key, Boolean.FALSE);
            }
        }
        return map;
    }

    //-------------------------------保存配置 start-----------------------

    /**
     * 标记完成绑定卡片
     *
     * @return
     */
    public ResultContent markFinishBindCard() {
        return saveUserConfig(UserProjectTaskConfig.userBindCardKey, Boolean.TRUE);
    }

    /**
     * 标记完成开通无感支付
     *
     * @return
     */
    public ResultContent markOpenUnPay() {
        return saveUserConfig(UserProjectTaskConfig.userOpenUnconsciousPayment, Boolean.TRUE);
    }

    public ResultContent saveUserConfig(String key, Object data) {
        ProjectUserConfigParam param = new ProjectUserConfigParam();
        param.setUserId(getCurrentUserId());
        param.setProjectOid(getCurrentProjectOid());
        param.setKey(key);
        param.setData(data);
        return saveUserConfig(param);
    }

    /**
     * 保存小程序用户配置
     *
     * @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 = getUserProjectConfig(param.getUserId(), param.getProjectOid(), 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);
    }

    public ProjectUserConfig getUserProjectConfig(String userId, String projectOid, String key) {
        if (StringUtils.isEmpty(userId)) {
            return null;
        }
        if (StringUtils.isEmpty(projectOid)) {
            return null;
        }
        if (StringUtils.isEmpty(key)) {
            return null;
        }
        return projectUserConfigDao.findTopByProjectOidAndUserIdAndKey(projectOid, userId, key);
    }

    public List<ProjectUserConfig> getUserProjectConfigs(String userId, String projectOid, List<String> keys) {
        if (StringUtils.isEmpty(userId)) {
            return null;
        }
        if (StringUtils.isEmpty(projectOid)) {
            return null;
        }
        if (ObjectUtils.isEmpty(keys)) {
            return null;
        }
        return projectUserConfigDao.findByProjectOidAndUserIdAndKeyIn(projectOid, userId, keys);
    }

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