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

import com.zhongshu.card.client.type.setting.SettingType;
import com.zhongshu.card.server.core.dao.org.PlatFormConfigInfoDao;
import com.zhongshu.card.server.core.domain.setting.PlatFormConfigInfo;
import com.zhongshu.card.server.core.service.base.SuperService;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 系统配置信息
 *
 * @author TRX
 * @date 2024/10/8
 */
@Slf4j
@Service
public class PlatFormConfigInfoService extends SuperService {

    @Autowired
    private PlatFormConfigInfoDao platFormConfigInfoDao;

    /**
     * 得到配置 实体
     *
     * @param settingType
     * @return
     */
    public PlatFormConfigInfo getEntity(@NotNull SettingType settingType) {
        return platFormConfigInfoDao.findTopBySettingType(settingType);
    }

    /**
     * 得到配置信息
     *
     * @param settingType
     * @return
     */
    public Object getInfo(@NotNull SettingType settingType) {
        PlatFormConfigInfo configInfo = getEntity(settingType);
        if (ObjectUtils.isNotEmpty(configInfo)) {
            return configInfo.getInfo();
        }
        return null;
    }

    /**
     * 保存配置信息
     *
     * @param info
     * @param settingType
     * @return
     */
    public boolean saveEntity(Object info, @NotNull SettingType settingType) {
        PlatFormConfigInfo entity = platFormConfigInfoDao.findTopBySettingType(settingType);
        if (ObjectUtils.isEmpty(entity)) {
            entity = new PlatFormConfigInfo();
            initEntity(entity);
        } else {
            initUpdateEntity(entity);
        }
        entity.setInfo(info);
        entity.setSettingType(settingType);
        platFormConfigInfoDao.save(entity);
        return true;
    }

}
