package services

import (
	"PaymentCenter/app/constants/errorcode"
	"PaymentCenter/app/constants/pojo"
	"PaymentCenter/app/models/appmodel"
	"PaymentCenter/app/utils"
	"strings"
)

type AppCheck struct {
	App  *appmodel.App
	Code int
}

func NewAppCheck(app *appmodel.App) *AppCheck {
	return &AppCheck{
		App:  app,
		Code: errorcode.Success,
	}
}

func (a *AppCheck) IpCheck(ip string) bool {
	if a.App.WhiteIp == "" {
		return true
	}
	if !utils.ContainsString(strings.Split(a.App.WhiteIp, ","), ip) {
		a.Code = errorcode.AppIpNotAllow
		return false
	}
	return true
}

func (a *AppCheck) Check() *AppCheck {

	if a.App.Status == pojo.STATUS_DISABLED {
		a.Code = errorcode.AppDisabled
	}
	if a.App.DeleteTime.Location() == nil {
		a.Code = errorcode.AppNotFound
	}
	return a
}

func (a *AppCheck) GetCode() int {
	return a.Code
}