import com.sdtool.common.api.*
import com.sdtool.common.api.mall.MallAdminService
import com.sdtool.common.api.mall.MallApiCallService
import com.sdtool.common.api.mall.MallFrontService

import com.sdtool.common.datas.ERPModule
import com.sdtool.common.datas.ERPModuleEntity
import com.sdtool.common.entity.base.BusinessOperation
import com.sdtool.common.entity.base.ProcessEntityItem
import com.sweetfish.service.RetResult
import org.apache.logging.log4j.LogManager
import org.apache.logging.log4j.Logger

import javax.annotation.Resource

/**
 * Created by jlutt on 2020-08-25
 *
 * @author jlutt
 */
@SuppressWarnings("unused")
class BE_DocClearScriptCache implements BusinessExecutor<ProcessEntityItem<ERPModuleEntity>, ERPModuleEntity> {

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

    @Resource
    SystemService systemService

    @Resource
    DocInfoService docInfoService

    @Resource
    MallAdminService mallAdminService

    @Resource
    MallFrontService mallFrontService

    @Resource
    ConfigService configService

    @Resource
    LoginService loginService

    @Resource
    AddressService addressService

    @Resource
    MallApiCallService mallApiCallService

    @Resource
    DataSyncService dataSyncService

    @Override
    String scriptName() {
        return "保存业务脚本清除对象"
    }

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

    @Override
    RetResult<ERPModuleEntity> execute(ProcessEntityItem<ERPModuleEntity> source) {
        ERPModule erpModule = source.inputItem.erpModule

        logger.info("修改模块" + erpModule.moduleDisplayName + source.inputItem.code)

        if ("Init_ExpandoGroovy".equalsIgnoreCase(source.inputItem.code)) {
            //Init_ExpandoGroovy每个使用脚本的service都载入了，由于是默认载入的，而且是启动自动载入的，所以不提供清除的方法，而是每个都重载一次
            //2024-01-26未测试这个重载能不能不重启给groovy增加扩展方法，需要测试，不过理论上没啥用，因为Init_ExpandoGroovy的脚本是用的其它groovy代码实现的，先这样吧，留着以后处理，暂时不经常修改GroovyExtensions代码算了
            systemService.reloadScriptCache(source.inputItem.code, erpModule, source.dataSourceId, source.supplierCode)
            docInfoService.reloadScriptCache(source.inputItem.code, erpModule, source.dataSourceId, source.supplierCode)

            if (mallAdminService != null) {
                mallAdminService.reloadScriptCache(source.inputItem.code, erpModule, source.dataSourceId, source.supplierCode)
            }
            if (mallFrontService != null) {
                mallFrontService.reloadScriptCache(source.inputItem.code, erpModule, source.dataSourceId, source.supplierCode)
            }

            if (configService != null) {
                configService.reloadScriptCache(source.inputItem.code, erpModule, source.dataSourceId, source.supplierCode)
            }
            if (loginService != null) {
                loginService.reloadScriptCache(source.inputItem.code, erpModule, source.dataSourceId, source.supplierCode)
            }

            if (addressService != null) {
                addressService.reloadScriptCache(source.inputItem.code, erpModule, source.dataSourceId, source.supplierCode)
            }

            if (mallApiCallService != null) {
                mallApiCallService.reloadScriptCache(source.inputItem.code, erpModule, source.dataSourceId, source.supplierCode)
            }

            if (dataSyncService != null) {
                dataSyncService.reloadScriptCache(source.inputItem.code, erpModule, source.dataSourceId, source.supplierCode)
            }

        } else {
            switch (erpModule) {
                case ERPModule.NONE:
                    break
                case ERPModule.SYSTEM:
                    if (source.operation == BusinessOperation.SAVE) {
                        systemService.invalidateScriptCache(source.supplierCode + "-" + source.inputItem.code)
                    } else {
                        systemService.reloadScriptCache(source.inputItem.code, erpModule, source.dataSourceId, source.supplierCode)
                    }
                    break
                case [ERPModule.DOC, ERPModule.INQUIREINFO]:
                    if (source.operation == BusinessOperation.SAVE) {
                        docInfoService.invalidateScriptCache(source.supplierCode + "-" + source.inputItem.code)
                    } else {
                        docInfoService.reloadScriptCache(source.inputItem.code, erpModule, source.dataSourceId, source.supplierCode)
                    }
                    break
                case ERPModule.CONFIG:
                    if (source.operation == BusinessOperation.SAVE) {
                        configService.invalidateScriptCache(source.supplierCode + "-" + source.inputItem.code)
                    } else {
                        configService.reloadScriptCache(source.inputItem.code, erpModule, source.dataSourceId, source.supplierCode)
                    }
                    break
                case ERPModule.LOGIN:
                    if (source.operation == BusinessOperation.SAVE) {
                        loginService.invalidateScriptCache(source.supplierCode + "-" + source.inputItem.code)
                    } else {
                        loginService.reloadScriptCache(source.inputItem.code, erpModule, source.dataSourceId, source.supplierCode)
                    }
                    break
                case [ERPModule.EXPORT, ERPModule.CHARTS]:
                    if (source.operation == BusinessOperation.SAVE) {
                        systemService.invalidateScriptCache(source.supplierCode + "-" + source.inputItem.code)
                    } else {
                        systemService.reloadScriptCache(source.inputItem.code, erpModule, source.dataSourceId, source.supplierCode)
                    }
                    break
                case ERPModule.ADDRESSPARSER:
                    if (source.operation == BusinessOperation.SAVE) {
                        addressService.invalidateScriptCache(source.supplierCode + "-" + source.inputItem.code)
                    } else {
                        addressService.reloadScriptCache(source.inputItem.code, erpModule, source.dataSourceId, source.supplierCode)
                    }
                    break
                case ERPModule.APP:
                    break
                case ERPModule.MALL_ADMIN:
                    if (source.operation == BusinessOperation.SAVE) {
                        mallAdminService.invalidateScriptCache(source.supplierCode + "-" + source.inputItem.code)
                    } else {
                        mallAdminService.reloadScriptCache(source.inputItem.code, erpModule, source.dataSourceId, source.supplierCode)
                    }
                    break
                case ERPModule.MALL_FRONT:
                    if (source.operation == BusinessOperation.SAVE) {
                        mallFrontService.invalidateScriptCache(source.supplierCode + "-" + source.inputItem.code)
                    } else {
                        mallFrontService.reloadScriptCache(source.inputItem.code, erpModule, source.dataSourceId, source.supplierCode)
                    }
                    break
                case ERPModule.MALL_CALL:
                    if (mallApiCallService != null) {
                        if (source.operation == BusinessOperation.SAVE) {
                            mallApiCallService.invalidateScriptCache(source.supplierCode + "-" + source.inputItem.code)
                        } else {
                            mallApiCallService.reloadScriptCache(source.inputItem.code, erpModule, source.dataSourceId, source.supplierCode)
                        }
                    }
                    break
                case ERPModule.DATA_SYNC:
                    if (dataSyncService != null) {
                        if (source.operation == BusinessOperation.SAVE) {
                            dataSyncService.invalidateScriptCache(source.supplierCode + "-" + source.inputItem.code)
                        } else {
                            dataSyncService.reloadScriptCache(source.inputItem.code, erpModule, source.dataSourceId, source.supplierCode)
                        }
                    }
                    break
                case ERPModule.MALL_MINI:
                    if (mallMiniService != null) {
                        if (source.operation == BusinessOperation.SAVE) {
                            mallMiniService.invalidateScriptCache(source.supplierCode + "-" + source.inputItem.code)
                        } else {
                            mallMiniService.reloadScriptCache(source.inputItem.code, erpModule, source.dataSourceId, source.supplierCode)
                        }
                    }
                    break
                default:
                    break
            }
        }

        return RetResult.success().result(source.inputItem)
    }
}
