改变路由层级,模块化
This commit is contained in:
@@ -0,0 +1,147 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"cutego/modules/core/api/v1/request"
|
||||
"cutego/modules/core/entity"
|
||||
"cutego/pkg/common"
|
||||
"cutego/pkg/page"
|
||||
"github.com/druidcaesa/gotool"
|
||||
"github.com/go-xorm/xorm"
|
||||
)
|
||||
|
||||
type ConfigDao struct {
|
||||
}
|
||||
|
||||
func (d ConfigDao) sql(session *xorm.Session) *xorm.Session {
|
||||
return session.Table("sys_config")
|
||||
}
|
||||
|
||||
// SelectByConfigKey 根据键名查询参数配置信息
|
||||
func (d ConfigDao) SelectByConfigKey(configKey string) *entity.SysConfig {
|
||||
config := entity.SysConfig{}
|
||||
_, err := d.sql(SqlDB.NewSession()).Where("config_key = ?", configKey).Get(&config)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return nil
|
||||
}
|
||||
return &config
|
||||
}
|
||||
|
||||
// SelectPage 分页查询数据
|
||||
func (d ConfigDao) SelectPage(query request.ConfigQuery) (*[]entity.SysConfig, int64) {
|
||||
configs := make([]entity.SysConfig, 0)
|
||||
session := d.sql(SqlDB.NewSession())
|
||||
if gotool.StrUtils.HasNotEmpty(query.ConfigName) {
|
||||
session.And("config_name like concat('%', ?, '%')", query.ConfigName)
|
||||
}
|
||||
if gotool.StrUtils.HasNotEmpty(query.ConfigType) {
|
||||
session.And("config_type = ?", query.ConfigType)
|
||||
}
|
||||
if gotool.StrUtils.HasNotEmpty(query.ConfigKey) {
|
||||
session.And("config_key like concat('%', ?, '%')", query.ConfigKey)
|
||||
}
|
||||
if gotool.StrUtils.HasNotEmpty(query.BeginTime) {
|
||||
session.And("date_format(create_time,'%y%m%d') >= date_format(?,'%y%m%d')", query.BeginTime)
|
||||
}
|
||||
if gotool.StrUtils.HasNotEmpty(query.EndTime) {
|
||||
session.And("date_format(create_time,'%y%m%d') <= date_format(?,'%y%m%d')", query.EndTime)
|
||||
}
|
||||
total, _ := page.GetTotal(session.Clone())
|
||||
err := session.Limit(query.PageSize, page.StartSize(query.PageNum, query.PageSize)).Find(&configs)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return nil, 0
|
||||
}
|
||||
return &configs, total
|
||||
}
|
||||
|
||||
// CheckConfigKeyUnique 校验是否存在
|
||||
func (d ConfigDao) CheckConfigKeyUnique(config entity.SysConfig) int64 {
|
||||
session := d.sql(SqlDB.NewSession())
|
||||
if config.ConfigId > 0 {
|
||||
session.Where("config_id != ?", config.ConfigId)
|
||||
}
|
||||
count, err := session.And("config_key = ?", config.ConfigKey).Cols("config_id").Count()
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return 0
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
// Insert 添加数据
|
||||
func (d ConfigDao) Insert(config entity.SysConfig) int64 {
|
||||
session := SqlDB.NewSession()
|
||||
session.Begin()
|
||||
insert, err := session.Insert(&config)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
session.Rollback()
|
||||
return 0
|
||||
}
|
||||
session.Commit()
|
||||
return insert
|
||||
}
|
||||
|
||||
// SelectById 查询数据
|
||||
func (d ConfigDao) SelectById(id int64) *entity.SysConfig {
|
||||
config := entity.SysConfig{}
|
||||
session := d.sql(SqlDB.NewSession())
|
||||
_, err := session.Where("config_id = ?", id).Get(&config)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return nil
|
||||
}
|
||||
return &config
|
||||
}
|
||||
|
||||
// Update 修改数据
|
||||
func (d ConfigDao) Update(config entity.SysConfig) int64 {
|
||||
session := SqlDB.NewSession()
|
||||
session.Begin()
|
||||
update, err := session.Where("config_id = ?", config.ConfigId).Update(&config)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
session.Rollback()
|
||||
return 0
|
||||
}
|
||||
session.Commit()
|
||||
return update
|
||||
}
|
||||
|
||||
// CheckConfigByIds 根据id集合查询
|
||||
func (d ConfigDao) CheckConfigByIds(list []int64) *[]entity.SysConfig {
|
||||
configs := make([]entity.SysConfig, 0)
|
||||
err := d.sql(SqlDB.NewSession()).In("config_id", list).Find(&configs)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return nil
|
||||
}
|
||||
return &configs
|
||||
}
|
||||
|
||||
// Remove 删除数据
|
||||
func (d ConfigDao) Delete(list []int64) bool {
|
||||
session := SqlDB.NewSession()
|
||||
session.Begin()
|
||||
_, err := session.In("config_id", list).Delete(&entity.SysConfig{})
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
session.Rollback()
|
||||
return false
|
||||
}
|
||||
session.Commit()
|
||||
return true
|
||||
}
|
||||
|
||||
// SelectAll 查询所有数据
|
||||
func (d ConfigDao) SelectAll() *[]entity.SysConfig {
|
||||
configs := make([]entity.SysConfig, 0)
|
||||
session := SqlDB.NewSession()
|
||||
err := session.Find(&configs)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return nil
|
||||
}
|
||||
return &configs
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"cutego/modules/core/api/v1/request"
|
||||
"cutego/modules/core/entity"
|
||||
"cutego/pkg/common"
|
||||
"cutego/pkg/page"
|
||||
"github.com/druidcaesa/gotool"
|
||||
"github.com/go-xorm/xorm"
|
||||
)
|
||||
|
||||
type CronJobDao struct {
|
||||
}
|
||||
|
||||
func (d CronJobDao) sql(session *xorm.Session) *xorm.Session {
|
||||
return session.Table("sys_cron_job")
|
||||
}
|
||||
|
||||
// SelectPage 分页查询数据
|
||||
func (d CronJobDao) SelectPage(query request.CronJobQuery) ([]entity.SysCronJob, int64) {
|
||||
configs := make([]entity.SysCronJob, 0)
|
||||
session := d.sql(SqlDB.NewSession())
|
||||
if gotool.StrUtils.HasNotEmpty(query.JobName) {
|
||||
session.And("job_name like concat('%', ?, '%')", query.JobName)
|
||||
}
|
||||
if gotool.StrUtils.HasNotEmpty(query.Status) {
|
||||
session.And("status = ?", query.Status)
|
||||
}
|
||||
total, _ := page.GetTotal(session.Clone())
|
||||
err := session.Limit(query.PageSize, page.StartSize(query.PageNum, query.PageSize)).Find(&configs)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return nil, 0
|
||||
}
|
||||
return configs, total
|
||||
}
|
||||
|
||||
// Insert 添加数据
|
||||
func (d CronJobDao) Insert(config entity.SysCronJob) int64 {
|
||||
session := SqlDB.NewSession()
|
||||
session.Begin()
|
||||
insert, err := session.Insert(&config)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
session.Rollback()
|
||||
return 0
|
||||
}
|
||||
session.Commit()
|
||||
return insert
|
||||
}
|
||||
|
||||
// SelectById 查询数据
|
||||
func (d CronJobDao) SelectById(id int64) *entity.SysCronJob {
|
||||
config := entity.SysCronJob{}
|
||||
session := d.sql(SqlDB.NewSession())
|
||||
_, err := session.Where("job_id = ?", id).Get(&config)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return nil
|
||||
}
|
||||
return &config
|
||||
}
|
||||
|
||||
// Update 修改数据
|
||||
func (d CronJobDao) Update(config entity.SysCronJob) int64 {
|
||||
session := SqlDB.NewSession()
|
||||
session.Begin()
|
||||
update, err := session.Where("job_id = ?", config.JobId).Update(&config)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
session.Rollback()
|
||||
return 0
|
||||
}
|
||||
session.Commit()
|
||||
return update
|
||||
}
|
||||
|
||||
// Delete 删除数据
|
||||
func (d CronJobDao) Delete(list []int64) bool {
|
||||
session := SqlDB.NewSession()
|
||||
session.Begin()
|
||||
_, err := session.In("job_id", list).Delete(&entity.SysCronJob{})
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
session.Rollback()
|
||||
return false
|
||||
}
|
||||
session.Commit()
|
||||
return true
|
||||
}
|
||||
|
||||
// SelectByFuncAlias 通过方法别名获取任务详情
|
||||
func (d CronJobDao) SelectByFuncAlias(funcAlias string) *entity.SysCronJob {
|
||||
config := entity.SysCronJob{}
|
||||
session := d.sql(SqlDB.NewSession())
|
||||
_, err := session.Where("func_alias = ?", funcAlias).Get(&config)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return nil
|
||||
}
|
||||
return &config
|
||||
}
|
||||
|
||||
// SelectAll 查找所有启用状态的任务
|
||||
func (d CronJobDao) SelectAll() ([]entity.SysCronJob, int) {
|
||||
configs := make([]entity.SysCronJob, 0)
|
||||
session := d.sql(SqlDB.NewSession())
|
||||
err := session.Where("status = ?", 1).Find(&configs)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return nil, 0
|
||||
}
|
||||
return configs, len(configs)
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"cutego/modules/core/api/v1/request"
|
||||
"cutego/modules/core/entity"
|
||||
"cutego/pkg/common"
|
||||
"github.com/druidcaesa/gotool"
|
||||
)
|
||||
|
||||
type DeptDao struct {
|
||||
}
|
||||
|
||||
// SelectTree 根据条件查询部门集合
|
||||
func (d DeptDao) SelectTree(query request.DeptQuery) *[]entity.SysDept {
|
||||
depts := make([]entity.SysDept, 0)
|
||||
session := SqlDB.NewSession().Where("del_flag = '0'")
|
||||
if query.ParentId > 0 {
|
||||
session.And("parent_id = ?", query.ParentId)
|
||||
}
|
||||
if !gotool.StrUtils.HasEmpty(query.DeptName) {
|
||||
session.And("dept_name like concat('%', ?, '%')", query.DeptName)
|
||||
}
|
||||
if !gotool.StrUtils.HasEmpty(query.Status) {
|
||||
session.And("status = ?", query.Status)
|
||||
}
|
||||
err := session.OrderBy("parent_id").OrderBy("order_num").Find(&depts)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return nil
|
||||
}
|
||||
return &depts
|
||||
}
|
||||
|
||||
// SelectDeptListByRoleId 根据角色ID查询部门树信息
|
||||
func (d DeptDao) SelectDeptListByRoleId(id int64, strictly bool) *[]int64 {
|
||||
list := make([]int64, 0)
|
||||
session := SqlDB.NewSession().Table([]string{"sys_dept", "d"}).Cols("d.dept_id")
|
||||
session.Join("LEFT", []string{"sys_role_dept", "rd"}, "d.dept_id = rd.dept_id").
|
||||
Where("rd.role_id = ?", id)
|
||||
if strictly {
|
||||
session.And("d.dept_id not in (select d.parent_id from sys_dept d inner join sys_role_dept rd on d.dept_id = rd.dept_id and rd.role_id = ?)", id)
|
||||
}
|
||||
err := session.OrderBy("d.parent_id").OrderBy("d.order_num").Find(&list)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return nil
|
||||
}
|
||||
return &list
|
||||
}
|
||||
|
||||
// GetList 查询部门列表
|
||||
func (d DeptDao) GetList(query request.DeptQuery) *[]entity.SysDept {
|
||||
list := make([]entity.SysDept, 0)
|
||||
session := SqlDB.NewSession().OrderBy("parent_id").OrderBy("order_num")
|
||||
session.Where("del_flag = '0'")
|
||||
if query.ParentId > 0 {
|
||||
session.And("parent_id = ?", query.ParentId)
|
||||
}
|
||||
if gotool.StrUtils.HasNotEmpty(query.DeptName) {
|
||||
session.And("dept_name like concat('%', ?, '%')", query.DeptName)
|
||||
}
|
||||
if gotool.StrUtils.HasNotEmpty(query.Status) {
|
||||
session.And("status = ?", query.Status)
|
||||
}
|
||||
err := session.Find(&list)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return nil
|
||||
}
|
||||
return &list
|
||||
}
|
||||
|
||||
// SelectDeptById 根据部门编号获取详细信息
|
||||
func (d DeptDao) SelectDeptById(id int) *entity.SysDept {
|
||||
dept := entity.SysDept{}
|
||||
_, err := SqlDB.NewSession().Where("dept_id = ?", id).Get(&dept)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return nil
|
||||
}
|
||||
return &dept
|
||||
}
|
||||
|
||||
// Insert 添加部门数据
|
||||
func (d DeptDao) Insert(dept entity.SysDept) int64 {
|
||||
session := SqlDB.NewSession()
|
||||
session.Begin()
|
||||
insert, err := session.Insert(&dept)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
session.Rollback()
|
||||
return 0
|
||||
}
|
||||
session.Commit()
|
||||
return insert
|
||||
}
|
||||
|
||||
// CheckDeptNameUnique 校验部门名称是否唯一
|
||||
func (d DeptDao) CheckDeptNameUnique(dept entity.SysDept) int64 {
|
||||
session := SqlDB.NewSession()
|
||||
count, err := session.Table("sys_dept").Cols("dept_id").Where("dept_name=?", dept.DeptName).And("parent_id = ?", dept.ParentId).Limit(1).Count()
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return 1
|
||||
}
|
||||
return count
|
||||
|
||||
}
|
||||
|
||||
// HasChildByDeptId 是否存在部门子节点
|
||||
func (d DeptDao) HasChildByDeptId(id int) int64 {
|
||||
count, _ := SqlDB.NewSession().Table("sys_dept").Cols("dept_id").Where("parent_id = ?", id).
|
||||
And("del_flag = '0'").Limit(1).Count()
|
||||
return count
|
||||
}
|
||||
|
||||
// CheckDeptExistUser 查询部门是否存在用户
|
||||
func (d DeptDao) CheckDeptExistUser(id int) int64 {
|
||||
count, _ := SqlDB.NewSession().Table("sys_user").Cols("user_id").Where("dept_id = ?", id).
|
||||
And("del_flag = '0'").Count()
|
||||
return count
|
||||
}
|
||||
|
||||
// Delete 删除部门
|
||||
func (d DeptDao) Delete(id int) int64 {
|
||||
dept := entity.SysDept{
|
||||
DeptId: id,
|
||||
}
|
||||
session := SqlDB.NewSession()
|
||||
session.Begin()
|
||||
i, err := session.Where("dept_id = ?", id).Delete(&dept)
|
||||
if err != nil {
|
||||
session.Rollback()
|
||||
common.ErrorLog(err)
|
||||
return 0
|
||||
}
|
||||
session.Commit()
|
||||
return i
|
||||
}
|
||||
|
||||
// Update 更新部门
|
||||
func (d DeptDao) Update(dept entity.SysDept) int64 {
|
||||
session := SqlDB.NewSession()
|
||||
session.Begin()
|
||||
update, err := session.Where("dept_id = ?", dept.DeptId).Update(&dept)
|
||||
if err != nil {
|
||||
session.Rollback()
|
||||
common.ErrorLog(err)
|
||||
return 0
|
||||
}
|
||||
session.Commit()
|
||||
return update
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"cutego/modules/core/api/v1/request"
|
||||
"cutego/modules/core/entity"
|
||||
"cutego/pkg/common"
|
||||
"cutego/pkg/page"
|
||||
"github.com/druidcaesa/gotool"
|
||||
"github.com/go-xorm/xorm"
|
||||
)
|
||||
|
||||
type DictDataDao struct {
|
||||
}
|
||||
|
||||
func (d *DictDataDao) sql(session *xorm.Session) *xorm.Session {
|
||||
return session.Table("sys_dict_data")
|
||||
}
|
||||
|
||||
// SelectByDictType 根据字典类型查询字典数据
|
||||
// @Param dictType string 字典类型
|
||||
// @Return []entity.SysDictData
|
||||
func (d *DictDataDao) SelectByDictType(dictType string) []entity.SysDictData {
|
||||
data := make([]entity.SysDictData, 0)
|
||||
session := d.sql(SqlDB.NewSession())
|
||||
err := session.Where("status = '0' ").And("dict_type = ?", dictType).OrderBy("dict_sort").Asc("dict_sort").
|
||||
Find(&data)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return nil
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
// GetDiceDataAll 查询所有字典数据
|
||||
// @Return *[]entity.SysDictData
|
||||
func (d DictDataDao) GetDiceDataAll() *[]entity.SysDictData {
|
||||
session := d.sql(SqlDB.NewSession())
|
||||
data := make([]entity.SysDictData, 0)
|
||||
err := session.Where("status = '0' ").OrderBy("dict_sort").Asc("dict_sort").
|
||||
Find(&data)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return nil
|
||||
}
|
||||
return &data
|
||||
}
|
||||
|
||||
// SelectPage 查询集合数据
|
||||
// @Param query request.DiceDataQuery
|
||||
// @Return *[]entity.SysDictData
|
||||
// @Return 总行数
|
||||
func (d *DictDataDao) SelectPage(query request.DiceDataQuery) (*[]entity.SysDictData, int64) {
|
||||
list := make([]entity.SysDictData, 0)
|
||||
session := SqlDB.NewSession().Table("sys_dict_data").OrderBy("dict_sort").Asc("dict_sort")
|
||||
if gotool.StrUtils.HasNotEmpty(query.DictType) {
|
||||
session.And("dict_type = ?", query.DictType)
|
||||
}
|
||||
if gotool.StrUtils.HasNotEmpty(query.DictLabel) {
|
||||
session.And("dict_label like concat('%', ?, '%')", query.DictLabel)
|
||||
}
|
||||
if gotool.StrUtils.HasNotEmpty(query.Status) {
|
||||
session.And("status = ?", query.Status)
|
||||
}
|
||||
total, _ := page.GetTotal(session.Clone())
|
||||
err := session.Limit(query.PageSize, page.StartSize(query.PageNum, query.PageSize)).Find(&list)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return nil, 0
|
||||
}
|
||||
return &list, total
|
||||
}
|
||||
|
||||
// SelectByDictCode 根据dictCode查询字典数据
|
||||
// @Param dictCode int64
|
||||
// @Return *entity.SysDictData
|
||||
func (d *DictDataDao) SelectByDictCode(dictCode int64) *entity.SysDictData {
|
||||
data := entity.SysDictData{}
|
||||
session := SqlDB.NewSession()
|
||||
_, err := session.Where("dict_code = ?", dictCode).Get(&data)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return nil
|
||||
}
|
||||
return &data
|
||||
}
|
||||
|
||||
// Insert 添加字典数据
|
||||
// @Param data entity.SysDictData
|
||||
// @Return 新增的行数
|
||||
func (d *DictDataDao) Insert(data entity.SysDictData) int64 {
|
||||
session := SqlDB.NewSession()
|
||||
session.Begin()
|
||||
insert, err := session.Insert(&data)
|
||||
if err != nil {
|
||||
session.Rollback()
|
||||
common.ErrorLog(err)
|
||||
return 0
|
||||
}
|
||||
session.Commit()
|
||||
return insert
|
||||
}
|
||||
|
||||
// Delete 删除字典数据
|
||||
func (d *DictDataDao) Delete(codes []int64) bool {
|
||||
session := SqlDB.NewSession()
|
||||
session.Begin()
|
||||
_, err := session.In("dict_code", codes).Delete(&entity.SysDictData{})
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
session.Rollback()
|
||||
return false
|
||||
}
|
||||
session.Commit()
|
||||
return true
|
||||
}
|
||||
|
||||
// 修改字典数据
|
||||
func (d *DictDataDao) Update(data entity.SysDictData) bool {
|
||||
session := SqlDB.NewSession()
|
||||
session.Begin()
|
||||
_, err := session.Where("dict_code = ?", data.DictCode).Update(&data)
|
||||
if err != nil {
|
||||
session.Rollback()
|
||||
common.ErrorLog(err)
|
||||
return false
|
||||
}
|
||||
session.Commit()
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"cutego/modules/core/api/v1/request"
|
||||
"cutego/modules/core/entity"
|
||||
"cutego/pkg/common"
|
||||
"cutego/pkg/page"
|
||||
"github.com/druidcaesa/gotool"
|
||||
"github.com/go-xorm/xorm"
|
||||
)
|
||||
|
||||
type DictTypeDao struct {
|
||||
}
|
||||
|
||||
func (d DictTypeDao) sql(session *xorm.Session) *xorm.Session {
|
||||
return session.Table("sys_dict_type")
|
||||
}
|
||||
|
||||
// SelectAll 查询所有字典类型数据
|
||||
func (d DictTypeDao) SelectAll() []*entity.SysDictType {
|
||||
types := make([]*entity.SysDictType, 0)
|
||||
err := d.sql(SqlDB.NewSession()).Where("status = '0'").Find(&types)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return nil
|
||||
}
|
||||
return types
|
||||
}
|
||||
|
||||
// SelectPage 分页查询字典类型数据
|
||||
func (d DictTypeDao) SelectPage(query request.DictTypeQuery) (*[]entity.SysDictType, int64) {
|
||||
list := make([]entity.SysDictType, 0)
|
||||
session := SqlDB.NewSession().Table("sys_dict_type")
|
||||
if gotool.StrUtils.HasNotEmpty(query.DictName) {
|
||||
session.And("dict_name like concat('%', ?, '%')", query.DictName)
|
||||
}
|
||||
if gotool.StrUtils.HasNotEmpty(query.Status) {
|
||||
session.And("status = ?", query.Status)
|
||||
}
|
||||
if gotool.StrUtils.HasNotEmpty(query.DictType) {
|
||||
session.And("AND dict_type like concat('%', ?, '%')", query.DictType)
|
||||
}
|
||||
if gotool.StrUtils.HasNotEmpty(query.BeginTime) {
|
||||
session.And("date_format(create_time,'%y%m%d') >= date_format(?,'%y%m%d')", query.BeginTime)
|
||||
}
|
||||
if gotool.StrUtils.HasNotEmpty(query.EndTime) {
|
||||
session.And("date_format(create_time,'%y%m%d') <= date_format(?,'%y%m%d')", query.EndTime)
|
||||
}
|
||||
total, _ := page.GetTotal(session.Clone())
|
||||
err := session.Limit(query.PageSize, page.StartSize(query.PageNum, query.PageSize)).Find(&list)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return nil, 0
|
||||
}
|
||||
return &list, total
|
||||
}
|
||||
|
||||
// SelectById 根据id查询字典类型数据
|
||||
func (d DictTypeDao) SelectById(id int64) *entity.SysDictType {
|
||||
dictType := entity.SysDictType{}
|
||||
_, err := SqlDB.NewSession().Where("dict_id = ?", id).Get(&dictType)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return nil
|
||||
}
|
||||
return &dictType
|
||||
}
|
||||
|
||||
// CheckDictTypeUnique 检验字典类型是否存在
|
||||
func (d DictTypeDao) CheckDictTypeUnique(dictType entity.SysDictType) int64 {
|
||||
session := SqlDB.NewSession().Table("sys_dict_type")
|
||||
if dictType.DictId > 0 {
|
||||
session.And("dict_id != ?", dictType.DictId)
|
||||
}
|
||||
count, err := session.Where("dict_type = ?", dictType.DictType).Cols("dict_id").Count()
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return 0
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
// Update 修改字典
|
||||
func (d DictTypeDao) Update(dictType entity.SysDictType) bool {
|
||||
session := SqlDB.NewSession()
|
||||
session.Begin()
|
||||
_, err := session.Where("dict_id = ?", dictType.DictId).Update(&dictType)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
session.Rollback()
|
||||
return false
|
||||
}
|
||||
session.Commit()
|
||||
return true
|
||||
}
|
||||
|
||||
// Insert 新增字典类型
|
||||
func (d DictTypeDao) Insert(dictType entity.SysDictType) int64 {
|
||||
session := SqlDB.NewSession()
|
||||
session.Begin()
|
||||
insert, err := session.Insert(&dictType)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
session.Rollback()
|
||||
return 0
|
||||
}
|
||||
session.Commit()
|
||||
return insert
|
||||
}
|
||||
|
||||
// Delete 批量删除
|
||||
func (d DictTypeDao) Delete(ids []int64) bool {
|
||||
session := SqlDB.NewSession()
|
||||
session.Begin()
|
||||
_, err := session.In("dict_id", ids).Delete(entity.SysDictType{})
|
||||
if err != nil {
|
||||
session.Rollback()
|
||||
common.ErrorLog(err)
|
||||
return false
|
||||
}
|
||||
session.Commit()
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
models2 "cutego/modules/core/entity"
|
||||
"cutego/pkg/common"
|
||||
"cutego/pkg/config"
|
||||
"cutego/pkg/constant"
|
||||
redisTool "cutego/pkg/redispool"
|
||||
"fmt"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/go-xorm/xorm"
|
||||
"time"
|
||||
)
|
||||
|
||||
// X 全局DB
|
||||
var (
|
||||
SqlDB *xorm.Engine
|
||||
RedisDB *redisTool.RedisClient
|
||||
)
|
||||
|
||||
func initDatabase() {
|
||||
var err error
|
||||
// 配置mysql数据库
|
||||
ds := config.AppEnvConfig.DataSource
|
||||
jdbc := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=True&loc=Local",
|
||||
ds.Username,
|
||||
ds.Password,
|
||||
ds.Host,
|
||||
ds.Port,
|
||||
ds.Database,
|
||||
ds.Charset)
|
||||
SqlDB, _ = xorm.NewEngine(ds.DbType, jdbc)
|
||||
if err != nil {
|
||||
common.FatalfLog("db error: %#v\n", err.Error())
|
||||
}
|
||||
err = SqlDB.Ping()
|
||||
if err != nil {
|
||||
common.FatalfLog("db connect error: %#v\n", err.Error())
|
||||
}
|
||||
SqlDB.SetMaxIdleConns(ds.MaxIdleSize)
|
||||
SqlDB.SetMaxOpenConns(ds.MaxOpenSize)
|
||||
timer := time.NewTicker(time.Minute * 30)
|
||||
go func(x *xorm.Engine) {
|
||||
for _ = range timer.C {
|
||||
err = x.Ping()
|
||||
if err != nil {
|
||||
common.FatalfLog("db connect error: %#v\n", err.Error())
|
||||
}
|
||||
}
|
||||
}(SqlDB)
|
||||
SqlDB.ShowSQL(true)
|
||||
// 开启缓存
|
||||
SqlDB.SetDefaultCacher(xorm.NewLRUCacher(xorm.NewMemoryStore(), 1000))
|
||||
}
|
||||
func initRedis() {
|
||||
// 配置redis数据库
|
||||
RedisDB = redisTool.NewRedis()
|
||||
}
|
||||
func init() {
|
||||
initDatabase()
|
||||
initRedis()
|
||||
cacheInitDataToRedis()
|
||||
}
|
||||
|
||||
// 初始化缓存数据
|
||||
func cacheInitDataToRedis() {
|
||||
initDict()
|
||||
initConfig()
|
||||
}
|
||||
|
||||
func initDict() {
|
||||
// 查询字典类型数据
|
||||
dictTypeDao := new(DictTypeDao)
|
||||
typeAll := dictTypeDao.SelectAll()
|
||||
// 所有字典数据
|
||||
d := new(DictDataDao)
|
||||
listData := d.GetDiceDataAll()
|
||||
for _, dictType := range typeAll {
|
||||
dictData := make([]map[string]interface{}, 0)
|
||||
for _, data := range *listData {
|
||||
if dictType.DictType == data.DictType {
|
||||
dictData = append(dictData, map[string]interface{}{
|
||||
"dictCode": data.DictCode,
|
||||
"dictSort": data.DictSort,
|
||||
"dictLabel": data.DictLabel,
|
||||
"dictValue": data.DictValue,
|
||||
"isDefault": data.IsDefault,
|
||||
"remark": data.Remark,
|
||||
})
|
||||
}
|
||||
}
|
||||
RedisDB.SET(constant.RedisConst{}.GetRedisDictKey()+dictType.DictType, common.StructToJson(dictData))
|
||||
}
|
||||
}
|
||||
|
||||
func initConfig() {
|
||||
// 查询配置数据存入到缓存中
|
||||
configDao := new(ConfigDao)
|
||||
configSession := configDao.sql(SqlDB.NewSession())
|
||||
configs := make([]*models2.SysConfig, 0)
|
||||
err := configSession.Find(&configs)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return
|
||||
}
|
||||
for _, sysConfig := range configs {
|
||||
RedisDB.SET(constant.RedisConst{}.GetRedisConfigKey()+sysConfig.ConfigKey, common.StructToJson(map[string]interface{}{
|
||||
"configId": sysConfig.ConfigId,
|
||||
"configName": sysConfig.ConfigName,
|
||||
"configKey": sysConfig.ConfigKey,
|
||||
"configValue": sysConfig.ConfigValue,
|
||||
"configType": sysConfig.ConfigType,
|
||||
"remark": sysConfig.Remark,
|
||||
}))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"cutego/modules/core/api/v1/request"
|
||||
"cutego/modules/core/entity"
|
||||
"cutego/pkg/common"
|
||||
"cutego/pkg/page"
|
||||
"github.com/druidcaesa/gotool"
|
||||
"github.com/go-xorm/xorm"
|
||||
)
|
||||
|
||||
type LogDao struct {
|
||||
}
|
||||
|
||||
func (d LogDao) sql(session *xorm.Session) *xorm.Session {
|
||||
return session.Table("sys_log")
|
||||
}
|
||||
|
||||
// SelectPage 分页查询数据
|
||||
func (d LogDao) SelectPage(query request.LogQuery) ([]entity.SysLog, int64) {
|
||||
configs := make([]entity.SysLog, 0)
|
||||
session := d.sql(SqlDB.NewSession())
|
||||
if gotool.StrUtils.HasNotEmpty(query.Content) {
|
||||
session.And("content like concat('%', ?, '%')", query.Content)
|
||||
}
|
||||
if gotool.StrUtils.HasNotEmpty(query.Uid) {
|
||||
session.And("uid = ?", query.Uid)
|
||||
}
|
||||
total, _ := page.GetTotal(session.Clone())
|
||||
err := session.Limit(query.PageSize, page.StartSize(query.PageNum, query.PageSize)).Find(&configs)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return nil, 0
|
||||
}
|
||||
return configs, total
|
||||
}
|
||||
|
||||
// Insert 添加数据
|
||||
func (d LogDao) Insert(config entity.SysLog) int64 {
|
||||
session := SqlDB.NewSession()
|
||||
session.Begin()
|
||||
insert, err := session.Insert(&config)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
session.Rollback()
|
||||
return 0
|
||||
}
|
||||
session.Commit()
|
||||
return insert
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"cutego/modules/core/api/v1/request"
|
||||
"cutego/modules/core/entity"
|
||||
"cutego/pkg/common"
|
||||
"cutego/pkg/page"
|
||||
"github.com/go-xorm/xorm"
|
||||
)
|
||||
|
||||
type LoginInfoDao struct {
|
||||
}
|
||||
|
||||
// 查询公共sql
|
||||
func (d LoginInfoDao) sql(session *xorm.Session) *xorm.Session {
|
||||
return session.Table("sys_login_info")
|
||||
}
|
||||
|
||||
// SelectPage 分页查询数据
|
||||
func (d LoginInfoDao) SelectPage(query request.LoginInfoQuery) (*[]entity.SysLoginInfo, int64) {
|
||||
loginInfos := make([]entity.SysLoginInfo, 0)
|
||||
session := d.sql(SqlDB.NewSession())
|
||||
session.And("user_name = ?", query.UserName)
|
||||
total, _ := page.GetTotal(session.Clone())
|
||||
err := session.Limit(query.PageSize, page.StartSize(query.PageNum, query.PageSize)).Find(&loginInfos)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return nil, 0
|
||||
}
|
||||
return &loginInfos, total
|
||||
}
|
||||
|
||||
// Insert 添加登录记录
|
||||
func (d LoginInfoDao) Insert(body entity.SysLoginInfo) *entity.SysLoginInfo {
|
||||
session := SqlDB.NewSession()
|
||||
session.Begin()
|
||||
_, err := session.Table("sys_login_info").Insert(&body)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
session.Rollback()
|
||||
}
|
||||
session.Commit()
|
||||
return &body
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"cutego/modules/core/api/v1/request"
|
||||
"cutego/modules/core/entity"
|
||||
"cutego/pkg/common"
|
||||
"github.com/druidcaesa/gotool"
|
||||
)
|
||||
|
||||
type MenuDao struct {
|
||||
}
|
||||
|
||||
// GetMenuPermission 根据用户ID查询权限
|
||||
func (d MenuDao) GetMenuPermission(id int64) *[]string {
|
||||
var perms []string
|
||||
session := SqlDB.Table([]string{"sys_menu", "m"})
|
||||
err := session.Distinct("m.perms").
|
||||
Join("LEFT", []string{"sys_role_menu", "rm"}, "m.menu_id = rm.menu_id").
|
||||
Join("LEFT", []string{"sys_user_role", "ur"}, "rm.role_id = ur.role_id").
|
||||
Join("LEFT", []string{"sys_role", "r"}, "r.role_id = ur.role_id").
|
||||
Where("m.status = '0'").And("r.status = '0'").And("ur.user_id = ?", id).Find(&perms)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return nil
|
||||
}
|
||||
return &perms
|
||||
}
|
||||
|
||||
// GetMenuAll 查询所有菜单数据
|
||||
func (d MenuDao) GetMenuAll() *[]entity.SysMenu {
|
||||
menus := make([]entity.SysMenu, 0)
|
||||
session := SqlDB.Table([]string{entity.SysMenu{}.TableName(), "m"})
|
||||
err := session.Distinct("m.menu_id").Cols("m.parent_id", "m.menu_name", "m.path", "m.component", "m.visible", "m.status", "m.perms", "m.is_frame", "m.is_cache", "m.menu_type", "m.icon", "m.order_num", "m.create_time").
|
||||
Where("m.menu_type in ('M', 'C')").And("m.status = 0").OrderBy("m.parent_id").OrderBy("m.order_num").Find(&menus)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return nil
|
||||
}
|
||||
return &menus
|
||||
}
|
||||
|
||||
// GetMenuByUserId 根据用户ID查询菜单
|
||||
func (d MenuDao) GetMenuByUserId(id int64) *[]entity.SysMenu {
|
||||
menus := make([]entity.SysMenu, 0)
|
||||
session := SqlDB.Table([]string{entity.SysMenu{}.TableName(), "m"})
|
||||
err := session.Distinct("m.menu_id").Cols("m.parent_id", "m.menu_name", "m.path", "m.component", "m.visible", "m.status", "m.perms", "m.is_frame", "m.is_cache", "m.menu_type", "m.icon", "m.order_num", "m.create_time").
|
||||
Join("LEFT", []string{"sys_role_menu", "rm"}, "m.menu_id = rm.menu_id").
|
||||
Join("LEFT", []string{"sys_user_role", "ur"}, "rm.role_id = ur.role_id").
|
||||
Join("LEFT", []string{"sys_role", "ro"}, "ur.role_id = ro.role_id").
|
||||
Join("LEFT", []string{"sys_user", "u"}, "ur.user_id = u.user_id").Where("u.user_id = ?", id).
|
||||
And("m.menu_type in ('M', 'C')").And("m.status = 0").OrderBy("m.parent_id").OrderBy("m.order_num").Find(&menus)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return nil
|
||||
}
|
||||
return &menus
|
||||
}
|
||||
|
||||
// SelectMenuByRoleId 根据角色ID查询菜单树信息
|
||||
func (d MenuDao) SelectMenuByRoleId(id int64, strictly bool) *[]int64 {
|
||||
list := make([]int64, 0)
|
||||
session := SqlDB.NewSession().Table([]string{"sys_menu", "m"})
|
||||
session.Join("LEFT", []string{"sys_role_menu", "rm"}, "m.menu_id = rm.menu_id")
|
||||
session.Where("rm.role_id = ?", id)
|
||||
if strictly {
|
||||
session.And("m.menu_id not in (select m.parent_id from sys_menu m inner join sys_role_menu rm on m.menu_id = rm.menu_id and rm.role_id = ?)", id)
|
||||
}
|
||||
err := session.OrderBy("m.parent_id").OrderBy("m.order_num").Cols("m.menu_id").Find(&list)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return nil
|
||||
}
|
||||
return &list
|
||||
}
|
||||
|
||||
// SelectMenuList 查询系统菜单列表
|
||||
func (d MenuDao) SelectMenuList(query request.MenuQuery) *[]entity.SysMenu {
|
||||
list := make([]entity.SysMenu, 0)
|
||||
session := SqlDB.NewSession().OrderBy("parent_id").OrderBy("order_num")
|
||||
if gotool.StrUtils.HasNotEmpty(query.MenuName) {
|
||||
session.And("menu_name like concat('%', ?, '%')", query.MenuName)
|
||||
}
|
||||
if gotool.StrUtils.HasNotEmpty(query.Visible) {
|
||||
session.And("visible = ?", query.Visible)
|
||||
}
|
||||
if gotool.StrUtils.HasNotEmpty(query.Status) {
|
||||
session.And("status = ?", query.Status)
|
||||
}
|
||||
err := session.Find(&list)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return nil
|
||||
}
|
||||
return &list
|
||||
}
|
||||
|
||||
// SelectMenuListByUserId 根据用户查询系统菜单列表
|
||||
func (d MenuDao) SelectMenuListByUserId(query request.MenuQuery) *[]entity.SysMenu {
|
||||
session := SqlDB.NewSession().OrderBy("parent_id").OrderBy("order_num")
|
||||
list := make([]entity.SysMenu, 0)
|
||||
session.Distinct("m.menu_id", "m.parent_id", "m.menu_name", "m.path", "m.component", "m.visible", "m.status", "ifnull(m.perms,'') as perms", "m.is_frame", "m.is_cache", "m.menu_type", "m.icon", "m.order_num", "m.create_time")
|
||||
session.Join("LEFT", []string{"sys_role_menu", "rm"}, "m.menu_id = rm.menu_id").
|
||||
Join("LEFT", []string{"sys_user_role", "ur"}, "rm.role_id = ur.role_id").
|
||||
Join("LEFT", []string{"sys_role", "ro"}, "ur.role_id = ro.role_id").
|
||||
Where("ur.user_id = ?", query.UserId)
|
||||
if gotool.StrUtils.HasNotEmpty(query.MenuName) {
|
||||
session.And("menu_name like concat('%', ?, '%')", query.MenuName)
|
||||
}
|
||||
if gotool.StrUtils.HasNotEmpty(query.Visible) {
|
||||
session.And("visible = ?", query.Visible)
|
||||
}
|
||||
if gotool.StrUtils.HasNotEmpty(query.Status) {
|
||||
session.And("status = ?", query.Status)
|
||||
}
|
||||
err := session.Find(&list)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return nil
|
||||
}
|
||||
return &list
|
||||
}
|
||||
|
||||
// SelectMenuByMenuId 根据菜单ID查询信息
|
||||
func (d MenuDao) SelectMenuByMenuId(id int) *entity.SysMenu {
|
||||
menu := entity.SysMenu{
|
||||
MenuId: id,
|
||||
}
|
||||
_, err := SqlDB.NewSession().Where("menu_id = ?", menu.MenuId).Get(&menu)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return nil
|
||||
}
|
||||
return &menu
|
||||
}
|
||||
|
||||
// Insert 添加菜单数据
|
||||
func (d MenuDao) Insert(menu entity.SysMenu) int64 {
|
||||
session := SqlDB.NewSession()
|
||||
session.Begin()
|
||||
insert, err := session.Insert(&menu)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
session.Rollback()
|
||||
return 0
|
||||
}
|
||||
session.Commit()
|
||||
return insert
|
||||
}
|
||||
|
||||
// Update 修改菜单数据
|
||||
func (d MenuDao) Update(menu entity.SysMenu) int64 {
|
||||
session := SqlDB.NewSession()
|
||||
session.Begin()
|
||||
update, err := session.Where("menu_id = ?", menu.MenuId).Update(&menu)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
session.Rollback()
|
||||
return 0
|
||||
}
|
||||
session.Commit()
|
||||
return update
|
||||
}
|
||||
|
||||
// Delete 删除菜单操作
|
||||
func (d MenuDao) Delete(id int) int64 {
|
||||
menu := entity.SysMenu{
|
||||
MenuId: id,
|
||||
}
|
||||
session := SqlDB.NewSession()
|
||||
session.Begin()
|
||||
i, err := session.Delete(&menu)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
session.Rollback()
|
||||
return 0
|
||||
}
|
||||
session.Commit()
|
||||
return i
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"cutego/modules/core/api/v1/request"
|
||||
"cutego/modules/core/entity"
|
||||
"cutego/pkg/common"
|
||||
"cutego/pkg/page"
|
||||
"github.com/druidcaesa/gotool"
|
||||
)
|
||||
|
||||
type NoticeDao struct {
|
||||
}
|
||||
|
||||
// SelectPage 查询集合
|
||||
func (d NoticeDao) SelectPage(query request.NoticeQuery) (*[]entity.SysNotice, int64) {
|
||||
notices := make([]entity.SysNotice, 0)
|
||||
session := SqlDB.NewSession().Table(entity.SysNotice{}.TableName())
|
||||
if gotool.StrUtils.HasNotEmpty(query.NoticeTitle) {
|
||||
session.And("notice_title like concat('%', ?, '%')", query.NoticeTitle)
|
||||
}
|
||||
if gotool.StrUtils.HasNotEmpty(query.NoticeType) {
|
||||
session.And("notice_type = ?", query.NoticeType)
|
||||
}
|
||||
if gotool.StrUtils.HasNotEmpty(query.CreateBy) {
|
||||
session.And("create_by like concat('%', ?, '%')", query.CreateBy)
|
||||
}
|
||||
total, _ := page.GetTotal(session.Clone())
|
||||
err := session.Limit(query.PageSize, page.StartSize(query.PageNum, query.PageSize)).Find(¬ices)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return nil, 0
|
||||
}
|
||||
return ¬ices, total
|
||||
}
|
||||
|
||||
// Insert 添加数据
|
||||
func (d NoticeDao) Insert(notice entity.SysNotice) int64 {
|
||||
session := SqlDB.NewSession()
|
||||
session.Begin()
|
||||
insert, err := session.Insert(¬ice)
|
||||
if err != nil {
|
||||
session.Rollback()
|
||||
common.ErrorLog(err)
|
||||
return 0
|
||||
}
|
||||
session.Commit()
|
||||
return insert
|
||||
}
|
||||
|
||||
// Delete 批量删除
|
||||
func (d NoticeDao) Delete(list []int64) int64 {
|
||||
session := SqlDB.NewSession()
|
||||
session.Begin()
|
||||
i, err := session.In("notice_id", list).Delete(&entity.SysNotice{})
|
||||
if err != nil {
|
||||
session.Rollback()
|
||||
common.ErrorLog(err)
|
||||
return 0
|
||||
}
|
||||
session.Commit()
|
||||
return i
|
||||
}
|
||||
|
||||
// SelectById 查询
|
||||
func (d NoticeDao) SelectById(id int64) *entity.SysNotice {
|
||||
notice := entity.SysNotice{}
|
||||
_, err := SqlDB.NewSession().Where("notice_id = ?", id).Get(¬ice)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return nil
|
||||
}
|
||||
return ¬ice
|
||||
}
|
||||
|
||||
// Update 修改数据
|
||||
func (d NoticeDao) Update(notice entity.SysNotice) int64 {
|
||||
session := SqlDB.NewSession()
|
||||
session.Begin()
|
||||
update, err := session.Where("notice_id = ?", notice.NoticeId).Update(¬ice)
|
||||
if err != nil {
|
||||
session.Rollback()
|
||||
common.ErrorLog(err)
|
||||
return 0
|
||||
}
|
||||
session.Commit()
|
||||
return update
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"cutego/modules/core/api/v1/request"
|
||||
"cutego/modules/core/entity"
|
||||
"cutego/pkg/common"
|
||||
"cutego/pkg/page"
|
||||
"github.com/druidcaesa/gotool"
|
||||
"github.com/go-xorm/xorm"
|
||||
)
|
||||
|
||||
type PostDao struct {
|
||||
}
|
||||
|
||||
func (d PostDao) sqlSelectJoin(session *xorm.Session) *xorm.Session {
|
||||
return session.Table([]string{"sys_post", "p"}).
|
||||
Join("LEFT", []string{"sys_user_post", "up"}, "up.post_id = p.post_id").
|
||||
Join("LEFT", []string{"sys_user", "u"}, "u.user_id = up.user_id")
|
||||
}
|
||||
|
||||
// SelectAll 查询所有岗位数据, 数据库操作
|
||||
func (d PostDao) SelectAll() []*entity.SysPost {
|
||||
session := SqlDB.NewSession()
|
||||
posts := make([]*entity.SysPost, 0)
|
||||
err := session.Find(&posts)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return nil
|
||||
}
|
||||
return posts
|
||||
}
|
||||
|
||||
// SelectPostListByUserId 根据用户id查询岗位id集合
|
||||
func (d PostDao) SelectPostListByUserId(userId int64) *[]int64 {
|
||||
var ids []int64
|
||||
selectSql := d.sqlSelectJoin(SqlDB.NewSession())
|
||||
err := selectSql.Where("u.user_id = ?", userId).Cols("p.post_id").Find(&ids)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return nil
|
||||
}
|
||||
return &ids
|
||||
}
|
||||
|
||||
// SelectPage 查询岗位分页数据
|
||||
func (d PostDao) SelectPage(query request.PostQuery) (*[]entity.SysPost, int64) {
|
||||
posts := make([]entity.SysPost, 0)
|
||||
session := SqlDB.NewSession().Table(entity.SysPost{}.TableName())
|
||||
if gotool.StrUtils.HasNotEmpty(query.PostCode) {
|
||||
session.And("post_code like concat('%', ?, '%')", query.PostCode)
|
||||
}
|
||||
if gotool.StrUtils.HasNotEmpty(query.Status) {
|
||||
session.And("status = ?", query.Status)
|
||||
}
|
||||
if gotool.StrUtils.HasNotEmpty(query.PostName) {
|
||||
session.And("post_name like concat('%', ?, '%')", query.PostName)
|
||||
}
|
||||
total, _ := page.GetTotal(session.Clone())
|
||||
err := session.Limit(query.PageSize, page.StartSize(query.PageNum, query.PageSize)).Find(&posts)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return nil, 0
|
||||
}
|
||||
return &posts, total
|
||||
}
|
||||
|
||||
// CheckPostNameUnique 校验岗位名称是否存在
|
||||
func (d PostDao) CheckPostNameUnique(post entity.SysPost) int64 {
|
||||
session := SqlDB.NewSession().Table("sys_post").Cols("post_id").
|
||||
Where("post_name = ?", post.PostName)
|
||||
if post.PostId > 0 {
|
||||
session.And("post_id != ?", post.PostId)
|
||||
}
|
||||
count, _ := session.Count()
|
||||
return count
|
||||
}
|
||||
|
||||
// CheckPostCodeUnique 校验岗位编码是否存在
|
||||
func (d PostDao) CheckPostCodeUnique(post entity.SysPost) int64 {
|
||||
session := SqlDB.NewSession().Table("sys_post").Cols("post_id").
|
||||
Where("post_code = ?", post.PostCode)
|
||||
if post.PostId > 0 {
|
||||
session.And("post_id != ?", post.PostId)
|
||||
}
|
||||
count, _ := session.Count()
|
||||
return count
|
||||
}
|
||||
|
||||
// Insert 添加岗位数据
|
||||
func (d PostDao) Insert(post entity.SysPost) int64 {
|
||||
session := SqlDB.NewSession()
|
||||
session.Begin()
|
||||
insert, err := session.Insert(&post)
|
||||
if err != nil {
|
||||
session.Rollback()
|
||||
common.ErrorLog(err)
|
||||
return 0
|
||||
}
|
||||
session.Commit()
|
||||
return insert
|
||||
}
|
||||
|
||||
// GetPostById 根据id查询岗位数据
|
||||
func (d PostDao) GetPostById(post entity.SysPost) *entity.SysPost {
|
||||
_, err := SqlDB.NewSession().Where("post_id = ?", post.PostId).Get(&post)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return nil
|
||||
}
|
||||
return &post
|
||||
}
|
||||
|
||||
// Delete 批量删除岗位
|
||||
func (d PostDao) Delete(posts []int64) int64 {
|
||||
|
||||
session := SqlDB.NewSession()
|
||||
session.Begin()
|
||||
i, err := session.In("post_id", posts).Delete(&entity.SysPost{})
|
||||
if err != nil {
|
||||
session.Rollback()
|
||||
common.ErrorLog(err)
|
||||
return 0
|
||||
}
|
||||
session.Commit()
|
||||
return i
|
||||
}
|
||||
|
||||
// Update 修改岗位数据
|
||||
func (d PostDao) Update(post entity.SysPost) bool {
|
||||
session := SqlDB.NewSession()
|
||||
session.Begin()
|
||||
_, err := session.Where("post_id = ?", post.PostId).Update(&post)
|
||||
if err != nil {
|
||||
session.Rollback()
|
||||
common.ErrorLog(err)
|
||||
return false
|
||||
}
|
||||
session.Commit()
|
||||
return true
|
||||
}
|
||||
|
||||
func (d PostDao) SelectPostByUserName(name string) *[]entity.SysPost {
|
||||
posts := make([]entity.SysPost, 0)
|
||||
session := SqlDB.NewSession().Table([]string{entity.SysPost{}.TableName(), "p"})
|
||||
err := session.Cols("p.post_id", "p.post_name", "p.post_code").
|
||||
Join("LEFT", []string{"sys_user_post", "up"}, "up.post_id = p.post_id").
|
||||
Join("LEFT", []string{"sys_user", "u"}, "u.user_id = up.user_id").Where("u.user_name = ?", name).Find(&posts)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return nil
|
||||
}
|
||||
return &posts
|
||||
}
|
||||
@@ -0,0 +1,205 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"cutego/modules/core/api/v1/request"
|
||||
"cutego/modules/core/entity"
|
||||
"cutego/pkg/common"
|
||||
"cutego/pkg/page"
|
||||
"github.com/druidcaesa/gotool"
|
||||
"github.com/go-xorm/xorm"
|
||||
)
|
||||
|
||||
type RoleDao struct {
|
||||
}
|
||||
|
||||
// 角色公用sql
|
||||
func (d RoleDao) sqlSelectJoin() *xorm.Session {
|
||||
return SqlDB.Table([]string{entity.SysRole{}.TableName(), "r"}).
|
||||
Join("LEFT", []string{"sys_user_role", "ur"}, "ur.role_id = r.role_id").
|
||||
Join("LEFT", []string{"sys_user", "u"}, "u.user_id = ur.user_id").
|
||||
Join("LEFT", []string{"sys_dept", "d"}, "u.dept_id = d.dept_id")
|
||||
}
|
||||
|
||||
// 用户角色关系查询sql
|
||||
func (d RoleDao) sqlSelectRoleAndUser() *xorm.Session {
|
||||
return SqlDB.Table([]string{entity.SysRole{}.TableName(), "r"}).
|
||||
Join("LEFT", []string{"sys_user_role", "ur"}, "ur.role_id = r.role_id").
|
||||
Join("LEFT", []string{"sys_user", "u"}, "u.user_id = ur.user_id")
|
||||
}
|
||||
|
||||
// SelectPage 根据条件查询角色数据
|
||||
func (d RoleDao) SelectPage(q *request.RoleQuery) ([]*entity.SysRole, int64) {
|
||||
roles := make([]*entity.SysRole, 0)
|
||||
session := d.sqlSelectJoin()
|
||||
if !gotool.StrUtils.HasEmpty(q.RoleName) {
|
||||
session.And("r.role_name like concat('%', ?, '%')", q.RoleName)
|
||||
}
|
||||
if !gotool.StrUtils.HasEmpty(q.Status) {
|
||||
session.And("r.status = ?", q.Status)
|
||||
}
|
||||
if !gotool.StrUtils.HasEmpty(q.RoleKey) {
|
||||
session.And("r.role_key like concat('%', ?, '%')", q.RoleKey)
|
||||
}
|
||||
if !gotool.StrUtils.HasEmpty(q.BeginTime) {
|
||||
session.And("date_format(r.create_time,'%y%m%d') >= date_format(?,'%y%m%d')", q.BeginTime)
|
||||
}
|
||||
if !gotool.StrUtils.HasEmpty(q.EndTime) {
|
||||
session.And("date_format(r.create_time,'%y%m%d') <= date_format(?,'%y%m%d')", q.EndTime)
|
||||
}
|
||||
total, _ := page.GetTotal(session.Clone())
|
||||
err := session.Limit(q.PageSize, page.StartSize(q.PageNum, q.PageSize)).OrderBy("r.role_sort").Find(&roles)
|
||||
if err != nil {
|
||||
return nil, 0
|
||||
}
|
||||
return roles, total
|
||||
}
|
||||
|
||||
// SelectAll 查询所有角色
|
||||
func (d RoleDao) SelectAll() []*entity.SysRole {
|
||||
sql := d.sqlSelectJoin()
|
||||
roles := make([]*entity.SysRole, 0)
|
||||
err := sql.Find(&roles)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return nil
|
||||
}
|
||||
return roles
|
||||
}
|
||||
|
||||
// SelectRoleListByUserId 根据用户id查询用户角色id集合
|
||||
func (d RoleDao) SelectRoleListByUserId(userId int64) *[]int64 {
|
||||
sqlSelectRoleAndUser := d.sqlSelectRoleAndUser()
|
||||
var roleIds []int64
|
||||
err := sqlSelectRoleAndUser.Cols("r.role_id").Where("u.user_id = ?", userId).Find(&roleIds)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return nil
|
||||
}
|
||||
return &roleIds
|
||||
}
|
||||
|
||||
// SelectRolePermissionByUserId 查询用户角色集合
|
||||
func (d RoleDao) SelectRolePermissionByUserId(id int64) *[]string {
|
||||
var roleKeys []string
|
||||
err := d.sqlSelectJoin().Cols("r.role_key").Where("r.del_flag = '0'").And("ur.user_id = ?", id).Find(&roleKeys)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return nil
|
||||
}
|
||||
return &roleKeys
|
||||
}
|
||||
|
||||
// GetRoleListByUserId 根据用户ID查询角色
|
||||
func (d RoleDao) GetRoleListByUserId(id int64) *[]entity.SysRole {
|
||||
roles := make([]entity.SysRole, 0)
|
||||
err := d.sqlSelectJoin().Where("r.del_flag = '0'").And("ur.user_id = ?", id).Find(&roles)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return nil
|
||||
}
|
||||
return &roles
|
||||
}
|
||||
|
||||
// SelectRoleByRoleId 根据角色id查询角色数据
|
||||
func (d RoleDao) SelectRoleByRoleId(id int64) *entity.SysRole {
|
||||
role := entity.SysRole{}
|
||||
_, err := d.sqlSelectJoin().Where("r.role_id = ?", id).Get(&role)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return nil
|
||||
}
|
||||
return &role
|
||||
}
|
||||
|
||||
// CheckRoleNameUnique 校验角色名称是否唯一
|
||||
func (d RoleDao) CheckRoleNameUnique(role entity.SysRole) int64 {
|
||||
session := SqlDB.Table(role.TableName()).Where("role_name = ?", role.RoleName)
|
||||
if role.RoleId > 0 {
|
||||
session.And("role_id != ?", role.RoleId)
|
||||
}
|
||||
count, err := session.Count(&role)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
// CheckRoleKeyUnique 校验角色权限是否唯一
|
||||
func (d RoleDao) CheckRoleKeyUnique(role entity.SysRole) int64 {
|
||||
session := SqlDB.Table(role.TableName()).Where("role_key = ?", role.RoleKey)
|
||||
if role.RoleId > 0 {
|
||||
session.And("role_id != ?", role.RoleId)
|
||||
}
|
||||
count, err := session.Count(&role)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
// Add 添加角色进入数据库操作
|
||||
func (d RoleDao) Insert(role entity.SysRole) entity.SysRole {
|
||||
session := SqlDB.NewSession()
|
||||
session.Begin()
|
||||
_, err := session.Insert(&role)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
session.Rollback()
|
||||
}
|
||||
session.Commit()
|
||||
return role
|
||||
}
|
||||
|
||||
// Update 修改数据
|
||||
func (d RoleDao) Update(role entity.SysRole) int64 {
|
||||
session := SqlDB.NewSession()
|
||||
session.Begin()
|
||||
update, err := session.Where("role_id = ?", role.RoleId).Update(&role)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
session.Rollback()
|
||||
return 0
|
||||
}
|
||||
session.Commit()
|
||||
return update
|
||||
}
|
||||
|
||||
// Delete 删除角色
|
||||
func (d RoleDao) Delete(role entity.SysRole) int64 {
|
||||
session := SqlDB.NewSession()
|
||||
session.Begin()
|
||||
i, err := session.Delete(&role)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
session.Rollback()
|
||||
return 0
|
||||
}
|
||||
session.Commit()
|
||||
return i
|
||||
}
|
||||
|
||||
// UpdateRoleStatus 修改角色状态
|
||||
func (d RoleDao) UpdateRoleStatus(role *entity.SysRole) int64 {
|
||||
session := SqlDB.NewSession()
|
||||
session.Begin()
|
||||
update, err := session.Where("role_id = ?", role.RoleId).Cols("status", "update_by", "update_time").Update(role)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
session.Rollback()
|
||||
return 0
|
||||
}
|
||||
session.Commit()
|
||||
return update
|
||||
}
|
||||
|
||||
// SelectRolesByUserName 查询角色组
|
||||
func (d RoleDao) SelectRolesByUserName(name string) *[]entity.SysRole {
|
||||
roles := make([]entity.SysRole, 0)
|
||||
session := d.sqlSelectJoin()
|
||||
err := session.Where("r.del_flag = '0'").And("u.user_name = ?", name).Find(&roles)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return nil
|
||||
}
|
||||
return &roles
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"cutego/modules/core/entity"
|
||||
"cutego/pkg/common"
|
||||
)
|
||||
|
||||
type RoleMenuDao struct {
|
||||
}
|
||||
|
||||
// Insert 添加角色菜单关系
|
||||
func (d RoleMenuDao) Insert(list []entity.SysRoleMenu) int64 {
|
||||
session := SqlDB.NewSession()
|
||||
session.Begin()
|
||||
insert, err := session.Insert(&list)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
session.Rollback()
|
||||
}
|
||||
session.Commit()
|
||||
return insert
|
||||
}
|
||||
|
||||
// Delete 删除角色和菜单关系
|
||||
func (d RoleMenuDao) Delete(role entity.SysRole) {
|
||||
menu := entity.SysRoleMenu{
|
||||
RoleId: role.RoleId,
|
||||
}
|
||||
session := SqlDB.NewSession()
|
||||
session.Begin()
|
||||
_, err := session.Delete(&menu)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
session.Rollback()
|
||||
}
|
||||
session.Commit()
|
||||
}
|
||||
@@ -0,0 +1,276 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"cutego/modules/core/api/v1/request"
|
||||
"cutego/modules/core/api/v1/response"
|
||||
"cutego/modules/core/entity"
|
||||
"cutego/pkg/common"
|
||||
"cutego/pkg/page"
|
||||
"github.com/druidcaesa/gotool"
|
||||
"github.com/go-xorm/xorm"
|
||||
"time"
|
||||
)
|
||||
|
||||
type UserDao struct {
|
||||
}
|
||||
|
||||
// 查询公共sql
|
||||
func (d UserDao) sqlSelectJoin() *xorm.Session {
|
||||
return SqlDB.NewSession().Table([]string{"sys_user", "u"}).
|
||||
Join("LEFT", []string{"sys_dept", "d"}, "u.dept_id = d.dept_id").
|
||||
Join("LEFT", []string{"sys_user_role", "ur"}, "u.user_id = ur.user_id").
|
||||
Join("LEFT", []string{"sys_role", "r"}, "r.role_id = ur.role_id")
|
||||
}
|
||||
|
||||
// SelectPage 查询用户集合
|
||||
func (d UserDao) SelectPage(query request.UserQuery) ([]*response.UserResponse, int64) {
|
||||
resp := make([]*response.UserResponse, 0)
|
||||
sql := d.sqlSelectJoin()
|
||||
if !gotool.StrUtils.HasEmpty(query.UserName) {
|
||||
sql.And("u.user_name like concat('%',?,'%')", query.UserName)
|
||||
}
|
||||
if !gotool.StrUtils.HasEmpty(query.Status) {
|
||||
sql.And("i.status = ?", query.Status)
|
||||
}
|
||||
if !gotool.StrUtils.HasEmpty(query.PhoneNumber) {
|
||||
sql.And("u.phone_number like concat('%',?,'%')", query.PhoneNumber)
|
||||
}
|
||||
if !gotool.StrUtils.HasEmpty(query.BeginTime) {
|
||||
sql.And("date_format(u.create_time,'%y%m%d') >= date_format(?,'%y%m%d')", query.BeginTime)
|
||||
}
|
||||
if !gotool.StrUtils.HasEmpty(query.EndTime) {
|
||||
sql.And("date_format(u.create_time,'%y%m%d') <= date_format(?,'%y%m%d')", query.EndTime)
|
||||
}
|
||||
if query.DeptId > 0 {
|
||||
sql.And("u.dept_id = ? OR u.dept_id in ( SELECT t.dept_id FROM sys_dept t WHERE find_in_set(?, ancestors))", query.DeptId, query.DeptId)
|
||||
}
|
||||
total, _ := page.GetTotal(sql.Clone())
|
||||
err := sql.Limit(query.PageSize, page.StartSize(query.PageNum, query.PageSize)).Find(&resp)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return nil, 0
|
||||
}
|
||||
return resp, total
|
||||
}
|
||||
|
||||
// GetUserById 根据id查询用户数据
|
||||
func (d UserDao) GetUserById(userId int64) *response.UserResponse {
|
||||
var resp response.UserResponse
|
||||
get, err := d.sqlSelectJoin().Where("u.user_id = ?", userId).Get(&resp)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
}
|
||||
if !get {
|
||||
return nil
|
||||
}
|
||||
return &resp
|
||||
}
|
||||
|
||||
// GetUserByUserName 根据用户名查询用户数据
|
||||
func (d UserDao) GetUserByUserName(user entity.SysUser) *entity.SysUser {
|
||||
i, err := SqlDB.Get(&user)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return nil
|
||||
}
|
||||
if i {
|
||||
return &user
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CheckEmailUnique 校验邮箱是否存在
|
||||
func (d UserDao) CheckEmailUnique(user request.UserBody) *entity.SysUser {
|
||||
sysUser := entity.SysUser{}
|
||||
session := SqlDB.NewSession().Table("sys_user")
|
||||
session.Cols("user_id", "email")
|
||||
session.Where("email = ?", user.Email)
|
||||
if user.UserId > 0 {
|
||||
session.And("user_id != ?", user.UserId)
|
||||
}
|
||||
get, _ := session.Limit(1).Get(&sysUser)
|
||||
if !get {
|
||||
return nil
|
||||
}
|
||||
return &sysUser
|
||||
}
|
||||
|
||||
// CheckPhoneNumUnique 校验手机号是否存在
|
||||
func (d UserDao) CheckPhoneNumUnique(body request.UserBody) *entity.SysUser {
|
||||
sysUser := entity.SysUser{}
|
||||
session := SqlDB.NewSession().Table("sys_user")
|
||||
session.Cols("user_id", "phone_num")
|
||||
session.Where("phone_num = ?", body.PhoneNumber)
|
||||
if body.UserId > 0 {
|
||||
session.And("user_id != ?", body.UserId)
|
||||
}
|
||||
get, _ := session.Limit(1).Get(&sysUser)
|
||||
if !get {
|
||||
return nil
|
||||
}
|
||||
return &sysUser
|
||||
}
|
||||
|
||||
// Insert 添加用户
|
||||
func (d UserDao) Insert(body request.UserBody) *request.UserBody {
|
||||
session := SqlDB.NewSession()
|
||||
session.Begin()
|
||||
_, err := session.Table("sys_user").Insert(&body)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
session.Rollback()
|
||||
}
|
||||
session.Commit()
|
||||
return &body
|
||||
}
|
||||
|
||||
// Update 修改用户数据
|
||||
func (d UserDao) Update(body request.UserBody) int64 {
|
||||
session := SqlDB.NewSession().Table("sys_user")
|
||||
session.Begin()
|
||||
_, err := session.Where("user_id = ?", body.UserId).Update(&body)
|
||||
if err != nil {
|
||||
session.Rollback()
|
||||
common.ErrorLog(err)
|
||||
return 0
|
||||
}
|
||||
session.Commit()
|
||||
return 1
|
||||
}
|
||||
|
||||
// Delete 根据id删除用户数据
|
||||
func (d UserDao) Delete(id int64) int64 {
|
||||
user := entity.SysUser{
|
||||
UserId: id,
|
||||
}
|
||||
session := SqlDB.NewSession().Table("sys_user")
|
||||
session.Begin()
|
||||
i, err := session.Delete(&user)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
session.Rollback()
|
||||
}
|
||||
session.Commit()
|
||||
return i
|
||||
}
|
||||
|
||||
// ResetPwd 修改用户密码数据库操作
|
||||
func (d UserDao) ResetPwd(body request.UserBody) int64 {
|
||||
user := entity.SysUser{
|
||||
UserId: body.UserId,
|
||||
Password: body.Password,
|
||||
}
|
||||
session := SqlDB.NewSession()
|
||||
session.Begin()
|
||||
_, err := session.Where("user_id = ?", user.UserId).Cols("password").Update(&user)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
session.Rollback()
|
||||
return 0
|
||||
}
|
||||
session.Commit()
|
||||
return 1
|
||||
}
|
||||
|
||||
// GetAllocatedList 查询未分配用户角色列表
|
||||
func (d UserDao) GetAllocatedList(query request.UserQuery) ([]*response.UserResponse, int64) {
|
||||
resp := make([]*response.UserResponse, 0)
|
||||
session := SqlDB.NewSession()
|
||||
session.Table([]string{"sys_user", "u"}).Distinct("u.user_id", "u.dept_id", "u.user_name", "u.nick_name", "u.email", "u.phone_number", "u.status", "u.create_time").
|
||||
Join("LEFT", []string{"sys_dept", "d"}, "u.dept_id = d.dept_id").
|
||||
Join("LEFT", []string{"sys_user_role", "ur"}, "u.user_id = ur.user_id").
|
||||
Join("LEFT", []string{"sys_role", "r"}, "r.role_id = ur.role_id").Where("u.del_flag = '0'").And("r.role_id = ?", query.RoleId)
|
||||
if gotool.StrUtils.HasNotEmpty(query.UserName) {
|
||||
session.And("u.user_name like concat('%', ?, '%')", query.UserName)
|
||||
}
|
||||
if gotool.StrUtils.HasNotEmpty(query.PhoneNumber) {
|
||||
session.And("u.phone_number like concat('%', ?, '%')", query.PhoneNumber)
|
||||
}
|
||||
total, _ := page.GetTotal(session.Clone())
|
||||
err := session.Limit(query.PageSize, page.StartSize(query.PageNum, query.PageSize)).Find(&resp)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return nil, 0
|
||||
}
|
||||
return resp, total
|
||||
}
|
||||
|
||||
// GetUnallocatedList 查询未分配用户角色列表
|
||||
func (d UserDao) GetUnallocatedList(query request.UserQuery) ([]*response.UserResponse, int64) {
|
||||
resp := make([]*response.UserResponse, 0)
|
||||
session := SqlDB.NewSession()
|
||||
session.Table([]string{"sys_user", "u"}).Distinct("u.user_id", "u.dept_id", "u.user_name", "u.nick_name", "u.email", "u.phone_number", "u.status", "u.create_time").
|
||||
Join("LEFT", []string{"sys_dept", "d"}, "u.dept_id = d.dept_id").
|
||||
Join("LEFT", []string{"sys_user_role", "ur"}, "u.user_id = ur.user_id").
|
||||
Join("LEFT", []string{"sys_role", "r"}, "r.role_id = ur.role_id").Where("u.del_flag = '0'").And("r.role_id = ? or r.role_id IS NULL", query.RoleId).
|
||||
And("u.user_id not in (select u.user_id from sys_user u inner join sys_user_role ur on u.user_id = ur.user_id and ur.role_id = ?)", query.RoleId)
|
||||
if gotool.StrUtils.HasNotEmpty(query.UserName) {
|
||||
session.And("u.user_name like concat('%', ?, '%')", query.UserName)
|
||||
}
|
||||
if gotool.StrUtils.HasNotEmpty(query.PhoneNumber) {
|
||||
session.And("u.phone_number like concat('%', ?, '%')", query.PhoneNumber)
|
||||
}
|
||||
total, _ := page.GetTotal(session.Clone())
|
||||
err := session.Limit(query.PageSize, page.StartSize(query.PageNum, query.PageSize)).Find(&resp)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return nil, 0
|
||||
}
|
||||
return resp, total
|
||||
}
|
||||
|
||||
// UpdatePwd 修改密码
|
||||
func (d UserDao) UpdatePwd(id int64, hash string) int64 {
|
||||
user := entity.SysUser{}
|
||||
user.UserId = id
|
||||
user.Password = hash
|
||||
session := SqlDB.NewSession()
|
||||
session.Begin()
|
||||
update, err := session.Cols("password").Where("user_id = ?", id).Update(&user)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
session.Rollback()
|
||||
return 0
|
||||
}
|
||||
session.Commit()
|
||||
return update
|
||||
}
|
||||
|
||||
// UpdateAvatar 修改头像
|
||||
func (d UserDao) UpdateAvatar(info *response.UserResponse) int64 {
|
||||
user := entity.SysUser{
|
||||
Avatar: info.Avatar,
|
||||
UserId: info.UserId,
|
||||
UpdateBy: info.UserName,
|
||||
UpdateTime: time.Now(),
|
||||
}
|
||||
session := SqlDB.NewSession()
|
||||
session.Begin()
|
||||
update, err := session.Cols("avatar", "update_by", "update_time").Where("user_id = ?", user.UserId).Update(&user)
|
||||
if err != nil {
|
||||
session.Rollback()
|
||||
common.ErrorLog(err)
|
||||
return 0
|
||||
}
|
||||
session.Commit()
|
||||
return update
|
||||
}
|
||||
|
||||
func (d UserDao) UpdateStatus(info request.UserBody) int64 {
|
||||
user := entity.SysUser{
|
||||
UserId: info.UserId,
|
||||
Status: info.Status,
|
||||
UpdateBy: info.UserName,
|
||||
UpdateTime: time.Now(),
|
||||
}
|
||||
session := SqlDB.NewSession()
|
||||
session.Begin()
|
||||
update, err := session.Cols("status", "update_by", "update_time").Where("user_id = ?", user.UserId).Update(&user)
|
||||
if err != nil {
|
||||
session.Rollback()
|
||||
common.ErrorLog(err)
|
||||
return 0
|
||||
}
|
||||
session.Commit()
|
||||
return update
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"cutego/modules/core/entity"
|
||||
"cutego/pkg/common"
|
||||
)
|
||||
|
||||
type UserPostDao struct {
|
||||
}
|
||||
|
||||
// BatchInsert 批量新增用户岗位信息
|
||||
func (d UserPostDao) BatchInsert(posts []entity.SysUserPost) {
|
||||
session := SqlDB.NewSession()
|
||||
session.Begin()
|
||||
_, err := session.Table(entity.SysUserPost{}.TableName()).Insert(&posts)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
session.Rollback()
|
||||
return
|
||||
}
|
||||
session.Commit()
|
||||
}
|
||||
|
||||
// Delete 删除用户和岗位关系
|
||||
func (d UserPostDao) Delete(id int64) {
|
||||
post := entity.SysUserPost{
|
||||
UserId: id,
|
||||
}
|
||||
session := SqlDB.NewSession()
|
||||
session.Begin()
|
||||
_, err := session.Delete(&post)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
session.Rollback()
|
||||
}
|
||||
session.Commit()
|
||||
}
|
||||
|
||||
// CountById 通过岗位ID查询岗位使用数量
|
||||
func (d UserPostDao) CountById(id int64) int64 {
|
||||
count, err := SqlDB.NewSession().Table("sys_user_post").Cols("post_id").Where("post_id = ?", id).Count()
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
return 0
|
||||
}
|
||||
return count
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"cutego/modules/core/api/v1/request"
|
||||
"cutego/modules/core/entity"
|
||||
"cutego/pkg/common"
|
||||
)
|
||||
|
||||
type UserRoleDao struct {
|
||||
}
|
||||
|
||||
// BatchInsert 批量新增用户角色信息
|
||||
func (d UserRoleDao) BatchInsert(roles []entity.SysUserRole) {
|
||||
session := SqlDB.NewSession()
|
||||
session.Begin()
|
||||
_, err := session.Table(entity.SysUserRole{}.TableName()).Insert(&roles)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
session.Rollback()
|
||||
return
|
||||
}
|
||||
session.Commit()
|
||||
}
|
||||
|
||||
// Delete 删除用户和角色关系
|
||||
func (d UserRoleDao) Delete(id int64) {
|
||||
role := entity.SysUserRole{
|
||||
UserId: id,
|
||||
}
|
||||
session := SqlDB.NewSession()
|
||||
session.Begin()
|
||||
_, err := session.Delete(&role)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
session.Rollback()
|
||||
return
|
||||
}
|
||||
session.Commit()
|
||||
}
|
||||
|
||||
// DeleteAuthUser 取消用户授权
|
||||
func (d UserRoleDao) DeleteAuthUser(role entity.SysUserRole) int64 {
|
||||
session := SqlDB.NewSession()
|
||||
session.Begin()
|
||||
i, err := session.Delete(&role)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
session.Rollback()
|
||||
return 0
|
||||
}
|
||||
session.Commit()
|
||||
return i
|
||||
}
|
||||
|
||||
// InsertAuthUsers 批量选择用户授权
|
||||
func (d UserRoleDao) InsertAuthUsers(body request.UserRoleBody) int64 {
|
||||
ids := body.UserIds
|
||||
roles := make([]entity.SysUserRole, 0)
|
||||
for i := 0; i < len(ids); i++ {
|
||||
role := entity.SysUserRole{
|
||||
RoleId: body.RoleId,
|
||||
UserId: ids[i],
|
||||
}
|
||||
roles = append(roles, role)
|
||||
}
|
||||
session := SqlDB.NewSession()
|
||||
session.Begin()
|
||||
insert, err := session.Insert(&roles)
|
||||
if err != nil {
|
||||
common.ErrorLog(err)
|
||||
session.Rollback()
|
||||
return 0
|
||||
}
|
||||
session.Commit()
|
||||
return insert
|
||||
}
|
||||
Reference in New Issue
Block a user