调整xorm层级

This commit is contained in:
2023-01-18 16:00:50 +08:00
parent 0f8c1f989b
commit a89c86023e
67 changed files with 500 additions and 460 deletions
+24 -23
View File
@@ -1,8 +1,9 @@
package dao
import (
"cutego/modules"
"cutego/modules/core/api/v1/request"
"cutego/modules/core/entity"
"cutego/modules/core/dataobject"
"cutego/pkg/common"
"cutego/pkg/page"
"github.com/druidcaesa/gotool"
@@ -19,9 +20,9 @@ func (d PostDao) sqlSelectJoin(session *xorm.Session) *xorm.Session {
}
// SelectAll 查询所有岗位数据, 数据库操作
func (d PostDao) SelectAll() []*entity.SysPost {
session := SqlDB.NewSession()
posts := make([]*entity.SysPost, 0)
func (d PostDao) SelectAll() []*dataobject.SysPost {
session := modules.SqlDB.NewSession()
posts := make([]*dataobject.SysPost, 0)
err := session.Find(&posts)
if err != nil {
common.ErrorLog(err)
@@ -33,7 +34,7 @@ func (d PostDao) SelectAll() []*entity.SysPost {
// SelectPostListByUserId 根据用户id查询岗位id集合
func (d PostDao) SelectPostListByUserId(userId int64) *[]int64 {
var ids []int64
selectSql := d.sqlSelectJoin(SqlDB.NewSession())
selectSql := d.sqlSelectJoin(modules.SqlDB.NewSession())
err := selectSql.Where("u.user_id = ?", userId).Cols("p.post_id").Find(&ids)
if err != nil {
common.ErrorLog(err)
@@ -43,9 +44,9 @@ func (d PostDao) SelectPostListByUserId(userId int64) *[]int64 {
}
// SelectPage 查询岗位分页数据
func (d PostDao) SelectPage(query request.PostQuery) (*[]entity.SysPost, int64) {
posts := make([]entity.SysPost, 0)
session := SqlDB.NewSession().Table(entity.SysPost{}.TableName())
func (d PostDao) SelectPage(query request.PostQuery) (*[]dataobject.SysPost, int64) {
posts := make([]dataobject.SysPost, 0)
session := modules.SqlDB.NewSession().Table(dataobject.SysPost{}.TableName())
if gotool.StrUtils.HasNotEmpty(query.PostCode) {
session.And("post_code like concat('%', ?, '%')", query.PostCode)
}
@@ -65,8 +66,8 @@ func (d PostDao) SelectPage(query request.PostQuery) (*[]entity.SysPost, int64)
}
// CheckPostNameUnique 校验岗位名称是否存在
func (d PostDao) CheckPostNameUnique(post entity.SysPost) int64 {
session := SqlDB.NewSession().Table("sys_post").Cols("post_id").
func (d PostDao) CheckPostNameUnique(post dataobject.SysPost) int64 {
session := modules.SqlDB.NewSession().Table("sys_post").Cols("post_id").
Where("post_name = ?", post.PostName)
if post.PostId > 0 {
session.And("post_id != ?", post.PostId)
@@ -76,8 +77,8 @@ func (d PostDao) CheckPostNameUnique(post entity.SysPost) int64 {
}
// CheckPostCodeUnique 校验岗位编码是否存在
func (d PostDao) CheckPostCodeUnique(post entity.SysPost) int64 {
session := SqlDB.NewSession().Table("sys_post").Cols("post_id").
func (d PostDao) CheckPostCodeUnique(post dataobject.SysPost) int64 {
session := modules.SqlDB.NewSession().Table("sys_post").Cols("post_id").
Where("post_code = ?", post.PostCode)
if post.PostId > 0 {
session.And("post_id != ?", post.PostId)
@@ -87,8 +88,8 @@ func (d PostDao) CheckPostCodeUnique(post entity.SysPost) int64 {
}
// Insert 添加岗位数据
func (d PostDao) Insert(post entity.SysPost) int64 {
session := SqlDB.NewSession()
func (d PostDao) Insert(post dataobject.SysPost) int64 {
session := modules.SqlDB.NewSession()
session.Begin()
insert, err := session.Insert(&post)
if err != nil {
@@ -101,8 +102,8 @@ func (d PostDao) Insert(post entity.SysPost) int64 {
}
// GetPostById 根据id查询岗位数据
func (d PostDao) GetPostById(post entity.SysPost) *entity.SysPost {
_, err := SqlDB.NewSession().Where("post_id = ?", post.PostId).Get(&post)
func (d PostDao) GetPostById(post dataobject.SysPost) *dataobject.SysPost {
_, err := modules.SqlDB.NewSession().Where("post_id = ?", post.PostId).Get(&post)
if err != nil {
common.ErrorLog(err)
return nil
@@ -113,9 +114,9 @@ func (d PostDao) GetPostById(post entity.SysPost) *entity.SysPost {
// Delete 批量删除岗位
func (d PostDao) Delete(posts []int64) int64 {
session := SqlDB.NewSession()
session := modules.SqlDB.NewSession()
session.Begin()
i, err := session.In("post_id", posts).Delete(&entity.SysPost{})
i, err := session.In("post_id", posts).Delete(&dataobject.SysPost{})
if err != nil {
session.Rollback()
common.ErrorLog(err)
@@ -126,8 +127,8 @@ func (d PostDao) Delete(posts []int64) int64 {
}
// Update 修改岗位数据
func (d PostDao) Update(post entity.SysPost) bool {
session := SqlDB.NewSession()
func (d PostDao) Update(post dataobject.SysPost) bool {
session := modules.SqlDB.NewSession()
session.Begin()
_, err := session.Where("post_id = ?", post.PostId).Update(&post)
if err != nil {
@@ -139,9 +140,9 @@ func (d PostDao) Update(post entity.SysPost) bool {
return true
}
func (d PostDao) SelectPostByUserName(name string) *[]entity.SysPost {
posts := make([]entity.SysPost, 0)
session := SqlDB.NewSession().Table([]string{entity.SysPost{}.TableName(), "p"})
func (d PostDao) SelectPostByUserName(name string) *[]dataobject.SysPost {
posts := make([]dataobject.SysPost, 0)
session := modules.SqlDB.NewSession().Table([]string{dataobject.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)