This commit is contained in:
2025-07-07 20:11:59 +08:00
parent ab0fdbc447
commit 06e3aa2eb3
2009 changed files with 193082 additions and 0 deletions

View File

@ -0,0 +1,484 @@
// ==========================================================================
// GFast自动生成logic操作代码。
// 生成日期2023-09-13 14:21:30
// 生成路径: internal/app/system/logic/bus_equipment_materials_inventory.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/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/service"
"github.com/tiger1103/gfast/v3/library/liberr"
tool "github.com/tiger1103/gfast/v3/utility/coryUtils"
"strings"
)
func init() {
service.RegisterBusEquipmentMaterialsInventory(New())
}
func New() *sBusEquipmentMaterialsInventory {
return &sBusEquipmentMaterialsInventory{}
}
type sBusEquipmentMaterialsInventory struct{}
func (s *sBusEquipmentMaterialsInventory) List(ctx context.Context, req *system.BusEquipmentMaterialsInventorySearchReq) (listRes *system.BusEquipmentMaterialsInventorySearchRes, err error) {
listRes = new(system.BusEquipmentMaterialsInventorySearchRes)
err = g.Try(ctx, func(ctx context.Context) {
m := dao.BusEquipmentMaterialsInventory.Ctx(ctx).WithAll()
if req.EquipmentMaterialsId != "" {
m = m.Where("equipment_materials_id", req.EquipmentMaterialsId)
}
// 创建时间模糊查询
if req.CreatedAt != "" {
date := tool.New().GetFormattedDate(gconv.Time(req.CreatedAt))
m = m.Where(dao.BusEquipmentMaterialsInventory.Columns().CreatedAt+" like ?", "%"+date+"%")
}
if len(req.DateRange) != 0 {
m = m.Where(dao.BusEquipmentMaterialsInventory.Columns().CreatedAt+" >=? AND "+dao.BusEquipmentMaterialsInventory.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.BusEquipmentMaterialsInventoryInfoRes
err = m.Fields(system.BusEquipmentMaterialsInventorySearchRes{}).Page(req.PageNum, req.PageSize).Order(order).Scan(&res)
liberr.ErrIsNil(ctx, err, "获取数据失败")
listRes.List = make([]*model.BusEquipmentMaterialsInventoryListRes, len(res))
for k, v := range res {
listRes.List[k] = &model.BusEquipmentMaterialsInventoryListRes{
Id: v.Id,
OutPut: v.OutPut,
Number: v.Number,
Residue: v.Residue,
Operator: v.Operator,
Remark: v.Remark,
OutPutTime: v.OutPutTime,
CreatedAt: v.CreatedAt,
Recipient: v.Recipient,
Shipper: v.Shipper,
Path: v.Path,
}
}
})
return
}
func (s *sBusEquipmentMaterialsInventory) GetById(ctx context.Context, id int64) (res *model.BusEquipmentMaterialsInventoryInfoRes, err error) {
err = g.Try(ctx, func(ctx context.Context) {
err = dao.BusEquipmentMaterialsInventory.Ctx(ctx).WithAll().Where(dao.BusEquipmentMaterialsInventory.Columns().Id, id).Scan(&res)
// 获取创建人 更新人
by := coryCommon.New().CreateByOrUpdateBy(ctx, res)
infoRes := by.(model.BusEquipmentMaterialsInventoryInfoRes)
res = &infoRes
liberr.ErrIsNil(ctx, err, "获取信息失败")
})
return
}
func (s *sBusEquipmentMaterialsInventory) Add(ctx context.Context, req *system.BusEquipmentMaterialsInventoryAddReq) (err error) {
var be *model.BusEquipmentMaterialsInventoryInfoRes
dao.BusEquipmentMaterialsInventory.Ctx(ctx).
Where("equipment_materials_id", req.EquipmentMaterialsId).
OrderDesc("id").
Limit(1).
Scan(&be)
// 如果上一次的数量不够(不够现在减)或者没有数量,那么就新增失败!
if req.OutPut == "1" {
if be == nil {
err = errors.New("还未入库!")
return
}
if be.Residue < req.Number {
err = errors.New("剩余库存不足!")
return
}
}
//入库量超过设计量 提示 入库量超过设计量 新加入库量 + 旧入库 大于 设计量 提示入库量超过设计量 超过设计量 入库失败
if req.OutPut == "2" {
var Num model.BemiInputLinkBemQCRes
dao.BusEquipmentMaterials.Ctx(ctx).As("bem").Fields("bem.quantity_count AS quantityCount,SUM( CASE WHEN bemi.out_put = '2' THEN bemi.number ELSE 0 END ) AS inputNumber ").
InnerJoin("bus_equipment_materials_inventory bemi ON bem.equipment_materials_id = bemi.equipment_materials_id ").
Where("bemi.equipment_materials_id = ?", req.EquipmentMaterialsId).
Group("bem.equipment_materials_id,bem.weight_id,bem.type_specification_name").Scan(&Num)
if Num.QuantityCount == "" {
dao.BusEquipmentMaterials.Ctx(ctx).Fields("quantity_count").Where("equipment_materials_id = ?", req.EquipmentMaterialsId).Scan(&Num)
}
inputNumber := Num.InputNumber + req.Number
if inputNumber > gconv.Int(Num.QuantityCount) {
err = errors.New("入库量超过预计用量")
return
}
}
// 获取上一次的数据,将上次剩余数量与这次的出入库数量进行加减,得出最终的当前剩余库存数量
residue := 0
if be != nil {
if req.OutPut == "1" {
residue = be.Residue - req.Number
} else if req.OutPut == "2" {
residue = be.Residue + req.Number
}
}
if residue == 0 && req.OutPut == "2" {
residue = req.Number
}
// 0、上传文件
var pathStr string
for i := range req.File {
str, err := coryCommon.UploadFile(ctx, req.File[i], coryCommon.Helmet)
if err != nil {
liberr.ErrIsNil(ctx, err)
return err
}
pathStr = pathStr + coryCommon.ResourcePublicToFunc("/"+str, 0) + ","
}
if len(pathStr) > 0 {
pathStr = pathStr[0 : len(pathStr)-1]
}
// 新增数据
err = g.Try(ctx, func(ctx context.Context) {
_, err = dao.BusEquipmentMaterialsInventory.Ctx(ctx).Insert(do.BusEquipmentMaterialsInventory{
EquipmentMaterialsId: req.EquipmentMaterialsId,
OutPut: req.OutPut,
Number: req.Number,
Residue: residue,
Operator: req.Operator,
Remark: req.Remark,
OutPutTime: req.OutPutTime,
Recipient: req.Recipient,
Shipper: req.Shipper,
CreateBy: ct.New().GetLoginUser(ctx).Id,
Path: pathStr,
})
liberr.ErrIsNil(ctx, err, "添加失败")
})
return
}
func (s *sBusEquipmentMaterialsInventory) Edit(ctx context.Context, req *system.BusEquipmentMaterialsInventoryEditReq) (err error) {
// 0、上传文件
var pathStr string
for i := range req.File {
//str, err := coryCommon.UploadFile(ctx, req.File[i], coryCommon.Helmet)
str, err := coryCommon.UploadFileTwo(ctx, req.File[i], coryCommon.Helmet)
if err != nil {
liberr.ErrIsNil(ctx, err)
return err
}
//将路径转换/resource/public/ 为 /flie
pathStr = pathStr + coryCommon.ResourcePublicToFunc("/"+str, 0) + ","
}
if req.Path != "" {
pathStr = pathStr + req.Path + ","
}
if len(pathStr) > 0 {
pathStr = pathStr[0 : len(pathStr)-1]
}
err = g.Try(ctx, func(ctx context.Context) {
_, err = dao.BusEquipmentMaterialsInventory.Ctx(ctx).WherePri(req.Id).Update(do.BusEquipmentMaterialsInventory{
Remark: req.Remark,
Recipient: req.Recipient,
Shipper: req.Shipper,
Path: pathStr,
UpdateBy: ct.New().GetLoginUser(ctx).Id,
})
liberr.ErrIsNil(ctx, err, "修改失败")
})
return
}
func (s *sBusEquipmentMaterialsInventory) Delete(ctx context.Context, ids []int64) (err error) {
err = g.Try(ctx, func(ctx context.Context) {
//err = errors.New("删除暂无")
safe := dao.BusEquipmentMaterialsInventory.Ctx(ctx).Safe()
//获取需要删除数据的附件
array, _ := safe.Where(dao.BusEquipmentMaterialsInventory.Columns().Id+" in (?)", ids).Fields("path").Array()
//删除mysql数据
_, err = safe.Delete(dao.BusEquipmentMaterialsInventory.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 *sBusEquipmentMaterialsInventory) AppAdd(ctx context.Context, req *system.BusEquipmentMaterialsInventoryAppAddReq) (err error) {
var be *model.BusEquipmentMaterialsInventoryInfoRes
dao.BusEquipmentMaterialsInventory.Ctx(ctx).
Where("equipment_materials_id", req.EquipmentMaterialsId).
OrderDesc("id").
Limit(1).
Scan(&be)
// 如果上一次的数量不够(不够现在减)或者没有数量,那么就新增失败!
if req.OutPut == "1" {
if be == nil {
err = errors.New("还未入库!")
return
}
if be.Residue < req.Number {
err = errors.New("剩余库存不足!")
return
}
}
//入库量超过设计量 提示 入库量超过设计量 新加入库量 + 旧入库 大于 设计量 提示入库量超过设计量 超过设计量 入库失败
if req.OutPut == "2" {
var Num model.BemiInputLinkBemQCRes
dao.BusEquipmentMaterials.Ctx(ctx).As("bem").Fields("bem.quantity_count AS quantityCount,SUM( CASE WHEN bemi.out_put = '2' THEN bemi.number ELSE 0 END ) AS inputNumber ").
InnerJoin("bus_equipment_materials_inventory bemi ON bem.equipment_materials_id = bemi.equipment_materials_id ").
Where("bemi.equipment_materials_id = ?", req.EquipmentMaterialsId).
Group("bem.equipment_materials_id,bem.weight_id,bem.type_specification_name").Scan(&Num)
if Num.QuantityCount == "" {
dao.BusEquipmentMaterials.Ctx(ctx).Fields("quantity_count").Where("equipment_materials_id = ?", req.EquipmentMaterialsId).Scan(&Num)
}
inputNumber := Num.InputNumber + req.Number
if inputNumber > gconv.Int(Num.QuantityCount) {
err = errors.New("入库量超过预计用量")
return
}
}
// 获取上一次的数据,将上次剩余数量与这次的出入库数量进行加减,得出最终的当前剩余库存数量
residue := 0
if be != nil {
if req.OutPut == "1" {
residue = be.Residue - req.Number
} else if req.OutPut == "2" {
residue = be.Residue + req.Number
}
}
if residue == 0 && req.OutPut == "2" {
residue = req.Number
}
// 新增数据 开启事物
err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
err = g.Try(ctx, func(ctx context.Context) {
InventoryId, err := dao.BusEquipmentMaterialsInventory.Ctx(ctx).InsertAndGetId(do.BusEquipmentMaterialsInventory{
EquipmentMaterialsId: req.EquipmentMaterialsId,
OutPut: req.OutPut,
Number: req.Number,
Residue: residue,
Operator: req.Operator,
Remark: req.Remark,
OutPutTime: req.OutPutTime,
Recipient: req.Recipient,
Shipper: req.Shipper,
CreateBy: ct.New().GetLoginUser(ctx).Id,
})
liberr.ErrIsNil(ctx, err, "添加失败")
//添加文件信息
if req.File != nil && err == nil {
pathStr, err := insertAppFiles(ctx, req.File, InventoryId)
liberr.ErrIsNil(ctx, err, "有错误文件")
//添加后台文件信息
_, err = dao.BusEquipmentMaterialsInventory.Ctx(ctx).WherePri(InventoryId).Update(do.BusEquipmentMaterialsInventory{
Path: pathStr,
})
liberr.ErrIsNil(ctx, err, "更新后台文件有误")
}
})
return err
})
return
}
func (s *sBusEquipmentMaterialsInventory) AppEdit(ctx context.Context, req *system.BusEquipmentMaterialsInventoryAppEditReq) (err error) {
err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
err = g.Try(ctx, func(ctx context.Context) {
/// 文件修改
pathStr, err := editAppFiles(ctx, req.File, req.Id)
liberr.ErrIsNil(ctx, err, "文件插入失败")
_, err = dao.BusEquipmentMaterialsInventory.Ctx(ctx).WherePri(req.Id).Update(do.BusEquipmentMaterialsInventory{
Remark: req.Remark,
Recipient: req.Recipient,
Shipper: req.Shipper,
Path: pathStr,
UpdateBy: ct.New().GetLoginUser(ctx).Id,
})
liberr.ErrIsNil(ctx, err, "修改失败")
})
return err
})
//删除文件
err = deleteAppFiles(ctx, req.DelFile)
liberr.ErrIsNil(ctx, err, "清楚文件有误")
return
}
// AppAdd函数
func insertAppFiles(ctx context.Context, file []model.MaterialsInventoryFileAddApp, InventoryId int64) (pathStr string, err error) {
var pathStrs []string
bulkInsert := make([]do.BusEquipmentMaterialsInventoryFile, 0, len(file))
for _, k := range file {
//拼接文件路径
if k.Path == "" {
continue
}
pathStrs = append(pathStrs, k.Path)
bulkInsert = append(bulkInsert, do.BusEquipmentMaterialsInventoryFile{
MaterialsInventoryId: InventoryId,
Suffix: k.Suffix,
Name: k.Name,
Path: k.Path,
Size: k.Size,
})
}
if len(bulkInsert) <= 0 {
return "", nil
}
//批量插入 Batch分批写入条数数量
_, err = dao.BusEquipmentMaterialsInventoryFile.Ctx(ctx).Data(bulkInsert).Batch(10).Insert()
liberr.ErrIsNil(ctx, err, "有错误文件")
pathStr = strings.Join(pathStrs, ",")
return pathStr, err
}
// AppEdit
func editAppFiles(ctx context.Context, file []model.MaterialsInventoryFileEditApp, InventoryId int64) (pathStr string, err error) {
//file有两种值 有id的修改值 无id的新增
var pathStrs []string
for _, k := range file {
if k.Path != "" {
pathStrs = append(pathStrs, k.Path)
}
if k.Id != 0 {
continue
}
_, err = dao.BusEquipmentMaterialsInventoryFile.Ctx(ctx).Insert(do.BusEquipmentMaterialsInventoryFile{
MaterialsInventoryId: InventoryId,
Name: k.Name,
Path: k.Path,
Size: k.Size,
Suffix: k.Suffix,
})
liberr.ErrIsNil(ctx, err, "有错误文件")
pathStr = strings.Join(pathStrs, ",")
}
return pathStr, err
}
// AppEdit 删除真实文件和数据库文件路径
func deleteAppFiles(ctx context.Context, file []model.MaterialsInventoryFileEditApp) (err error) {
var filepath []string
var ids []int64
for _, K := range file {
if K.Path == "" {
continue
}
filepath = append(filepath, K.Path)
ids = append(ids, K.Id)
}
_, err = dao.BusEquipmentMaterialsInventoryFile.Ctx(ctx).Delete("id in (?)", ids)
liberr.ErrIsNil(ctx, err, "无记录值")
coryCommon.BatchFile(filepath)
return err
}
func (s *sBusEquipmentMaterialsInventory) AppList(ctx context.Context, req *system.BusEquipmentMaterialsInventoryAppSearchReq) (listRes *system.BusEquipmentMaterialsInventoryAppSearchRes, err error) {
listRes = new(system.BusEquipmentMaterialsInventoryAppSearchRes)
err = g.Try(ctx, func(ctx context.Context) {
m := dao.BusEquipmentMaterialsInventory.Ctx(ctx).WithAll()
if req.EquipmentMaterialsId != "" {
m = m.Where("equipment_materials_id", req.EquipmentMaterialsId)
}
// 创建时间模糊查询
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.BusEquipmentMaterialsInventoryInfoRes
err = m.Fields(system.BusEquipmentMaterialsInventorySearchRes{}).Page(req.PageNum, req.PageSize).Order(order).Scan(&res)
liberr.ErrIsNil(ctx, err, "获取数据失败")
listRes.List = make([]*model.BusEquipmentMaterialsInventoryAppListRes, len(res))
for k, v := range res {
listRes.List[k] = &model.BusEquipmentMaterialsInventoryAppListRes{
Id: v.Id,
OutPut: v.OutPut,
Number: v.Number,
Residue: v.Residue,
OutPutTime: v.OutPutTime,
Recipient: v.Recipient,
Shipper: v.Shipper,
}
}
})
return
}
func (s *sBusEquipmentMaterialsInventory) AppGetById(ctx context.Context, id int64) (res *model.BusEquipmentMaterialsInventoryAppInfoRes, err error) {
err = g.Try(ctx, func(ctx context.Context) {
err = dao.BusEquipmentMaterialsInventory.Ctx(ctx).As("bemi").Fields("bem.equipment_materials_name,bemi.*").
RightJoin("bus_equipment_materials bem on bemi.equipment_materials_id =bem.equipment_materials_id").
Where("bemi."+dao.BusEquipmentMaterialsInventory.Columns().Id, id).Scan(&res)
liberr.ErrIsNil(ctx, err, "获取信息失败")
dao.BusEquipmentMaterialsInventoryFile.Ctx(ctx).Where(dao.BusEquipmentMaterialsInventoryFile.Columns().MaterialsInventoryId, id).Scan(&res.File)
// 获取创建人 更新人
by := coryCommon.New().CreateByOrUpdateBy(ctx, res)
infoRes := by.(model.BusEquipmentMaterialsInventoryAppInfoRes)
res = &infoRes
liberr.ErrIsNil(ctx, err, "获取信息失败")
})
return
}
func (s *sBusEquipmentMaterialsInventory) AppDelete(ctx context.Context, ids []int64) (err error) {
err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
err = g.Try(ctx, func(ctx context.Context) {
//根据id查询出BusEquipmentMaterialsInventoryFile信息 组合成数组InventoryFile数组
var InventoryFile []model.MaterialsInventoryFileEditApp
dao.BusEquipmentMaterialsInventoryFile.Ctx(ctx).Where("materials_inventory_id in (?)", ids).Scan(&InventoryFile)
err = deleteAppFiles(ctx, InventoryFile) //删除BusEquipmentMaterialsInventoryFile信息和删除真实路径文件
liberr.ErrIsNil(ctx, err, "删除失败")
//删除BusEquipmentMaterialsInventory信息
_, err = dao.BusEquipmentMaterialsInventory.Ctx(ctx).Delete("id in (?)", ids)
liberr.ErrIsNil(ctx, err, "删除失败")
})
return err
})
return err
}