import com.sdtool.common.api.BusinessExecutor
import com.sdtool.common.api.NoSqlKeysService
import com.sdtool.common.datas.ERPModule
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.MallUserLevel
import com.alibaba.fastjson2.JSON
import com.dySweetFishPlugin.redis.RedisService
import com.dySweetFishPlugin.sql.dao.OperatorWait
import com.sweetfish.convert.json.JsonConvert
import com.sweetfish.service.RetResult

import javax.annotation.Resource

/**
 * @Author: xl Created on 2024-05-11
 * @Content:
 */
class BE_MallUserLevel_ListNoPage implements BusinessExecutor<InvokeCallParams, InvokeCallResult> {

    @Resource
    JsonConvert jsonConvert

    @Resource
    RedisService redisService

    @Resource
    NoSqlKeysService keysService

    @Override
    String scriptName() {
        return "用户级别-增加"
    }

    @Override
    ERPModule module() {
        return ERPModule.MALL_ADMIN
    }

    @Override
    OperatorWait getAWait(InvokeCallParams s) {
        return OperatorWait.AWAIT
    }

    @Override
    boolean needLogin(InvokeCallParams source) {
        return true
    }

    RetResult<InvokeCallResult> execute(InvokeCallParams source) {
        Map<String, String> docMap = redisService.hgetAll(keysService.getRedisKey(RedisKeys.KEY_ERP_MALL_USER_LEVEL, source.supplierCode))
        List<MallUserLevel> resultList = new ArrayList<>()
        docMap.forEach((key, value) -> resultList.add(jsonConvert.convertFrom(MallUserLevel.class, value)))

        def returnDef = resultList.findAll { it.voidFlag == 0 }.collect {
            [
                    id           : it.id,
                    levelName    : it.levelName,
                    levelValue   : it.levelValue,
                    checkFileFlag: it.checkFileFlag
            ]
        }.sort{it["id"]}

        return RetResult.<InvokeCallResult> successT().result(InvokeCallResult.success().data(JSON.toJSONString(returnDef)))
    }
}
