初始
This commit is contained in:
346
internal/app/system/logic/busDesignAudit/bus_design_audit.go
Normal file
346
internal/app/system/logic/busDesignAudit/bus_design_audit.go
Normal file
@ -0,0 +1,346 @@
|
||||
// ==========================================================================
|
||||
// GFast自动生成logic操作代码。
|
||||
// 生成日期:2024-04-09 15:40:16
|
||||
// 生成路径: internal/app/system/logic/bus_design_audit.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/gogf/gf/v2/os/gtime"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"github.com/tiger1103/gfast/v3/api/v1/common/coryCommon"
|
||||
"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"
|
||||
logicDocument "github.com/tiger1103/gfast/v3/internal/app/system/logic/document"
|
||||
"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"
|
||||
tool "github.com/tiger1103/gfast/v3/utility/coryUtils"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func init() {
|
||||
service.RegisterBusDesignAudit(New())
|
||||
}
|
||||
|
||||
func New() *sBusDesignAudit {
|
||||
return &sBusDesignAudit{}
|
||||
}
|
||||
|
||||
type sBusDesignAudit struct{}
|
||||
|
||||
func TableTypeFunc(num string) (tableName string) {
|
||||
switch num {
|
||||
case "1":
|
||||
tableName = dao.DocumentCompletion.Table()
|
||||
case "2":
|
||||
tableName = dao.DocumentProductionDrawing.Table()
|
||||
case "3":
|
||||
tableName = dao.DocumentReport.Table()
|
||||
default:
|
||||
tableName = ""
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sBusDesignAudit) List(ctx context.Context, req *system.BusDesignAuditSearchReq) (listRes *system.BusDesignAuditSearchRes, err error) {
|
||||
listRes = new(system.BusDesignAuditSearchRes)
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
m := dao.BusDesignAudit.Ctx(ctx).WithAll()
|
||||
if req.ProjectId != 0 {
|
||||
m = m.Where(dao.BusDesignAudit.Columns().ProjectId+" = ?", req.ProjectId)
|
||||
}
|
||||
if req.TableType != "" {
|
||||
m = m.Where(dao.BusDesignAudit.Columns().TableName+" = ?", TableTypeFunc(req.TableType))
|
||||
}
|
||||
if req.Status != "" && req.Status != "0" {
|
||||
m = m.Where(dao.BusDesignAudit.Columns().Status+" = ?", req.Status)
|
||||
}
|
||||
//创建时间模糊查询
|
||||
if req.CreatedAt != "" {
|
||||
date := tool.New().GetFormattedDate(gconv.Time(req.CreatedAt))
|
||||
m = m.Where(dao.BusDesignAudit.Columns().CreatedAt+" like ?", "%"+date+"%")
|
||||
}
|
||||
if len(req.DateRange) != 0 {
|
||||
m = m.Where(dao.BusDesignAudit.Columns().CreatedAt+" >=? AND "+dao.BusDesignAudit.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 asc"
|
||||
if req.OrderBy != "" {
|
||||
order = req.OrderBy
|
||||
}
|
||||
var res []*model.CoryAuditListRes
|
||||
err = m.Fields(system.BusDesignAuditSearchRes{}).Page(req.PageNum, req.PageSize).Order(order).Scan(&res)
|
||||
liberr.ErrIsNil(ctx, err, "获取数据失败")
|
||||
//查询文件名称 与 文件路径
|
||||
type entity struct {
|
||||
Name string `json:"name"`
|
||||
FilenPath string `json:"filenPath"`
|
||||
DeletedAt *gtime.Time `json:"deletedAt"`
|
||||
}
|
||||
for i, data := range res {
|
||||
res[i].FileType = 1 //文件正常
|
||||
//获取文件名和路径
|
||||
var aEntity *entity
|
||||
err = g.DB().Model(data.TableName).Ctx(ctx).Unscoped().WherePri(data.TableId).Scan(&aEntity)
|
||||
liberr.ErrIsNil(ctx, err, "数组数据失败")
|
||||
if aEntity != nil {
|
||||
res[i].FileName = aEntity.Name
|
||||
var path = "/"
|
||||
split := strings.Split(aEntity.FilenPath, "/")
|
||||
for j := range split {
|
||||
if i == 5 { //5是固定的
|
||||
path = path + split[j]
|
||||
}
|
||||
}
|
||||
res[i].FilePath = path
|
||||
if aEntity.DeletedAt != nil {
|
||||
res[i].FileType = 2 //文件进入回收站
|
||||
}
|
||||
//判断文件类型
|
||||
rpath := coryCommon.FileToFunc(aEntity.FilenPath, 2)
|
||||
info, err := os.Stat(rpath)
|
||||
if err == nil {
|
||||
if info.IsDir() {
|
||||
res[i].Type = "2"
|
||||
} else {
|
||||
res[i].Type = "1"
|
||||
}
|
||||
}
|
||||
} else {
|
||||
res[i].FileType = 3 //文件已被彻底删除
|
||||
}
|
||||
//获取审核人名称
|
||||
if data.Auditor != 0 {
|
||||
value, _ := dao.SysUser.Ctx(ctx).WherePri(data.Auditor).Fields("user_name").Value()
|
||||
res[i].AuditorName = value.String()
|
||||
}
|
||||
}
|
||||
listRes.List = res
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sBusDesignAudit) GetById(ctx context.Context, id int64) (res *model.BusDesignAuditInfoRes, err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
err = dao.BusDesignAudit.Ctx(ctx).WithAll().Where(dao.BusDesignAudit.Columns().Id, id).Scan(&res)
|
||||
liberr.ErrIsNil(ctx, err, "获取信息失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sBusDesignAudit) Add(ctx context.Context, req *system.BusDesignAuditAddReq) (err error) {
|
||||
err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
//1、将需要审核的记录插入到审核表中进行数据存储
|
||||
_, err = dao.BusDesignAudit.Ctx(ctx).Insert(do.BusDesignAudit{
|
||||
ProjectId: req.ProjectId,
|
||||
TableName: req.TableName,
|
||||
TableId: req.TableId,
|
||||
})
|
||||
liberr.ErrIsNil(ctx, err, "添加失败")
|
||||
//2、并且将这条数据信息加入到文档记录表中进行记录
|
||||
err = AddApprovedMemo(ctx, req.TableId, req.ProjectId, req.TableName, "1") //新增记录
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
})
|
||||
return err
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sBusDesignAudit) Edit(ctx context.Context, req *system.BusDesignAuditEditReq) (err error) {
|
||||
err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
var entity *model.BusDesignAuditInfoRes
|
||||
dao.BusDesignAudit.Ctx(ctx).WherePri(req.Id).Scan(&entity)
|
||||
//1、校验当前人员是否是项目经理
|
||||
user := ct.New().GetLoginUser(ctx)
|
||||
count, err := dao.SysUserPost.Ctx(ctx).
|
||||
Where(dao.SysUserPost.Columns().UserId, user.Id).
|
||||
Where(dao.SysUserPost.Columns().PostId, "2").Count() //2 代表项目经理
|
||||
if count == 0 {
|
||||
err = errors.New("抱歉,您未有操作权限!")
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
return
|
||||
}
|
||||
//2、如果是项目经理那么就允许去修改当前数据(审核)
|
||||
_, err = dao.BusDesignAudit.Ctx(ctx).OmitEmpty().WherePri(req.Id).Update(do.BusDesignAudit{
|
||||
Status: req.Status,
|
||||
Cause: req.Cause,
|
||||
Auditor: ct.New().GetLoginUser(ctx).Id,
|
||||
})
|
||||
liberr.ErrIsNil(ctx, err, "修改失败")
|
||||
//3、审核完毕去修改对应文档的状态
|
||||
var ids []int64
|
||||
value, _ := g.DB().Model(entity.TableName).Ctx(ctx).WherePri(entity.TableId).Fields("id_str").Value()
|
||||
if value.String() == "" {
|
||||
ids = append(ids, entity.TableId)
|
||||
} else {
|
||||
sql := `WITH RECURSIVE cte AS (
|
||||
SELECT
|
||||
id,filen_path,id_str
|
||||
FROM
|
||||
` + entity.TableName + `
|
||||
WHERE
|
||||
id_str = '` + value.String() + `'
|
||||
UNION ALL
|
||||
SELECT
|
||||
u.id,u.filen_path,u.id_str
|
||||
FROM
|
||||
` + entity.TableName + ` u
|
||||
INNER JOIN cte ON u.pid = cte.id_str
|
||||
)
|
||||
SELECT id FROM cte ORDER BY id`
|
||||
var ey []*ThisDelEntity
|
||||
err = g.DB().GetScan(ctx, &ey, sql)
|
||||
if err != nil {
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
return
|
||||
}
|
||||
for i := range ey {
|
||||
ids = append(ids, ey[i].Id)
|
||||
}
|
||||
}
|
||||
if entity != nil {
|
||||
_, err := g.DB().Model(entity.TableName).Ctx(ctx).Where("id in (?)", ids).Update(g.Map{"state": req.Status})
|
||||
liberr.ErrIsNil(ctx, err, "修改状态失败")
|
||||
}
|
||||
//4、审核完毕就需要存储当前审核的记录信息
|
||||
err = AddApprovedMemo(ctx, entity.TableId, entity.ProjectId, entity.TableName, "4")
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
//5、如果是拒绝
|
||||
})
|
||||
return err
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sBusDesignAudit) Delete(ctx context.Context, id int64) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
//0、先查询到当前数据支持接下来的数据操作
|
||||
var entity *model.BusDesignAuditInfoRes
|
||||
dao.BusDesignAudit.Ctx(ctx).WherePri(id).Scan(&entity)
|
||||
if entity == nil {
|
||||
return
|
||||
}
|
||||
//1、判断当前数据(1未读不允许删除 2拒绝删除当前数据信息并删除对应文档信息 3通过只能删除当前数据信息 )
|
||||
if entity.Status == "1" {
|
||||
err = errors.New("未读状态下不允许删除数据!")
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
} else if entity.Status == "2" {
|
||||
err = DelCorrespondingData(ctx, id, entity)
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
} else {
|
||||
_, err = dao.BusDesignAudit.Ctx(ctx).WherePri(id).Delete()
|
||||
liberr.ErrIsNil(ctx, err, "删除失败")
|
||||
}
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// AddApprovedMemo 操作文档,向文档操作记录表中追加数据信息 typeStr(1新增 2修改 3删除 4审核 5下载)
|
||||
func AddApprovedMemo(ctx context.Context, tabId, projectId int64, tabName, typeStr string) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
_, err = g.DB().Model("bus_design_document_record").Ctx(ctx).Insert(
|
||||
g.Map{
|
||||
"project_id": projectId,
|
||||
"table_name": tabName,
|
||||
"table_id": tabId,
|
||||
"design_operate_type": typeStr,
|
||||
"created_by": ct.New().GetLoginUser(ctx).Id,
|
||||
})
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// SelectTabNameAndTabIdByDateFunc 根据表名+id获取当前文件夹的文档记录信息
|
||||
func SelectTabNameAndTabIdByDateFunc(ctx context.Context, data *model.SelectTabNameAndTabIdByDateReq) (entity []*model.SelectTabNameAndTabIdByDateRes, err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
var ids []int64
|
||||
ids = append(ids, data.TableId)
|
||||
if data.FatherId != 0 {
|
||||
ids = append(ids, data.FatherId)
|
||||
}
|
||||
err = g.DB().Model("bus_design_document_record").Ctx(ctx).
|
||||
Where("table_name", data.TableName).
|
||||
Where("table_id in (?)", ids).Order("id desc").Scan(&entity)
|
||||
liberr.ErrIsNil(ctx, err, "获取文件/文件夹记录失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
type ThisDelEntity struct {
|
||||
Id int64 `p:"id" dc:"主键ID"`
|
||||
FilenPath string `p:"filenPath" dc:"文件路径"`
|
||||
}
|
||||
|
||||
// DelCorrespondingData 根据id删除当前审核数据与审核对应的文档数据
|
||||
func DelCorrespondingData(ctx context.Context, id int64, entity *model.BusDesignAuditInfoRes) (err error) {
|
||||
err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
err = g.Try(ctx, func(ectx context.Context) {
|
||||
//删除审核记录
|
||||
_, err = dao.BusDesignAudit.Ctx(ctx).WherePri(id).Delete()
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
//获取id_str用来作为递归查询参数
|
||||
value, err := g.DB().Model(entity.TableName).Ctx(ctx).WherePri(entity.TableId).Fields("id_str").Value()
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
//获取需要删除的文档信息
|
||||
|
||||
var ey []*ThisDelEntity
|
||||
sql := `WITH RECURSIVE cte AS (
|
||||
SELECT
|
||||
id,filen_path,id_str
|
||||
FROM
|
||||
` + entity.TableName + `
|
||||
WHERE
|
||||
id_str = '` + value.String() + `'
|
||||
UNION ALL
|
||||
SELECT
|
||||
u.id,u.filen_path,u.id_str
|
||||
FROM
|
||||
` + entity.TableName + ` u
|
||||
INNER JOIN cte ON u.pid = cte.id_str
|
||||
)
|
||||
SELECT id FROM cte ORDER BY id`
|
||||
err = g.DB().GetScan(ctx, &ey, sql)
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
//删除第一个数据的路径(第一个就会删除所有文件夹的数据)
|
||||
if len(ey) > 0 {
|
||||
var idArrDlr []int64
|
||||
for _, data := range ey {
|
||||
os.RemoveAll(coryCommon.FileToFunc(data.FilenPath, 2))
|
||||
idList := logicDocument.RecursiveDeletion(ctx, data.Id)
|
||||
if idList != nil {
|
||||
idArrDlr = append(idArrDlr, idList...)
|
||||
}
|
||||
idArrDlr = append(idArrDlr, data.Id)
|
||||
}
|
||||
//直接物理删除数据
|
||||
_, err = dao.Document.Ctx(ctx).Unscoped().Delete(dao.Document.Columns().Id+" in (?)", idArrDlr)
|
||||
liberr.ErrIsNil(ctx, err, "删除失败")
|
||||
}
|
||||
})
|
||||
return err
|
||||
})
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user