package com.kmall.admin.utils;

import com.kmall.common.constant.Dict;
import com.kmall.common.entity.SysUserEntity;
import com.kmall.common.utils.RRException;
import com.kmall.common.utils.ShiroUtils;

import java.util.Map;

/**
 * 参数工具类
 */
public class ParamUtils {

    /**
     * 根据用户类型设置查询权限
     *
     * @param params   查询参数
     * @param storeKey 当用户是门店时,放入的key,值为门店id
     * @param merchKey 当用户是商户时,放入的key,值为商户编号
     * @param bothSet  为true时,不管是门店还是商户都只设置storeKey和merchKey中不为null的值
     */
    public static void setQueryPowerByRoleType(Map params, String storeKey, String merchKey, boolean bothSet) {
        SysUserEntity user = ShiroUtils.getUserEntity();
        if (user == null) {
            throw new RRException("用户登录超时，请重新登录");
        }

        if (bothSet) {
            if (Dict.roleType.item_2.getItem().equals(user.getRoleType()) ||
                Dict.roleType.item_3.getItem().equals(user.getRoleType())) {
                if (storeKey == null && merchKey == null) {
                    throw new RRException("参数错误");
                }

                if (storeKey != null && merchKey != null) {
                    throw new RRException("参数错误");
                }

                if (storeKey != null) {
                    params.put(storeKey, user.getStoreId());
                } else if (merchKey != null) {
                    params.put(merchKey, user.getMerchSn());
                }
            }
        } else {
            if (Dict.roleType.item_2.getItem().equals(user.getRoleType())) {
                params.put(storeKey, user.getStoreId());
            } else if (Dict.roleType.item_3.getItem().equals(user.getRoleType())) {
                params.put(merchKey, user.getMerchSn());
            }
        }
    }
}

