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.PosterLocation
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 javax.annotation.Resource

/**
 * @Author: xl Created on 2024-04-01
 * @Content:
 */
class BE_PosterLocation_Add 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
    void start(long supplierCode) {
        mallDao = tunaService.generate(MallDao.class)
    }

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

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

    @Override
    RetResult<InvokeCallResult> execute(InvokeCallParams source) {
        PosterLocation posterLocation = jsonConvert.convertFromO(PosterLocation.class, source.params)
        posterLocation.id = tableIdService.getTableIdMulti("maPosterLocation.id", 1, source.dataSourceId, String.valueOf(source.supplierCode))
        PosterLocation.create(posterLocation, source.currentUser.id)
        PosterLocation.update(posterLocation, source.currentUser.id)

        int iData = mallDao.addPosterLocation(posterLocation, source.dataSourceId, source.supplierCode)
        if (iData > 0) {
            redisService.hset(keysService.getRedisKey(RedisKeys.KEY_ERP_MALL_POSTER_LOCATION, source.supplierCode),
                    String.valueOf(posterLocation.id), jsonConvert.convertTo(posterLocation)
            )

            return RetResult.<InvokeCallResult> successT().result(InvokeCallResult.success().data(jsonConvert.convertTo(posterLocation)))
        } else RetResult.error().retinfo("增加商城广告位失败")
    }
}
