初始
This commit is contained in:
251
internal/app/system/logic/busSafety/bus_safety.go
Normal file
251
internal/app/system/logic/busSafety/bus_safety.go
Normal file
@ -0,0 +1,251 @@
|
||||
// ==========================================================================
|
||||
// GFast自动生成logic操作代码。
|
||||
// 生成日期:2023-08-02 15:40:48
|
||||
// 生成路径: internal/app/system/logic/bus_safety.go
|
||||
// 生成人:gfast
|
||||
// desc:安全例会管理
|
||||
// company:云南奇讯科技有限公司
|
||||
// ==========================================================================
|
||||
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"github.com/tiger1103/gfast/v3/api/v1/common/coryCommon"
|
||||
"github.com/tiger1103/gfast/v3/api/v1/system"
|
||||
comModel "github.com/tiger1103/gfast/v3/internal/app/common/model"
|
||||
"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/service"
|
||||
"github.com/tiger1103/gfast/v3/library/liberr"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func init() {
|
||||
service.RegisterBusSafety(New())
|
||||
}
|
||||
|
||||
func New() *sBusSafety {
|
||||
return &sBusSafety{}
|
||||
}
|
||||
|
||||
type sBusSafety struct{}
|
||||
|
||||
func (s *sBusSafety) WxAddFunc(ctx context.Context, req *system.WxBusSafetyAddReq) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
req.SafetyDocument[0].Url = strings.ReplaceAll(req.SafetyDocument[0].Url, "/file", "/wxfile") + ","
|
||||
_, err = dao.BusSafety.Ctx(ctx).Insert(do.BusSafety{
|
||||
SafetyName: req.SafetyName,
|
||||
SafetyExplain: req.SafetyExplain,
|
||||
SafetyDocument: req.SafetyDocument[0].Url,
|
||||
SafetyType: req.SafetyType,
|
||||
ProjectId: req.ProjectId,
|
||||
SafetyGrade: req.SafetyGrade,
|
||||
StartDate: req.StartDate,
|
||||
UserType: "2",
|
||||
CreateBy: req.Openid,
|
||||
})
|
||||
liberr.ErrIsNil(ctx, err, "添加失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sBusSafety) List(ctx context.Context, req *system.BusSafetySearchReq) (listRes *system.BusSafetySearchRes, err error) {
|
||||
listRes = new(system.BusSafetySearchRes)
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
m := dao.BusSafety.Ctx(ctx).WithAll()
|
||||
if req.SafetyName != "" {
|
||||
m = m.Where(dao.BusSafety.Columns().SafetyName+" like ?", "%"+req.SafetyName+"%")
|
||||
}
|
||||
if req.SafetyExplain != "" {
|
||||
m = m.Where(dao.BusSafety.Columns().SafetyExplain+" = ?", req.SafetyExplain)
|
||||
}
|
||||
if req.SafetyDocument != "" {
|
||||
m = m.Where(dao.BusSafety.Columns().SafetyDocument+" = ?", req.SafetyDocument)
|
||||
}
|
||||
if req.SafetyType != "" {
|
||||
m = m.Where(dao.BusSafety.Columns().SafetyType+" = ?", req.SafetyType)
|
||||
}
|
||||
if req.ProjectId != "" {
|
||||
m = m.Where(dao.BusSafety.Columns().ProjectId+" = ?", gconv.Int64(req.ProjectId))
|
||||
}
|
||||
if req.Status != "" {
|
||||
m = m.Where(dao.BusSafety.Columns().Status+" = ?", req.Status)
|
||||
}
|
||||
if req.CreateBy != "" {
|
||||
m = m.Where(dao.BusSafety.Columns().CreateBy+" = ?", req.CreateBy)
|
||||
}
|
||||
if req.UpdateAt != "" {
|
||||
m = m.Where(dao.BusSafety.Columns().UpdateAt+" = ?", req.UpdateAt)
|
||||
}
|
||||
if len(req.DateRange) != 0 {
|
||||
m = m.Where(dao.BusSafety.Columns().CreatedAt+" >=? AND "+dao.BusSafety.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.BusSafetyInfoRes
|
||||
err = m.Fields(system.BusSafetySearchRes{}).Page(req.PageNum, req.PageSize).Order(order).Scan(&res)
|
||||
liberr.ErrIsNil(ctx, err, "获取数据失败")
|
||||
listRes.List = make([]*model.BusSafetyListRes, len(res))
|
||||
for k, v := range res {
|
||||
//safetyDocument := ([]*comModel.UpFile)(nil)
|
||||
//err = gjson.DecodeTo(v.SafetyDocument, &safetyDocument)
|
||||
//liberr.ErrIsNil(ctx, err)
|
||||
listRes.List[k] = &model.BusSafetyListRes{
|
||||
Id: v.Id,
|
||||
SafetyName: v.SafetyName,
|
||||
SafetyExplain: v.SafetyExplain,
|
||||
//SafetyDocument: v.SafetyDocument,
|
||||
SafetyType: v.SafetyType,
|
||||
ProjectId: v.ProjectId,
|
||||
Status: v.Status,
|
||||
SafetyGrade: v.SafetyGrade,
|
||||
StartDate: v.StartDate,
|
||||
CreatedAt: v.CreatedAt,
|
||||
}
|
||||
}
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sBusSafety) GetById(ctx context.Context, id int64) (res *model.BusSafetyInfoRes, err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
//1、查询原来数据进行删除
|
||||
var ufList []*comModel.UpFile
|
||||
err = dao.BusSafety.Ctx(ctx).WithAll().Where(dao.BusSafety.Columns().Id, id).Scan(&res)
|
||||
if err == nil {
|
||||
split := strings.Split(res.SafetyDocument, ";")
|
||||
for _, data := range split {
|
||||
var uf *comModel.UpFile
|
||||
err := json.Unmarshal([]byte(data), &uf)
|
||||
if err != nil {
|
||||
uf = new(comModel.UpFile)
|
||||
uf.Name = res.SafetyName
|
||||
uf.Url = res.SafetyDocument
|
||||
}
|
||||
ufList = append(ufList, uf)
|
||||
}
|
||||
}
|
||||
res.SafetyDocumentObj = ufList
|
||||
liberr.ErrIsNil(ctx, err, "获取信息失败")
|
||||
if res.UserType == "1" {
|
||||
//获取创建人 更新人
|
||||
by := coryCommon.New().CreateByOrUpdateBy(ctx, res)
|
||||
infoRes := by.(model.BusSafetyInfoRes)
|
||||
res = &infoRes
|
||||
}
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sBusSafety) Add(ctx context.Context, req *system.BusSafetyAddReq) (err error) {
|
||||
if len(req.SafetyDocument) > 2 {
|
||||
err = errors.New("最多仅支持2个文件!")
|
||||
return err
|
||||
}
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
safety := do.BusSafety{
|
||||
SafetyName: req.SafetyName,
|
||||
SafetyExplain: req.SafetyExplain,
|
||||
SafetyType: req.SafetyType,
|
||||
ProjectId: req.ProjectId,
|
||||
SafetyGrade: req.SafetyGrade,
|
||||
StartDate: req.StartDate,
|
||||
}
|
||||
//2为小程序,小程序要为wxfile开头
|
||||
if req.WxOrPc != "1" {
|
||||
req.WxOrPc = "2"
|
||||
safety.CreateBy = req.Openid
|
||||
if len(req.SafetyDocument) > 0 {
|
||||
for i := range req.SafetyDocument {
|
||||
req.SafetyDocument[i].Url = strings.Replace(req.SafetyDocument[0].Url, "file", "wxfile", 1)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
safety.CreateBy = ct.New().GetLoginUser(ctx).Id
|
||||
}
|
||||
safety.UserType = req.WxOrPc
|
||||
if len(req.SafetyDocument) > 0 {
|
||||
var str = ""
|
||||
for _, data := range req.SafetyDocument {
|
||||
marshal, _ := json.Marshal(data)
|
||||
str = str + string(marshal) + ";"
|
||||
}
|
||||
if len(str) > 0 {
|
||||
safety.SafetyDocument = str[:len(str)-1]
|
||||
}
|
||||
}
|
||||
_, err = dao.BusSafety.Ctx(ctx).Insert(safety)
|
||||
liberr.ErrIsNil(ctx, err, "添加失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sBusSafety) Edit(ctx context.Context, req *system.BusSafetyEditReq) (err error) {
|
||||
if len(req.SafetyDocument) > 2 {
|
||||
err = errors.New("最多仅支持2个文件!")
|
||||
return err
|
||||
}
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
safety := do.BusSafety{
|
||||
SafetyName: req.SafetyName,
|
||||
SafetyExplain: req.SafetyExplain,
|
||||
SafetyType: req.SafetyType,
|
||||
Status: req.Status,
|
||||
SafetyGrade: req.SafetyGrade,
|
||||
}
|
||||
//2、为小程序,小程序要为wxfile开头
|
||||
if req.WxOrPc != "1" {
|
||||
req.WxOrPc = "2"
|
||||
safety.CreateBy = req.Openid
|
||||
if len(req.SafetyDocument) > 0 {
|
||||
for i := range req.SafetyDocument {
|
||||
req.SafetyDocument[i].Url = strings.Replace(req.SafetyDocument[0].Url, "file", "wxfile", 1)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
safety.UpdateBy = ct.New().GetLoginUser(ctx).Id
|
||||
}
|
||||
//3、最终存储数据
|
||||
safety.UserType = req.WxOrPc
|
||||
if len(req.SafetyDocument) > 0 {
|
||||
var str = ""
|
||||
for _, data := range req.SafetyDocument {
|
||||
marshal, _ := json.Marshal(data)
|
||||
str = str + string(marshal) + ";"
|
||||
}
|
||||
if len(str) > 0 {
|
||||
safety.SafetyDocument = str[:len(str)-1]
|
||||
}
|
||||
}
|
||||
_, err = dao.BusSafety.Ctx(ctx).WherePri(req.Id).Update(safety)
|
||||
liberr.ErrIsNil(ctx, err, "修改失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sBusSafety) Delete(ctx context.Context, ids []int64) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
_, err = dao.BusSafety.Ctx(ctx).Delete(dao.BusSafety.Columns().Id+" in (?)", ids)
|
||||
liberr.ErrIsNil(ctx, err, "删除失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user