import com.sdtool.business.dao.MallDao
import com.sdtool.common.api.BusinessExecutor
import com.sdtool.common.api.NoSqlKeysService
import com.sdtool.common.api.mall.MallAdminService
import com.sdtool.common.datas.ERPModule
import com.sdtool.common.datas.RedisKeys
import com.sdtool.common.entity.base.InvokeCallParams
import com.sdtool.common.entity.base.InvokeCallResult
import com.sdtool.common.entity.mall.MallUser
import com.dySweetFishPlugin.redis.RedisService
import com.dySweetFishPlugin.sql.dao.OperatorWait
import com.dySweetFishPlugin.sql.dao.TunaService
import com.dySweetFishPlugin.tool.lang.DateUtil
import com.sweetfish.convert.json.JsonConvert
import com.sweetfish.service.RetResult
import groovy.json.JsonSlurper
import org.apache.commons.lang3.StringUtils

import javax.annotation.Resource

/**
 * @Author: xl Created on 2024-05-11
 * @Content:
 */
class BE_MallUser_SetExpire implements BusinessExecutor<InvokeCallParams, InvokeCallResult> {

    @Resource
    TunaService tunaService

    @Resource
    JsonConvert jsonConvert

    @Resource
    RedisService redisService

    @Resource
    NoSqlKeysService keysService

    @Resource
    MallAdminService mallAdminService

    private MallDao mallDao

    @Override
    String scriptName() {
        return "商城客户-修改此客户过期时间"
    }

    @Override
    ERPModule module() {
        return ERPModule.MALL_ADMIN
    }

    @Override
    OperatorWait getAWait(InvokeCallParams s) {
        return OperatorWait.ASNYC
    }

    @Override
    boolean needLogin(InvokeCallParams source) {
        return true
    }

    @Override
    void start(long supplierCode) {
        mallDao = tunaService.generate(MallDao.class)
    }

    @Override
    RetResult<InvokeCallParams> checkExecute(InvokeCallParams source) {
        JsonSlurper jsonSlurper = new JsonSlurper()
        def invokeData = jsonSlurper.parseText(source.params)
        String ids = invokeData["ids"]
        String expireDate = invokeData["expireDate"] as String

        if (StringUtils.isBlank(ids)) {
            return RetResult.<InvokeCallParams> errorT().retinfo("没有传入用户id")
        }

        if (StringUtils.isBlank(expireDate)) {
            return RetResult.<InvokeCallParams> errorT().retinfo("没有传入过期时间")
        }

        return RetResult.<InvokeCallParams> successT().result(source)
    }

    RetResult<InvokeCallResult> execute(InvokeCallParams source) {
        JsonSlurper jsonSlurper = new JsonSlurper()
        def invokeData = jsonSlurper.parseText(source.params)
        String ids = invokeData["ids"]
        String expireDate = invokeData["expireDate"] as String


        List<MallUser> updateList = mallAdminService.getRedisMallUserList(ids.split(","), source.supplierCode).findAll { it != null }
        updateList.each { mallUser ->
            Date newDate = DateUtil.getStartOfDay(DateUtil.asDate(expireDate, "yyyy-MM-dd"))
            mallUser.levelExpireDateLong = newDate.getTime()
            mallUser.levelExpireDate = newDate
        }


        int iData = mallDao.setMallUserExpire(updateList, source.dataSourceId, source.supplierCode)
        if (iData > 0) {
            Map<String, String> redisMap = updateList.collectEntries { [String.valueOf(it.id), jsonConvert.convertTo(it)] }
            redisService.hmset(keysService.getRedisKey(RedisKeys.KEY_ERP_MALL_User, source.supplierCode), redisMap)

            return RetResult.<InvokeCallResult> successT()
        }

        return RetResult.<InvokeCallResult> errorT().retinfo("修改用户过期时间失败")
    }
}

