import com.sdtool.common.api.BusinessExecutor
import com.sdtool.common.api.LoginUserService
import com.sdtool.common.api.mall.MallAdminService

import com.sdtool.common.datas.ERPModule
import com.sdtool.common.entity.base.BaseEntity
import com.sdtool.common.entity.base.ProcessEntityItem
import com.sdtool.common.entity.mall.MallUser
import com.sdtool.common.entity.site.ERPTokenUser
import com.sdtool.common.entity.site.LoginResult
import com.sdtool.common.entity.system.LoginUser
import com.sdtool.common.entity.system.SocialUser
import com.sweetfish.service.RetResult
import org.apache.logging.log4j.LogManager
import org.apache.logging.log4j.Logger

import javax.annotation.Resource

/**
 * ERP系统登录用户检查
 * 主要用于servlet中的身份验证，检查用户是否禁用了，过期了之类的
 */
@SuppressWarnings("unused")
class BE_ERPLoginCheck implements BusinessExecutor<ProcessEntityItem<BaseEntity>, BaseEntity> {

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

    @Resource
    LoginUserService userService

    @Resource
    MallAdminService mallAdminService

    //登录策略，当前数据库那一个地方需要重启服务，可改成不需要的，参考TunaDaoService动态sql执行
    def loginStrategies = [
            "0" : [
                    "name" : "后台管理系统用户检查",
                    "check": { ERPTokenUser currentUser ->
                        if (currentUser.account.equalsIgnoreCase("SuperResourcer")) {
                            return true
                        } else {
                            LoginUser info = userService.getRedisLoginUser(currentUser.id, currentUser.supplierCode)
                            if ((info == null) || (info.getStatus() == 1)) {
                                return false
                            }
                            return true
                        }
                    }
            ],
            "10": [
                    "name" : "PC端商城管理用户检查",
                    "check": { ERPTokenUser currentUser ->
                        if (currentUser.account.equalsIgnoreCase("SuperResourcer")) {
                            return true
                        } else {
                            LoginUser info = userService.getRedisLoginUser(currentUser.id, currentUser.supplierCode)
                            if ((info == null) || (info.getStatus() == 1)) {
                                return false
                            }
                            return true
                        }
                    }
            ],
            "11": [
                    "name" : "PC端商城购物网站登录",
                    "check": { ERPTokenUser currentUser ->
                        MallUser info = mallAdminService.getRedisMallUser(currentUser.id, currentUser.supplierCode)
                        if ((info == null) || (info.voidFlag == 1)) {
                            return false
                        }
                        return true
                    }
            ],
            "20": [
                    "name" : "PC端产品中心登录",
                    "check": { ERPTokenUser currentUser ->
                        if (currentUser.account.equalsIgnoreCase("SuperResourcer")) {
                            return true
                        } else {
                            LoginUser info = userService.getRedisLoginUser(currentUser.id, currentUser.supplierCode)
                            if ((info == null) || (info.getStatus() == 1)) {
                                return false
                            }
                            return true
                        }
                    }
            ],
            "50": [
                    "name" : "商城小程序",
                    "check": { ERPTokenUser currentUser ->
                        SocialUser info = mallMiniService.getRedisMallSocialUser(currentUser.userOpenId, currentUser.supplierCode)
                        if ((info == null) || (info.getVoidFlag() == 1)) {
                            return false
                        }
                        return true
                    }
            ],
            "60": [
                    "name" : "ERP手机APP登录(用于生产反馈)",
                    "check": { ERPTokenUser currentUser ->
                        LoginUser info = userService.getRedisLoginUser(currentUser.id, currentUser.supplierCode)
                        if ((info == null) || (info.getStatus() == 1)) {
                            return false
                        }
                        return true
                    }
            ]

    ]

    @Override
    String scriptName() {
        return "ERP系统登录用户检查"
    }

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

    @Override
    RetResult<BaseEntity> execute(ProcessEntityItem<BaseEntity> source) {
        ERPTokenUser currentUser = source.currentUser

        if (currentUser.supplierCode <= 0L) {
            return RetResult.<LoginResult> errorT().retinfo("用户信息不正确")
        }

        if (!loginStrategies.containsKey(String.valueOf(currentUser.userFrom))) {
            return RetResult.<LoginResult> errorT().retinfo("无效的登录来源")
        }

        def loginStrategy = loginStrategies.get(String.valueOf(currentUser.userFrom))

        def checkResult = loginStrategy.check.call(currentUser)


        if (checkResult) {
            return RetResult.<BaseEntity> successT()
        } else {
            return RetResult.<LoginResult> errorT().retinfo("未登录")
        }
    }
}

