zhangqian
2024-07-01 73b6baf6af3d88cdcb0e2df7932a9bd96b0b85c5
models/material.go
@@ -1,9 +1,12 @@
package models
import (
   "encoding/json"
   "fmt"
   "github.com/shopspring/decimal"
   "github.com/spf13/cast"
   "gorm.io/gorm"
   "strings"
   "wms/constvar"
   "wms/pkg/mysqlx"
)
@@ -12,19 +15,19 @@
   // Material 物料
   Material struct {
      BaseModelString
      Name string `gorm:"unique;type:varchar(191);not null;comment:物料名称" json:"name"` //物料名称
      Name string `gorm:"type:varchar(191);not null;comment:物料名称" json:"name"` //物料名称
      //MaterialType constvar.ProductType  `gorm:"index;type:int(11);comment:物料类型(数字)" json:"materialType"`
      Model constvar.MaterialMode `gorm:"type:varchar(191);not null;comment:物料类型(字符串)" json:"model"` //物料类型(字符串)
      //Explain           string                  `gorm:"type:varchar(512);comment:编号说明" json:"explain"`
      //CodeStandardID    string                  `gorm:"type:varchar(191);comment:编码规范ID" json:"codeStandardID"`
      //Specs             string                  `gorm:"type:varchar(191);comment:物料规格" json:"specs"`
      //Type              string                  `gorm:"type:varchar(191);comment:物料型号" json:"type"`
      MinInventory decimal.Decimal `gorm:"type:decimal(35,18);comment:最小库存" json:"minInventory"` //最小库存
      MaxInventory decimal.Decimal `gorm:"type:decimal(35,18);comment:最大库存" json:"maxInventory"` //最大库存
      Amount       decimal.Decimal `gorm:"type:decimal(35,18);comment:数量" json:"amount"`         //数量
      CodeStandardID string          `gorm:"type:varchar(191);comment:编码规范ID" json:"codeStandardID"`
      Specs          string          `gorm:"type:varchar(191);comment:物料规格" json:"specs"`
      Type           string          `gorm:"type:varchar(191);comment:物料型号" json:"type"`
      MinInventory   decimal.Decimal `gorm:"type:decimal(35,18);comment:最小库存" json:"minInventory"` //最小库存
      MaxInventory   decimal.Decimal `gorm:"type:decimal(35,18);comment:最大库存" json:"maxInventory"` //最大库存
      Amount         decimal.Decimal `gorm:"type:decimal(35,18);comment:数量" json:"amount"`         //数量
      //LockAmount        decimal.Decimal         `gorm:"type:decimal(35,18);default:0;comment:锁定数量" json:"lockAmount"`
      Unit string `gorm:"type:varchar(100);comment:单位" json:"unit"` //单位
      //Note              string                  `gorm:"type:varchar(1024);comment:备注" json:"note"`
      Unit       string `gorm:"type:varchar(100);comment:单位" json:"unit"` //单位
      Note       string `gorm:"type:varchar(1024);comment:备注" json:"note"`
      TemplateID string `gorm:"type:varchar(191);comment:模板ID" json:"templateID"` //模板ID
      //FSource           string                  `gorm:"type:varchar(191);comment:生产车间" json:"-"`
      //Status            constvar.MaterialStatus `gorm:"type:int(11);comment:状态" json:"status"`
@@ -32,11 +35,13 @@
      PurchasePrice decimal.Decimal `gorm:"type:decimal(35,18);comment:采购价格" json:"purchasePrice"` //采购价格
      //PurchaseAheadDay  int                     `gorm:"type:int(11);comment:采购提前期(天)" json:"purchaseAheadDay"`
      //ProduceAheadDay   int                     `gorm:"type:int(11);comment:制造提前期(天)" json:"produceAheadDay"`
      MinPurchaseAmount decimal.Decimal `gorm:"type:decimal(35,18);comment:最小采购量" json:"minPurchaseAmount"` //最小采购量
      //PurchaseType      constvar.PurchaseType `gorm:"type:int(11);comment:采购类型" json:"purchaseType"`
      IsSale    bool            `gorm:"type:tinyint(1);comment:是否销售" json:"isSale"`        //是否销售
      SalePrice decimal.Decimal `gorm:"type:decimal(35,18);comment:销售单价" json:"salePrice"` //销售单价
      AutoIncr  uint            `gorm:"type:int(11);comment:自增ID;default:0;" json:"-"`
      MinPurchaseAmount decimal.Decimal       `gorm:"type:decimal(35,18);comment:最小采购量" json:"minPurchaseAmount"` //最小采购量
      PurchaseType      constvar.PurchaseType `gorm:"type:int(11);comment:采购类型" json:"purchaseType"`
      PurchaseTypes     string                `gorm:"type:varchar(255);comment:采购类型范围" json:"-"`
      PurchaseTypeList  []int                 `gorm:"-" json:"purchaseTypeList"`
      IsSale            bool                  `gorm:"type:tinyint(1);comment:是否销售" json:"isSale"`        //是否销售
      SalePrice         decimal.Decimal       `gorm:"type:decimal(35,18);comment:销售单价" json:"salePrice"` //销售单价
      AutoIncr          uint                  `gorm:"type:int(11);comment:自增ID;default:0;" json:"autoIncr"`
      //wms添加字段
      ProductType         constvar.ProductType       `gorm:"type:int(11);comment:产品类型" json:"productType"`                 //产品类型
      InvoicingStrategy   constvar.InvoicingStrategy `gorm:"type:int(11);comment:开票策略" json:"invoicingStrategy"`           //开票策略
@@ -46,7 +51,6 @@
      CategoryId          int                        `gorm:"type:int(11);comment:产品类别id" json:"categoryId"`                //产品类别id
      CategoryName        string                     `gorm:"type:varchar(255);comment:产品类别名称" json:"categoryName"`         //产品类别名称
      InternalReference   string                     `gorm:"type:varchar(255);comment:内部参考" json:"internalReference"`      //内部参考
      Barcode             string                     `gorm:"type:varchar(255);comment:条码" json:"barcode"`                  //条码
      ProductTagId        int                        `gorm:"type:int(11);comment:产品标签id" json:"productTagId"`              //产品标签id
      ProductTagName      string                     `gorm:"type:varchar(255);comment:产品标签名称" json:"productTagName"`       //产品标签名称
      CompanyId           int                        `gorm:"type:int(11);comment:公司id" json:"companyId"`                   //公司id
@@ -59,7 +63,7 @@
      ControlStrategy     constvar.InvoicingStrategy `gorm:"type:int(11);comment:控制策略" json:"controlStrategy"`             //控制策略
      BuyExplain          string                     `gorm:"type:varchar(512);comment:采购说明" json:"buyExplain"`             //采购说明
      Principal           string                     `gorm:"type:varchar(255);comment:负责人" json:"principal"`               //负责人
      Weight              decimal.Decimal            `gorm:"type:decimal(20,2);comment:重量" json:"weight"`                  //重量
      Weight              decimal.Decimal            `gorm:"type:decimal(20,3);comment:重量" json:"weight"`                  //重量
      Volume              decimal.Decimal            `gorm:"type:decimal(20,2);comment:体积" json:"volume"`                  //体积
      MakeAdvanceTime     decimal.Decimal            `gorm:"type:decimal(20,2);comment:制造前置时间" json:"makeAdvanceTime"`     //制造前置时间(天)
      OrderAdvanceTime    decimal.Decimal            `gorm:"type:decimal(20,2);comment:订单准备天数" json:"orderAdvanceTime"`    //订单准备天数(天)
@@ -67,9 +71,32 @@
      //HSCode                  string                     `gorm:"type:varchar(255);comment:HS编码" json:"HSCode"`                    //HS编码
      //OriginCountryId         int                        `gorm:"type:int(11);comment:原产地id" json:"originCountryId"`               //原产地id
      //OriginCountryName       string                     `gorm:"type:varchar(255);comment:原产地名称" json:"originCountryName"`        //原产地名称
      InStorageExplain        string `gorm:"type:varchar(512);comment:入库说明" json:"inStorageExplain"`          //入库说明
      OutStorageExplain       string `gorm:"type:varchar(512);comment:出库说明" json:"outStorageExplain"`         //出库说明
      InternalTransferExplain string `gorm:"type:varchar(512);comment:内部调拨说明" json:"internalTransferExplain"` //内部调拨说明
      InStorageExplain        string          `gorm:"type:varchar(512);comment:入库说明" json:"inStorageExplain"`          //入库说明
      OutStorageExplain       string          `gorm:"type:varchar(512);comment:出库说明" json:"outStorageExplain"`         //出库说明
      InternalTransferExplain string          `gorm:"type:varchar(512);comment:内部调拨说明" json:"internalTransferExplain"` //内部调拨说明
      AttachmentList          []*Attachment   `json:"attachmentList" gorm:"many2many:material_attachment"`
      IsStorage               int             `gorm:"type:tinyint(1);default:1;comment:是否存库(1是2否)" json:"isStorage"`   //无库存的在wms以及srm中需要过滤掉
      IsVirtual               int             `json:"isVirtual" gorm:"type:tinyint(1);default:2;comment:是否虚拟物料(1是2否)"` //虚拟物料在MRP计算时跳过该层级直接领用下级物料,虚拟物料不生成工单
      ReorderRuleNum          int64           `json:"reorderRuleNum"`
      MoreUnit                *bool           `json:"moreUnit" gorm:"type:tinyint(1);default:false;comment:启动多单位"`
      MoreUnitList            []UnitItems     `json:"moreUnitList" gorm:"-"`
      MoreUnitValue           string          `json:"-" gorm:"type:varchar(255);comment:多单位值"`
      GrossWeight             decimal.Decimal `json:"grossWeight" gorm:"type:decimal(20,3);comment:毛重"`
      NetWeight               decimal.Decimal `json:"netWeight" gorm:"type:decimal(20,3);comment:净重"`
      GrossUnit               string          `json:"grossUnit" gorm:"type:varchar(255);comment:毛重单位"`
      NetUnit                 string          `json:"netUnit" gorm:"type:varchar(255);comment:净重单位"`
      Attributes              []Attribute     `json:"attributes" gorm:"-"` //动态属性
      //以下为不存库的字段
      AttachmentIDs    []uint          `json:"attachmentIDs" gorm:"-"`
      PredictionAmount decimal.Decimal `json:"predictionAmount" gorm:"-"`
      InputAmount      decimal.Decimal `json:"inputAmount" gorm:"-"`
      OutputAmount     decimal.Decimal `json:"outputAmount" gorm:"-"`
      MinInventoryRule decimal.Decimal `json:"minInventoryRule" gorm:"-"` //最小库存
      MaxInventoryRule decimal.Decimal `json:"maxInventoryRule" gorm:"-"` //最大库存
      CreateBy string `gorm:"type:varchar(255);comment:导入人、创建人" json:"createBy"`       //创建人
      BarCode  string `gorm:"type:varchar(255);comment:(商品/产品/物料的)条形码" json:"barCode"` //条码
   }
   MaterialSearch struct {
@@ -83,11 +110,19 @@
      Ids         []string
      Orm         *gorm.DB
      CategoryIds []int
      Preload     bool
      Fields      string
   }
   IdAndName struct {
      Id   string `json:"id"`
      Name string `json:"name"`
   }
   UnitItems struct {
      Amount   decimal.Decimal `json:"amount"`
      Unit     string          `json:"unit"`
      Floating bool            `json:"floating"` //是否浮动利率
   }
)
@@ -95,8 +130,62 @@
   return "material"
}
func (slf *Material) AfterFind(tx *gorm.DB) (err error) {
   if slf.PurchaseTypes != "" && strings.Contains(slf.PurchaseTypes, ",") {
      list := strings.Split(slf.PurchaseTypes, ",")
      for _, v := range list {
         slf.PurchaseTypeList = append(slf.PurchaseTypeList, cast.ToInt(v))
      }
   } else if slf.PurchaseType != 0 { //兼容旧数据
      slf.PurchaseTypeList = append(slf.PurchaseTypeList, int(slf.PurchaseType))
   }
   if slf.MoreUnitValue != "" {
      var arr []UnitItems
      err := json.Unmarshal([]byte(slf.MoreUnitValue), &arr)
      if err != nil {
         return err
      }
      slf.MoreUnitList = arr
   }
   return
}
func (slf *Material) BeforeCreate(tx *gorm.DB) (err error) {
   if len(slf.PurchaseTypeList) > 0 {
      var typeList []string
      for _, v := range slf.PurchaseTypeList {
         if v != 0 {
            typeList = append(typeList, cast.ToString(v))
         }
      }
      slf.PurchaseTypes = strings.Join(typeList, ",")
      if len(slf.PurchaseTypeList) == 1 {
         slf.PurchaseType = constvar.PurchaseType(slf.PurchaseTypeList[0])
      }
   }
   if len(slf.MoreUnitList) != 0 {
      items := make([]UnitItems, 0)
      for k, item := range slf.MoreUnitList {
         if item.Unit != "" && !item.Amount.IsZero() {
            items = append(items, slf.MoreUnitList[k])
         }
      }
      str, err := json.Marshal(items)
      if err != nil {
         return err
      }
      slf.MoreUnitValue = string(str)
   }
   return
}
func (slf *Material) BeforeUpdate(tx *gorm.DB) (err error) {
   return slf.BeforeCreate(tx)
}
func NewMaterialSearch() *MaterialSearch {
   return &MaterialSearch{Orm: mysqlx.GetDB()}
   return &MaterialSearch{Orm: mysqlx.GetDB().Where("is_storage = ?", 1)} //只查询有库存的
}
func (slf *MaterialSearch) SetOrm(tx *gorm.DB) *MaterialSearch {
@@ -116,6 +205,11 @@
func (slf *MaterialSearch) SetID(id string) *MaterialSearch {
   slf.ID = id
   return slf
}
func (slf *MaterialSearch) SetBarCode(barCode string) *MaterialSearch {
   slf.BarCode = barCode
   return slf
}
@@ -174,11 +268,24 @@
//   return slf
//}
func (slf *MaterialSearch) SetPreload(preload bool) *MaterialSearch {
   slf.Preload = preload
   return slf
}
func (slf *MaterialSearch) SetFields(fields string) *MaterialSearch {
   slf.Fields = fields
   return slf
}
func (slf *MaterialSearch) build() *gorm.DB {
   var db = slf.Orm.Table(slf.TableName())
   if slf.ID != "" {
      db = db.Where("id = ?", slf.ID)
   }
   if slf.BarCode != "" {
      db = db.Where("bar_code = ?", slf.BarCode)
   }
   if slf.Name != "" {
@@ -238,6 +345,14 @@
   }
   if len(slf.CategoryIds) > 0 {
      db = db.Where("category_id in ?", slf.CategoryIds)
   }
   if slf.Fields != "" {
      db = db.Select(slf.Fields)
   }
   if slf.Preload {
      db = db.Preload("AttachmentList")
   }
   return db
@@ -329,7 +444,7 @@
      db      = slf.build()
   )
   if err := db.Count(&total).Error; err != nil {
   if err := db.Model(&Material{}).Count(&total).Error; err != nil {
      return records, total, fmt.Errorf("find count err: %v", err)
   }
   if slf.PageNum*slf.PageSize > 0 {
@@ -475,9 +590,9 @@
//   return fileName, nil
//}
func (slf *MaterialSearch) MaxAutoIncr() (int64, error) {
func (slf *MaterialSearch) MaxAutoIncr() (int, error) {
   type Result struct {
      Max int64
      Max int
   }
   var (
@@ -493,22 +608,41 @@
}
type ResponseDisuseList struct {
   Id             int                      `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
   Number         string                   `json:"number" gorm:"type:varchar(255)"`                                 //单号
   SourceNumber   string                   `json:"sourceNumber" gorm:"type:varchar(255)"`                           //源单号
   Status         constvar.OperationStatus `json:"status" gorm:"type:int(11);not null;comment:状态"`                  //状态
   FromLocationID int                      `json:"fromLocationId"   gorm:"type:int;not null;comment:源位置id"`         //源位置id
   FromLocation   Location                 `json:"fromLocation"     gorm:"foreignKey:FromLocationID;references:Id"` //源位置
   ToLocation     Location                 `json:"toLocation"      gorm:"foreignKey:ToLocationID;references:Id"`    //目标位置
   ToLocationID   int                      `json:"toLocationId"    gorm:"type:int;not null;comment:目标位置id"`         //目标位置id
   OperationDate  string                   `json:"operationDate" gorm:"type:varchar(31);comment:安排日期"`
   ContacterID    int                      `json:"contacterID" gorm:"type:int;comment:联系人ID"`
   ContacterName  string                   `json:"contacterName" gorm:"type:varchar(63);comment:联系人姓名"`
   CompanyID      int                      `json:"companyID" gorm:"type:int;comment:公司ID-客户"`
   CompanyName    string                   `json:"companyName" gorm:"type:varchar(127);comment:公司名称-客户"`
   Comment        string                   `json:"comment" gorm:"type:text;comment:备注"`
   ProductId      string                   `json:"productId" gorm:"type:varchar(191);not null;comment:产品id"`   //产品id
   ProductName    string                   `json:"productName" gorm:"type:varchar(255);not null;comment:产品名称"` //产品名称
   Amount         decimal.Decimal          `json:"amount" gorm:"type:decimal(20,2);not null;comment:数量"`       //数量
   Unit           string                   `json:"unit" gorm:"type:varchar(31);comment:单位"`
   Id                int                        `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
   Number            string                     `json:"number" gorm:"type:varchar(255)"`                                 //单号
   SourceNumber      string                     `json:"sourceNumber" gorm:"type:varchar(255)"`                           //源单号
   Status            constvar.OperationStatus   `json:"status" gorm:"type:int(11);not null;comment:状态"`                  //状态
   FromLocationID    int                        `json:"fromLocationId"   gorm:"type:int;not null;comment:源位置id"`         //源位置id
   FromLocation      Location                   `json:"fromLocation"     gorm:"foreignKey:FromLocationID;references:Id"` //源位置
   ToLocation        Location                   `json:"toLocation"      gorm:"foreignKey:ToLocationID;references:Id"`    //目标位置
   ToLocationID      int                        `json:"toLocationId"    gorm:"type:int;not null;comment:目标位置id"`         //目标位置id
   OperationDate     string                     `json:"operationDate" gorm:"type:varchar(31);comment:安排日期"`
   ContacterID       int                        `json:"contacterID" gorm:"type:int;comment:联系人ID"`
   ContacterName     string                     `json:"contacterName" gorm:"type:varchar(63);comment:联系人姓名"`
   CompanyID         string                     `json:"companyID"` //公司ID-客户
   CompanyName       string                     `json:"companyName" gorm:"type:varchar(127);comment:公司名称-客户"`
   Comment           string                     `json:"comment" gorm:"type:text;comment:备注"`
   ProductId         string                     `json:"productId" gorm:"type:varchar(191);not null;comment:产品id"`   //产品id
   ProductName       string                     `json:"productName" gorm:"type:varchar(255);not null;comment:产品名称"` //产品名称
   Amount            decimal.Decimal            `json:"amount" gorm:"type:decimal(20,2);not null;comment:数量"`       //数量
   Unit              string                     `json:"unit" gorm:"type:varchar(31);comment:单位"`
   BaseOperationType constvar.BaseOperationType `json:"baseOperationType" gorm:"type:tinyint;not null;comment:基础作业类型"` //基础作业类型
}
func (slf *MaterialSearch) Update(record *Material) error {
   var db = slf.build()
   if err := db.Omit("CreatedAt").Updates(record).Error; err != nil {
      return fmt.Errorf("save err: %v, record: %+v", err, record)
   }
   return nil
}
func MaterialMap(records []*Material) (m map[string]*Material) {
   m = make(map[string]*Material, len(records))
   for _, record := range records {
      m[record.ID] = record
   }
   return m
}