package models
|
|
import (
|
"fmt"
|
"gorm.io/gorm"
|
"srm/constvar"
|
"srm/global"
|
)
|
|
type (
|
// OutsourcingEnterprise 委外企业
|
OutsourcingEnterprise struct {
|
gorm.Model
|
Number string `json:"number" gorm:"unique;type:varchar(255);not null;comment:委外企业编号"` //委外企业编号
|
Name string `json:"name" gorm:"type:varchar(255);not null;comment:委外企业名称"` //委外企业编号名称
|
EnterpriseType string `json:"enterpriseType" gorm:"type:varchar(255);not null;comment:企业类型"` //企业类型
|
Contact string `json:"contact" gorm:"type:varchar(255);not null;comment:联系人"` //联系人
|
Tel string `json:"tel" gorm:"type:varchar(255);not null;comment:联系方式"` //联系方式
|
Address string `json:"address" gorm:"type:varchar(255);not null;comment:地址"` //地址
|
CreditGrade string `json:"creditGrade" gorm:"type:varchar(255);not null;comment:信用等级"` //信用等级
|
SupplyCapacity string `json:"supplyCapacity" gorm:"type:varchar(255);not null;comment:供货能力"` //供货能力
|
OrganizationCode string `json:"organizationCode" gorm:"type:varchar(255);not null;comment:组织机构代码"` //组织机构代码
|
SupplyRange string `json:"supplyRange" gorm:"type:varchar(255);not null;comment:供货范围"` //供货范围
|
Status constvar.RecordStatus `json:"status" gorm:"type:tinyint(1);not null;default:0;comment:状态 0 新建 1 启用 2停用"` //状态 0 新建 1 启用 2停用
|
}
|
|
OutsourcingEnterpriseSearch struct {
|
OutsourcingEnterprise
|
Preload bool
|
Order string
|
PageNum int
|
PageSize int
|
Orm *gorm.DB
|
Keyword string
|
}
|
)
|
|
func (slf OutsourcingEnterprise) TableName() string {
|
return "outsourcing_enterprise"
|
}
|
|
func NewOutsourcingEnterpriseSearch() *OutsourcingEnterpriseSearch {
|
return &OutsourcingEnterpriseSearch{Orm: global.GVA_DB}
|
}
|
|
func (slf *OutsourcingEnterpriseSearch) SetOrm(tx *gorm.DB) *OutsourcingEnterpriseSearch {
|
slf.Orm = tx
|
return slf
|
}
|
|
func (slf *OutsourcingEnterpriseSearch) SetPage(page, size int) *OutsourcingEnterpriseSearch {
|
slf.PageNum, slf.PageSize = page, size
|
return slf
|
}
|
|
func (slf *OutsourcingEnterpriseSearch) SetOrder(order string) *OutsourcingEnterpriseSearch {
|
slf.Order = order
|
return slf
|
}
|
|
func (slf *OutsourcingEnterpriseSearch) SetID(id uint) *OutsourcingEnterpriseSearch {
|
slf.ID = id
|
return slf
|
}
|
|
func (slf *OutsourcingEnterpriseSearch) SetStatus(status constvar.RecordStatus) *OutsourcingEnterpriseSearch {
|
slf.Status = status
|
return slf
|
}
|
|
func (slf *OutsourcingEnterpriseSearch) SetNumber(number string) *OutsourcingEnterpriseSearch {
|
slf.Number = number
|
return slf
|
}
|
|
func (slf *OutsourcingEnterpriseSearch) SetKeyword(keyword string) *OutsourcingEnterpriseSearch {
|
slf.Keyword = keyword
|
return slf
|
}
|
|
func (slf *OutsourcingEnterpriseSearch) SetPreload(preload bool) *OutsourcingEnterpriseSearch {
|
slf.Preload = preload
|
return slf
|
}
|
|
func (slf *OutsourcingEnterpriseSearch) build() *gorm.DB {
|
var db = slf.Orm.Table(slf.TableName())
|
|
if slf.ID != 0 {
|
db = db.Where("id = ?", slf.ID)
|
}
|
|
if slf.Number != "" {
|
db = db.Where("number = ?", slf.Number)
|
}
|
|
if slf.Order != "" {
|
db = db.Order(slf.Order)
|
}
|
|
if slf.Keyword != "" {
|
keywordFmt := fmt.Sprintf("%%%s%%", slf.Keyword)
|
db = db.Where("name like ? or number like ? or enterprise_type like ?", keywordFmt, keywordFmt, keywordFmt)
|
}
|
|
if slf.Status != 0 {
|
db = db.Where("status = ?", slf.Status)
|
}
|
|
return db
|
}
|
|
// Create 单条插入
|
func (slf *OutsourcingEnterpriseSearch) Create(record *OutsourcingEnterprise) error {
|
var db = slf.build()
|
|
if err := db.Create(record).Error; err != nil {
|
return fmt.Errorf("create err: %v, record: %+v", err, record)
|
}
|
|
return nil
|
}
|
|
// CreateBatch 批量插入
|
func (slf *OutsourcingEnterpriseSearch) CreateBatch(records []*OutsourcingEnterprise) 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 *OutsourcingEnterpriseSearch) Save(record *OutsourcingEnterprise) error {
|
var db = slf.build()
|
|
if err := db.Omit("CreatedAt").Save(record).Error; err != nil {
|
return fmt.Errorf("save err: %v, record: %+v", err, record)
|
}
|
|
return nil
|
}
|
|
func (slf *OutsourcingEnterpriseSearch) 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 *OutsourcingEnterpriseSearch) 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 *OutsourcingEnterpriseSearch) Delete() error {
|
var db = slf.build()
|
|
if err := db.Unscoped().Delete(&OutsourcingEnterprise{}).Error; err != nil {
|
return err
|
}
|
|
return nil
|
}
|
|
func (slf *OutsourcingEnterpriseSearch) First() (*OutsourcingEnterprise, error) {
|
var (
|
record = new(OutsourcingEnterprise)
|
db = slf.build()
|
)
|
|
if err := db.First(record).Error; err != nil {
|
return record, err
|
}
|
|
return record, nil
|
}
|
|
func (slf *OutsourcingEnterpriseSearch) Find() ([]*OutsourcingEnterprise, int64, error) {
|
var (
|
records = make([]*OutsourcingEnterprise, 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 *OutsourcingEnterpriseSearch) FindNotTotal() ([]*OutsourcingEnterprise, error) {
|
var (
|
records = make([]*OutsourcingEnterprise, 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 *OutsourcingEnterpriseSearch) FindByQuery(query string, args []interface{}) ([]*OutsourcingEnterprise, int64, error) {
|
var (
|
records = make([]*OutsourcingEnterprise, 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 *OutsourcingEnterpriseSearch) FindByQueryNotTotal(query string, args []interface{}) ([]*OutsourcingEnterprise, error) {
|
var (
|
records = make([]*OutsourcingEnterprise, 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
|
}
|
|
type CountGroupByStatus struct {
|
Status int
|
Total int64
|
}
|
|
func (slf *OutsourcingEnterpriseSearch) CountGroupByStatus() ([]CountGroupByStatus, error) {
|
var (
|
records = make([]CountGroupByStatus, 0)
|
db = slf.build()
|
)
|
if err := db.Select("count(status) as total, status").Group("status").Find(&records).Error; err != nil {
|
return nil, fmt.Errorf("CountGroupByStatus err: %v", err)
|
}
|
return records, nil
|
}
|