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.LoginUser
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_ERP_ONLINEUSER, supplierCode))
        redisService.del(keysService.getRedisKey(RedisKeys.KEY_ERP_LOGINUSER, supplierCode))

        RMap tmpParams = ERPUtils.ofRMap("status", -1)

        ExecPageFlipper.execute(500, { PageFlipper p ->
            List<LoginUser> loginUserList = systemDao.queryLoginUsers(tmpParams, p, dataSourceId, supplierCode);

            if (!loginUserList.isEmpty()) {
                Map<String, String> redisMap = loginUserList.collectEntries { [String.valueOf(it.getId()), jsonConvert.convertTo(it)] }
                redisService.hmset(keysService.getRedisKey(RedisKeys.KEY_ERP_LOGINUSER, supplierCode), redisMap)
            }
        })

        //默认一个菜单管理，防止管理员被修改
        LoginUser resourceUser = new LoginUser()
        resourceUser.setId(-999L)
        resourceUser.setLoginName("SuperResourcer")
        resourceUser.setUserName("资源管理员")
        resourceUser.setDeptName("资源管理")
        resourceUser.setStatus(0)
        redisService.hset(keysService.getRedisKey(RedisKeys.KEY_ERP_LOGINUSER, supplierCode), String.valueOf(resourceUser.getId()), resourceUser);
    } as Runnable
}

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