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.sweetfish.convert.json.JsonConvert
import com.sweetfish.service.RetResult
import groovy.json.JsonSlurper

import javax.annotation.Resource

class BE_MallUser_Void 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.SYNC
    }

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

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

    RetResult<InvokeCallResult> execute(InvokeCallParams source) {
        def jsonSlurper = new JsonSlurper()
        def invokeData = jsonSlurper.parseText(source.params)
        long idMallUser = invokeData["id"] as long
        int voidFlag = invokeData["voidFlag"] as int

        MallUser mallUser = mallAdminService.getRedisMallUser(idMallUser, source.supplierCode)
        if (mallUser == null) {
            return RetResult.<InvokeCallResult> errorT().retinfo("无效的用户信息")
        }

        mallUser.voidFlag = voidFlag

        redisService.hset(keysService.getRedisKey(RedisKeys.KEY_ERP_MALL_User, source.supplierCode), String.valueOf(mallUser.getId()), mallUser)

        mallDao.voidMallLoginUser(mallUser, source.dataSourceId, source.supplierCode)

        return RetResult.<InvokeCallResult> successT().result(InvokeCallResult.success())
    }
}


