初始
This commit is contained in:
@ -0,0 +1,420 @@
|
||||
// ==========================================================================
|
||||
// GFast自动生成logic操作代码。
|
||||
// 生成日期:2024-04-07 17:00:45
|
||||
// 生成路径: internal/app/system/logic/bus_hse_security_log.go
|
||||
// 生成人:gfast
|
||||
// desc:HSE安全日志
|
||||
// company:云南奇讯科技有限公司
|
||||
// ==========================================================================
|
||||
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/v2/container/gvar"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"github.com/jinzhu/copier"
|
||||
"github.com/samber/lo"
|
||||
"github.com/tiger1103/gfast/v3/api/v1/common/coryCommon"
|
||||
"github.com/tiger1103/gfast/v3/api/v1/system"
|
||||
wxSystem "github.com/tiger1103/gfast/v3/api/wxApplet/wxApplet"
|
||||
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"
|
||||
"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"
|
||||
systemService "github.com/tiger1103/gfast/v3/internal/app/system/service"
|
||||
"github.com/tiger1103/gfast/v3/library/liberr"
|
||||
)
|
||||
|
||||
func init() {
|
||||
service.RegisterBusHseSecurityLog(New())
|
||||
}
|
||||
|
||||
func New() *sBusHseSecurityLog {
|
||||
return &sBusHseSecurityLog{}
|
||||
}
|
||||
|
||||
type sBusHseSecurityLog struct{}
|
||||
|
||||
var suffix = ".jpg,.jpeg,.png"
|
||||
|
||||
func (s *sBusHseSecurityLog) HseSecurityLogAppletDetails(ctx context.Context, req *wxSystem.HseSecurityLogAppletDetailsReq) (res *wxSystem.HseSecurityLogAppletDetailsRes, err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
err = dao.BusHseSecurityLog.Ctx(ctx).As("a").
|
||||
LeftJoin("sys_dict_data", "b", "b.dict_type = 'climate' and b.dict_value = a.climate").
|
||||
LeftJoin(dao.SysProject.Table(), "c", "c.id = a.project_id").
|
||||
Fields("a.*,"+
|
||||
"b.dict_label as climateName,"+
|
||||
"c.project_name as projectName").
|
||||
Where("a."+dao.BusHseSecurityLog.Columns().Id, req.Id).Scan(&res)
|
||||
// 获取创建人 更新人名称
|
||||
createdBy := res.CreatedBy
|
||||
by := coryCommon.New().CreateByOrUpdateBy(ctx, res)
|
||||
infoRes := by.(wxSystem.HseSecurityLogAppletDetailsRes)
|
||||
infoRes.Fill = infoRes.CreatedBy
|
||||
infoRes.CreatedBy = createdBy
|
||||
res = &infoRes
|
||||
liberr.ErrIsNil(ctx, err, "获取信息失败")
|
||||
// 获取附件
|
||||
var pathEntity []*model.HseSecurityLogPath
|
||||
err = g.DB().Model("bus_hse_security_log_path").Ctx(ctx).Where("ticket_id", req.Id).Scan(&pathEntity)
|
||||
liberr.ErrIsNil(ctx, err, "查询失败")
|
||||
res.FileList = pathEntity
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sBusHseSecurityLog) HseSecurityLogAppletList(ctx context.Context, req *wxSystem.HseSecurityLogAppletListReq) (listRes *wxSystem.HseSecurityLogAppletListRes, err error) {
|
||||
listRes = new(wxSystem.HseSecurityLogAppletListRes)
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
// 1、根据名字模糊搜索到项目id
|
||||
var projectIds []string
|
||||
if req.ProjectName != "" {
|
||||
value, err := dao.SysProject.Ctx(ctx).
|
||||
Where("(project_name like ? or short_name like ?)", "%"+req.ProjectName+"%", "%"+req.ProjectName+"%").
|
||||
Fields("GROUP_CONCAT(id)").Value()
|
||||
liberr.ErrIsNil(ctx, err, "模糊查询失败")
|
||||
split := strings.Split(value.String(), ",")
|
||||
for i := range split {
|
||||
if split[i] != "" {
|
||||
projectIds = append(projectIds, split[i])
|
||||
}
|
||||
}
|
||||
if len(projectIds) == 0 {
|
||||
return
|
||||
}
|
||||
}
|
||||
// 2、查询数据
|
||||
m := dao.BusHseSecurityLog.Ctx(ctx).As("a").
|
||||
LeftJoin(dao.SysProject.Table(), "c", "c.id = a.project_id").
|
||||
Fields("a.id,a.created_by,a.created_at,a.date_of_occurrence,c.project_name as projectName")
|
||||
if req.CreatedAt != "" {
|
||||
m = m.Where("a.date_of_occurrence = ?", req.CreatedAt)
|
||||
//m = m.Where("DATE_FORMAT(a.created_at,'%Y-%m-%d') = ?", req.CreatedAt)
|
||||
}
|
||||
if req.OpenId != "" {
|
||||
m = m.Where("a."+dao.BusHseSecurityLog.Columns().CreatedBy+" = ?", req.OpenId)
|
||||
}
|
||||
if len(req.DateRange) != 0 {
|
||||
m = m.Where("a."+dao.BusHseSecurityLog.Columns().CreatedAt+" >=? AND "+"a."+dao.BusHseSecurityLog.Columns().CreatedAt+" <=?", req.DateRange[0], req.DateRange[1])
|
||||
}
|
||||
array, err := m.Array()
|
||||
liberr.ErrIsNil(ctx, err, "获取总行数失败")
|
||||
listRes.Total = len(array)
|
||||
if req.PageNum == 0 {
|
||||
req.PageNum = 1
|
||||
}
|
||||
listRes.CurrentPage = req.PageNum
|
||||
if req.PageSize == 0 {
|
||||
req.PageSize = consts.PageSize
|
||||
}
|
||||
order := "a.id desc"
|
||||
if req.OrderBy != "" {
|
||||
order = req.OrderBy
|
||||
}
|
||||
var res []*model.BusHseSecurityLogInfoTwoRes
|
||||
err = m.Page(req.PageNum, req.PageSize).Order(order).Scan(&res)
|
||||
liberr.ErrIsNil(ctx, err, "获取数据失败")
|
||||
for i := range res {
|
||||
res[i].Fill = coryCommon.SelectByString(ctx, coryCommon.IsNumeric(res[i].CreatedBy), res[i].CreatedBy)
|
||||
}
|
||||
listRes.List = res
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sBusHseSecurityLog) HseSecurityLogAppletAdd(ctx context.Context, req *wxSystem.HseSecurityLogAppletAddReq) (err error) {
|
||||
err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
// 1、初始新增数据
|
||||
entity := do.BusHseSecurityLog{
|
||||
ProjectId: req.ProjectId,
|
||||
DateOfOccurrence: req.DateOfOccurrence,
|
||||
Remark: req.Remark,
|
||||
CreatedBy: req.Openid,
|
||||
}
|
||||
if len(req.File) == 0 {
|
||||
securityLog := do.BusHseSecurityLog{
|
||||
AirTemperatureMax: req.AirTemperatureMax,
|
||||
AirTemperatureMin: req.AirTemperatureMin,
|
||||
Climate: req.Climate,
|
||||
Progress: req.Progress,
|
||||
JobContent: req.JobContent,
|
||||
DiscloseTheFacts: req.DiscloseTheFacts,
|
||||
ProgressOfActivity: req.ProgressOfActivity,
|
||||
Examine: req.Examine,
|
||||
Implementation: req.Implementation,
|
||||
SafetyInspectionSituation: req.SafetyInspectionSituation,
|
||||
StoppageOrOvertime: req.StoppageOrOvertime,
|
||||
OtherRecords: req.OtherRecords,
|
||||
}
|
||||
err = copier.Copy(&entity, &securityLog)
|
||||
if err == nil {
|
||||
entity.ProjectId = req.ProjectId
|
||||
entity.DateOfOccurrence = req.DateOfOccurrence
|
||||
entity.Remark = req.Remark
|
||||
entity.CreatedBy = req.Openid
|
||||
}
|
||||
}
|
||||
// 2、新增数据
|
||||
info, err := dao.BusHseSecurityLog.Ctx(ctx).Insert(entity)
|
||||
liberr.ErrIsNil(ctx, err, "添加失败")
|
||||
// 3、有附件就新增附件信息
|
||||
if len(req.File) > 0 {
|
||||
id, _ := info.LastInsertId()
|
||||
err = SaveFunc(ctx, req.File, id, "1")
|
||||
}
|
||||
})
|
||||
return err
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sBusHseSecurityLog) List(ctx context.Context, req *system.BusHseSecurityLogSearchReq) (listRes *system.BusHseSecurityLogSearchRes, err error) {
|
||||
listRes = new(system.BusHseSecurityLogSearchRes)
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
// 1、查询人
|
||||
var openids []string
|
||||
if req.Fill != "" {
|
||||
values, err := dao.BusConstructionUser.Ctx(ctx).Where("user_name like ?", "%"+req.Fill+"%").Fields("openid").Array()
|
||||
liberr.ErrIsNil(ctx, err, "获取数据失败")
|
||||
if len(values) > 0 {
|
||||
for i := range values {
|
||||
vs := values[i].String()
|
||||
if vs != "" {
|
||||
openids = append(openids, vs)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 根据用户名从 sys_user 表中根据 user_nickname
|
||||
if req.Fill != "" {
|
||||
values, err := dao.SysUser.Ctx(ctx).Where("user_nickname like ?", "%"+req.Fill+"%").Fields("id").Array()
|
||||
liberr.ErrIsNil(ctx, err, "获取数据失败")
|
||||
if len(values) > 0 {
|
||||
openids = append(openids, lo.Map(values, func(item *gvar.Var, _ int) string {
|
||||
return item.String()
|
||||
})...)
|
||||
}
|
||||
}
|
||||
|
||||
// 2、查询具体数据
|
||||
m := dao.BusHseSecurityLog.Ctx(ctx).WithAll()
|
||||
if req.ProjectId != "" {
|
||||
m = m.Where(dao.BusHseSecurityLog.Columns().ProjectId+" = ?", gconv.Int64(req.ProjectId))
|
||||
}
|
||||
if len(req.DateOfOccurrence) != 0 {
|
||||
m = m.Where(dao.BusHseSecurityLog.Columns().DateOfOccurrence+" >=? AND "+dao.BusHseSecurityLog.Columns().DateOfOccurrence+" <=?", req.DateRange[0], req.DateRange[1])
|
||||
}
|
||||
if len(openids) != 0 {
|
||||
m = m.Where(dao.BusHseSecurityLog.Columns().CreatedBy+" in (?)", openids)
|
||||
}
|
||||
if len(req.DateRange) != 0 {
|
||||
m = m.Where(dao.BusHseSecurityLog.Columns().CreatedAt+" >=? AND "+dao.BusHseSecurityLog.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.BusHseSecurityLogInfoTwoRes
|
||||
err = m.Page(req.PageNum, req.PageSize).Order(order).Scan(&res)
|
||||
liberr.ErrIsNil(ctx, err, "获取数据失败")
|
||||
for i := range res {
|
||||
res[i].Fill = coryCommon.SelectByString(ctx, coryCommon.IsNumeric(res[i].CreatedBy), res[i].CreatedBy)
|
||||
}
|
||||
listRes.List = res
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sBusHseSecurityLog) GetById(ctx context.Context, id int64) (res *model.BusHseSecurityLogInfoTwoRes, err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
// 1、获取数据
|
||||
err = dao.BusHseSecurityLog.Ctx(ctx).As("a").
|
||||
LeftJoin("sys_dict_data", "b", "b.dict_type = 'climate' and b.dict_value = a.climate").
|
||||
LeftJoin(dao.SysProject.Table(), "c", "c.id = a.project_id").
|
||||
Fields("a.*,"+
|
||||
"b.dict_label as climateName,"+
|
||||
"c.project_name as projectName").
|
||||
Where("a."+dao.BusHseSecurityLog.Columns().Id, id).Scan(&res)
|
||||
// 获取创建人 更新人名称
|
||||
res.Fill = coryCommon.SelectByString(ctx, coryCommon.IsNumeric(res.CreatedBy), res.CreatedBy)
|
||||
liberr.ErrIsNil(ctx, err, "获取信息失败")
|
||||
// 2、获取附件
|
||||
var pathEntity []*model.HseSecurityLogPath
|
||||
err = g.DB().Model("bus_hse_security_log_path").Ctx(ctx).Where("ticket_id", id).Scan(&pathEntity)
|
||||
liberr.ErrIsNil(ctx, err, "查询失败")
|
||||
res.FileList = pathEntity
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sBusHseSecurityLog) AddText(ctx context.Context, req *system.BusHseSecurityLogAddTxetReq) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
_, err = dao.BusHseSecurityLog.Ctx(ctx).OmitEmpty().Insert(do.BusHseSecurityLog{
|
||||
ProjectId: req.ProjectId,
|
||||
DateOfOccurrence: req.DateOfOccurrence,
|
||||
AirTemperatureMax: req.AirTemperatureMax,
|
||||
AirTemperatureMin: req.AirTemperatureMin,
|
||||
Climate: req.Climate,
|
||||
Progress: req.Progress,
|
||||
JobContent: req.JobContent,
|
||||
DiscloseTheFacts: req.DiscloseTheFacts,
|
||||
ProgressOfActivity: req.ProgressOfActivity,
|
||||
Examine: req.Examine,
|
||||
Implementation: req.Implementation,
|
||||
SafetyInspectionSituation: req.SafetyInspectionSituation,
|
||||
StoppageOrOvertime: req.StoppageOrOvertime,
|
||||
OtherRecords: req.OtherRecords,
|
||||
Remark: req.Remark,
|
||||
CreatedBy: systemService.Context().GetUserId(ctx),
|
||||
})
|
||||
liberr.ErrIsNil(ctx, err, "添加失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sBusHseSecurityLog) AddFile(ctx context.Context, req *system.BusHseSecurityLogAddFileReq) (err error) {
|
||||
err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
// 1、新增文件信息
|
||||
info, err := dao.BusHseSecurityLog.Ctx(ctx).OmitEmpty().Insert(do.BusHseSecurityLog{
|
||||
ProjectId: req.ProjectId,
|
||||
DateOfOccurrence: req.DateOfOccurrence,
|
||||
Remark: req.Remark,
|
||||
CreatedBy: systemService.Context().GetUserId(ctx),
|
||||
})
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
id, _ := info.LastInsertId()
|
||||
// 2、新增附件信息
|
||||
if len(req.File) > 0 {
|
||||
err = UploadAndSaveFunc(ctx, req.File, id, "2") // 在不清楚的情况下直接是图片,具体是不是图片类型,方法内部去判断
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
}
|
||||
liberr.ErrIsNil(ctx, err, "添加失败")
|
||||
})
|
||||
return err
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sBusHseSecurityLog) Edit(ctx context.Context, req *system.BusHseSecurityLogEditReq) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
_, err = dao.BusHseSecurityLog.Ctx(ctx).WherePri(req.Id).Update(do.BusHseSecurityLog{
|
||||
ProjectId: req.ProjectId,
|
||||
DateOfOccurrence: req.DateOfOccurrence,
|
||||
AirTemperatureMax: req.AirTemperatureMax,
|
||||
AirTemperatureMin: req.AirTemperatureMin,
|
||||
Climate: req.Climate,
|
||||
Progress: req.Progress,
|
||||
JobContent: req.JobContent,
|
||||
DiscloseTheFacts: req.DiscloseTheFacts,
|
||||
ProgressOfActivity: req.ProgressOfActivity,
|
||||
Examine: req.Examine,
|
||||
Implementation: req.Implementation,
|
||||
SafetyInspectionSituation: req.SafetyInspectionSituation,
|
||||
StoppageOrOvertime: req.StoppageOrOvertime,
|
||||
OtherRecords: req.OtherRecords,
|
||||
Remark: req.Remark,
|
||||
UpdatedBy: systemService.Context().GetUserId(ctx),
|
||||
})
|
||||
liberr.ErrIsNil(ctx, err, "修改失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sBusHseSecurityLog) Delete(ctx context.Context, ids []int64) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
_, err = dao.BusHseSecurityLog.Ctx(ctx).Delete(dao.BusHseSecurityLog.Columns().Id+" in (?)", ids)
|
||||
liberr.ErrIsNil(ctx, err, "删除失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
type FileEntity struct {
|
||||
TicketId int64 `p:"ticket_id" dc:"父主键ID"`
|
||||
Type string `p:"type" dc:"类型(1文件 2图片)"`
|
||||
Name string `p:"name" dc:"文件名称"`
|
||||
Path string `p:"path" dc:"文件路径"`
|
||||
FileType string `p:"file_type" dc:"文件类型(后缀)"`
|
||||
}
|
||||
|
||||
// UploadAndSaveFunc 上传并存储文件
|
||||
func UploadAndSaveFunc(ctx context.Context, File []*ghttp.UploadFile, id int64, typeStr string) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
// 2、获取文件数据,然后对文件进行上传
|
||||
var fileEntitys []*FileEntity
|
||||
for i := range File {
|
||||
// 上传附件
|
||||
str, err := coryCommon.UploadFileTwo(ctx, File[i], coryCommon.Helmet)
|
||||
liberr.ErrIsNil(ctx, err, "上传附件失败!")
|
||||
str = filepath.ToSlash(str)
|
||||
rpath := coryCommon.ResourcePublicToFunc("/"+str, 0)
|
||||
// 获取文件信息
|
||||
_, ext, _ := coryCommon.FileInfo(rpath)
|
||||
// 判断文件是否属于其中的一种(路径要绝对路径)
|
||||
flag := coryCommon.FormatRestrictionFunc(coryCommon.FileToFunc(rpath, 2), suffix)
|
||||
if flag {
|
||||
typeStr = "1"
|
||||
}
|
||||
entity := FileEntity{
|
||||
TicketId: id,
|
||||
Type: typeStr,
|
||||
Name: File[i].Filename,
|
||||
Path: rpath,
|
||||
FileType: ext,
|
||||
}
|
||||
fileEntitys = append(fileEntitys, &entity)
|
||||
}
|
||||
// 3、上传文件的附件信息
|
||||
_, err = g.DB().Model("bus_hse_security_log_path").Ctx(ctx).Insert(fileEntitys)
|
||||
liberr.ErrIsNil(ctx, err, "添加失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// SaveFunc 存储文件
|
||||
func SaveFunc(ctx context.Context, fileInfo []*comModel.UpFile, id int64, typeStr string) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
// 2、获取文件数据,然后对文件进行上传
|
||||
var fileEntitys []*FileEntity
|
||||
for i := range fileInfo {
|
||||
rpath := coryCommon.FileToFunc(filepath.ToSlash(fileInfo[i].Url), 3)
|
||||
// 判断文件是否属于其中的一种(路径要绝对路径)
|
||||
flag := coryCommon.FormatRestrictionFunc(coryCommon.FileToFunc(rpath, 4), suffix)
|
||||
if flag {
|
||||
typeStr = "2"
|
||||
}
|
||||
entity := FileEntity{
|
||||
TicketId: id,
|
||||
Type: typeStr,
|
||||
Name: fileInfo[i].Name,
|
||||
Path: rpath,
|
||||
FileType: fileInfo[i].FileType,
|
||||
}
|
||||
fileEntitys = append(fileEntitys, &entity)
|
||||
}
|
||||
// 3、上传文件的附件信息
|
||||
_, err = g.DB().Model("bus_hse_security_log_path").Ctx(ctx).Insert(fileEntitys)
|
||||
liberr.ErrIsNil(ctx, err, "添加失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user