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.MallUser
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-03-27
 * @Content:
 */
class BE_MallAdmin_MallUserList implements BusinessExecutor<InvokeCallParams, InvokeCallResult> {

    @Resource
    TunaService tunaService

    @Resource
    JsonConvert jsonConvert

    @Resource
    MallAdminService mallAdminService

    private MallDao mallDao

    @Override
    String scriptName() {
        return "商城后台8080-获取注册用户列表"
    }

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

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

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

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

    @Override
    RetResult<InvokeCallResult> execute(InvokeCallParams source) {
        RMap params
        if (StringUtils.isBlank(source.params)) {
            params = new RMap()
        } else {
            params = jsonConvert.convertFrom(RMap.class, source.params)
        }

        List<MallUser> mallUserList = mallDao.queryMallUserList(params, source.page, source.dataSourceId, source.supplierCode)
        mallUserList.each {
            it.password = ""
            it.levelName = mallAdminService.getRedisMallUserLevel(it.idLevel, source.supplierCode)?.levelName ?: ""
        }

//        select a.id, a.userName, a.loginAccount,
//                (select count(b.id) from moquoteorder1000_current b where b.idClient = a.id) as quoteCount,
//        (select count(c.id) from moprintorder1000_current c where c.idClient = a.id) as orderCount
//        from mamalluser1000 a

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