初始
This commit is contained in:
@ -0,0 +1,599 @@
|
||||
// ==========================================================================
|
||||
// GFast自动生成logic操作代码。
|
||||
// 生成日期:2023-10-12 11:41:32
|
||||
// 生成路径: internal/app/system/logic/bus_machinery_detail.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"
|
||||
"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"
|
||||
tool "github.com/tiger1103/gfast/v3/utility/coryUtils"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func init() {
|
||||
service.RegisterBusMachineryDetail(New())
|
||||
}
|
||||
|
||||
func New() *sBusMachineryDetail {
|
||||
return &sBusMachineryDetail{}
|
||||
}
|
||||
|
||||
type sBusMachineryDetail struct{}
|
||||
|
||||
func (s *sBusMachineryDetail) List(ctx context.Context, req *system.BusMachineryDetailSearchReq) (listRes *system.BusMachineryDetailSearchRes, err error) {
|
||||
listRes = new(system.BusMachineryDetailSearchRes)
|
||||
//err = g.Try(ctx, func(ctx context.Context) {
|
||||
m := dao.BusMachineryDetail.Ctx(ctx).WithAll()
|
||||
if req.MachineryId != "" {
|
||||
m = m.Where(dao.BusMachineryDetail.Columns().MachineryId, req.MachineryId)
|
||||
}
|
||||
if req.CheckoutNumber != "" {
|
||||
m = m.Where(dao.BusMachineryDetail.Columns().CheckoutNumber+" like ?", "%"+req.CheckoutNumber+"%")
|
||||
}
|
||||
if req.Status != "" {
|
||||
m = m.Where(dao.BusMachineryDetail.Columns().Status+" = ?", req.Status)
|
||||
}
|
||||
if req.EntryTime != "" {
|
||||
date := tool.New().GetFormattedDate(gconv.Time(req.EntryTime))
|
||||
m = m.Where(dao.BusMachineryDetail.Columns().EntryTime+" like ?", "%"+date+"%")
|
||||
}
|
||||
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.BusMachineryDetailInfoRes
|
||||
err = m.Fields(system.BusMachineryDetailSearchRes{}).Page(req.PageNum, req.PageSize).Order(order).Scan(&res)
|
||||
liberr.ErrIsNil(ctx, err, "获取数据失败")
|
||||
listRes.List = make([]*model.BusMachineryDetailListRes, len(res))
|
||||
for k, v := range res {
|
||||
listRes.List[k] = &model.BusMachineryDetailListRes{
|
||||
Id: v.Id,
|
||||
CheckoutNumber: v.CheckoutNumber,
|
||||
CheckoutUnit: v.CheckoutUnit,
|
||||
CheckoutDate: v.CheckoutDate,
|
||||
Status: v.Status,
|
||||
Type: v.Type,
|
||||
EntryTime: v.EntryTime,
|
||||
CreatedAt: v.CreatedAt,
|
||||
Remark: v.Remark,
|
||||
Picture: v.Picture,
|
||||
MachineryId: v.MachineryId,
|
||||
Principal: v.Principal,
|
||||
}
|
||||
}
|
||||
//})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sBusMachineryDetail) GetById(ctx context.Context, id int64) (res *model.BusMachineryDetailGetIdInfoRes, err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
err = dao.BusMachineryDetail.Ctx(ctx).WithAll().Where(dao.BusMachineryDetail.Columns().Id, id).Scan(&res)
|
||||
liberr.ErrIsNil(ctx, err, "获取信息失败")
|
||||
var record []*model.BusMachineryRecordList
|
||||
err = dao.BusMachineryDetail.Ctx(ctx).As("bmd").
|
||||
Fields("bmr.*").
|
||||
LeftJoin(" bus_machinery_record AS bmr ON bmd.id=bmr.machinery_detail_id").
|
||||
Where("bmd.id = ?", id).Order("record_time DESC").
|
||||
Scan(&record)
|
||||
liberr.ErrIsNil(ctx, err, "获取信息失败")
|
||||
// 判断详细记录是否为空 获取创建人 更新人
|
||||
if len(record) == 0 {
|
||||
res.RecordList = nil
|
||||
} else {
|
||||
for i := range record {
|
||||
record[i].CreateBy = coryCommon.SelectByString(ctx, coryCommon.IsNumeric(record[i].CreateBy), record[i].CreateBy)
|
||||
}
|
||||
res.RecordList = record
|
||||
}
|
||||
//获取创建人 更新人
|
||||
by := coryCommon.New().CreateByOrUpdateBy(ctx, res)
|
||||
infoRes := by.(model.BusMachineryDetailGetIdInfoRes)
|
||||
res = &infoRes
|
||||
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sBusMachineryDetail) Add(ctx context.Context, req *system.BusMachineryDetailAddReq) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
num, err := dao.BusMachineryDetail.Ctx(ctx).Where(dao.BusMachineryDetail.Columns().MachineryId, req.MachineryId).
|
||||
Where(dao.BusMachineryDetail.Columns().CheckoutNumber, req.CheckoutNumber).Count()
|
||||
var NowTime *gtime.Time
|
||||
if req.EntryTime == "" {
|
||||
NowTime = gtime.Now()
|
||||
} else {
|
||||
NowTime = gconv.GTime(req.EntryTime)
|
||||
}
|
||||
|
||||
if num == 0 {
|
||||
//默认为入场
|
||||
req.Type = 2
|
||||
detail := do.BusMachineryDetail{
|
||||
MachineryId: req.MachineryId,
|
||||
CheckoutNumber: req.CheckoutNumber,
|
||||
CheckoutUnit: req.CheckoutUnit,
|
||||
CheckoutDate: req.CheckoutDate,
|
||||
Remark: req.Remark,
|
||||
Type: req.Type,
|
||||
EntryTime: NowTime,
|
||||
Picture: req.Picture,
|
||||
Principal: req.Principal,
|
||||
CreateBy: ct.New().GetLoginUser(ctx).Id,
|
||||
}
|
||||
DetailId, err := dao.BusMachineryDetail.Ctx(ctx).InsertAndGetId(detail)
|
||||
liberr.ErrIsNil(ctx, err, "添加失败")
|
||||
//机械设备详细新增记录
|
||||
if err == nil {
|
||||
dao.BusMachineryRecord.Ctx(ctx).Insert(do.BusMachineryRecord{
|
||||
MachineryDetailId: DetailId,
|
||||
RecordTime: NowTime,
|
||||
Type: req.Type,
|
||||
CreateBy: ct.New().GetLoginUser(ctx).Id,
|
||||
})
|
||||
}
|
||||
} else {
|
||||
err = errors.New("编号已存在")
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
return
|
||||
}
|
||||
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sBusMachineryDetail) Edit(ctx context.Context, req *system.BusMachineryDetailEditReq) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
//优化
|
||||
machineryId, err := dao.BusMachineryDetail.Ctx(ctx).Where(dao.BusMachineryDetail.Columns().Id, req.Id).
|
||||
Fields("machinery_id").Value()
|
||||
// 检查同machineryId下是否有其他记录的CheckoutNumber与待更新的一致
|
||||
// 注意排除当前记录自身
|
||||
num, err := dao.BusMachineryDetail.Ctx(ctx).
|
||||
Where(dao.BusMachineryDetail.Columns().MachineryId, gconv.Int(machineryId)).
|
||||
Where(dao.BusMachineryDetail.Columns().CheckoutNumber, req.CheckoutNumber).
|
||||
Where(dao.BusMachineryDetail.Columns().Id+"!= ?", req.Id). // 排除当前记录
|
||||
Count()
|
||||
// 如果找到了重复的CheckoutNumber,则返回错误
|
||||
if num > 0 {
|
||||
err = errors.New("该机械设备下已有相同的合格编号")
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
return
|
||||
}
|
||||
// 没有重复,继续执行更新逻辑
|
||||
var NowTime *gtime.Time
|
||||
if req.EntryTime == "" {
|
||||
NowTime = gtime.Now()
|
||||
} else {
|
||||
NowTime = gconv.GTime(req.EntryTime)
|
||||
}
|
||||
detail := do.BusMachineryDetail{
|
||||
CheckoutNumber: req.CheckoutNumber,
|
||||
CheckoutUnit: req.CheckoutUnit,
|
||||
CheckoutDate: req.CheckoutDate,
|
||||
Status: req.Status,
|
||||
Type: req.Type,
|
||||
EntryTime: NowTime,
|
||||
Remark: req.Remark,
|
||||
Picture: req.Picture,
|
||||
Principal: req.Principal,
|
||||
UpdateBy: ct.New().GetLoginUser(ctx).Id,
|
||||
}
|
||||
_, err = dao.BusMachineryDetail.Ctx(ctx).WherePri(req.Id).Update(detail)
|
||||
liberr.ErrIsNil(ctx, err, "修改失败")
|
||||
//机械设备详细修改记录
|
||||
if err == nil {
|
||||
_, err = dao.BusMachineryRecord.Ctx(ctx).Insert(do.BusMachineryRecord{
|
||||
MachineryDetailId: req.Id,
|
||||
Status: req.Status,
|
||||
Type: req.Type,
|
||||
RecordTime: NowTime,
|
||||
CreateBy: ct.New().GetLoginUser(ctx).Id,
|
||||
})
|
||||
liberr.ErrIsNil(ctx, err, "记录修改日志失败")
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *sBusMachineryDetail) Delete(ctx context.Context, ids []int64) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
safe := dao.BusMachineryDetail.Ctx(ctx).Safe()
|
||||
//获取需要删除数据的附件
|
||||
array, _ := safe.Where(dao.BusMachineryDetail.Columns().Id+" in (?)", ids).Fields("picture").Array()
|
||||
//删除mysql数据
|
||||
_, err = safe.Delete(dao.BusMachineryDetail.Columns().Id+" in (?)", ids)
|
||||
//清除附件
|
||||
if err != nil {
|
||||
var str []string
|
||||
for i := range array {
|
||||
split := strings.Split(array[i].String(), ",")
|
||||
str = append(str, split...)
|
||||
}
|
||||
coryCommon.BatchFile(str)
|
||||
}
|
||||
liberr.ErrIsNil(ctx, err, "删除失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 详情记录
|
||||
func (s *sBusMachineryDetail) RecordGetById(ctx context.Context, id int64) (res *model.BusMachineryDetailGetIdInfoRes, err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
err = dao.BusMachineryRecord.Ctx(ctx).WithAll().Where(dao.BusMachineryDetail.Columns().Id, id).Scan(&res)
|
||||
liberr.ErrIsNil(ctx, err, "获取信息失败")
|
||||
var record []*model.BusMachineryRecordList
|
||||
err = dao.BusMachineryDetail.Ctx(ctx).As("bmd").
|
||||
Fields("bmr.*").
|
||||
LeftJoin(" bus_machinery_record AS bmr ON bmd.id=bmr.machinery_detail_id").
|
||||
Where("bmd.id = ?", id).Order("record_time DESC").
|
||||
Scan(&record)
|
||||
liberr.ErrIsNil(ctx, err, "获取信息失败")
|
||||
//获取创建人 更新人
|
||||
for i := range record {
|
||||
record[i].CreateBy = coryCommon.SelectByString(ctx, coryCommon.IsNumeric(record[i].CreateBy), record[i].CreateBy)
|
||||
}
|
||||
res.RecordList = record
|
||||
//获取创建人 更新人
|
||||
by := coryCommon.New().CreateByOrUpdateBy(ctx, res)
|
||||
infoRes := by.(model.BusMachineryDetailGetIdInfoRes)
|
||||
res = &infoRes
|
||||
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
//app
|
||||
|
||||
func (s *sBusMachineryDetail) AppList(ctx context.Context, req *system.BusMachineryDetailAppSearchReq) (listRes *system.BusMachineryDetailAppSearchRes, err error) {
|
||||
listRes = new(system.BusMachineryDetailAppSearchRes)
|
||||
//err = g.Try(ctx, func(ctx context.Context) {
|
||||
m := dao.BusMachineryDetail.Ctx(ctx).WithAll()
|
||||
if req.MachineryId != "" {
|
||||
m = m.Where(dao.BusMachineryDetail.Columns().MachineryId, req.MachineryId)
|
||||
}
|
||||
switch req.AppStatus { //1入场 2停用 3出场
|
||||
case 1: //入场
|
||||
m = m.Where(" type = 2 ").Where("status = 0")
|
||||
case 2:
|
||||
m = m.Where("status = 1").Where("type = 2")
|
||||
case 3:
|
||||
m = m.Where(" type = 1")
|
||||
case 0:
|
||||
m = m
|
||||
default:
|
||||
return nil, errors.New("状态参数错误")
|
||||
}
|
||||
|
||||
//if req.EntryTime != "" {
|
||||
// date := tool.New().GetFormattedDate(gconv.Time(req.EntryTime))
|
||||
// m = m.Where(dao.BusMachineryDetail.Columns().EntryTime+" like ?", "%"+date+"%")
|
||||
//}
|
||||
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
|
||||
}
|
||||
//BusMachineryDetailAppListRes
|
||||
var res []*model.BusMachineryDetailInfoRes
|
||||
err = m.Fields(system.BusMachineryDetailAppSearchRes{}).Page(req.PageNum, req.PageSize).Order(order).Scan(&res)
|
||||
liberr.ErrIsNil(ctx, err, "获取数据失败")
|
||||
listRes.List = make([]*model.BusMachineryDetailAppListRes, len(res))
|
||||
for k, v := range res {
|
||||
listRes.List[k] = &model.BusMachineryDetailAppListRes{
|
||||
Id: v.Id,
|
||||
CheckoutNumber: v.CheckoutNumber,
|
||||
CheckoutUnit: v.CheckoutUnit,
|
||||
CheckoutDate: v.CheckoutDate,
|
||||
Status: v.Status,
|
||||
Type: v.Type,
|
||||
EntryTime: v.EntryTime,
|
||||
CreatedAt: v.CreatedAt,
|
||||
Remark: v.Remark,
|
||||
Picture: v.Picture,
|
||||
MachineryId: v.MachineryId,
|
||||
UpdatedAt: v.UpdatedAt,
|
||||
}
|
||||
}
|
||||
//})
|
||||
return
|
||||
}
|
||||
|
||||
const (
|
||||
Exarch = 1 //退场
|
||||
Entry = 2 //入场
|
||||
TrueStatus = 0 //正常
|
||||
FalseStatus = 1 //停用
|
||||
InspectionFile = 1
|
||||
LeaseFile = 2
|
||||
)
|
||||
|
||||
func (s *sBusMachineryDetail) AppAdd(ctx context.Context, req *system.BusMachineryDetailAppAddReq) (err error) {
|
||||
err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
num, err := dao.BusMachineryDetail.Ctx(ctx).Where(dao.BusMachineryDetail.Columns().MachineryId, req.MachineryId).
|
||||
Where(dao.BusMachineryDetail.Columns().CheckoutNumber, req.CheckoutNumber).Count()
|
||||
//不存在重复
|
||||
if num == 0 {
|
||||
var NowTime *gtime.Time
|
||||
if req.EntryTime == "" {
|
||||
NowTime = gtime.Now()
|
||||
} else {
|
||||
NowTime = gconv.GTime(req.EntryTime)
|
||||
}
|
||||
detail := do.BusMachineryDetail{
|
||||
MachineryId: req.MachineryId,
|
||||
CheckoutNumber: req.CheckoutNumber,
|
||||
CheckoutUnit: req.CheckoutUnit,
|
||||
CheckoutDate: req.CheckoutDate,
|
||||
Remark: req.Remark,
|
||||
Status: req.Status,
|
||||
Type: Entry,
|
||||
EntryTime: NowTime,
|
||||
Picture: req.Picture,
|
||||
CreateBy: ct.New().GetLoginUser(ctx).Id,
|
||||
}
|
||||
DetailId, err := dao.BusMachineryDetail.Ctx(ctx).InsertAndGetId(detail)
|
||||
liberr.ErrIsNil(ctx, err, "添加失败")
|
||||
//机械设备详细新增记录
|
||||
if err == nil {
|
||||
dao.BusMachineryRecord.Ctx(ctx).Insert(do.BusMachineryRecord{
|
||||
MachineryDetailId: DetailId,
|
||||
RecordTime: NowTime,
|
||||
Type: Entry,
|
||||
Status: req.Status,
|
||||
CreateBy: ct.New().GetLoginUser(ctx).Id,
|
||||
})
|
||||
}
|
||||
//添加合格校验单
|
||||
if len(req.InspectionFile) > 0 {
|
||||
if err := insertAppAddFiles(ctx, req.InspectionFile, DetailId, InspectionFile); err != nil {
|
||||
liberr.ErrIsNil(ctx, err, "处理合格校验单错误")
|
||||
}
|
||||
}
|
||||
// 处理租赁合同
|
||||
if len(req.LeaseFile) > 0 {
|
||||
if err := insertAppAddFiles(ctx, req.LeaseFile, DetailId, LeaseFile); err != nil {
|
||||
liberr.ErrIsNil(ctx, err, "处理租赁合同错误")
|
||||
}
|
||||
}
|
||||
} else {
|
||||
err = errors.New("编号已存在")
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
return
|
||||
}
|
||||
|
||||
})
|
||||
return err
|
||||
})
|
||||
return
|
||||
|
||||
}
|
||||
|
||||
// app Edit
|
||||
func (s *sBusMachineryDetail) AppEdit(ctx context.Context, req *system.BusMachineryDetailAppEditReq) (err error) {
|
||||
err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
//优化
|
||||
machineryId, err := dao.BusMachineryDetail.Ctx(ctx).Where(dao.BusMachineryDetail.Columns().Id, req.Id).
|
||||
Fields("machinery_id").Value()
|
||||
liberr.ErrIsNil(ctx, err, "查询有问题")
|
||||
// 检查同machineryId下是否有其他记录的CheckoutNumber与待更新的一致,排除当前记录自身
|
||||
num, err := dao.BusMachineryDetail.Ctx(ctx).
|
||||
Where(dao.BusMachineryDetail.Columns().MachineryId, gconv.Int(machineryId)).
|
||||
Where(dao.BusMachineryDetail.Columns().CheckoutNumber, req.CheckoutNumber).
|
||||
Where(dao.BusMachineryDetail.Columns().Id+"!= ?", req.Id). // 排除当前记录
|
||||
Count()
|
||||
// 重复的CheckoutNumber,则返回错误
|
||||
if num > 0 {
|
||||
err = errors.New("该机械设备下已有相同的合格编号")
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
return
|
||||
}
|
||||
// 没有重复,继续执行更新逻辑
|
||||
//获取最新时间
|
||||
var NowTime *gtime.Time
|
||||
if req.EntryTime == "" {
|
||||
NowTime = gtime.Now()
|
||||
} else {
|
||||
NowTime = gconv.GTime(req.EntryTime)
|
||||
}
|
||||
//获取状态
|
||||
var Detail struct {
|
||||
Status string
|
||||
Type string
|
||||
}
|
||||
dao.BusMachineryDetail.Ctx(ctx).Fields("status,type").Where(dao.BusMachineryDetail.Columns().Id, req.Id).Scan(&Detail)
|
||||
detail := do.BusMachineryDetail{
|
||||
CheckoutNumber: req.CheckoutNumber,
|
||||
CheckoutUnit: req.CheckoutUnit,
|
||||
CheckoutDate: req.CheckoutDate,
|
||||
Status: req.Status,
|
||||
Type: req.Type,
|
||||
EntryTime: NowTime,
|
||||
Remark: req.Remark,
|
||||
Picture: req.Picture,
|
||||
UpdateBy: ct.New().GetLoginUser(ctx).Id,
|
||||
}
|
||||
_, err = dao.BusMachineryDetail.Ctx(ctx).WherePri(req.Id).Update(detail)
|
||||
liberr.ErrIsNil(ctx, err, "修改失败")
|
||||
//机械设备详细状态修改记录
|
||||
if err == nil {
|
||||
if Detail.Status != req.Status || Detail.Type != gconv.String(req.Type) {
|
||||
_, err = dao.BusMachineryRecord.Ctx(ctx).Insert(do.BusMachineryRecord{
|
||||
MachineryDetailId: req.Id,
|
||||
Status: req.Status,
|
||||
Type: req.Type,
|
||||
RecordTime: NowTime,
|
||||
CreateBy: ct.New().GetLoginUser(ctx).Id,
|
||||
})
|
||||
liberr.ErrIsNil(ctx, err, "记录修改日志失败")
|
||||
}
|
||||
}
|
||||
//删除真实文件和数据库文件路径
|
||||
if req.DelFile != nil {
|
||||
if err := deleteAppEditFiles(ctx, req.DelFile); err != nil {
|
||||
liberr.ErrIsNil(ctx, err, "文件删除失败")
|
||||
}
|
||||
}
|
||||
//修改合格校验单
|
||||
if len(req.InspectionFile) > 0 {
|
||||
if err := insertAppEditFiles(ctx, req.InspectionFile, req.Id, InspectionFile); err != nil {
|
||||
liberr.ErrIsNil(ctx, err, "处理合格校验单错误")
|
||||
}
|
||||
}
|
||||
// 处理租赁合同
|
||||
if len(req.LeaseFile) > 0 {
|
||||
if err := insertAppEditFiles(ctx, req.LeaseFile, req.Id, LeaseFile); err != nil {
|
||||
liberr.ErrIsNil(ctx, err, "处理租赁合同错误")
|
||||
}
|
||||
}
|
||||
})
|
||||
return err
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// app getid
|
||||
func (s *sBusMachineryDetail) AppGetById(ctx context.Context, id int64) (res *model.BusMachineryDetailAppGetIdInfoRes, err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
err = dao.BusMachineryDetail.Ctx(ctx).As("bmd").WithAll().
|
||||
Fields("bmd.*,bm.machinery_name,bm.principal").
|
||||
LeftJoin("bus_machinery as bm on bmd.machinery_id=bm.id").Where("bmd."+dao.BusMachineryDetail.Columns().Id, id).Scan(&res)
|
||||
liberr.ErrIsNil(ctx, err, "获取信息失败")
|
||||
var AllFile []*entity.BusMachineryDetailFile
|
||||
err = dao.BusMachineryDetail.Ctx(ctx).As("bmd").
|
||||
Fields("bmdf.*").
|
||||
LeftJoin(" bus_machinery_detail_file AS bmdf on bmd.id=bmdf.machinery_detail_id").
|
||||
Where("bmd.id = ?", id).
|
||||
Scan(&AllFile)
|
||||
liberr.ErrIsNil(ctx, err, "获取信息失败")
|
||||
if len(AllFile) != 0 {
|
||||
for _, K := range AllFile {
|
||||
if K.SourceType == "1" {
|
||||
res.InspectionFile = append(res.InspectionFile, model.BusMachineryDetailFile{
|
||||
Name: K.Name,
|
||||
Url: K.Path,
|
||||
Size: gconv.Int64(K.Size),
|
||||
FileType: K.Type,
|
||||
Id: K.Id,
|
||||
})
|
||||
} else if K.SourceType == "2" {
|
||||
res.LeaseFile = append(res.LeaseFile, model.BusMachineryDetailFile{
|
||||
Name: K.Name,
|
||||
Url: K.Path,
|
||||
Size: gconv.Int64(K.Size),
|
||||
FileType: K.Type,
|
||||
Id: K.Id,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
//获取创建人 更新人
|
||||
by := coryCommon.New().CreateByOrUpdateBy(ctx, res)
|
||||
infoRes := by.(model.BusMachineryDetailAppGetIdInfoRes)
|
||||
res = &infoRes
|
||||
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// AppAdd函数
|
||||
func insertAppAddFiles(ctx context.Context, file []model.BusMachineryDetailAddFile, detailId int64, sourceType int) error {
|
||||
bulkInsert := make([]do.BusMachineryDetailFile, 0, len(file))
|
||||
for _, k := range file {
|
||||
if k.Url == "" {
|
||||
continue
|
||||
}
|
||||
bulkInsert = append(bulkInsert, do.BusMachineryDetailFile{
|
||||
MachineryDetailId: detailId,
|
||||
SourceType: sourceType,
|
||||
Name: k.Name,
|
||||
Path: k.Url,
|
||||
Size: k.Size,
|
||||
Type: k.FileType,
|
||||
})
|
||||
}
|
||||
if len(bulkInsert) <= 0 {
|
||||
return nil
|
||||
}
|
||||
//批量插入 Batch分批写入条数数量
|
||||
_, err := dao.BusMachineryDetailFile.Ctx(ctx).Data(bulkInsert).Batch(10).Insert()
|
||||
liberr.ErrIsNil(ctx, err, "有错误文件")
|
||||
return nil
|
||||
}
|
||||
|
||||
// AppEdit
|
||||
func insertAppEditFiles(ctx context.Context, file []model.BusMachineryDetailFile, detailId int64, sourceType int) (err error) {
|
||||
//file有两种值 有id的修改值 无id的新增
|
||||
for _, k := range file {
|
||||
if k.Id != 0 {
|
||||
continue
|
||||
}
|
||||
_, err = dao.BusMachineryDetailFile.Ctx(ctx).Insert(do.BusMachineryDetailFile{
|
||||
MachineryDetailId: detailId,
|
||||
SourceType: sourceType,
|
||||
Name: k.Name,
|
||||
Path: k.Url,
|
||||
Size: k.Size,
|
||||
Type: k.FileType,
|
||||
})
|
||||
liberr.ErrIsNil(ctx, err, "有错误文件")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AppEdit 删除真实文件和数据库文件路径
|
||||
func deleteAppEditFiles(ctx context.Context, file []model.BusMachineryDetailFile) (err error) {
|
||||
var filepath []string
|
||||
var ids []int64
|
||||
for _, K := range file {
|
||||
if K.Url == "" {
|
||||
continue
|
||||
}
|
||||
filepath = append(filepath, K.Url)
|
||||
ids = append(ids, K.Id)
|
||||
}
|
||||
_, err = dao.BusMachineryDetailFile.Ctx(ctx).Delete("id in (?)", ids)
|
||||
liberr.ErrIsNil(ctx, err, "无记录值")
|
||||
coryCommon.BatchFile(filepath)
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user