import com.sdtool.business.dao.MallDao
import com.sdtool.common.api.BusinessExecutor
import com.sdtool.common.api.NoSqlKeysService
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.sdtool.common.entity.mall.MallUserLevel
import com.dySweetFishPlugin.redis.RedisService
import com.dySweetFishPlugin.sql.TableIdService
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 org.apache.commons.lang3.StringUtils

import javax.annotation.Resource

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

    @Resource
    JsonConvert jsonConvert

    @Resource
    TableIdService tableIdService

    @Resource
    RedisService redisService

    @Resource
    NoSqlKeysService keysService

    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"]
        long idLevel = invokeData["idLevel"] as long

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

        if (idLevel <= 0) {
            return RetResult.<InvokeCallParams>errorT().retinfo("没有传入级别id")
        }

        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"]
        long idLevel = invokeData["idLevel"] as long

        Map<String, String> docMap = redisService.hgetAll(keysService.getRedisKey(RedisKeys.KEY_ERP_MALL_User, source.supplierCode))
        List<MallUser> userList = new ArrayList<>()
        docMap.forEach((key, value) -> userList.add(jsonConvert.convertFrom(MallUser.class, value)))

        List<MallUser> updateList = new ArrayList<>()
        userList.each {
            if (!("," + ids + ",").contains("," + it.id + ",")) {
                it.idLevel = idLevel
                updateList.add(it)
            }
        }

        int iData = mallDao.setMallUserIdLevel(userList,source.dataSourceId,source.supplierCode)
        if (iData > 0) {
//            updateList.each {
//                redisService.hset(keysService.getRedisKey(RedisKeys.KEY_ERP_MALL_User,source.supplierCode),
//                        String.valueOf(it.id),jsonConvert.convertTo(it))
//            }

            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("修改用户级别失败")
    }
}
