158 lines
4.7 KiB
Go
158 lines
4.7 KiB
Go
|
package models
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"github.com/codinl/go-logger"
|
||
|
orm "go-admin/common/global"
|
||
|
"math/rand"
|
||
|
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
type ErpDispatchTask struct {
|
||
|
Model
|
||
|
Sn string `json:"sn" gorm:"index"`
|
||
|
FromStoreId uint32 `json:"from_store_id" gorm:"index"`
|
||
|
FromStoreName string `json:"from_store_name"`
|
||
|
ToStoreId uint32 `json:"to_store_id" gorm:"index"`
|
||
|
ToStoreName string `json:"to_store_name"`
|
||
|
MakerId uint32 `json:"maker_id" gorm:"index"`
|
||
|
MakerName string `json:"maker_name"`
|
||
|
MakerTime time.Time `json:"maker_time"`
|
||
|
AuditorId uint32 `json:"auditor_id" gorm:"index"`
|
||
|
AuditorName string `json:"auditor_name"`
|
||
|
AuditorTime time.Time `json:"auditor_time"`
|
||
|
State uint32 `json:"state"` // 1-待审核 2-待发货 3-待收货 4-已完成
|
||
|
ExpressNo string `json:"express_no" gorm:"index"`
|
||
|
Remark string `json:"remark"`
|
||
|
TaskCommodities []ErpDispatchTaskCommodity `json:"task_commodities" gorm:"-"`
|
||
|
// erp_dispatch_task
|
||
|
}
|
||
|
|
||
|
type ErpDispatchTaskCommodity struct {
|
||
|
Model
|
||
|
DispatchTaskId uint32 `json:"dispatch_task_id" gorm:"index"`
|
||
|
ErpCategoryId uint32 `json:"erp_category_id" gorm:"index"`
|
||
|
ErpCategoryName string `json:"erp_category_name"`
|
||
|
ErpCommodityId uint32 `json:"erp_commodity_id" gorm:"index"`
|
||
|
ErpCommodityName string `json:"erp_commodity_name"`
|
||
|
IMEIType uint32 `json:"imei_type"` // 1-无串码 2-串码
|
||
|
IMEI string `json:"imei" gorm:"index"`
|
||
|
Count uint32 `json:"count"`
|
||
|
Remark string `json:"remark"`
|
||
|
// erp_dispatch_task_commodity
|
||
|
}
|
||
|
|
||
|
type DispatchTaskListReq struct {
|
||
|
Sn string `json:"sn"`
|
||
|
FromStoreId uint32 `json:"from_store_id"`
|
||
|
ToStoreId uint32 `json:"to_store_id"`
|
||
|
State uint32 `json:"state"`
|
||
|
StartMakerTime string `json:"start_maker_time"`
|
||
|
EndMakerTime string `json:"end_maker_time"`
|
||
|
PageNum int `json:"page_num"`
|
||
|
PageSize int `json:"page_size"`
|
||
|
IsExport uint32 `json:"is_export"` // 1-导出
|
||
|
}
|
||
|
type DispatchTaskListResp struct {
|
||
|
List []ErpDispatchTask `json:"list"`
|
||
|
Total int `json:"total"`
|
||
|
PageNum int `json:"page_num"`
|
||
|
PageSize int `json:"page_size"`
|
||
|
ExportUrl string `json:"export_url"`
|
||
|
}
|
||
|
|
||
|
func (m *DispatchTaskListReq) List() (*DispatchTaskListResp, error) {
|
||
|
resp := &DispatchTaskListResp{
|
||
|
PageNum: m.PageNum,
|
||
|
PageSize: m.PageSize,
|
||
|
}
|
||
|
page := m.PageNum - 1
|
||
|
if page < 0 {
|
||
|
page = 0
|
||
|
}
|
||
|
if m.PageSize == 0 {
|
||
|
m.PageSize = 10
|
||
|
}
|
||
|
qs := orm.Eloquent.Table("erp_dispatch_task")
|
||
|
if m.Sn != "" {
|
||
|
qs = qs.Where("sn=?", m.Sn)
|
||
|
}
|
||
|
if m.FromStoreId != 0 {
|
||
|
qs = qs.Where("from_store_id=?", m.FromStoreId)
|
||
|
}
|
||
|
if m.ToStoreId != 0 {
|
||
|
qs = qs.Where("to_store_id=?", m.ToStoreId)
|
||
|
}
|
||
|
if m.StartMakerTime != "" {
|
||
|
parse, err := time.Parse(DateTimeFormat, m.StartMakerTime)
|
||
|
if err != nil {
|
||
|
logger.Error("StartMakerTime Parse err:", err)
|
||
|
return resp, err
|
||
|
}
|
||
|
qs = qs.Where("auditor_time > ?", parse)
|
||
|
}
|
||
|
if m.EndMakerTime != "" {
|
||
|
parse, err := time.Parse(DateTimeFormat, m.EndMakerTime)
|
||
|
if err != nil {
|
||
|
logger.Error("err:", err)
|
||
|
return resp, err
|
||
|
}
|
||
|
qs = qs.Where("auditor_time < ?", parse)
|
||
|
}
|
||
|
var count int64
|
||
|
err := qs.Count(&count).Error
|
||
|
if err != nil {
|
||
|
logger.Error("count err:", err)
|
||
|
return resp, err
|
||
|
}
|
||
|
resp.Total = int(count)/m.PageSize + 1
|
||
|
var dispatchTasks []ErpDispatchTask
|
||
|
|
||
|
if m.IsExport == 1 {
|
||
|
err = qs.Order("id DESC").Find(&dispatchTasks).Error
|
||
|
if err != nil && err != RecordNotFound {
|
||
|
logger.Error("dailys err:", err)
|
||
|
return resp, err
|
||
|
}
|
||
|
|
||
|
//listExport, err := ErpCommodityListExport(dispatchTasks)
|
||
|
//if err != nil {
|
||
|
// logger.Error("list export err:", err)
|
||
|
//}
|
||
|
//resp.ExportUrl = listExport
|
||
|
} else {
|
||
|
err = qs.Order("id DESC").Offset(page * m.PageSize).Limit(m.PageSize).Find(&dispatchTasks).Error
|
||
|
if err != nil && err != RecordNotFound {
|
||
|
logger.Error("erp commodity list err:", err)
|
||
|
return resp, err
|
||
|
}
|
||
|
resp.List = dispatchTasks
|
||
|
}
|
||
|
|
||
|
return resp, nil
|
||
|
}
|
||
|
|
||
|
func NewErpDispatchTaskSn() string {
|
||
|
nowTime := time.Now()
|
||
|
rand.Seed(nowTime.UnixNano())
|
||
|
max := 1
|
||
|
for {
|
||
|
if max > 5 {
|
||
|
logger.Error("create sn err")
|
||
|
return ""
|
||
|
}
|
||
|
random := rand.Int31n(9999) + 1000
|
||
|
sn := fmt.Sprintf("db%s%d", nowTime.Format("060102"), random)
|
||
|
exist, err := QueryRecordExist(fmt.Sprintf("SELECT * FROM erp_dispatch_task WHERE sn='%s'", sn))
|
||
|
if err != nil {
|
||
|
logger.Error("exist sn err")
|
||
|
}
|
||
|
if !exist {
|
||
|
return sn
|
||
|
}
|
||
|
|
||
|
max++
|
||
|
}
|
||
|
}
|