zhangqian
2024-07-01 73b6baf6af3d88cdcb0e2df7932a9bd96b0b85c5
models/operation.go
@@ -2,7 +2,9 @@
import (
   "fmt"
   "github.com/shopspring/decimal"
   "gorm.io/gorm"
   "time"
   "wms/constvar"
   "wms/pkg/mysqlx"
)
@@ -11,39 +13,77 @@
   // Operation 操作表
   Operation struct {
      WmsModel
      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)"`                           //源单号
      OperationTypeId int                      `json:"operationTypeId" gorm:"type:int;not null;comment:作业类型id"`         //作业类型id
      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"` //源位置
      ToLocationID    int                      `json:"toLocationId"    gorm:"type:int;not null;comment:目标位置id"`         //目标位置id
      ToLocation      Location                 `json:"toLocation"      gorm:"foreignKey:ToLocationID;references: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:备注"`
      Id                int                      `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
      Number            string                   `json:"number" gorm:"type:varchar(255)"`                           //单号
      SourceNumber      string                   `json:"sourceNumber" gorm:"index;type:varchar(255)"`               //源单号
      OperationTypeId   int                      `json:"operationTypeId" gorm:"type:int;not null;comment:作业类型id"`   //作业类型id
      OperationTypeName string                   `json:"operationTypeName" gorm:"type:varchar(127);comment:作业类型名称"` //作业类型名称
      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"` //源位置
      //ToLocationID      int                      `json:"toLocationId"    gorm:"type:int;not null;comment:目标位置id"`         //目标位置id
      //ToLocation        Location                 `json:"toLocation"      gorm:"foreignKey:ToLocationID;references: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" gorm:"type:varchar(255);comment:公司ID-客户"`
      CompanyName       string                   `json:"companyName" gorm:"type:varchar(127);comment:公司名称-客户"`
      Comment           string                   `json:"comment" gorm:"type:text;comment:备注"`
      LogisticCompanyId string                   `json:"logisticCompanyId"   gorm:"type:varchar(191);comment:物流公司id"`
      LogisticCompany   LogisticCompany          `json:"logisticCompany"      gorm:"foreignKey:LogisticCompanyId"`
      WaybillNumber     string                   `json:"waybillNumber" gorm:"type:varchar(255);comment:运单号"`    //运单号
      Weight            decimal.Decimal          `gorm:"type:decimal(20,2);comment:重量" json:"weight"`           //重量
      LogisticWeight    decimal.Decimal          `gorm:"type:decimal(20,2);comment:物流重量" json:"logisticWeight"` //物流重量
      Source            string                   `json:"source" gorm:"type:varchar(255);comment:来源系统,用于返回修改状态"`
      OperationSource   constvar.OperationSource `json:"operationSource" gorm:"type:tinyint(3);not null;default:0;comment:操作来源"` //操作来源
      Details []*OperationDetails `json:"details" gorm:"foreignKey:OperationID;references:Id"`
      Details           []*OperationDetails        `json:"details" gorm:"foreignKey:OperationID;references:Id"`
      BaseOperationType constvar.BaseOperationType `json:"baseOperationType" gorm:"type:tinyint;not null;comment:基础作业类型"` //基础作业类型
      //Weight          decimal.Decimal          `json:"weight" gorm:"type:decimal(20,2);comment:重量(kg)"`
      //TransferWeight  decimal.Decimal          `json:"transferWeight" gorm:"type:decimal(20,2);comment:物流重量(kg)"`
      //CarrierID       int                      `json:"carrierID" gorm:"type:int;comment:承运商ID"`
      //CarrierName     string                   `json:"carrierName" gorm:"type:varchar(63);comment:承运商名称"`
      //Tracking        string                   `json:"tracking" gorm:"type:varchar(127);comment:追踪参考"`
      AuditDate     string `json:"auditDate" gorm:"type:varchar(31);comment:审批时间"`
      ReceiverName  string `json:"receiverName" gorm:"type:varchar(31);comment:收货人姓名"`
      ReceiverPhone string `json:"receiverPhone" gorm:"type:varchar(31);comment:联系电话"`
      ReceiverAddr  string `json:"receiverAddr" gorm:"type:varchar(255);comment:收货地址"`
      LocationID         int      `json:"locationID"   gorm:"type:int;not null;comment:源位置id"`          //源位置id
      Location           Location `json:"location" gorm:"foreignkey:LocationID;references:Id"`          //源位置
      ToLocationID       int      `json:"toLocationId"    gorm:"type:int;not null;comment:仓库位置id"`      //目标位置id
      ToLocation         Location `json:"toLocation"      gorm:"foreignKey:ToLocationID;references:Id"` //目标位置
      SalesDetailsNumber string   `gorm:"type:varchar(191);comment:销售明细编码" json:"salesDetailsNumber"`
      ManagerId    string `json:"managerId" gorm:"type:varchar(255);comment:主管id"`
      Manager      string `json:"manager" gorm:"type:varchar(255);comment:主管名称"`
      AccountantId string `json:"accountantId" gorm:"type:varchar(255);comment:会计id"`
      Accountant   string `json:"accountant" gorm:"type:varchar(255);comment:会计名称"`
      CustodianId  string `json:"custodianId" gorm:"type:varchar(255);comment:保管员id"`
      Custodian    string `json:"custodian" gorm:"type:varchar(255);comment:保管员名称"`
      CreatedBy    string `json:"createBy" gorm:"type:varchar(255);comment:创建者UserId"`
      CheckedBy    string `json:"checkedBy" gorm:"type:varchar(255);comment:验证者UserId"`
      Remark       string `json:"remark"`
      WarehouseId         int       `json:"warehouseId" gorm:"type:int;not null;comment:仓库id"`
      Warehouse           Warehouse `json:"warehouse" gorm:"foreignKey:WarehouseId"`
      IsInternalOutput    bool      `json:"isInternalOutput"` //是否调拨产生的出库
      InventoryDealerType int       `json:"inventoryDealerType" gorm:"type:varchar(255);comment:调拨出入库分类(对应dict字典表的ID)"`
      InventoryDealer     MiniDict  `json:"inventoryDealer" gorm:"foreignKey:InventoryDealerType;"`
   }
   OperationSearch struct {
      Operation
      Order    string
      PageNum  int
      PageSize int
      Keyword  string
      Orm      *gorm.DB
      Preload  bool
      Order                  string
      PageNum                int
      PageSize               int
      Keyword                string
      Orm                    *gorm.DB
      Preload                bool
      Disuse                 bool
      Ids                    []int
      SourceNumbers          []string
      SalesDetailsNumbers    []string
      Fields                 string
      BeginTime              time.Time
      EndTime                time.Time
      InventoryDealerTypeIds []int //
   }
)
@@ -80,6 +120,11 @@
   return slf
}
func (slf *OperationSearch) SetFields(fields string) *OperationSearch {
   slf.Fields = fields
   return slf
}
func (slf *OperationSearch) SetOperationTypeId(operationTypeId int) *OperationSearch {
   slf.OperationTypeId = operationTypeId
   return slf
@@ -90,26 +135,155 @@
   return slf
}
func (slf *OperationSearch) SetNumber(number string) *OperationSearch {
   slf.Number = number
   return slf
}
func (slf *OperationSearch) SetSourceNumber(sourceNumber string) *OperationSearch {
   slf.SourceNumber = sourceNumber
   return slf
}
func (slf *OperationSearch) SetSourceNumbers(numbers []string) *OperationSearch {
   slf.SourceNumbers = numbers
   return slf
}
func (slf *OperationSearch) SetDisuse(disuse bool) *OperationSearch {
   slf.Disuse = disuse
   return slf
}
func (slf *OperationSearch) SetIds(ids []int) *OperationSearch {
   slf.Ids = ids
   return slf
}
func (slf *OperationSearch) SetStatus(status constvar.OperationStatus) *OperationSearch {
   slf.Status = status
   return slf
}
func (slf *OperationSearch) SetBaseOperationType(baseOperationType constvar.BaseOperationType) *OperationSearch {
   slf.BaseOperationType = baseOperationType
   return slf
}
func (slf *OperationSearch) SetSalesDetailsNumber(salesDetailsNumber string) *OperationSearch {
   slf.SalesDetailsNumber = salesDetailsNumber
   return slf
}
func (slf *OperationSearch) SetSalesDetailsNumbers(salesDetailsNumbers []string) *OperationSearch {
   slf.SalesDetailsNumbers = salesDetailsNumbers
   return slf
}
func (slf *OperationSearch) SetOperationSource(operationSource constvar.OperationSource) *OperationSearch {
   slf.OperationSource = operationSource
   return slf
}
func (slf *OperationSearch) SetTimeBetween(beginTime, endTime time.Time) *OperationSearch {
   slf.BeginTime = beginTime
   slf.EndTime = endTime
   return slf
}
func (slf *OperationSearch) SetInventoryDealerTypeIds(inventoryDealerTypeArr []int) *OperationSearch {
   slf.InventoryDealerTypeIds = inventoryDealerTypeArr
   return slf
}
func (slf *OperationSearch) SetWarehouseId(warehouseId int) *OperationSearch {
   slf.WarehouseId = warehouseId
   return slf
}
func (slf *OperationSearch) build() *gorm.DB {
   var db = slf.Orm.Model(&Operation{})
   if slf.ID != 0 {
      db = db.Where("id = ?", slf.ID)
   if slf.Id != 0 {
      db = db.Where("id = ?", slf.Id)
   }
   if slf.Order != "" {
      db = db.Order(slf.Order)
   }
   //if slf.Keyword != "" {
   //   db = db.Where("product_name like ?", fmt.Sprintf("%%%v%%", slf.Keyword))
   //}
   if slf.Keyword != "" {
      db = db.Where("number like ? or source_number like ?", fmt.Sprintf("%%%v%%", slf.Keyword), fmt.Sprintf("%%%v%%", slf.Keyword))
   }
   if slf.SourceNumber != "" {
      db = db.Where("source_number = ?", slf.SourceNumber)
   }
   if len(slf.SourceNumbers) > 0 {
      db = db.Where("source_number in (?)", slf.SourceNumbers)
   }
   if slf.Number != "" {
      db = db.Where("number like ?", fmt.Sprintf("%%%v%%", slf.Number))
   }
   if slf.OperationTypeId != 0 {
      db.Where("operation_type_id = ?", slf.OperationTypeId)
   }
   if slf.Preload {
      db = db.Model(&Operation{}).Preload("Details").Preload("FromLocation").Preload("ToLocation")
      db = db.Model(&Operation{}).Preload("Details").
         Preload("Details.Product").
         Preload("LogisticCompany").
         Preload("Location").
         Preload("ToLocation").
         Preload("Details.FromLocation").
         Preload("Details.ToLocation").
         Preload("InventoryDealer").Preload("Warehouse")
   }
   if slf.Disuse {
      db = db.Where("operation_type_id = ?", 0)
   }
   if len(slf.Ids) > 0 {
      db = db.Where("id in (?)", slf.Ids)
   }
   if slf.Status > 0 {
      db = db.Where("status = ?", slf.Status)
   }
   if slf.BaseOperationType != 0 {
      db = db.Where("base_operation_type = ?", slf.BaseOperationType)
   }
   if slf.SalesDetailsNumber != "" {
      db = db.Where("sales_details_number = ?", slf.SalesDetailsNumber)
   }
   if len(slf.SalesDetailsNumbers) > 0 {
      db = db.Where("sales_details_number in (?)", slf.SalesDetailsNumbers)
   }
   if slf.OperationSource != 0 {
      db = db.Where("operation_source = ?", slf.OperationSource)
   }
   if slf.Fields != "" {
      db = db.Select(slf.Fields)
   }
   if !slf.BeginTime.IsZero() && !slf.EndTime.IsZero() {
      db = db.Where("created_at between ? and ?", slf.BeginTime, slf.EndTime)
   }
   if len(slf.InventoryDealerTypeIds) > 0 {
      db = db.Where("inventory_dealer_type in (?)", slf.InventoryDealerTypeIds)
   }
   if slf.WarehouseId != 0 {
      db = db.Where("warehouse_id = ?", slf.WarehouseId)
   }
   return db
@@ -212,7 +386,7 @@
   if slf.PageNum*slf.PageSize > 0 {
      db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize)
   }
   if err := db.Find(&records).Error; err != nil {
   if err := db.Order("created_at desc").Find(&records).Error; err != nil {
      return records, total, fmt.Errorf("find records err: %v", err)
   }
@@ -228,6 +402,18 @@
   if slf.PageNum*slf.PageSize > 0 {
      db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize)
   }
   if err := db.Find(&records).Error; err != nil {
      return records, fmt.Errorf("find records err: %v", err)
   }
   return records, nil
}
func (slf *OperationSearch) FindIds() ([]int, error) {
   var (
      records = make([]int, 0)
      db      = slf.build()
   )
   if err := db.Find(&records).Error; err != nil {
      return records, fmt.Errorf("find records err: %v", err)
   }
@@ -272,3 +458,25 @@
   return records, nil
}
func (slf *OperationSearch) MaxAutoIncr() (int, error) {
   var (
      db    = slf.build()
      total int64
   )
   if err := db.Count(&total).Error; err != nil {
      return int(total), fmt.Errorf("max err: %v", err)
   }
   return int(total), nil
}
func (slf *OperationSearch) Count() (int64, error) {
   var (
      total int64
      db    = slf.build()
   )
   err := db.Count(&total).Error
   return total, err
}