package com.sdtool.business.service.mall;

import com.sdtool.common.api.NoSqlKeysService;
import com.sdtool.common.api.mall.MallAdminService;
import com.sdtool.common.base.BaseService;
import com.sdtool.common.datas.RedisKeys;
import com.sdtool.common.entity.base.InvokeCallParams;
import com.sdtool.common.entity.base.InvokeCallResult;
import com.sdtool.common.entity.mall.MallUser;
import com.sdtool.common.entity.mall.MallUserLevel;
import com.sdtool.common.entity.site.ERPTokenUser;
import com.sdtool.common.tool.ERPUtils;
import com.sweetfish.service.Local;
import com.sweetfish.service.RetResult;
import com.sweetfish.util.AutoLoad;
import com.sweetfish.util.ResourceType;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: xl Created on 2024-03-26
 * @Content:
 */
@Local
@AutoLoad(false)
@ResourceType(MallAdminService.class)
public class MallAdminServiceImpl extends BaseService implements MallAdminService {

    @Resource
    NoSqlKeysService keysService;

    @Override
    public RetResult<InvokeCallResult> callMall(InvokeCallParams source, ERPTokenUser currentUser, String dataSourceId, long supplierCode) {
        if (source == null) {
            return RetResult.<InvokeCallResult>errorT().retinfo("无效的参数信息");
        }

        if (StringUtils.isEmpty(source.getBusinessMethod())) {
            return RetResult.<InvokeCallResult>errorT().retinfo("无效的参数方法[businessMethod]信息");
        }

        source.setCurrentUser(currentUser);
        source.setDataSourceId(dataSourceId);
        source.setSupplierCode(supplierCode);

        return callScript(source.getBusinessMethod(), source, currentUser, dataSourceId, supplierCode);
    }

    @Override
    public MallUser getRedisMallUser(long userId, long supplierCode) {
        return getRedisDocInfo(keysService.getRedisKey(RedisKeys.KEY_ERP_MALL_User, supplierCode), String.valueOf(userId), MallUser.class);
    }

    public List<MallUser> getRedisMallUserList(String[] userIds, long supplierCode) {
        return getRedisDocInfos(MallUser.class, keysService.getRedisKey(RedisKeys.KEY_ERP_MALL_User, supplierCode), userIds);
    }

    @Override
    public MallUserLevel getRedisMallUserLevel(long id, long supplierCode) {
        return getRedisDocInfo(keysService.getRedisKey(RedisKeys.KEY_ERP_MALL_USER_LEVEL, supplierCode), String.valueOf(id), MallUserLevel.class);
    }

    public List<MallUserLevel> getRedisMallUserLevelList(long[] ids, long supplierCode) {
        return getRedisDocInfos(MallUserLevel.class, keysService.getRedisKey(RedisKeys.KEY_ERP_MALL_USER_LEVEL, supplierCode), ERPUtils.longArrayToStrArray(ids));
    }

    @Override
    public MallUserLevel getMallUserLevelInfo(long userId, long supplierCode) {
        MallUser mallUser = this.getRedisMallUser(userId,supplierCode);
        if (mallUser == null || mallUser.getIdLevel() <= 0) {
            return null;
        }
        return this.getRedisMallUserLevel(mallUser.getIdLevel(),supplierCode);
    }
}
