import Ignore_ExecPageFlipper as ExecPageFlipper
import com.sdtool.business.dao.SystemDao
import com.sdtool.common.api.NoSqlKeysService
import com.sdtool.common.datas.RedisKeys
import com.sdtool.common.entity.system.DictData
import com.sdtool.common.entity.system.DictType
import com.sdtool.common.tool.ERPUtils
import com.dySweetFishPlugin.redis.RedisService
import com.sweetfish.convert.json.JsonConvert
import com.sweetfish.source.PageFlipper
import org.rex.RMap

static def createRunnable(RedisService redisService, NoSqlKeysService keysService, SystemDao systemDao, JsonConvert jsonConvert, String dataSourceId, long supplierCode) {
    return {
        redisService.del(keysService.getRedisKey(RedisKeys.KEY_ERPDICTTYPE, supplierCode))

        RMap dictTypeParams = ERPUtils.ofRMap("voidFlag", -1)
        ExecPageFlipper.execute(500, { PageFlipper p ->

            List<DictType> dictTypeList = systemDao.queryDictTypes(dictTypeParams, dataSourceId, supplierCode, p)
            Map<String, String> dictTypeMap = dictTypeList.collectEntries { [String.valueOf(it.getId()), jsonConvert.convertTo(it)] }

            if (!dictTypeMap.isEmpty()) {
                redisService.hmset(keysService.getRedisKey(RedisKeys.KEY_ERPDICTTYPE, supplierCode), dictTypeMap)
            }

            dictTypeList.each { x ->
                RMap dictParams = ERPUtils.ofRMap("idType", x.getId(), "voidFlag", -1)
                redisService.del(keysService.getRedisKey(RedisKeys.KEY_ERPDICTDATA, supplierCode, true) + x.getId())
                ExecPageFlipper.execute(500, { PageFlipper p1 ->
                    List<DictData> dictDataList = systemDao.queryDictDatas(dictParams, dataSourceId, supplierCode, p1)
                    Map<String, String> dictDataMap = dictDataList.collectEntries { [String.valueOf(it.getId()), jsonConvert.convertTo(it)] }
                    if (!dictDataMap.isEmpty()) {
                        redisService.hmset(keysService.getRedisKey(RedisKeys.KEY_ERPDICTDATA, supplierCode, true) + x.getId(), dictDataMap)
                    }
                })
            }
        })
    } as Runnable
}

//groovy最后一个表达式的值为返回
createRunnable(redisService, keysService, systemDao, jsonConvert, dataSourceId, supplierCode)



