package eta_trial

import (
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	"time"
)

type ETATrialAdmin struct {
	AdminId                   int    `orm:"column(admin_id);pk" description:"系统用户id"`
	AdminName                 string `description:"系统用户名称"`
	AdminAvatar               string `description:"用户头像"`
	RealName                  string `description:"系统用户姓名"`
	Password                  string `json:"-"`
	LastUpdatedPasswordTime   string `json:"-"`
	Enabled                   int
	Email                     string `description:"系统用户邮箱"`
	LastLoginTime             string
	CreatedTime               time.Time
	LastUpdatedTime           string
	Role                      string    `description:"系统用户角色"`
	Mobile                    string    `description:"手机号"`
	RoleType                  int       `description:"角色类型:1需要录入指标,0:不需要"`
	RoleId                    int       `description:"角色ID"`
	RoleName                  string    `description:"角色名称"`
	RoleTypeCode              string    `description:"角色类型编码"`
	DepartmentId              int       `description:"部门id"`
	DepartmentName            string    `description:"部门名称"`
	GroupId                   int       `description:"分组id"`
	GroupName                 string    `description:"分组名称"`
	Authority                 int       `description:"管理权限，0：无，1：部门负责人，2：小组负责人,或者ficc销售主管，3：超级管理员，4:ficc销售组长"`
	Position                  string    `description:"职位"`
	DisableTime               time.Time `description:"禁用时间"`
	ChartPermission           int8      `description:"图表权限id"`
	OpenId                    string    `description:"弘则部门公众号的openid"`
	UnionId                   string    `description:"微信公众平台唯一标识"`
	EdbPermission             int8      `description:"指标库操作权限，0：只能操作 自己的，1：所有指标可操作"`
	MysteelChemicalPermission int8      `description:"钢联化工指标操作权限，0：只能操作 自己的，1：所有指标可操作"`
}

func (item *ETATrialAdmin) Update(cols []string) (err error) {
	o := orm.NewOrm()
	_, err = o.Update(item, cols...)
	return
}

func AddAdmin(item *ETATrialAdmin) (lastId int, err error) {
	o := orm.NewOrmUsingDB("weekly_trial")
	sql := `INSERT INTO admin ( admin_name, real_name, password, last_updated_password_time, enabled, last_login_time, created_time, last_updated_time, mobile, role_id, role_name, role_type_code, department_id, department_name, role, position) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )`
	_, err = o.Raw(sql, item.AdminName, item.RealName, item.Password, item.LastUpdatedPasswordTime, item.Enabled, item.LastLoginTime, item.CreatedTime, item.LastUpdatedTime, item.Mobile, item.RoleId, item.RoleName, item.RoleTypeCode, item.DepartmentId, item.DepartmentName, item.Role, item.Position).Exec()

	sql = `SELECT MAX(admin_id) FROM admin`
	err = o.Raw(sql).QueryRow(&lastId)
	return
}

func GetSysUserByRoleAndDepartment(role, dep string) (item *ETATrialAdmin, err error) {
	sql := `SELECT * FROM admin WHERE role_name=? AND department_name=? LIMIT 1`
	o := orm.NewOrmUsingDB("weekly_trial")
	err = o.Raw(sql, role, dep).QueryRow(&item)
	return
}

// GetSysAdminCountByMobile 查询系统中是否存在该手机号（如果有传入用户id，那么排除该用户）
func GetSysAdminCountByMobile(mobile string, adminId int) (count int, err error) {
	sql := `SELECT COUNT(1) AS count FROM admin WHERE mobile=? `
	if adminId > 0 {
		sql += ` AND admin_id != ` + fmt.Sprint(adminId)
	}
	o := orm.NewOrmUsingDB("weekly_trial")
	err = o.Raw(sql, mobile).QueryRow(&count)
	return
}

func (item *ETATrialAdmin) TableName() string {
	return "admin"
}

func UpdateAdminEnable(mobile string) (err error) {
	sql := `UPDATE admin SET enabled=1 WHERE mobile=? `
	o := orm.NewOrmUsingDB("weekly_trial")
	_, err = o.Raw(sql, mobile).Exec()
	return
}