package models import ( "fmt" "gorm.io/gorm" "wms/constvar" "wms/pkg/mysqlx" ) type ( // Location 位置 Location struct { WmsModel Id int `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"` Name string `json:"name" gorm:"index;type:varchar(255);not null;comment:位置名称"` //位置名称 ParentId int `json:"parentId" gorm:"type:int;comment:上级id"` //上级id CompanyId int `json:"companyId" gorm:"type:int"` //公司id //Company Company `json:"company" gorm:"foreignKey:CompanyId"` //公司 Type constvar.LocationType `json:"type" gorm:"type:int(11);comment:位置类型"` //位置类型 CountFrequency int `json:"countFrequency" gorm:"type:tinyint;comment:盘点频率(天)"` //盘点频率(天) IsScrapLocation bool `json:"isScrapLocation" gorm:"type:tinyint;comment:是否报废位置"` //是否报废位置 IsReturnLocation bool `json:"isReturnLocation" gorm:"type:tinyint;comment:是否退货位置"` //是否退货位置 ReplenishLocation bool `json:"replenishLocation" gorm:"type:tinyint;comment:是否补充位置"` //是否补充位置 ForceRemovalStrategy constvar.ForceRemovalStrategy `json:"forceRemovalStrategy" gorm:"type:tinyint;comment:下架策略"` //下架策略 Notes string `json:"notes" gorm:"type:varchar(255);comment:外部备注"` //外部备注 RecentlyCount string `json:"recentlyCount" gorm:"type:varchar(255);comment:最近盘点"` //最近盘点 NextCount string `json:"nextCount" gorm:"type:varchar(255);comment:下次盘点"` //下次盘点 JointName string `json:"jointName" gorm:"type:varchar(255);comment:拼接名称"` //拼接名称 Children []*Location `json:"children" gorm:"-"` WarehouseId int `json:"warehouseId" gorm:"type:int;not null;default:0;comment:仓库ID"` //仓库ID Warehouse Warehouse `json:"warehouse" gorm:"foreignKey:WarehouseId"` //仓库 } LocationSearch struct { Location Order string PageNum int PageSize int Keyword string Orm *gorm.DB Preload bool JointNames []string Ids []int } ) func (slf *Location) TableName() string { return "wms_location" } func NewLocationSearch() *LocationSearch { return &LocationSearch{Orm: mysqlx.GetDB()} } func (slf *LocationSearch) SetOrm(tx *gorm.DB) *LocationSearch { slf.Orm = tx return slf } func (slf *LocationSearch) SetPage(page, size int) *LocationSearch { slf.PageNum, slf.PageSize = page, size return slf } func (slf *LocationSearch) SetOrder(order string) *LocationSearch { slf.Order = order return slf } func (slf *LocationSearch) SetID(ID int) *LocationSearch { slf.Id = ID return slf } func (slf *LocationSearch) SetIds(ids []int) *LocationSearch { slf.Ids = ids return slf } func (slf *LocationSearch) SetJointName(code string) *LocationSearch { slf.JointName = code return slf } func (slf *LocationSearch) SetJointNames(codes []string) *LocationSearch { slf.JointNames = codes return slf } func (slf *LocationSearch) SetName(name string) *LocationSearch { slf.Name = name return slf } func (slf *LocationSearch) SetKeyword(keyword string) *LocationSearch { slf.Keyword = keyword return slf } func (slf *LocationSearch) SetPreload(preload bool) *LocationSearch { slf.Preload = preload return slf } func (slf *LocationSearch) SetType(_type int) *LocationSearch { slf.Type = constvar.LocationType(_type) return slf } func (slf *LocationSearch) SetParentId(parentId int) *LocationSearch { slf.ParentId = parentId return slf } func (slf *LocationSearch) SetCompanyId(companyId int) *LocationSearch { slf.CompanyId = companyId return slf } func (slf *LocationSearch) SetIsScrapLocation(isScrapLocation bool) *LocationSearch { slf.IsScrapLocation = isScrapLocation return slf } func (slf *LocationSearch) build() *gorm.DB { var db = slf.Orm.Table(slf.TableName()) if slf.Id != 0 { db = db.Where("id = ?", slf.Id) } if len(slf.Ids) > 0 { db = db.Where("id in (?)", slf.Ids) } if slf.Order != "" { db = db.Order(slf.Order) } if slf.Keyword != "" { db = db.Where("name like ?", fmt.Sprintf("%%%v%%", slf.Keyword)) } if slf.Name != "" { db = db.Where("name = ?", slf.Name) } if slf.Type != 0 { db = db.Where("type=?", slf.Type) } if slf.ParentId > 0 { db = db.Where("parent_id=?", slf.ParentId) } if slf.CompanyId != 0 { db = db.Where("company_id=?", slf.CompanyId) } if slf.JointName != "" { db = db.Where("joint_name like ?", slf.JointName+"%") } if len(slf.JointNames) != 0 { db = db.Where("joint_name in (?)", slf.JointNames) } if slf.IsScrapLocation { db = db.Where("is_scrap_location = ?", slf.IsScrapLocation) } if slf.Preload { db = db.Preload("Warehouse") } return db } // Create 单条插入 func (slf *LocationSearch) Create(record *Location) error { var db = slf.build() if err := db.Create(record).Error; err != nil { return err } return nil } func (slf *LocationSearch) CreateReturnId(record *Location) (int, error) { var db = slf.build() if err := db.Create(record).Error; err != nil { return 0, err } return record.Id, nil } // CreateBatch 批量插入 func (slf *LocationSearch) CreateBatch(records []*Location) error { var db = slf.build() if err := db.Create(&records).Error; err != nil { return fmt.Errorf("create batch err: %v, records: %+v", err, records) } return nil } func (slf *LocationSearch) Update(record *Location) 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 (slf *LocationSearch) UpdateByMap(upMap map[string]interface{}) error { var ( db = slf.build() ) if err := db.Updates(upMap).Error; err != nil { return fmt.Errorf("update by map err: %v, upMap: %+v", err, upMap) } return nil } func (slf *LocationSearch) UpdateByQuery(query string, args []interface{}, upMap map[string]interface{}) error { var ( db = slf.Orm.Table(slf.TableName()).Where(query, args...) ) if err := db.Updates(upMap).Error; err != nil { return fmt.Errorf("update by query err: %v, query: %s, args: %+v, upMap: %+v", err, query, args, upMap) } return nil } func (slf *LocationSearch) Delete() error { var db = slf.build() return db.Unscoped().Delete(&Location{}).Error } func (slf *LocationSearch) First() (*Location, error) { var ( record = new(Location) db = slf.build() ) if err := db.First(record).Error; err != nil { return record, err } return record, nil } func (slf *LocationSearch) Find() ([]*Location, int64, error) { var ( records = make([]*Location, 0) total int64 db = slf.build() ) if err := db.Count(&total).Error; err != nil { return records, total, fmt.Errorf("find count err: %v", err) } 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, total, fmt.Errorf("find records err: %v", err) } return records, total, nil } func (slf *LocationSearch) FindNotTotal() ([]*Location, error) { var ( records = make([]*Location, 0) db = slf.build() ) 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 } // FindByQuery 指定条件查询. func (slf *LocationSearch) FindByQuery(query string, args []interface{}) ([]*Location, int64, error) { var ( records = make([]*Location, 0) total int64 db = slf.Orm.Table(slf.TableName()).Where(query, args...) ) if err := db.Count(&total).Error; err != nil { return records, total, fmt.Errorf("find by query count err: %v", err) } 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, total, fmt.Errorf("find by query records err: %v, query: %s, args: %+v", err, query, args) } return records, total, nil } // FindByQueryNotTotal 指定条件查询&不查询总条数. func (slf *LocationSearch) FindByQueryNotTotal(query string, args []interface{}) ([]*Location, error) { var ( records = make([]*Location, 0) db = slf.Orm.Table(slf.TableName()).Where(query, args...) ) 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 by query records err: %v, query: %s, args: %+v", err, query, args) } return records, nil } func (slf *LocationSearch) FindAll() ([]*Location, error) { var ( records = make([]*Location, 0) db = slf.build() ) err := db.Find(&records).Error if err != nil { fmt.Println(err) return records, fmt.Errorf("func FindAll err: %v", err) } return records, nil } // InitDefaultData 初始化数据 func (slf *LocationSearch) InitDefaultData() error { var ( db = slf.Orm.Table(slf.TableName()) total int64 = 0 ) if err := db.Count(&total).Error; err != nil { return err } if total != 0 { return nil } locations := make([]*Location, 0) locations = append(locations, &Location{Name: "供应商位置", Type: 1, JointName: "供应商位置"}) locations = append(locations, &Location{Name: "视图", Type: 2, JointName: "视图"}) locations = append(locations, &Location{Name: "客户位置", Type: 4, JointName: "客户位置"}) locations = append(locations, &Location{Name: "库存损失", Type: 5, JointName: "库存损失"}) locations = append(locations, &Location{Name: "生产", Type: 6, JointName: "生产"}) locations = append(locations, &Location{Name: "中转位置", Type: 7, JointName: "中转位置"}) locations = append(locations, &Location{Name: "报废位置", Type: 8, JointName: "报废位置", IsScrapLocation: true}) locations = append(locations, &Location{Name: "库存盘点", Type: 9, JointName: "库存盘点"}) return slf.CreateBatch(locations) }