package data_manage

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

type ManualUser struct {
	ManualUserId    int       `orm:"column(manual_user_id);pk"`
	AdminId         int       `description:"用户id"`
	AdminRealName   string    `description:"用户姓名"`
	SysUserId       int       `description:"创建人id"`
	SysUserRealName string    `description:"创建人姓名"`
	CreateTime      time.Time `description:"创建时间"`
}

func DeleteManualUser(adminId int) (err error) {
	sql := `DELETE FROM manual_user WHERE admin_id=? `
	o := orm.NewOrmUsingDB("data")
	_, err = o.Raw(sql, adminId).Exec()
	return
}

func AddManualUser(item *ManualUser) (err error) {
	o := orm.NewOrmUsingDB("data")
	_, err = o.Insert(item)
	return
}

type ManualUserItem struct {
	AdminId       int    `description:"用户id"`
	AdminRealName string `description:"用户姓名"`
}

func GetManualUserList() (list []*ManualUserItem, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT admin_id,admin_real_name FROM manual_user
			GROUP BY admin_id
			ORDER BY create_time DESC `
	fmt.Println("GetManualUserList:", sql)
	_, err = o.Raw(sql).QueryRows(&list)
	return
}

type ManualUserList struct {
	AdminId       int `description:"用户id"`
	AdminRealName int `description:"用户姓名"`
}

func GetManualSysuserAll() (list []*ManualUserList, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT * FROM manual_user
			ORDER BY create_time ASC `
	o.Raw(sql).QueryRows()
	return
}

type ManualSysUserClassifySaveReq struct {
	AdminId    int   `description:"用户id"`
	ClassifyId []int `description:"分类id数组"`
}

type ManualUserClassify struct {
	ManualUserClassifyId int `orm:"column(manual_user_classify_id);pk"`
	AdminId              int
	ClassifyId           int
	CreateTime           time.Time
}

func AddManualUserClassifyMulti(items []*ManualUserClassify) (err error) {
	o := orm.NewOrmUsingDB("data")
	_, err = o.InsertMulti(1, items)
	return
}

func DeleteManualUserClassifyCount(adminId int) (err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` DELETE FROM manual_user_classify WHERE admin_id=? `
	_, err = o.Raw(sql, adminId).Exec()
	return
}

func GetManualUserClassifyCount(adminId, classifyId int) (count int, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT COUNT(1) AS count FROM manual_user_classify WHERE admin_id=? AND classify_id=? `
	err = o.Raw(sql, adminId, classifyId).QueryRow(&count)
	return
}

func GetManualUserCount(adminId int) (count int, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT COUNT(1) AS count FROM manual_user WHERE admin_id=? `
	err = o.Raw(sql, adminId).QueryRow(&count)
	return
}

func GetManualUserSetClassify(adminId int) (list []*EdbdataClassify, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT * FROM manual_user_classify WHERE admin_id=? `
	_, err = o.Raw(sql, adminId).QueryRows(&list)
	return
}
