liujiandao
2024-04-17 216c9e49dafdb7a5bd025f6d6fa899a33befc38c
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"
)
@@ -34,6 +37,8 @@
      //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"`
      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"`
@@ -67,13 +72,20 @@
      //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"` //内部调拨说明
      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"`
      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:净重单位"`
      //以下为不存库的字段
      AttachmentIDs    []uint          `json:"attachmentIDs" gorm:"-"`
@@ -103,12 +115,65 @@
      Id   string `json:"id"`
      Name string `json:"name"`
   }
   UnitItems struct {
      Amount   decimal.Decimal `json:"amount"`
      Unit     string          `json:"unit"`
      Floating bool            `json:"floating"`
   }
)
func (slf Material) TableName() string {
   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 {
      str, err := json.Marshal(slf.MoreUnitList)
      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().Where("is_storage = ?", 1)} //只查询有库存的
}