import com.sdtool.common.api.BusinessExecutor
import com.sdtool.common.api.ERPService
import com.sdtool.common.api.NoSqlKeysService
import com.sdtool.common.datas.ERPModule
import com.sdtool.common.datas.RedisKeys
import com.sdtool.common.entity.base.DataBaseMultiItemEx
import com.sdtool.common.entity.base.ProcessERPClientItem
import com.sdtool.common.entity.base.ProcessStringItem
import com.dySweetFishPlugin.redis.RedisService
import com.dySweetFishPlugin.sql.DBService
import com.sweetfish.service.RetResult
import org.apache.logging.log4j.LogManager
import org.apache.logging.log4j.Logger
import org.rex.DB
import org.rex.RMap

import javax.annotation.Resource

@SuppressWarnings("unused")
class BE_System_Void_ERPClient implements BusinessExecutor<ProcessERPClientItem, ProcessStringItem> {

    protected final Logger logger = LogManager.getLogger(this.getClass().getSimpleName())

    @Resource
    private ERPService erpService

    @Resource
    private DBService dbService

    @Resource
    private RedisService redisService

    @Resource
    private NoSqlKeysService keysService

    @Override
    String scriptName() {
        return "启用禁用SAAS商户"
    }

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

    RetResult<ProcessStringItem> execute(ProcessERPClientItem source) {
        //源头只需要传递itemCode和voidFlag即可
        DataBaseMultiItemEx dataItem = source.dataItem

        DataBaseMultiItemEx srcItem = erpService.getERPSupplierInfoByCode(dataItem.getItemCode())
        if (srcItem == null) {
            return RetResult.<ProcessStringItem> errorT().retinfo("无效的商户信息")
        }

        //禁用后需要通过BaseServlet之类的去判断最好，下次启动会删掉注入的脚本之类的，数据暂时保留

        srcItem.voidFlag = dataItem.voidFlag

        RMap dataParams = new RMap()
        dataParams.put("id", srcItem.id)
        dataParams.put("voidFlag", srcItem.voidFlag)

        DB.update("update sys_DataBaseMultiItem set voidFlag = #{voidFlag} where id = #{id}", dataParams)

        redisService.hset(keysService.getRedisKey(RedisKeys.KEY_SUPPLIER_PLATFORM), String.valueOf(srcItem.id), srcItem)
        redisService.hset(keysService.getRedisKey(RedisKeys.KEY_SUPPLIER_SHARDING_KEY_PLATFORM), srcItem.shardingKey, srcItem)
        redisService.hset(keysService.getRedisKey(RedisKeys.KEY_SUPPLIER_ITEM_CODE_PLATFORM), srcItem.itemCode, srcItem)

        return RetResult.<ProcessStringItem> successT()
    }
}


