package models

import (
	"bwallet/pkg/setting"
	"github.com/jinzhu/gorm"
	"time"
)

type Manager struct {
	Uid           int32  `gorm:"type:int(11);primary_key" json:"uid,omitempty"`
	Username      string `gorm:"type:varchar(32);unique;not null" json:"username,omitempty"`
	Password      string `gorm:"type:varchar(32);not null" json:"password,omitempty"`
	Salt          string `gorm:"type:varchar(64);not null" json:"salt,omitempty"`
	RegTime       int64  `gorm:"type:datetime" json:"reg_time,omitempty"`
	LastLoginTime int64  `gorm:"type:datetime" json:"last_login_time,omitempty"`
	RegIp         uint32 `gorm:"type:int(10)" json:"reg_ip,omitempty"`
	LastLoginIp   uint32 `gorm:"type:int(10)" json:"last_login_ip,omitempty"`
	Status        uint8  `gorm:"not null;default:0" json:"status,omitempty"`
	Group         string `gorm:"type:varchar(32);not null" json:"group,omitempty"`
	PlatformId    int32  `gorm:"not null;default:0" json:"platform_id,omitempty"`
}

type ManagerSearch struct {
	Uid           int32  `json:"uid,omitempty"`
	Username      string `json:"username,omitempty"`
	RegTime       string `json:"reg_time,omitempty"`
	LastLoginTime string `json:"last_login_time,omitempty"`
	RegIp         string `json:"reg_ip,omitempty"`
	LastLoginIp   string `json:"last_login_ip,omitempty"`
	Status        uint8  `json:"status,omitempty"`
	Group         string `json:"group,omitempty"`
	PlatformId    int32  `json:"platform_id,omitempty"`
}

func (m Manager) TableName() string {
	return setting.DatabaseSetting.Name_Manage + ".gli_admin"
}

func GetManager(id int32) (*Manager, error) {
	var manager Manager
	err := db.Where("uid = ?", id).First(&manager).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}

	err = db.Model(&manager).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}

	return &manager, nil
}

func ExistManagerById(id int32) (bool, error) {
	var manager Manager
	err := db.Select("uid").Where("uid = ?", id).First(&manager).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return false, err
	}

	if manager.Uid > 0 {
		return true, nil
	}

	return false, nil
}

func GetManagerTotal(maps interface{}) (int, error) {
	var count int

	if err := db.Model(&Manager{}).Where(maps).Count(&count).Error; err != nil {
		return 0, err
	}

	return count, nil
}

func GetManagers(pageNum, pageSize int, maps interface{}) ([]*Manager, error) {
	var manager []*Manager
	var uid int32
	for key, val := range maps.(map[string]interface{}) {
		if ("uid" == key) {
			uid = val.(int32)
			delete(maps.(map[string]interface{}), "uid")
		}
	}
	err := db.Where(maps).Where("uid <> ?", uid).Order("uid desc").Offset(pageNum).Limit(pageSize).Find(&manager).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}

	return manager, nil
}

func AddManager(data map[string]interface{}) (*Manager, error) {
	manager := &Manager{
		Username:      data["username"].(string),
		Password:      data["password"].(string),
		RegTime:       time.Now().Unix(),
		LastLoginTime: time.Now().Unix(),
		RegIp:         data["reg_ip"].(uint32),
		LastLoginIp:   data["last_login_ip"].(uint32),
		Group:         data["group"].(string),
		Status:        1,
		PlatformId:    data["platform_id"].(int32),
	}

	if err := db.Create(&manager).Error; err != nil {
		return nil, err
	}
	return manager, nil
}

func EditManager(id int32, data interface{}) error {
	if err := db.Model(&Manager{}).Where("uid = ?", id).Updates(data).Error; err != nil {
		return err
	}

	return nil
}

func DeleteManager(id int32) error {
	if err := db.Where("uid = ?", id).Delete(Manager{}).Error; err != nil {
		return err
	}

	return nil
}
