|
@@ -197,6 +197,9 @@ func (e *Waybill) GetByWaybillNo(d *dto.WaybillGetByWaybillPdfReq, waybillModel
|
|
|
Scopes(actions.Permission(waybillModel.TableName(), p)).
|
|
|
Where("waybill_no = ?", d.WaybillNo).
|
|
|
Preload("Dept").
|
|
|
+ Preload("CoolerBox").
|
|
|
+ Preload("Delivery").
|
|
|
+ Preload("ReCheck").
|
|
|
First(waybillModel).Error
|
|
|
if err != nil {
|
|
|
e.Log.Errorf("db error: %s", err)
|
|
@@ -405,7 +408,7 @@ func (e *Waybill) Delivery(c *dto.WaybillDeliveryReq, p *actions.DataPermission)
|
|
|
if len(extraData) > 0 {
|
|
|
for _, code := range extraData {
|
|
|
var iceRaft model.IceRaft
|
|
|
- err = tx.Model(&model.IceRaft{}).Where("code = ?", code).First(&iceRaft).Error
|
|
|
+ err = tx.Model(&model.IceRaft{}).Where("code = ?", code).Preload("IceRaftRecord").First(&iceRaft).Error
|
|
|
if err != nil {
|
|
|
e.Log.Errorf("db error: %s", err)
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
@@ -415,6 +418,10 @@ func (e *Waybill) Delivery(c *dto.WaybillDeliveryReq, p *actions.DataPermission)
|
|
|
}
|
|
|
|
|
|
}
|
|
|
+ if iceRaft.IceRaftRecord.Status != model.IceRaftRecordStatusWaitUse {
|
|
|
+ err = errors.New(fmt.Sprintf("冰排【%s】状态为%s,出库保温箱失败!", code, model.GetIceRaftRecordStatus(iceRaft.IceRaftRecord.Status)))
|
|
|
+ return err
|
|
|
+ }
|
|
|
// 获取冰排记录
|
|
|
err = tx.Model(&model.IceRaftRecord{}).
|
|
|
Where("ice_raft_id = ? and status = ?", iceRaft.Id, model.IceRaftRecordStatusWaitUse).
|
|
@@ -557,7 +564,7 @@ func (e *Waybill) Delivery(c *dto.WaybillDeliveryReq, p *actions.DataPermission)
|
|
|
}
|
|
|
waybillModel.SendLog = model.WaybillSendLog{
|
|
|
Phone: waybillModel.ConsigneeAddressPhone,
|
|
|
- Content: "【冷链送药平台】您的订单正在派送中,点击查看详情:" + addr,
|
|
|
+ Content: "【冷链送药追溯平台】您的运单正在派送中,点击查看详情:" + addr,
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -857,7 +864,7 @@ func (e *Waybill) GetDeliveryStats(c *dto.WaybillUserStatsReq, p *actions.DataPe
|
|
|
// 获取用户列表
|
|
|
var userList []model.SysUser
|
|
|
if err := e.Orm.Model(&userList).Scopes(actions.Permission(model.SysUser{}.TableName(), p), WaybillUserStatsUserIdScopes(c.UserIds)).
|
|
|
- Where("status = 2").Find(&userList).Error; err != nil {
|
|
|
+ Where("status = 2").Order("created_at desc").Find(&userList).Error; err != nil {
|
|
|
return nil
|
|
|
}
|
|
|
for _, user := range userList {
|
|
@@ -871,7 +878,7 @@ func (e *Waybill) GetReCheckStats(c *dto.WaybillUserStatsReq, p *actions.DataPer
|
|
|
// 获取用户列表
|
|
|
var userList []model.SysUser
|
|
|
if err := e.Orm.Model(&userList).Scopes(actions.Permission(model.SysUser{}.TableName(), p), WaybillUserStatsUserIdScopes(c.UserIds)).
|
|
|
- Where("status = 2").Find(&userList).Error; err != nil {
|
|
|
+ Where("status = 2").Order("created_at desc").Find(&userList).Error; err != nil {
|
|
|
return nil
|
|
|
}
|
|
|
for _, user := range userList {
|
|
@@ -888,9 +895,9 @@ func WaybillUserStatsUserIdScopes(userIds []int) func(db *gorm.DB) *gorm.DB {
|
|
|
return db
|
|
|
}
|
|
|
if len(userIds) == 1 {
|
|
|
- return db.Where("user_id = ?", userIds[0])
|
|
|
+ return db.Where("id = ?", userIds[0])
|
|
|
}
|
|
|
- return db.Where("user_id in (?)", userIds)
|
|
|
+ return db.Where("id in (?)", userIds)
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -1006,6 +1013,10 @@ func (e *Waybill) GetUserStats(c *dto.WaybillUserStatsReq, p *actions.DataPermis
|
|
|
// 年度数据统计
|
|
|
e.Orm.Model(&data).Select("date_format(order_time,'%Y-%m') date,count(1) as num ").Scopes(actions.Permission(data.TableName(), p), scopes).
|
|
|
Where("order_time between ? and ?", firstDayOfyear, lastDayOfyear).Group("date").Find(&res.Stats)
|
|
|
+ // 获取这个月的所有日期
|
|
|
+ allDates := getAllDatesInYear(c.Date)
|
|
|
+ // 补齐缺失日期的数据
|
|
|
+ res.Stats = completeMissingDates(allDates, res.Stats)
|
|
|
}
|
|
|
|
|
|
if c.Type == "month" {
|
|
@@ -1015,11 +1026,72 @@ func (e *Waybill) GetUserStats(c *dto.WaybillUserStatsReq, p *actions.DataPermis
|
|
|
lastDayOfMonth := time.Date(month.Year(), month.Month()+1, 1, 23, 59, 59, 0, now.Location()).Add(-time.Hour * 24)
|
|
|
e.Orm.Model(&data).Select("date_format(order_time,'%Y-%m-%d') date,count(1) as num ").Scopes(actions.Permission(data.TableName(), p), scopes).
|
|
|
Where("order_time between ? and ?", firstDayOfMonth, lastDayOfMonth).Group("date").Find(&res.Stats)
|
|
|
+ // 获取这个月的所有日期
|
|
|
+ allDates := getAllDatesInMonth(c.Date)
|
|
|
+ // 补齐缺失日期的数据
|
|
|
+ res.Stats = completeMissingDates(allDates, res.Stats)
|
|
|
}
|
|
|
|
|
|
return res
|
|
|
}
|
|
|
|
|
|
+// 获取指定月份的所有日期
|
|
|
+func getAllDatesInMonth(month string) []string {
|
|
|
+ var dates []string
|
|
|
+ layout := "2006-01-02"
|
|
|
+ firstDay, _ := time.Parse(layout, month+"-01")
|
|
|
+ if time.Now().Before(firstDay) {
|
|
|
+ return dates
|
|
|
+ }
|
|
|
+ lastDay := firstDay.AddDate(0, 1, -1)
|
|
|
+ if time.Now().Before(lastDay) {
|
|
|
+ lastDay = time.Now()
|
|
|
+ }
|
|
|
+
|
|
|
+ for d := firstDay; !d.After(lastDay); d = d.AddDate(0, 0, 1) {
|
|
|
+ dates = append(dates, d.Format(layout))
|
|
|
+ }
|
|
|
+
|
|
|
+ return dates
|
|
|
+}
|
|
|
+
|
|
|
+// 获取指定年份的所有日期
|
|
|
+func getAllDatesInYear(year string) []string {
|
|
|
+ var dates []string
|
|
|
+ layout := "2006-01"
|
|
|
+ firstDay, _ := time.Parse(layout, year+"-01")
|
|
|
+ lastDay := firstDay.AddDate(1, 0, -1)
|
|
|
+ if time.Now().Before(firstDay) {
|
|
|
+ return dates
|
|
|
+ }
|
|
|
+ if time.Now().Before(lastDay) {
|
|
|
+ lastDay = time.Now()
|
|
|
+ }
|
|
|
+ for d := firstDay; !d.After(lastDay); d = d.AddDate(0, 1, 0) {
|
|
|
+ dates = append(dates, d.Format(layout))
|
|
|
+ }
|
|
|
+ return dates
|
|
|
+}
|
|
|
+
|
|
|
+// 补齐缺失日期的数据
|
|
|
+func completeMissingDates(allDates []string, stats []dto.WaybillStats) []dto.WaybillStats {
|
|
|
+ dateMap := make(map[string]int64)
|
|
|
+ for _, stat := range stats {
|
|
|
+ dateMap[stat.Date] = stat.Num
|
|
|
+ }
|
|
|
+
|
|
|
+ var completedStats []dto.WaybillStats
|
|
|
+ for _, date := range allDates {
|
|
|
+ num, exists := dateMap[date]
|
|
|
+ if !exists {
|
|
|
+ num = 0
|
|
|
+ }
|
|
|
+ completedStats = append(completedStats, dto.WaybillStats{Date: date, Num: num})
|
|
|
+ }
|
|
|
+
|
|
|
+ return completedStats
|
|
|
+}
|
|
|
+
|
|
|
// 获取运单所有温湿度素具
|
|
|
func (e *Waybill) GetAllData(c *dto.WaybillGetByWaybillNoReq) ([]nats_server.DeviceData_R, []WaybillPDF, error) {
|
|
|
var err error
|