import com.sdtool.business.dao.MallDao
import com.sdtool.common.api.NoSqlKeysService
import com.sdtool.common.datas.RedisKeys
import com.sdtool.common.entity.mall.MallUserLevel
import com.dySweetFishPlugin.redis.RedisService
import com.sweetfish.convert.json.JsonConvert
import com.sweetfish.source.PageFlipper
import org.apache.logging.log4j.Logger
import org.rex.RMap

/**
 * @Author: xl Created on 2024-05-11
 * @Content:
 */

static def createRunnable(RedisService redisService, NoSqlKeysService keysService,
                          MallDao mallDao, Logger logger, JsonConvert jsonConvert, String dataSourceId, long supplierCode) {
    RMap params = new RMap()
    return {
        redisService.del(keysService.getRedisKey(RedisKeys.KEY_ERP_MALL_USER_LEVEL, supplierCode))
        Ignore_ExecPageFlipper.execute(1000, { PageFlipper p ->
            List<MallUserLevel> mallUserLevelList = mallDao.queryMallUserLevel(params, p, dataSourceId, supplierCode)
            if (!mallUserLevelList.isEmpty()) {
                Map<String, String> redisMap = mallUserLevelList.collectEntries { [String.valueOf(it.getId()), jsonConvert.convertTo(it)] }
                redisService.hmset(keysService.getRedisKey(RedisKeys.KEY_ERP_MALL_USER_LEVEL, supplierCode), redisMap)
                logger.info("购物商城用户级别共" + mallUserLevelList.size() + "条记录")
            }
        })
    } as Runnable
}

createRunnable(redisService, keysService, mallDao, logger, jsonConvert, dataSourceId, supplierCode)