package system

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

type SysDepartmentAddReq struct {
	DepartmentName string `description:"部门名称"`
}

type SysDepartment struct {
	DepartmentId   int       `orm:"column(department_id);pk" description:"部门Id"`
	DepartmentName string    `description:"部门名称"`
	Sort           int       `description:"排序"`
	CreateTime     time.Time `description:"创建时间"`
}

func GetSysDepartmentCount(departmentName string) (count int, err error) {
	sql := `SELECT COUNT(1) AS count FROM sys_department WHERE department_name=? `
	o := orm.NewOrm()
	err = o.Raw(sql, departmentName).QueryRow(&count)
	return
}

func AddSysDepartment(item *SysDepartment) (lastId int64, err error) {
	o := orm.NewOrm()
	lastId, err = o.Insert(item)
	return
}

type SysDepartmentEditReq struct {
	DepartmentId   int    `description:"部门Id"`
	DepartmentName string `description:"部门名称"`
}

func GetSysDepartmentById(departmentId int) (item *SysDepartment, err error) {
	sql := `SELECT * FROM sys_department WHERE department_id=? `
	o := orm.NewOrm()
	err = o.Raw(sql, departmentId).QueryRow(&item)
	return
}

func GetSysDepartmentByName(departmentName string) (item *SysDepartment, err error) {
	sql := `SELECT * FROM sys_department WHERE department_name=? `
	o := orm.NewOrm()
	err = o.Raw(sql, departmentName).QueryRow(&item)
	return
}

func ModifySysDepartment(departmentName string, departmentId int) (err error) {
	o := orm.NewOrm()
	to, err := o.Begin()
	if err != nil {
		return
	}
	defer func() {
		if err != nil {
			_ = to.Rollback()
		} else {
			_ = to.Commit()
		}
	}()
	sql := `UPDATE sys_department SET department_name=? WHERE department_id=? `
	_, err = to.Raw(sql, departmentName, departmentId).Exec()
	if err != nil {
		return
	}
	sql = `UPDATE admin SET department_name=? WHERE department_id=? `
	_, err = to.Raw(sql, departmentName, departmentId).Exec()
	return
}

type SysDepartmentDeleteReq struct {
	DepartmentId int `description:"部门Id"`
}

func DeleteSysDepartment(departmentId int) (err error) {
	sql := `DELETE FROM sys_department WHERE department_id=? `
	o := orm.NewOrm()
	_, err = o.Raw(sql, departmentId).Exec()
	return
}

type SysDepartmentList struct {
	DepartmentId   int             `orm:"column(department_id);pk" description:"部门Id"`
	DepartmentName string          `description:"部门名称"`
	CreateTime     time.Time       `description:"创建时间"`
	Child          []*SysGroupList `description:"分组"`
	IsDepartment   bool            `description:"true:部门，false:分组"`
}

func GetDepartmentList() (items []*SysDepartmentList, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM sys_department ORDER BY sort ASC, create_time ASC `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// GetDepartmentListByIds 根据部门id集合获取部门列表信息
func GetDepartmentListByIds(departmentIds string) (items []*SysDepartmentList, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM sys_department where 1 = 1 `
	if departmentIds != "" {
		sql += `and department_id in(` + departmentIds + `) `
	}
	sql += ` ORDER BY sort ASC, create_time ASC `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

type SysDepartmentListResp struct {
	List []*SysDepartmentList
}

func GetSysDepartmentAll() (item []*SysDepartment, err error) {
	sql := `SELECT * FROM sys_department ORDER BY sort ASC, department_id ASC `
	o := orm.NewOrm()
	_, err = o.Raw(sql).QueryRows(&item)
	return
}

type DepartmentSort struct {
	DepartmentId int
	Sort         int
}

func MultiUpdateDepartmentSort(items []*DepartmentSort) (err error) {
	if len(items) == 0 {
		return
	}
	o := orm.NewOrm()
	p, err := o.Raw("UPDATE sys_department SET sort = ? WHERE department_id = ?").Prepare()
	if err != nil {
		return
	}
	defer func() {
		_ = p.Close()
	}()
	for _, v := range items {
		_, err = p.Exec(v.Sort, v.DepartmentId)
		if err != nil {
			return
		}
	}
	return
}

// DepartmentUserTree 部门用户树
type DepartmentUserTree struct {
	NodeId   int                   `description:"节点ID"`
	NodeType int                   `description:"节点类型：1-部门；2-分组；3-用户"`
	NodeName string                `description:"节点名称"`
	Children []*DepartmentUserTree `description:"子节点"`
}
