import com.sdtool.business.dao.MallDao
import com.sdtool.common.api.NoSqlKeysService
import com.sdtool.common.datas.RedisKeys
import com.sdtool.common.entity.mall.MallUser
import com.sdtool.common.entity.system.SocialUser
import com.sdtool.common.tool.ERPUtils
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-03-27
 * @Content:
 */

static def createRunnable(RedisService redisService, NoSqlKeysService keysService,
                          MallDao mallDao, Logger logger, JsonConvert jsonConvert, String dataSourceId, long supplierCode) {
    RMap params = ERPUtils.ofRMap("voidFlag", -1)
    return {
        redisService.del(keysService.getRedisKey(RedisKeys.KEY_ERP_MALL_User, supplierCode))
        Ignore_ExecPageFlipper.execute(1000, { PageFlipper p ->
            List<MallUser> mallUserList = mallDao.queryMallUserList(params, p, dataSourceId, supplierCode)
            if (!mallUserList.isEmpty()) {
                Map<String, String> redisMap = mallUserList.collectEntries { [String.valueOf(it.getId()), jsonConvert.convertTo(it)] }
                redisService.hmset(keysService.getRedisKey(RedisKeys.KEY_ERP_MALL_User, supplierCode), redisMap)
                logger.info("商城用户共" + mallUserList.size() + "条记录")
            }
        })

        redisService.del(keysService.getRedisKey(RedisKeys.KEY_ERP_MALL_SOCIAL_User, supplierCode))
        Ignore_ExecPageFlipper.execute(1000, { PageFlipper p ->
            List<SocialUser> socialUserList = mallDao.queryMallSocialUser(params, p, dataSourceId, supplierCode)
            if (!socialUserList.isEmpty()) {
                Map<String, String> redisMap = socialUserList.collectEntries { [it.userOpenId, jsonConvert.convertTo(it)] }
                redisService.hmset(keysService.getRedisKey(RedisKeys.KEY_ERP_MALL_SOCIAL_User, supplierCode), redisMap)
                logger.info("商城三方绑定用户共" + socialUserList.size() + "条记录")
            }
        })
    } as Runnable
}

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