Zhiqim Master(运营管理台)是在ZhiqimManager基础上改造成用于运营思路的管理系统,增加余额,和组织结构等,开放组织管理员,可以添加组织内的部门和角色和操作员。并增加该管理台上的一些组件,如充值支付等组件。适用于二级代理管理或该大型组织机构

森中灵 最后提交于7月前 替换为8.0.5版本
ZmrParamDao.java15KB
/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[遇见知启蒙,邂逅框架梦]
 * 
 * https://zhiqim.org/project/zhiqim_components/zhiqim_master.htm
 *
 * Zhiqim Master is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package org.zhiqim.manager.dao;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import org.zhiqim.kernel.annotation.AnAlias;
import org.zhiqim.kernel.config.ItemType;
import org.zhiqim.kernel.constants.SignConstants;
import org.zhiqim.kernel.model.maps.LinkedMapSV;
import org.zhiqim.kernel.model.maps.MapSV;
import org.zhiqim.kernel.model.results.R1;
import org.zhiqim.kernel.model.results.RE;
import org.zhiqim.kernel.util.Ints;
import org.zhiqim.kernel.util.Longs;
import org.zhiqim.kernel.util.Validates;
import org.zhiqim.manager.ZmrConstants;
import org.zhiqim.manager.dbo.ZmrParam;
import org.zhiqim.orm.ORM;
import org.zhiqim.orm.ORMServer;
import org.zhiqim.orm.dbo.Selector;
import org.zhiqim.orm.dbo.Updater;

/**
 * 参数访问对象
 *
 * @version v1.0.0 @author zouzhigang 2015-5-28 新建与整理
 * @version v1.4.1 @author zouzhigang 2018-5-3 由ZmrParamCache改为ZmrParamDao
 */
@AnAlias("ZmrParamDao")
public class ZmrParamDao implements ZmrConstants, SignConstants
{
    /*************************************************************************************************/
    //获取标准ZMR自带的参数
    /*************************************************************************************************/
    
    /** 获取首页主题风格 */
    public static String getThemeIndex()
    {
        return getString(ZMR_NAME, ZMR_THEME_INDEX, ZMR_THEME_00_DEFAULT);
    }
    
    /** 获取主页主题风格 */
    public static String getThemeMain()
    {
        return getString(ZMR_NAME, ZMR_THEME_MAIN, ZMR_THEME_00_DEFAULT);
    }
    
    public static boolean isThemeFrame()
    {
        return isTrue(ZMR_NAME, ZMR_THEME_FRAME, _FALSE_);
    }
    
    /** 是否有管理登录页 */
    public static boolean hasManageLogin()
    {
        return isTrue(ZMR_NAME, ZMR_HAS_MANAGE_LOGIN);
    }
    
    /** 是否有验证码 */
    public static boolean hasVerificationCode()
    {
        return isTrue(ZMR_NAME, ZMR_HAS_VERIFICATION_CODE);
    }
    
    /** 是否记住用户名 */
    public static boolean hasRememberCode()
    {
        return isTrue(ZMR_NAME, ZMR_HAS_REMEMBER_CODE);
    }
    
    /** 是否记住密码 */
    public static boolean hasRememberPass()
    {
        return isTrue(ZMR_NAME, ZMR_HAS_REMEMBER_PASS);
    }
    
    /** 是否需要任务检查关闭超级管理员 */
    public static boolean chkCloseSuperAdmin()
    {
        return isTrue(ZMR_NAME, ZMR_CHK_CLOSE_SUPER_ADMIN);
    }
    
    /** 获取主页地址 */
    public static String getMainUrl()
    {
        return getString(ZMR_NAME, ZMR_MAIN_URL);
    }
    
    /** 获取主页首页地址 */
    public static String getMainIndexUrl()
    {
        return isThemeFrame()?ZMR_MAIN_URL_WELCOME:ZMR_MAIN_URL_DEFAULT;
    }
    
    /** 获取保留操作日志天数 */
    public static int getRemainLogDay()
    {
        return getInt(ZMR_NAME, ZMR_REMAIN_LOG_DAY, 7);
    }
    
    /** 是否使用CDN路径 */
    public static boolean hasCdnPath()
    {
        return !Validates.isEmptyBlank(getString(ZMR_NAME, ZMR_CDN_PATH));
    }
    
    /** 获取CDN路径 */
    public static String getCdnPath()
    {
        return getString(ZMR_NAME, ZMR_CDN_PATH);
    }
    
    /** 获取公钥 */
    public static String getPublicKey()
    {
        return getString(ZMR_NAME, ZMR_PUBLIC_KEY);
    }
    
    /** 获取私钥 */
    public static String getPrivateKey()
    {
        return getString(ZMR_NAME, ZMR_PRIVATE_KEY);
    }
    
    /** 是否取操作员继承的所有部门权限 */
    public static boolean isOperatorDeptAllRule()
    {
        return isTrue(ZMR_NAME, ZMR_OPERATOR_DEPT_ALL_RULE);
    }
    
    /*************************************************************************************************/
    //更新标准ZMR自带的参数
    /*************************************************************************************************/
    
    /** 更新首页主题风格 */
    public static void doUpdateThemeIndex(String theme)
    {
        doUpdate(ZMR_NAME, ZMR_THEME_INDEX, theme);
    }
    
    /** 更新主页主题风格 */
    public static void doUpdateThemeMain(String theme)
    {
        doUpdate(ZMR_NAME, ZMR_THEME_MAIN, theme);
    }
    
    /** 更新是否有管理登录页 */
    public static void doUpdateManageLogin(boolean value)
    {
        doUpdate(ZMR_NAME, ZMR_HAS_MANAGE_LOGIN, value);
    }
    
    /** 更新记住用户名 */
    public static void doUpdateRememberCode(boolean value)
    {
        doUpdate(ZMR_NAME, ZMR_HAS_REMEMBER_CODE, value?_TRUE_:_FALSE_);
    }
    
    /** 更新需要验证码 */
    public static void doUpdateVerificationCode(boolean value)
    {
        doUpdate(ZMR_NAME, ZMR_HAS_VERIFICATION_CODE, value);
    }
    
    /** 更新是否需要任务检查关闭超级管理员 */
    public static void doUpdateChkCloseSuperAdmin(boolean value)
    {
        doUpdate(ZMR_NAME, ZMR_CHK_CLOSE_SUPER_ADMIN, value);
    }
    
    /*************************************************************************************************/
    //默认为第一个数据库初始化参数
    /*************************************************************************************************/
    
    /** 增加组 */
    public static void addGroupIfNotExists(String paramGroup, int paramSeq, ItemType paramType, String paramDesc) throws Exception
    {
        addParamIfNotExists(paramGroup, ZMR_GROUP, ZMR_GROUP, paramSeq, paramType, null, paramDesc);
    }
    
    /** 检查数据不存在时新建参数对象,默认值和验证为空,允许修改 */
    public static void addParamIfNotExists(String paramGroup, String paramKey, int paramSeq, String paramDesc) throws Exception
    {
        addParamIfNotExists(paramGroup, paramKey, null, paramSeq, ItemType.PUBLIC, null, paramDesc);
    }
    
    /** 检查数据不存在时新建参数对象,默认验证为空 */
    public static void addParamIfNotExists(String paramGroup, String paramKey, String paramValue, int paramSeq, ItemType paramType, String paramDesc) throws Exception
    {
        addParamIfNotExists(paramGroup, paramKey, paramValue, paramSeq, paramType, null, paramDesc);
    }
    
    /** 检查数据不存在时新建参数对象,全7个字段 */
    public static void addParamIfNotExists(String paramGroup, String paramKey, String paramValue, int paramSeq, ItemType paramType, String paramValidate, String paramDesc) throws Exception
    {
        if (ORM.table().count(ZmrParam.class, paramGroup, paramKey) == 0)
        {
            replaceParam(paramGroup, paramKey, paramValue, paramSeq, paramType, paramValidate, paramDesc);
        }
    }
    
    /** 新建或替换参数对象,默认验证为空 */
    public static void replaceParam(String paramGroup, String paramKey, String paramValue, int paramSeq, ItemType paramType, String paramDesc) throws Exception
    {
        replaceParam(paramGroup, paramKey, paramValue, paramSeq, paramType, null, paramDesc);
    }
    
    /** 新建或替换参数对象,全7个字段 */
    public static void replaceParam(String paramGroup, String paramKey, String paramValue, int paramSeq, ItemType paramType, String paramValidate, String paramDesc) throws Exception
    {
        ZmrParam param = new ZmrParam();
        param.setParamGroup(paramGroup);
        param.setParamKey(paramKey);
        param.setParamValue(paramValue);
        param.setParamSeq(paramSeq);
        param.setParamType(ItemType.toString(paramType));
        param.setParamValidate(paramValidate);
        param.setParamDesc(paramDesc);

        ORM.table().replace(param);
    }
    
    /*************************************************************************************************/
    //默认为第一个数据库配置的获取参数方法
    /*************************************************************************************************/
    
    /** 判断是否存在参数组 */
    public static boolean hasGroup(String paramGroup)
    {
        return ORM.tabler().count(ZmrParam.class, new Selector("paramGroup", paramGroup)).result() > 0;
    }
    
    /** 判断是否存在参数 */
    public static boolean hasParam(String paramGroup, String paramKey)
    {
        return ORM.tabler().count(ZmrParam.class, paramGroup, paramKey).result() > 0;
    }
    
    /** 获取参数组表(排除私有) */
    public static List<ZmrParam> getGroupList()
    {
        return ORM.tabler().list(ZmrParam.class, new Selector("paramKey", ZMR_GROUP).addMustNotEqual("paramType", "private").addMust("paramType", "private").addOrderbyAsc("paramSeq")).result();
    }
    
    /** 获取参数组表(仅私有) */
    public static List<ZmrParam> getGroupPrivate()
    {
        return ORM.tabler().list(ZmrParam.class, new Selector("paramKey", ZMR_GROUP).addMust("paramType", "private").addOrderbyAsc("paramSeq")).result();
    }
    
    /** 获取参数组表(排除私有) */
    public static LinkedHashMap<ZmrParam, List<ZmrParam>> getGroupMap()
    {
        List<ZmrParam> gList = ORM.tabler().list(ZmrParam.class, new Selector("paramKey", ZMR_GROUP).addMustNotEqual("paramType", "private").addOrderbyAsc("paramSeq")).result();
        if (gList.isEmpty())
            return new LinkedHashMap<>();
        
        MapSV<ZmrParam> gMap = new LinkedMapSV<>();
        LinkedHashMap<ZmrParam, List<ZmrParam>> groupMap = new LinkedHashMap<>();
        for (ZmrParam group : gList)
        {
            gMap.put(group.getParamGroup(), group);
            groupMap.put(group, new ArrayList<>());
        }
        
        List<ZmrParam> paramList = ORM.tabler().list(ZmrParam.class, new Selector().addMustNotEqual("paramKey", ZMR_GROUP).addMustNotEqual("paramType", "private").addOrderbyAsc("paramGroup,paramSeq")).result();
        for (ZmrParam param : paramList)
        {
            ZmrParam group = gMap.get(param.getParamGroup());
            if (group == null)
                continue;
            
            groupMap.get(group).add(param);
        }
        
        return groupMap;
    }
    
    /** 获取参数组表(仅私有) */
    public static LinkedHashMap<ZmrParam, List<ZmrParam>> getGroupMapPrivate()
    {
        List<ZmrParam> gList = ORM.tabler().list(ZmrParam.class, new Selector("paramKey", ZMR_GROUP).addMust("paramType", "private").addOrderbyAsc("paramSeq")).result();
        if (gList.isEmpty())
            return new LinkedHashMap<>();
        
        MapSV<ZmrParam> gMap = new LinkedMapSV<>();
        LinkedHashMap<ZmrParam, List<ZmrParam>> groupMap = new LinkedHashMap<>();
        for (ZmrParam group : gList)
        {
            gMap.put(group.getParamGroup(), group);
            groupMap.put(group, new ArrayList<>());
        }
        
        List<ZmrParam> paramList = ORM.tabler().list(ZmrParam.class, new Selector().addMustNotEqual("paramKey", ZMR_GROUP).addMust("paramType", "private").addOrderbyAsc("paramGroup,paramSeq")).result();
        for (ZmrParam param : paramList)
        {
            ZmrParam group = gMap.get(param.getParamGroup());
            if (group == null)
                continue;
            
            groupMap.get(group).add(param);
        }
        
        return groupMap;
    }
    
    /** 获取参数组表 */
    public static List<ZmrParam> getParamList(String paramGroup)
    {
        return ORM.tabler().list(ZmrParam.class, new Selector("paramGroup", paramGroup).addOrderbyAsc("paramSeq")).result();
    }
    
    /** 获取参数(获取缓存拷贝数据) */
    public static ZmrParam getParam(String paramGroup, String paramKey)
    {
        return ORM.tabler().item(ZmrParam.class, paramGroup, paramKey).result();
    }
    
    /** 获取参数(获取缓存原始数据) */
    public static ZmrParam getParamCacheRaw(String paramGroup, String paramKey)
    {
        return ORM.tabler().cacheItemRaw(ZmrParam.class, paramGroup, paramKey).result();
    }
    
    /** 获取字符串数据(如果有缓存,取原始缓存,否则取数据库) */
    public static String getString(String paramGroup, String paramKey)
    {
        ORMServer server = ORM.server();
        
        ZmrParam param = null;
        if (server.isCache(ZmrParam.class))
            param = server.tabler().cacheItemRaw(ZmrParam.class, paramGroup, paramKey).result();
        else
            param = server.tabler().item(ZmrParam.class, paramGroup, paramKey).result();
        
        return (param == null)?null:param.getParamValue();
    }
    
    /** 获取字符串数据,为null取缺省值 */
    public static String getString(String paramGroup, String paramKey, String defaultValue)
    {
        String value = getString(paramGroup, paramKey);
        return (Validates.isEmptyBlank(value))?defaultValue:value;
    }
    
    /** 获取整型数据 */
    public static int getInt(String paramGroup, String paramKey)
    {
        return getInt(paramGroup, paramKey, -1);
    }
    
    /** 获取整型数据,为null取缺省值 */
    public static int getInt(String paramGroup, String paramKey, int defaultValue)
    {
        return Ints.toInt(getString(paramGroup, paramKey), defaultValue);
    }
    
    /** 获取长整型数据 */
    public static long getLong(String paramGroup, String paramKey)
    {
        return getLong(paramGroup, paramKey, -1);
    }
    
    /** 获取整型数据,为null取缺省值 */
    public static long getLong(String paramGroup, String paramKey, int defaultValue)
    {
        return Longs.toLong(getString(paramGroup, paramKey), defaultValue);
    }
    
    /** 判断是否为真 */
    public static boolean isTrue(String paramGroup, String paramKey)
    {
        return _TRUE_.equals(getString(paramGroup, paramKey));
    }
    
    /** 判断是否为真,为null取缺省值 */
    public static boolean isTrue(String paramGroup, String paramKey, String defaultValue)
    {
        return _TRUE_.equals(getString(paramGroup, paramKey, defaultValue));
    }

    /*************************************************************************************************/
    //默认为第一个数据库配置的更新和刷新参数
    /*************************************************************************************************/
    
    /** 更新缓存值 */
    public static R1 doUpdate(String paramGroup, String paramKey, int paramValue)
    {
        return doUpdate(paramGroup, paramKey, String.valueOf(paramValue));
    }
    
    /** 更新缓存值 */
    public static R1 doUpdate(String paramGroup, String paramKey, long paramValue)
    {
        return doUpdate(paramGroup, paramKey, String.valueOf(paramValue));
    }
    
    /** 更新缓存值 */
    public static R1 doUpdate(String paramGroup, String paramKey, boolean paramValue)
    {
        return doUpdate(paramGroup, paramKey, String.valueOf(paramValue));
    }
    
    /** 更新缓存值 */
    public static R1 doUpdate(String paramGroup, String paramKey, String paramValue)
    {
        //更新到数据库
        Updater updater = new Updater();
        updater.addMust("paramGroup", paramGroup);
        updater.addMust("paramKey", paramKey);
        updater.addField("paramValue", paramValue);
        
        RE<Integer> result = ORM.tabler().update(ZmrParam.class, updater);
        return result.success()?R1.correct():R1.error(result.exception());
    }
}