From af976f83c1ac6a2255481ad8fd7c1ac48dcd3c8e Mon Sep 17 00:00:00 2001 From: henry Date: Wed, 3 Nov 2021 17:55:27 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=E5=AE=8C=E5=96=84=E9=A1=B9?= =?UTF-8?q?=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/init.go | 3 +- app/common/model/manage_equipment.go | 1 + app/common/model/manage_material.go | 17 +- app/common/model/manage_material_purchase.go | 7 +- app/common/model/manage_material_warehouse.go | 3 +- app/common/model/manage_material_work.go | 4 + app/controller/manage/equipment.go | 9 +- app/controller/manage/material.go | 242 +++++++++++++++++- app/model/base.go | 9 + app/model/manage_material.go | 26 +- app/model/manage_materiral_purchase.go | 47 ++++ app/model/manage_materiral_warehouse.go | 11 + lib/order.go | 13 + lib/order_test.go | 11 + 14 files changed, 376 insertions(+), 27 deletions(-) create mode 100644 app/model/base.go create mode 100644 app/model/manage_materiral_purchase.go create mode 100644 app/model/manage_materiral_warehouse.go create mode 100644 lib/order.go create mode 100644 lib/order_test.go diff --git a/app/common/init.go b/app/common/init.go index cf05928..9aa2d72 100644 --- a/app/common/init.go +++ b/app/common/init.go @@ -42,14 +42,13 @@ func initModel() { return &model.SysUser{Account: "admin", Name: "超级管理员", Mobile: "13888888888", Password: "123456", IsAdmin: model.SysUserAdministratorForAdmin, Remark: "超级管理员"} }}, - &synchronized{iModel: model.NewSysUserTenant()}, - &synchronized{iModel: model.NewSysDepartment()}, &synchronized{iModel: model.NewSysRole()}, &synchronized{iModel: model.NewSysRoleMenu()}, &synchronized{iModel: model.NewSysRoleAuth()}, &synchronized{iModel: model.NewSysUserRole()}, // 日志管理 &synchronized{iModel: model.NewSysLog()}, &synchronized{iModel: model.NewSysUserLoginLog()}, // 功能信息 &synchronized{iModel: model.NewManageSupplier()}, + &synchronized{iModel: model.NewManageMaterial()}, &synchronized{iModel: model.NewManageMaterialPurchase()}, ) } func initCacheMode() { diff --git a/app/common/model/manage_equipment.go b/app/common/model/manage_equipment.go index 8722d86..29a8f08 100644 --- a/app/common/model/manage_equipment.go +++ b/app/common/model/manage_equipment.go @@ -4,6 +4,7 @@ package model type ManageEquipment struct { Model ModelTenant + Code string `gorm:"column:code;type:varchar(35);default:null;comment:编号" json:"code"` Title string `gorm:"column:title;type:varchar(100);default:null;comment:名称" json:"title"` Image Config string `gorm:"column:config;type:varchar(255);default:null;comment:配置" json:"config"` diff --git a/app/common/model/manage_material.go b/app/common/model/manage_material.go index 08283a5..448c9e1 100644 --- a/app/common/model/manage_material.go +++ b/app/common/model/manage_material.go @@ -4,15 +4,26 @@ package model type ManageMaterial struct { Model ModelTenant - SupplierID uint64 `gorm:"column:supplier_id;type:int;default:0;comment:供应商ID" json:"supplier_id"` + SupplierID uint64 `gorm:"column:supplier_id;type:int;default:0;comment:供应商ID" json:""` + Code string `gorm:"column:code;type:varchar(35);default:null;comment:编号" json:"code"` Title string `gorm:"column:title;type:varchar(100);default:null;comment:名称" json:"title"` Image - Unit int `gorm:"column:unit;type:tinyint(1);default:0;comment:单位" json:"unit"` - Remark string `gorm:"column:remark;type:varchar(255);default:null;comment:备注" json:"remark"` + Stock int `gorm:"column:stock;type:int(8);default:0;comment:库存数" json:"stock"` + FrozenStock int `gorm:"column:frozen_stock;type:int(8);default:0;comment:冻结的库存数" json:"-"` + Unit ManageMaterialUnit `gorm:"column:unit;type:tinyint(1);default:0;comment:单位" json:"unit"` + Remark string `gorm:"column:remark;type:varchar(255);default:null;comment:备注" json:"remark"` ModelDeleted ModelAt } +// ManageMaterialUnit 单位类型 +type ManageMaterialUnit int + +const ( + // ManageMaterialUnitForIndividual 个 + ManageMaterialUnitForIndividual ManageMaterialUnit = iota + 1 +) + func (m *ManageMaterial) TableName() string { return "manage_material" } diff --git a/app/common/model/manage_material_purchase.go b/app/common/model/manage_material_purchase.go index b09711b..9ae2f9e 100644 --- a/app/common/model/manage_material_purchase.go +++ b/app/common/model/manage_material_purchase.go @@ -3,10 +3,11 @@ package model // ManageMaterialPurchase 维修器材采购数据明细 type ManageMaterialPurchase struct { Model - OrderNo string `gorm:"column:order_no;type:varchar(20);default:null;comment:采购单号" json:"order_no"` - MaterialID uint64 `gorm:"column:material_id;type:int(6);default:0;comment:器材ID" json:"material_id"` + UID uint64 `gorm:"column:uid;type:int;default:0;comment:用户uuid" json:"-"` + OrderNo string `gorm:"column:order_no;type:varchar(30);default:null;comment:采购单号" json:"order_no"` + MaterialID uint64 `gorm:"column:material_id;type:int(6);default:0;comment:器材ID" json:"-"` Price float64 `gorm:"column:price;type:decimal(10,2);default:0;comment:采购单价" json:"price"` - Number int `gorm:"column:number;type:int(6);default:0;comment:采购数量" json:"number"` + Number int `gorm:"column:number;type:int(8);default:0;comment:采购数量" json:"number"` Remark string `gorm:"column:remark;type:varchar(255);default:null;comment:采购备注" json:"remark"` ModelDeleted ModelAt diff --git a/app/common/model/manage_material_warehouse.go b/app/common/model/manage_material_warehouse.go index 4fc37b6..fdeda2b 100644 --- a/app/common/model/manage_material_warehouse.go +++ b/app/common/model/manage_material_warehouse.go @@ -3,9 +3,10 @@ package model // ManageMaterialWarehouse 维修器材采购入库明细 type ManageMaterialWarehouse struct { Model + UID uint64 `gorm:"column:uid;type:int;default:0;comment:用户uuid" json:"-"` OrderNo string `gorm:"column:order_no;type:varchar(20);default:null;comment:采购单号" json:"order_no"` MaterialPurchaseID uint64 `gorm:"column:material_purchase_id;type:int(11);default:0;comment:器材采购ID" json:"material_purchase_id"` - Number int `gorm:"column:number;type:int(6);default:0;comment:入库数量" json:"number"` + Number int `gorm:"column:number;type:int(8);default:0;comment:入库数量" json:"number"` Remark string `gorm:"column:remark;type:varchar(255);default:null;comment:入库备注" json:"remark"` ModelDeleted ModelAt diff --git a/app/common/model/manage_material_work.go b/app/common/model/manage_material_work.go index ea069c0..81470fc 100644 --- a/app/common/model/manage_material_work.go +++ b/app/common/model/manage_material_work.go @@ -1,7 +1,11 @@ package model +// ManageMaterialWork 维修器材工单明细 type ManageMaterialWork struct { Model + WorkID uint64 `gorm:"column:work_id;type:int(11);default:0;comment:工单ID" json:"work_id"` + MaterialPurchaseID uint64 `gorm:"column:material_purchase_id;type:int(11);default:0;comment:器材采购ID" json:"material_purchase_id"` + Number int `json:"number"` ModelDeleted ModelAt } diff --git a/app/controller/manage/equipment.go b/app/controller/manage/equipment.go index 9f28c9e..a297f89 100644 --- a/app/controller/manage/equipment.go +++ b/app/controller/manage/equipment.go @@ -18,6 +18,7 @@ type ( // EquipmentInfo 装备信息 EquipmentInfo struct { basic.CommonIDString + Code string `json:"code"` Title string `json:"title"` Image string `json:"image"` CreatedAt time.Time `json:"created_at"` @@ -62,7 +63,7 @@ func (c *Equipment) List(title string, page, pageSize int) (*basic.PageDataRespo } var count int64 - if err := model2.PagesFields(mManageEquipment.ManageEquipment, &out, []string{"id", "title", "image", "created_at"}, + if err := model2.PagesFields(mManageEquipment.ManageEquipment, &out, []string{"id", "code", "title", "image", "created_at"}, page, pageSize, &count, where...); err != nil { return nil, err } @@ -71,9 +72,9 @@ func (c *Equipment) List(title string, page, pageSize int) (*basic.PageDataRespo for _, v := range out { list = append(list, &EquipmentInfo{ CommonIDString: basic.CommonIDString{ID: v.GetEncodeID()}, - Title: v.Title, - Image: v.Image.Analysis(config.SettingInfo.Domain), - CreatedAt: v.CreatedAt, + Code: v.Code, Title: v.Title, + Image: v.Image.Analysis(config.SettingInfo.Domain), + CreatedAt: v.CreatedAt, }) } return &basic.PageDataResponse{Data: list, Count: count}, nil diff --git a/app/controller/manage/material.go b/app/controller/manage/material.go index 52b4193..54ebc9b 100644 --- a/app/controller/manage/material.go +++ b/app/controller/manage/material.go @@ -5,30 +5,250 @@ import ( "ArmedPolice/app/controller/basic" "ArmedPolice/app/model" "ArmedPolice/app/service" + "ArmedPolice/config" + "ArmedPolice/lib" + "ArmedPolice/serve/orm" + "errors" + "gorm.io/gorm" + "time" ) type Material struct{ *service.Session } type MaterialHandle func(session *service.Session) *Material -func (c *Material) List(title string, page, pageSize int) (*basic.PageDataResponse, error) { - mManageMaterial := model.NewManageMaterial() - - out := make([]*model2.ManageMaterial, 0) +type ( + // MaterialInfo 基本信息 + MaterialInfo struct { + basic.CommonIDString + *model.ManageMaterialInfo + } + // MaterialParams 基本参数 + MaterialParams struct { + ID, SupplierID uint64 + Code, Title, Image, Remark string + Unit int + } + // MaterialPurchaseInfo 采购信息 + MaterialPurchaseInfo struct { + basic.CommonIDString + *model.ManageMaterialPurchaseInfo + } +) +func (c *MaterialParams) isExistForCode(iModel model2.IModel) (bool, error) { var count int64 - model2.PagesFields(mManageMaterial.ManageMaterial, &out, []string{"id", "title"}, page, pageSize, &count) - - return &basic.PageDataResponse{Data: nil, Count: 0}, nil + if err := model2.Count(iModel, &count, model2.NewWhere("code", c.Code), + model2.NewWhere("supplier_id", c.SupplierID)); err != nil { + return false, err + } + return count > 0, nil } -func (c *Material) Form() error { - return nil +func (c *MaterialParams) isExistForTitle(iModel model2.IModel) (bool, error) { + var count int64 + + if err := model2.Count(iModel, &count, model2.NewWhere("title", c.Title), + model2.NewWhere("supplier_id", c.SupplierID)); err != nil { + return false, err + } + return count > 0, nil } -func (c *Material) Delete() error { - return nil +// List 列表信息 +func (c *Material) List(supplierID uint64, code, title string, page, pageSize int) (*basic.PageDataResponse, error) { + mManageMaterial := model.NewManageMaterial() + + where := make([]*model2.ModelWhere, 0) + + if supplierID > 0 { + where = append(where, model2.NewWhere("m.supplier_id", supplierID)) + } + if code != "" { + where = append(where, model2.NewWhereLike("m.code", code)) + } + if title != "" { + where = append(where, model2.NewWhereLike("m.title", title)) + } + var count int64 + + out, err := mManageMaterial.Materials(page, pageSize, &count) + + if err != nil { + return nil, err + } + list := make([]*MaterialInfo, 0) + + for _, v := range out { + v.Image.Image = v.Analysis(config.SettingInfo.Domain) + list = append(list, &MaterialInfo{ + CommonIDString: basic.CommonIDString{ID: v.GetEncodeID()}, + ManageMaterialInfo: v, + }) + } + return &basic.PageDataResponse{Data: list, Count: count}, nil +} + +func (c *Material) Form(params *MaterialParams) error { + mManageMaterial := model.NewManageMaterial() + + if params.ID > 0 { + mManageMaterial.ID = params.ID + + isExist, err := model2.First(mManageMaterial.ManageMaterial) + + if err != nil { + return err + } else if !isExist { + return errors.New("操作错误,器材信息不存在") + } + if mManageMaterial.SupplierID != params.SupplierID { + + } + if mManageMaterial.Code != params.Code { + if isExist, err = params.isExistForCode(mManageMaterial); err != nil { + return err + } else if isExist { + return errors.New("操作错误,当前供应商下已存在此编码") + } + } + if mManageMaterial.Title != params.Title { + if isExist, err = params.isExistForTitle(mManageMaterial); err != nil { + return err + } else if isExist { + return errors.New("操作错误,当前供应商下已存在此名称") + } + } + } + mManageMaterial.SupplierID = params.SupplierID + mManageMaterial.Code = params.Code + mManageMaterial.Title = params.Title + mManageMaterial.Unit = model2.ManageMaterialUnit(params.Unit) + mManageMaterial.Remark = params.Remark + + if mManageMaterial.ID > 0 { + mManageMaterial.UpdatedAt = time.Now() + return model2.Updates(mManageMaterial.ManageMaterial, mManageMaterial.ManageMaterial) + } + isExist, err := params.isExistForCode(mManageMaterial) + + if err != nil { + return err + } else if isExist { + return errors.New("操作错误,当前供应商下已存在此编码") + } + if isExist, err = params.isExistForTitle(mManageMaterial); err != nil { + return err + } else if isExist { + return errors.New("操作错误,当前供应商下已存在此名称") + } + mManageMaterial.TenantID = c.TenantID + return model2.Create(mManageMaterial.ManageMaterial) +} + +// Delete 删除操作 +func (c *Material) Delete(id uint64) error { + mManageMaterial := model.NewManageMaterial() + mManageMaterial.ID = id + return model2.Delete(mManageMaterial.ManageMaterial) +} + +// Purchase 采购信息 +func (c *Material) Purchase(orderNo, supplierName, materialTitle string, page, pageSize int) (*basic.PageDataResponse, error) { + mManageMaterialPurchase := model.NewManageMaterialPurchase() + + where := make([]*model2.ModelWhere, 0) + + if orderNo != "" { + where = append(where, model2.NewWhereLike("p.order_no", orderNo)) + } + if supplierName != "" { + where = append(where, model2.NewWhereLike("s.name", supplierName)) + } + if materialTitle != "" { + where = append(where, model2.NewWhereLike("m.title", materialTitle)) + } + var count int64 + + out, err := mManageMaterialPurchase.Purchases(page, pageSize, &count, where...) + + if err != nil { + return nil, err + } + list := make([]*MaterialPurchaseInfo, 0) + + for _, v := range out { + list = append(list, &MaterialPurchaseInfo{ + CommonIDString: basic.CommonIDString{ID: v.GetEncodeID()}, + ManageMaterialPurchaseInfo: v, + }) + } + return &basic.PageDataResponse{Data: list, Count: count}, nil +} + +// PurchaseLaunch 采购发起 +func (c *Material) PurchaseLaunch(materialID uint64, price float64, number int, remark string) error { + mManageMaterialPurchase := model.NewManageMaterialPurchase() + mManageMaterialPurchase.UID = c.UID + mManageMaterialPurchase.OrderNo = lib.OrderNo() + mManageMaterialPurchase.MaterialID = materialID + mManageMaterialPurchase.Price = price + mManageMaterialPurchase.Number = number + mManageMaterialPurchase.Remark = remark + return model2.Create(mManageMaterialPurchase.ManageMaterialPurchase) +} + +// PurchaseDelete 采购删除 +func (c *Material) PurchaseDelete(id uint64) error { + mManageMaterialPurchase := model.NewManageMaterialPurchase() + mManageMaterialPurchase.ID = id + return model2.Delete(mManageMaterialPurchase.ManageMaterialPurchase) +} + +func (c *Material) Warehouse() { + +} + +// WarehouseLaunch 入库发起 +func (c *Material) WarehouseLaunch(materialPurchaseID uint64, number int, remark string) error { + mManageMaterialPurchase := model.NewManageMaterialPurchase() + mManageMaterialPurchase.ID = materialPurchaseID + + isExist, err := model2.FirstWhere(mManageMaterialPurchase.ManageMaterialPurchase) + + if err != nil { + return err + } else if !isExist { + return errors.New("操作错误,未找到相应的采购单") + } + mManageMaterialWarehouse := model.NewManageMaterialWarehouse() + mManageMaterialWarehouse.UID = c.UID + mManageMaterialWarehouse.MaterialPurchaseID = materialPurchaseID + mManageMaterialWarehouse.Number = number + mManageMaterialWarehouse.Remark = remark + + return orm.GetDB().Transaction(func(tx *gorm.DB) error { + if err = model2.Create(mManageMaterialWarehouse.ManageMaterialWarehouse); err != nil { + return err + } + // 同步库存 + mManageMaterial := model.NewManageMaterial() + + if err = model2.Updates(mManageMaterial.ManageMaterial, map[string]interface{}{ + "stock": gorm.Expr("stock + ?", number), + }); err != nil { + return err + } + return nil + }) +} + +// WarehouseDelete 入库删除 +func (c *Material) WarehouseDelete(id uint64) error { + mManageMaterialWarehouse := model.NewManageMaterialWarehouse() + mManageMaterialWarehouse.ID = id + return model2.Delete(mManageMaterialWarehouse.ManageMaterialWarehouse) } func NewMaterial() MaterialHandle { diff --git a/app/model/base.go b/app/model/base.go new file mode 100644 index 0000000..549fc8a --- /dev/null +++ b/app/model/base.go @@ -0,0 +1,9 @@ +package model + +import "ArmedPolice/app/common/model" + +type PageParams struct { + Page, PageSize int + Count *int64 + Where []*model.ModelWhere +} diff --git a/app/model/manage_material.go b/app/model/manage_material.go index 5d039a8..1b49c5f 100644 --- a/app/model/manage_material.go +++ b/app/model/manage_material.go @@ -3,6 +3,7 @@ package model import ( "ArmedPolice/app/common/model" "ArmedPolice/serve/orm" + "fmt" ) type ManageMaterial struct { @@ -10,11 +11,30 @@ type ManageMaterial struct { } type ManageMaterialInfo struct { - Title string `json:"title"` + *model.ManageMaterial + SupplierName string `json:"supplier_name"` } -func (m *ManageMaterial) Materials() { - orm.GetDB().Table(m.TableName() + " AS m").Select("") +func (m *ManageMaterial) Materials(page, pageSize int, count *int64, where ...*model.ModelWhere) ([]*ManageMaterialInfo, error) { + db := orm.GetDB().Table(m.TableName()+" AS m"). + Select("m.*", "s.name AS supplier_name"). + Joins(fmt.Sprintf("LEFT JOIN %s AS s ON m.supplier_id = s.id", model.NewManageSupplier().TableName())) + + if len(where) > 0 { + for _, wo := range where { + db = db.Where(wo.Condition, wo.Value) + } + } + out := make([]*ManageMaterialInfo, 0) + + if err := db.Order("p.id " + model.OrderModeToDesc).Offset((page - 1) * pageSize).Limit(pageSize).Scan(&out).Error; err != nil { + return nil, err + } + if err := db.Count(count).Error; err != nil { + return nil, err + } + return out, nil + } func NewManageMaterial() *ManageMaterial { diff --git a/app/model/manage_materiral_purchase.go b/app/model/manage_materiral_purchase.go new file mode 100644 index 0000000..fe4b1ee --- /dev/null +++ b/app/model/manage_materiral_purchase.go @@ -0,0 +1,47 @@ +package model + +import ( + "ArmedPolice/app/common/model" + "ArmedPolice/serve/orm" + "fmt" +) + +type ManageMaterialPurchase struct { + *model.ManageMaterialPurchase +} + +// ManageMaterialPurchaseInfo 器材采购信息 +type ManageMaterialPurchaseInfo struct { + *model.ManageMaterialPurchase + MaterialTitle string `json:"material_title"` + SupplierName string `json:"supplier_name"` + Username string `json:"username"` +} + +func (m *ManageMaterialPurchase) Purchases(page, pageSize int, count *int64, where ...*model.ModelWhere) ([]*ManageMaterialPurchaseInfo, error) { + db := orm.GetDB().Table(m.TableName()+" AS p"). + Select("p.*", "m.title AS material_title", "s.name AS supplier_names", "u.name AS username"). + Joins(fmt.Sprintf("LEFT JOIN %s AS m ON p.material_id = m.id", model.NewManageMaterial().TableName())). + Joins(fmt.Sprintf("LEFT JOIN %s AS s ON m.supplier_id = s.id", model.NewManageSupplier().TableName())). + Joins(fmt.Sprintf("LEFT JOIN %s AS u ON p.uid = u.uuid", model.NewSysUser().TableName())). + Where("p.is_deleted = ?", model.DeleteStatusForNot) + + if len(where) > 0 { + for _, wo := range where { + db = db.Where(wo.Condition, wo.Value) + } + } + out := make([]*ManageMaterialPurchaseInfo, 0) + + if err := db.Order("p.id " + model.OrderModeToDesc).Offset((page - 1) * pageSize).Limit(pageSize).Scan(&out).Error; err != nil { + return nil, err + } + if err := db.Count(count).Error; err != nil { + return nil, err + } + return out, nil +} + +func NewManageMaterialPurchase() *ManageMaterialPurchase { + return &ManageMaterialPurchase{} +} diff --git a/app/model/manage_materiral_warehouse.go b/app/model/manage_materiral_warehouse.go new file mode 100644 index 0000000..83d19db --- /dev/null +++ b/app/model/manage_materiral_warehouse.go @@ -0,0 +1,11 @@ +package model + +import "ArmedPolice/app/common/model" + +type ManageMaterialWarehouse struct { + *model.ManageMaterialWarehouse +} + +func NewManageMaterialWarehouse() *ManageMaterialWarehouse { + return &ManageMaterialWarehouse{} +} diff --git a/lib/order.go b/lib/order.go new file mode 100644 index 0000000..7891d2e --- /dev/null +++ b/lib/order.go @@ -0,0 +1,13 @@ +package lib + +import ( + "fmt" + "math/rand" + "time" +) + +// OrderNo 订单号 +func OrderNo() string { + now := time.Now() + return fmt.Sprintf("%s%d%05d", now.Format("20060102"), now.Unix(), rand.Intn(100000)) +} diff --git a/lib/order_test.go b/lib/order_test.go new file mode 100644 index 0000000..75e5d05 --- /dev/null +++ b/lib/order_test.go @@ -0,0 +1,11 @@ +package lib + +import "testing" + +func TestOrderNo(t *testing.T) { + i := 0 + for i < 20 { + t.Log(OrderNo()) + i++ + } +}