package data_manage

import (
	"eta_gn/eta_api/global"
	"eta_gn/eta_api/utils"
	"fmt"
	"time"
)

type ManualUser struct {
	ManualUserId    int       `gorm:"primaryKey" `
	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=? `
	err = global.DmSQL["data"].Exec(sql, adminId).Error
	return
}

func AddManualUser(item *ManualUser) (err error) {
	err = global.DmSQL["data"].Create(item).Error
	return
}

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

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

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

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

type ManualUserClassify struct {
	ManualUserClassifyId int `gorm:"primaryKey" `
	AdminId              int
	ClassifyId           int
	CreateTime           time.Time
}

func AddManualUserClassifyMulti(items []*ManualUserClassify) (err error) {
	err = global.DmSQL["data"].CreateInBatches(items, utils.MultiAddNum).Error
	return
}

func DeleteManualUserClassifyCount(adminId int) (err error) {
	sql := ` DELETE FROM manual_user_classify WHERE admin_id=? `
	err = global.DmSQL["data"].Exec(sql, adminId).Error
	return
}

func GetManualUserClassifyCount(adminId, classifyId int) (count int, err error) {
	sql := ` SELECT COUNT(1) AS count FROM manual_user_classify WHERE admin_id=? AND classify_id=? `
	err = global.DmSQL["data"].Raw(sql, adminId, classifyId).Scan(&count).Error
	return
}

func GetManualUserCount(adminId int) (count int, err error) {
	sql := ` SELECT COUNT(1) AS count FROM manual_user WHERE admin_id=? `
	err = global.DmSQL["data"].Raw(sql, adminId).Scan(&count).Error
	return
}

func GetManualUserSetClassify(adminId int) (list []*EdbdataClassify, err error) {
	sql := ` SELECT * FROM manual_user_classify WHERE admin_id=? `
	err = global.DmSQL["data"].Raw(sql, adminId).Find(&list).Error
	return
}