初始
This commit is contained in:
@ -0,0 +1,206 @@
|
||||
// ==========================================================================
|
||||
// GFast自动生成logic操作代码。
|
||||
// 生成日期:2023-11-20 11:18:36
|
||||
// 生成路径: internal/app/system/logic/bus_violation_level.go
|
||||
// 生成人:gfast
|
||||
// desc:违章等级
|
||||
// company:云南奇讯科技有限公司
|
||||
// ==========================================================================
|
||||
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/tiger1103/gfast/v3/api/v1/system"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/system/consts"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/system/dao"
|
||||
ct "github.com/tiger1103/gfast/v3/internal/app/system/logic/context"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/system/model"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/system/model/do"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/system/model/entity"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/system/service"
|
||||
"github.com/tiger1103/gfast/v3/library/liberr"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func init() {
|
||||
service.RegisterBusViolationLevel(New())
|
||||
}
|
||||
|
||||
func New() *sBusViolationLevel {
|
||||
return &sBusViolationLevel{}
|
||||
}
|
||||
|
||||
type sBusViolationLevel struct{}
|
||||
|
||||
func (s *sBusViolationLevel) List(ctx context.Context, req *system.BusViolationLevelSearchReq) (listRes *system.BusViolationLevelSearchRes, err error) {
|
||||
listRes = new(system.BusViolationLevelSearchRes)
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
m := dao.BusViolationLevel.Ctx(ctx).WithAll()
|
||||
if req.TourType != "" {
|
||||
m = m.Where(dao.BusViolationLevel.Columns().TourType+" = ?", req.TourType)
|
||||
}
|
||||
if len(req.DateRange) != 0 {
|
||||
m = m.Where(dao.BusViolationLevel.Columns().CreatedAt+" >=? AND "+dao.BusViolationLevel.Columns().CreatedAt+" <=?", req.DateRange[0], req.DateRange[1])
|
||||
}
|
||||
listRes.Total, err = m.Count()
|
||||
liberr.ErrIsNil(ctx, err, "获取总行数失败")
|
||||
if req.PageNum == 0 {
|
||||
req.PageNum = 1
|
||||
}
|
||||
listRes.CurrentPage = req.PageNum
|
||||
if req.PageSize == 0 {
|
||||
req.PageSize = consts.PageSize
|
||||
}
|
||||
order := "id desc"
|
||||
if req.OrderBy != "" {
|
||||
order = req.OrderBy
|
||||
}
|
||||
var res []*model.BusViolationLevelInfoRes
|
||||
err = m.Fields(system.BusViolationLevelSearchRes{}).Page(req.PageNum, req.PageSize).Order(order).Scan(&res)
|
||||
liberr.ErrIsNil(ctx, err, "获取数据失败")
|
||||
listRes.List = make([]*model.BusViolationLevelListRes, len(res))
|
||||
for k, v := range res {
|
||||
listRes.List[k] = &model.BusViolationLevelListRes{
|
||||
Id: v.Id,
|
||||
Grade: v.Grade,
|
||||
Color: v.Color,
|
||||
TourType: v.TourType,
|
||||
CreatedAt: v.CreatedAt,
|
||||
Risx: v.Risx,
|
||||
}
|
||||
}
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sBusViolationLevel) GetById(ctx context.Context, id int64) (res *model.BusViolationLevelInfoRes, err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
//1、获取等级信息
|
||||
err = dao.BusViolationLevel.Ctx(ctx).WithAll().Where(dao.BusViolationLevel.Columns().Id, id).Scan(&res)
|
||||
liberr.ErrIsNil(ctx, err, "获取信息失败")
|
||||
err, postEntity := SelectLevelByPostInfo(ctx, id)
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
res.PostEntity = append(res.PostEntity, postEntity...)
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sBusViolationLevel) Add(ctx context.Context, req *system.BusViolationLevelAddReq) (err error) {
|
||||
err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
//1、级别不能重复、添加了的类别也不许重复、颜色也不许重复
|
||||
tourTypeStr := strings.Split(req.TourType, ",")
|
||||
count, err := dao.BusViolationLevel.Ctx(ctx).Where("grade", req.Grade).WhereOr("color", req.Color).WhereOrIn("tour_type", tourTypeStr).Count()
|
||||
if count > 0 {
|
||||
err = errors.New("请检查当前等级、颜色、类型是否被重复添加!")
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
return
|
||||
}
|
||||
//2、新增数据
|
||||
result, err := dao.BusViolationLevel.Ctx(ctx).Insert(do.BusViolationLevel{
|
||||
ProjectId: req.ProjectId,
|
||||
Grade: req.Grade,
|
||||
Color: req.Color,
|
||||
TourType: req.TourType,
|
||||
Risx: req.Risx,
|
||||
WxOrPc: "1",
|
||||
CreateBy: ct.New().GetLoginUser(ctx).Id,
|
||||
})
|
||||
liberr.ErrIsNil(ctx, err, "添加失败")
|
||||
//3、等级与岗位进行关联
|
||||
if len(req.Posts) == 0 {
|
||||
return
|
||||
}
|
||||
id, _ := result.LastInsertId()
|
||||
err = InsertLevelPost(ctx, req.Posts, id)
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
})
|
||||
return err
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sBusViolationLevel) Edit(ctx context.Context, req *system.BusViolationLevelEditReq) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
////1、级别不能重复、添加了的类别也不许重复、颜色也不许重复
|
||||
//tourTypeStr := strings.Split(req.TourType, ",")
|
||||
//for i := range tourTypeStr {
|
||||
// count, err := dao.BusViolationLevel.Ctx(ctx).
|
||||
// WhereNot("id", req.Id).
|
||||
// Where("grade = ? OR color = ? OR tour_type like (?)", req.Grade, req.Color, "%"+tourTypeStr[i]+"%").Count()
|
||||
// if count > 0 {
|
||||
// err = errors.New("请检查当前等级、颜色、类型是否被重复添加!")
|
||||
// liberr.ErrIsNil(ctx, err)
|
||||
// return
|
||||
// }
|
||||
//}
|
||||
//2、修改数据
|
||||
_, err := dao.BusViolationLevel.Ctx(ctx).WherePri(req.Id).Update(do.BusViolationLevel{
|
||||
Grade: req.Grade,
|
||||
Color: req.Color,
|
||||
TourType: req.TourType,
|
||||
Risx: req.Risx,
|
||||
WxOrPc: "1",
|
||||
UpdateBy: ct.New().GetLoginUser(ctx).Id,
|
||||
})
|
||||
liberr.ErrIsNil(ctx, err, "修改失败")
|
||||
//3、等级与岗位进行关联(先删再增加)
|
||||
if len(req.Posts) == 0 {
|
||||
return
|
||||
}
|
||||
//id, _ := result.LastInsertId()
|
||||
_, err = g.DB().Model("bus_violation_level_post").Ctx(ctx).Where("level", req.Id).Delete()
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
err = InsertLevelPost(ctx, req.Posts, req.Id)
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sBusViolationLevel) Delete(ctx context.Context, ids []int64) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
_, err = dao.BusViolationLevel.Ctx(ctx).Delete(dao.BusViolationLevel.Columns().Id+" in (?)", ids)
|
||||
_, err = g.DB().Model("bus_violation_level_post").Ctx(ctx).Unscoped().Delete(" in (?)", ids)
|
||||
liberr.ErrIsNil(ctx, err, "删除失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// InsertLevelPost 插入数据(等级与岗位关联)
|
||||
func InsertLevelPost(ctx context.Context, posts []int64, id int64) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
var violationLevelPostEntity []*model.ViolationLevelPost
|
||||
for i := range posts {
|
||||
violationLevelPostEntity = append(violationLevelPostEntity, &model.ViolationLevelPost{
|
||||
Level: id,
|
||||
Post: posts[i],
|
||||
})
|
||||
}
|
||||
_, err = g.DB().Model("bus_violation_level_post").Ctx(ctx).Insert(violationLevelPostEntity)
|
||||
liberr.ErrIsNil(ctx, err, "岗位关联失败")
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
// SelectLevelByPostInfo 通过等级id获取到岗位信息
|
||||
func SelectLevelByPostInfo(ctx context.Context, id int64) (err error, postEntity []*entity.SysPost) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
//2、获取到当前等级的岗位信息
|
||||
array, err := g.DB().Model("bus_violation_level_post").Ctx(ctx).Where("level", id).Fields("post").Array()
|
||||
liberr.ErrIsNil(ctx, err, "获取岗位失败")
|
||||
if len(array) > 0 {
|
||||
//3、通过岗位信息得到岗位信息
|
||||
dao.SysPost.Ctx(ctx).Fields("post_id,post_code,post_name").
|
||||
Where("post_id in (?)", array).
|
||||
Where("status = 1").
|
||||
OrderAsc("post_sort").
|
||||
Scan(&postEntity)
|
||||
}
|
||||
|
||||
})
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user