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
import java.time.Instant

class BE_MallUser_Verify 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 verifyFlag = invokeData["verifyFlag"] as int

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

        mallUser.verifyFlag = verifyFlag
        mallUser.idVerifyUser = source.currentUser.id
        mallUser.verifyUser = source.currentUser.userName
        mallUser.verifyDate = Date.from(Instant.now())
        mallUser.verifyDateLong = mallUser.verifyDate.time

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

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

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



