import Ignore_ExecPageFlipper as ExecPageFlipper
import com.sdtool.business.dao.DocDao
import com.sdtool.common.api.NoSqlKeysService
import com.sdtool.common.datas.RedisKeys
import com.sdtool.common.entity.doc.BusinessScript
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.commons.lang3.StringUtils
import org.rex.RMap

static def createRunnable(String runEnvironment, RedisService redisService, NoSqlKeysService keysService, DocDao docDao, JsonConvert jsonConvert, String dataSourceId, long supplierCode) {
    return {

        if (!StringUtils.equalsIgnoreCase(runEnvironment, "dev")) {
            redisService.del(keysService.getRedisKey(RedisKeys.KEY_BUSINESSSCRIPT, supplierCode))
        }


        ExecPageFlipper.execute(300, { PageFlipper p ->
            RMap scriptParams = ERPUtils.ofRMap("voidFlag", -1, "businessModule", -1)
            List<BusinessScript> scriptList = docDao.selectBusinessScript(scriptParams, dataSourceId, supplierCode, p)
            Map<String, String> redisMap = scriptList.collectEntries { [it.businessCode, jsonConvert.convertTo(it)] }
            if (!redisMap.isEmpty()) {
                redisService.hmset(keysService.getRedisKey(RedisKeys.KEY_BUSINESSSCRIPT, supplierCode), redisMap)
            }
        })
    } as Runnable
}

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



