import com.sdtool.business.dao.MallDao
import com.sdtool.common.api.BusinessExecutor
import com.sdtool.common.api.mall.MallAdminService
import com.sdtool.common.datas.ERPModule
import com.sdtool.common.entity.base.InvokeCallParams
import com.sdtool.common.entity.base.InvokeCallResult
import com.sdtool.common.entity.mall.MallUserLevel
import com.alibaba.fastjson2.JSON
import com.dySweetFishPlugin.sql.dao.OperatorWait
import com.dySweetFishPlugin.sql.dao.TunaService
import com.sweetfish.convert.json.JsonConvert
import com.sweetfish.service.RetResult
import org.apache.commons.lang3.StringUtils
import org.rex.RMap

import javax.annotation.Resource

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

    @Resource
    TunaService tunaService

    @Resource
    JsonConvert jsonConvert

    @Resource
    MallAdminService mallAdminService

    private MallDao mallDao

    @Override
    String scriptName() {
        return "用户级别-列表"
    }

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

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

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

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

    RetResult<InvokeCallResult> execute(InvokeCallParams source) {
        RMap searchData
        if (StringUtils.isBlank(source.params)) {
            searchData = new RMap()
        } else {
            searchData = jsonConvert.convertFrom(RMap.class, source.params)
        }
        List<MallUserLevel> userLevelList = mallDao.queryMallUserLevel(searchData, source.page, source.dataSourceId, source.supplierCode)

        QT.simpleUpdateListField(userLevelList, "idDowngradeLevel", mallAdminService.&getRedisMallUserLevelList, "id", "downgradeLevelName", "levelName", "", source.supplierCode)

        def returnDef = userLevelList.collect {
            [
                    id                 : it.id,
                    levelName          : it.levelName,
                    levelValue         : it.levelValue,
                    voidFlag           : it.voidFlag,
                    checkFileFlag      : it.checkFileFlag,
                    viewPriceDetailFlag: it.viewPriceDetailFlag,
                    expireDays         : it.expireDays,
                    idDowngradeLevel   : it.idDowngradeLevel,
                    downgradeLevelName : it.downgradeLevelName,
                    createTime         : it.createTime,
            ]
        }

        return RetResult.<InvokeCallResult> successT().result(InvokeCallResult.success().data(JSON.toJSONString(returnDef)).page(source.page))
    }

}
