package system

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

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

type SysDepartment struct {
	DepartmentId   int       `gorm:"primaryKey;" 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=? `
	err = global.DEFAULT_DmSQL.Raw(sql, departmentName).Scan(&count).Error
	return
}

func AddSysDepartment(item *SysDepartment) (lastId int64, err error) {
	err = global.DEFAULT_DmSQL.Create(item).Error
	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=? `
	err = global.DEFAULT_DmSQL.Raw(sql, departmentId).First(&item).Error
	return
}

func GetSysDepartmentByName(departmentName string) (item *SysDepartment, err error) {
	sql := `SELECT * FROM sys_department WHERE department_name=? `
	err = global.DEFAULT_DmSQL.Raw(sql, departmentName).First(&item).Error
	return
}

func ModifySysDepartment(departmentName string, departmentId int) (err error) {
	tx := global.DEFAULT_DmSQL.Begin()
	defer func() {
		if err != nil {
			_ = tx.Rollback()
			return
		}
		_ = tx.Commit()
	}()
	sql := `UPDATE sys_department SET department_name=? WHERE department_id=? `
	err = tx.Exec(sql, departmentName, departmentId).Error
	if err != nil {
		return
	}
	sql = `UPDATE "admin" SET department_name=? WHERE department_id=? `
	err = tx.Exec(sql, departmentName, departmentId).Error
	return
}

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

func DeleteSysDepartment(departmentId int) (err error) {
	sql := `DELETE FROM sys_department WHERE department_id=? `
	err = global.DEFAULT_DmSQL.Exec(sql, departmentId).Error
	return
}

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

func GetDepartmentList() (items []*SysDepartmentList, err error) {
	sql := `SELECT * FROM sys_department ORDER BY sort ASC, create_time ASC `
	err = global.DEFAULT_DmSQL.Raw(sql).Find(&items).Error
	return
}

// GetDepartmentListByIds 根据部门id集合获取部门列表信息
func GetDepartmentListByIds(departmentIds string) (items []*SysDepartmentList, err error) {
	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 = global.DEFAULT_DmSQL.Raw(sql).Find(&items).Error
	return
}

type SysDepartmentListResp struct {
	List []*SysDepartmentList
}

func GetSysDepartmentAll() (item []*SysDepartment, err error) {
	sql := `SELECT * FROM sys_department ORDER BY sort ASC, department_id ASC `
	err = global.DEFAULT_DmSQL.Raw(sql).Find(&item).Error
	return
}

type DepartmentSort struct {
	DepartmentId int
	Sort         int
}

func MultiUpdateDepartmentSort(items []*DepartmentSort) (err error) {
	if len(items) == 0 {
		return
	}
	sql := "UPDATE sys_department SET sort = ? WHERE department_id = ?"
	for _, v := range items {
		err = global.DEFAULT_DmSQL.Exec(sql, v.Sort, v.DepartmentId).Error
		if err != nil {
			return
		}
	}
	return
}

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