import com.sdtool.business.dao.DocDao
import com.sdtool.common.api.NoSqlKeysService
import com.sdtool.common.datas.RedisKeys
import com.sdtool.common.entity.system.CompanyInfo
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


static def createRunnable(RedisService redisService, NoSqlKeysService keysService,
                          DocDao docDao, Logger logger, JsonConvert jsonConvert, String dataSourceId, long supplierCode) {
    return {
        redisService.del(keysService.getRedisKey(RedisKeys.KEY_COMPANY_INFO, supplierCode))
        Ignore_ExecPageFlipper.execute(1000, { PageFlipper p ->
            RMap params = new RMap()
            params.put("voidFlag", -1)
            List<CompanyInfo> companyInfoList = docDao.selectCompanyInfo(params, dataSourceId, supplierCode, p)
            if (!companyInfoList.isEmpty()) {
                Map<String, String> redisMap = companyInfoList.collectEntries { [String.valueOf(it.getId()), jsonConvert.convertTo(it)] }
                redisService.hmset(keysService.getRedisKey(RedisKeys.KEY_COMPANY_INFO, supplierCode), redisMap)
                logger.info("工厂档案,共" + companyInfoList.size() + "条记录")
            }
        })
    } as Runnable
}

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