Browse Source

update:修改运单物流时间、修改运单任务数据接口开发

zoie 5 months ago
parent
commit
11b426b724

+ 7 - 7
app/admin/controller/waybill.go

@@ -1159,7 +1159,7 @@ func (e WaybillController) TemperaturePDF(c *gin.Context) {
 	req := dto.WaybillGetByWaybillPdfReq{}
 	err := e.MakeContext(c).
 		MakeOrm().
-		Bind(&req, binding.Query).
+		Bind(&req, binding.JSON, nil).
 		MakeService(&s.Service).
 		MakeService(&companySvc.Service).
 		Errors
@@ -1180,7 +1180,7 @@ func (e WaybillController) TemperaturePDF(c *gin.Context) {
 		e.Error(500, err, err.Error())
 		return
 	}
-	filename, filePath, err := PDF(s, waybill, company, req.HumidityShow)
+	filename, filePath, err := PDF(s, waybill, company, req.WaybillTaskIds, req.HumidityShow)
 	defer func() {
 		os.Remove(filePath)
 	}()
@@ -1208,7 +1208,7 @@ func (e WaybillController) TemperaturePDFUrl(c *gin.Context) {
 	req := dto.WaybillGetByWaybillPdfReq{}
 	err := e.MakeContext(c).
 		MakeOrm().
-		Bind(&req, binding.Query).
+		Bind(&req, binding.JSON, nil).
 		MakeService(&s.Service).
 		MakeService(&companySvc.Service).
 		Errors
@@ -1229,7 +1229,7 @@ func (e WaybillController) TemperaturePDFUrl(c *gin.Context) {
 		e.Error(500, err, err.Error())
 		return
 	}
-	filename, filePath, err := PDF(s, waybill, company, req.HumidityShow)
+	filename, filePath, err := PDF(s, waybill, company, req.WaybillTaskIds, req.HumidityShow)
 	defer func() {
 		os.Remove(filePath)
 	}()
@@ -1247,9 +1247,9 @@ func (e WaybillController) TemperaturePDFUrl(c *gin.Context) {
 	})
 
 }
-func PDF(s service.Waybill, waybill model.Waybill, company model.SysDept, humidityShow bool) (filename, filePath string, err error) {
+func PDF(s service.Waybill, waybill model.Waybill, company model.SysDept, waybillTaskIds []int, humidityShow bool) (filename, filePath string, err error) {
 
-	DeviceSensor_data, Pdf_data, waybillPDF, err := s.GetTwoDeviceSensorData(&dto.WaybillGetByWaybillNoReq{WaybillNo: waybill.WaybillNo})
+	DeviceSensor_data, Pdf_data, waybillPDF, err := s.GetTwoDeviceSensorData(&dto.WaybillPdfReq{WaybillNo: waybill.WaybillNo, WaybillTaskIds: waybillTaskIds})
 	// 最高温度、最低温度、最高湿度、最低湿度
 	var maxTemp, minTemp, maxHumidity, minHumidity float32
 	// 最高温度时间、最低温度时间、最高湿度时间、最低湿度时间
@@ -1940,7 +1940,7 @@ func (e WaybillController) SendMail(c *gin.Context) {
 		e.Error(500, err, err.Error())
 		return
 	}
-	_, filePath, err := PDF(s, waybill, company, req.HumidityShow)
+	_, filePath, err := PDF(s, waybill, company, req.WaybillTaskIds, req.HumidityShow)
 	defer func() {
 		os.Remove(filePath)
 	}()

+ 37 - 0
app/admin/controller/waybill_logistics.go

@@ -4,9 +4,11 @@ import (
 	"cold-logistics/app/admin/model"
 	"cold-logistics/app/admin/service"
 	"cold-logistics/app/admin/service/dto"
+	"cold-logistics/common/actions"
 	"github.com/gin-gonic/gin"
 	"github.com/gin-gonic/gin/binding"
 	"gogs.baozhida.cn/zoie/OAuth-core/api"
+	"gogs.baozhida.cn/zoie/OAuth-core/pkg/jwtauth/user"
 	_ "gogs.baozhida.cn/zoie/OAuth-core/pkg/response"
 )
 
@@ -50,3 +52,38 @@ func (e WaybillLogisticsController) GetPage(c *gin.Context) {
 	//e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
 	e.PageOK(list, int(count), 0, 0, "查询成功")
 }
+
+// Update 修改运单物流
+// @Summary 修改运单物流
+// @Description 修改运单物流
+// @Tags 运单物流信息
+// @Accept  application/json
+// @Product application/json
+// @Param data body dto.WaybillLogisticsUpdateReq true "body"
+// @Success 200 {string} string	"{"code": 200, "message": "修改成功"}"
+// @Success 200 {string} string	"{"code": -1, "message": "修改失败"}"
+// @Router /api/waybill [put]
+// @Security Bearer
+func (e WaybillLogisticsController) Update(c *gin.Context) {
+	s := service.WaybillLogistics{}
+	req := dto.WaybillLogisticsUpdateReq{}
+	err := e.MakeContext(c).
+		MakeOrm().
+		Bind(&req).
+		MakeService(&s.Service).
+		Errors
+	if err != nil {
+		e.Logger.Error(err)
+		e.Error(500, err, err.Error())
+		return
+	}
+	p := actions.GetPermissionFromContext(c)
+
+	req.SetUpdateBy(user.GetUserId(c))
+	err = s.Update(&req, p)
+	if err != nil {
+		e.Error(500, err, err.Error())
+		return
+	}
+	e.OK(nil, "修改成功")
+}

+ 11 - 11
app/admin/router/waybill.go

@@ -36,16 +36,16 @@ func registerWaybillRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddle
 		r.GET("/customer", cont.GetCustomerPage)   // 客户下单列表
 		r.POST("/customer", cont.CustomerInsert)   // 客户下单
 
-		r.GET("/applet", cont.GetAppletPage)           // app 运单列表
-		r.GET("/applet-count", cont.GetAppletCount)    // app 运单-统计
-		r.POST("/import", cont.Import)                 // 导入运单
-		r.GET("/export-template", cont.ExportTemplate) // 导出运单模板
-		r.GET("/home", cont.Home)                      // 首页统计
-		r.GET("/export", cont.Export)                  // 运单管理-导出
-		r.GET("/customer/export", cont.CustomerExport) // 运单管理-客户导出
-		r.GET("/temperature-pdf", cont.TemperaturePDF) // 导出温湿度pdf
-		r.GET("/waybill-pdf", cont.WaybillPDF)         // 导出运单pdf
-		r.POST("/send-mail", cont.SendMail)            // 发送温湿度邮件
+		r.GET("/applet", cont.GetAppletPage)            // app 运单列表
+		r.GET("/applet-count", cont.GetAppletCount)     // app 运单-统计
+		r.POST("/import", cont.Import)                  // 导入运单
+		r.GET("/export-template", cont.ExportTemplate)  // 导出运单模板
+		r.GET("/home", cont.Home)                       // 首页统计
+		r.GET("/export", cont.Export)                   // 运单管理-导出
+		r.GET("/customer/export", cont.CustomerExport)  // 运单管理-客户导出
+		r.POST("/temperature-pdf", cont.TemperaturePDF) // 导出温湿度pdf
+		r.GET("/waybill-pdf", cont.WaybillPDF)          // 导出运单pdf
+		r.POST("/send-mail", cont.SendMail)             // 发送温湿度邮件
 	}
 }
 
@@ -53,6 +53,6 @@ func registerWaybillRouter2(v1 *gin.RouterGroup) {
 	cont := controller.WaybillController{}
 	r := v1.Group("/waybill")
 	{
-		r.GET("/temperature-pdf-url", cont.TemperaturePDFUrl) // 导出温湿度pdf
+		r.POST("/temperature-pdf-url", cont.TemperaturePDFUrl) // 导出温湿度pdf
 	}
 }

+ 12 - 0
app/admin/router/waybill_logistics.go

@@ -2,11 +2,15 @@ package router
 
 import (
 	"cold-logistics/app/admin/controller"
+	"cold-logistics/common/actions"
 	"github.com/gin-gonic/gin"
+	jwt "gogs.baozhida.cn/zoie/OAuth-core/pkg/jwtauth"
 )
 
 func init() {
 	routerNoCheckRole = append(routerNoCheckRole, registerWaybillLogisticsRouter)
+	routerCheckRole = append(routerCheckRole, registerWaybillLogisticsRouter2)
+
 }
 
 // 物流详情
@@ -18,3 +22,11 @@ func registerWaybillLogisticsRouter(v1 *gin.RouterGroup) {
 		r.GET("", cont.GetPage)
 	}
 }
+
+func registerWaybillLogisticsRouter2(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) {
+	cont := controller.WaybillLogisticsController{}
+	r := v1.Group("/waybill-logistics").Use(authMiddleware.MiddlewareFunc()).Use(actions.PermissionAction())
+	{
+		r.PUT("", cont.Update)
+	}
+}

+ 20 - 5
app/admin/service/dto/waybill.go

@@ -241,14 +241,29 @@ type WaybillGetByWaybillNoReq struct {
 	WaybillNo string `form:"waybillNo" vd:"len($)>0;msg:'运单编号不能为空'"` // 运单编号-必填
 }
 
+type WaybillPdfReq struct {
+	WaybillNo      string `form:"waybillNo" vd:"len($)>0;msg:'运单编号不能为空'"` // 运单编号-必填
+	WaybillTaskIds []int  `form:"waybillTaskIds" vd:""`                   // 运单任务id
+}
 type WaybillGetByWaybillPdfReq struct {
-	WaybillNo    string `form:"waybillNo" vd:"len($)>0;msg:'运单编号不能为空'"` // 运单编号-必填
-	HumidityShow bool   `form:"humidityShow" vd:""`                     // 湿度显示
+	WaybillNo      string `form:"waybillNo" vd:"len($)>0;msg:'运单编号不能为空'"` // 运单编号-必填
+	HumidityShow   bool   `form:"humidityShow" vd:""`                     // 湿度显示
+	WaybillTaskIds []int  `form:"waybillTaskIds" vd:""`                   // 运单任务id
 	//TemplateShow bool   `form:"templateShow" vd:""`                     // 湿度显示
 }
 
 type WaybillSendMailPdfReq struct {
-	WaybillNo    string   `form:"waybillNo" vd:"len($)>0;msg:'运单编号不能为空'"` // 运单编号-必填
-	HumidityShow bool     `form:"humidityShow" vd:""`                     // 湿度显示
-	To           []string `form:"to" vd:""`                               // 收件人
+	WaybillNo      string   `form:"waybillNo" vd:"len($)>0;msg:'运单编号不能为空'"` // 运单编号-必填
+	HumidityShow   bool     `form:"humidityShow" vd:""`                     // 湿度显示
+	WaybillTaskIds []int    `form:"waybillTaskIds" vd:""`                   // 运单任务id
+	To             []string `form:"to" vd:""`                               // 收件人
+}
+
+type WaybillLogisticsUpdateReq struct {
+	WaybillLogisticsList []struct {
+		Id   int         `json:"id" comment:"编码" swaggerignore:"true"` // 编码
+		Time model2.Time `json:"time"  gorm:"size:128"`                // 开始时间
+	} `json:"waybillLogistics"`
+	model2.ControlBy `swaggerignore:"true"`
+	model2.DeptBy    `swaggerignore:"true"`
 }

+ 9 - 19
app/admin/service/waybill.go

@@ -2352,7 +2352,7 @@ func (e *Waybill) GetAllData(c *dto.WaybillGetByWaybillNoReq) ([]nats_server.Dev
 }
 
 // 获取前两个探头数据
-func (e *Waybill) GetTwoDeviceSensorData(c *dto.WaybillGetByWaybillNoReq) ([]nats_server.DeviceData_R, []DeviceDataPdf, []WaybillPDF, error) {
+func (e *Waybill) GetTwoDeviceSensorData(c *dto.WaybillPdfReq) ([]nats_server.DeviceData_R, []DeviceDataPdf, []WaybillPDF, error) {
 	var err error
 	var data model.WaybillTask
 	var waybill model.Waybill
@@ -2383,9 +2383,8 @@ func (e *Waybill) GetTwoDeviceSensorData(c *dto.WaybillGetByWaybillNoReq) ([]nat
 		return dataList, deviceDataPdf, waybillPDF, model.GetCompanyKeyErr
 	}
 
-	err = e.Orm.Model(&data).
+	err = e.Orm.Model(&data).Scopes(WaybillTaskIdsScopes(c.WaybillTaskIds)).
 		Where("waybill_no = ?", c.WaybillNo).
-		//Order("id desc").
 		Preload("Warehouse").Preload("Car").Preload("CoolerBox").
 		Find(&taskList).Error
 	if err != nil {
@@ -2393,15 +2392,6 @@ func (e *Waybill) GetTwoDeviceSensorData(c *dto.WaybillGetByWaybillNoReq) ([]nat
 		return dataList, deviceDataPdf, waybillPDF, global.GetFailedErr
 	}
 
-	// 获取最后一个任务id
-	var lastWaybillTask model.WaybillTask
-	err = e.Orm.Model(&lastWaybillTask).Where("waybill_no = ?", c.WaybillNo).Last(&lastWaybillTask).Error
-	if err != nil {
-		e.Log.Errorf("db error: %s", err)
-		err = errors.New("获取运单信息错误!")
-		return dataList, deviceDataPdf, waybillPDF, err
-	}
-
 	// 创建名称到权重的映射
 	orderMap := make(map[string]int)
 	for i, v := range taskList {
@@ -2601,13 +2591,13 @@ func (e *Waybill) VerifyDataQualified(waybillNo string) (qualified bool, err err
 	}
 
 	// 获取最后一个任务id
-	var lastWaybillTask model.WaybillTask
-	err = e.Orm.Model(&lastWaybillTask).Where("waybill_no = ?", waybillNo).Last(&lastWaybillTask).Error
-	if err != nil {
-		e.Log.Errorf("db error: %s", err)
-		err = errors.New("获取运单信息错误!")
-		return qualified, err
-	}
+	//var lastWaybillTask model.WaybillTask
+	//err = e.Orm.Model(&lastWaybillTask).Where("waybill_no = ?", waybillNo).Last(&lastWaybillTask).Error
+	//if err != nil {
+	//	e.Log.Errorf("db error: %s", err)
+	//	err = errors.New("获取运单信息错误!")
+	//	return qualified, err
+	//}
 
 	// 创建名称到权重的映射
 	orderMap := make(map[string]int)

+ 65 - 0
app/admin/service/waybill_logistics.go

@@ -3,9 +3,13 @@ package service
 import (
 	"cold-logistics/app/admin/model"
 	"cold-logistics/app/admin/service/dto"
+	"cold-logistics/common/actions"
 	cDto "cold-logistics/common/dto"
 	"cold-logistics/common/global"
+	"errors"
+	"fmt"
 	"gogs.baozhida.cn/zoie/OAuth-core/service"
+	"gorm.io/gorm"
 )
 
 type WaybillLogistics struct {
@@ -41,3 +45,64 @@ func (e *WaybillLogistics) GetPage(c *dto.WaybillLogisticsGetPageReq, list *[]mo
 	}
 	return nil
 }
+
+// Update 修改Waybill对象
+func (e *WaybillLogistics) Update(c *dto.WaybillLogisticsUpdateReq, p *actions.DataPermission) error {
+	var err error
+
+	tx := e.Orm.Begin()
+	defer func() {
+		if err != nil {
+			tx.Rollback()
+		} else {
+			tx.Commit()
+		}
+	}()
+
+	for _, logistics := range c.WaybillLogisticsList {
+		var waybillLogisticsModel = model.WaybillLogistics{}
+		// 查询运单是否存在
+		err = e.Orm.Scopes(actions.Permission(waybillLogisticsModel.TableName(), p)).
+			First(&waybillLogisticsModel, logistics.Id).Error
+		if err != nil {
+			e.Log.Errorf("db error: %s", err)
+			if errors.Is(err, gorm.ErrRecordNotFound) {
+				return global.UpdateNotFoundOrNoPermissionErr
+			}
+			return global.UpdateFailedErr
+		}
+
+		waybillLogisticsModel.CreatedAt = logistics.Time
+		waybillLogisticsModel.UpdateBy = c.UpdateBy
+		err = tx.Save(&waybillLogisticsModel).Error
+		if err != nil {
+			e.Log.Errorf("db error: %s", err)
+			return global.UpdateFailedErr
+		}
+
+		if waybillLogisticsModel.Status == model.WaybillStatusReceipt {
+			// 修改运单签收时间
+			var waybillModel = model.Waybill{}
+			// 查询运单是否存在
+			err = tx.Scopes(actions.Permission(waybillModel.TableName(), p)).
+				Where("waybill_no = ?", waybillLogisticsModel.WaybillNo).
+				First(&waybillModel).Error
+			if err != nil {
+				e.Log.Errorf("db error: %s", err)
+				if errors.Is(err, gorm.ErrRecordNotFound) {
+					return errors.New(fmt.Sprintf("运单号%s不存在", waybillLogisticsModel.WaybillNo))
+				}
+				return errors.New(fmt.Sprintf("运单号%s查询失败", waybillLogisticsModel.WaybillNo))
+			}
+			waybillModel.ReceiptTime = logistics.Time
+			err = tx.Save(&waybillModel).Error
+			if err != nil {
+				e.Log.Errorf("db error: %s", err)
+				return errors.New(fmt.Sprintf("保存运单信息失败:%s", err))
+			}
+		}
+
+	}
+
+	return nil
+}

+ 94 - 0
app/admin/service/waybill_task.go

@@ -30,6 +30,16 @@ func WaybillTaskTimeScopes(startTime, endTime string) func(db *gorm.DB) *gorm.DB
 	}
 }
 
+func WaybillTaskIdsScopes(ids []int) func(db *gorm.DB) *gorm.DB {
+	return func(db *gorm.DB) *gorm.DB {
+		if len(ids) == 0 {
+			return db
+		}
+		return db.Where("id in (?)", ids)
+	}
+
+}
+
 // GetPage 获取WaybillTask列表
 func (e *WaybillTask) GetPage(c *dto.WaybillTaskGetPageReq, list *[]model.WaybillTask, count *int64) error {
 	var err error
@@ -495,6 +505,89 @@ func (e *WaybillTask) Update(c *dto.WaybillTaskUpdateReq, p *actions.DataPermiss
 			}
 			return global.UpdateFailedErr
 		}
+		oldStartTime := waybillTaskModel.StartTime
+		oldEndTime := waybillTaskModel.EndTime
+
+		// 修改运单物流时间
+		if waybillTaskModel.StartTime != task.StartTime {
+			var waybillLogistics = model.WaybillLogistics{}
+			// 查询运单是否存在
+			err = e.Orm.Scopes(actions.Permission(waybillLogistics.TableName(), p)).
+				Where("created_at = ?", oldStartTime).First(&waybillLogistics).Error
+			if err != nil {
+				e.Log.Errorf("db error: %s", err)
+				if errors.Is(err, gorm.ErrRecordNotFound) {
+					return global.UpdateNotFoundOrNoPermissionErr
+				}
+				return global.UpdateFailedErr
+			}
+			waybillLogistics.CreatedAt = task.StartTime
+			err = tx.Save(&waybillLogistics).Error
+			if err != nil {
+				e.Log.Errorf("db error: %s", err)
+				return global.UpdateFailedErr
+			}
+		}
+
+		// 修改运单物流时间
+		if waybillTaskModel.EndTime != task.EndTime {
+			var waybillLogistics = model.WaybillLogistics{}
+			// 查询运单是否存在
+			err = e.Orm.Scopes(actions.Permission(waybillLogistics.TableName(), p)).
+				Where("created_at = ? and status != ?", oldEndTime, model.WaybillStatusReceipt).Last(&waybillLogistics).Error
+			if err != nil {
+				e.Log.Errorf("db error: %s", err)
+				if errors.Is(err, gorm.ErrRecordNotFound) {
+					return global.UpdateNotFoundOrNoPermissionErr
+				}
+				return global.UpdateFailedErr
+			}
+			waybillLogistics.CreatedAt = task.EndTime
+			err = tx.Save(&waybillLogistics).Error
+			if err != nil {
+				e.Log.Errorf("db error: %s", err)
+				return global.UpdateFailedErr
+			}
+		}
+
+		// 修改运单物流时间
+		if waybillTaskModel.EndTime != task.EndTime {
+			var waybillLogistics = model.WaybillLogistics{}
+			// 查询运单是否存在
+			err = e.Orm.Scopes(actions.Permission(waybillLogistics.TableName(), p)).
+				Where("created_at = ? and status = ?", oldEndTime, model.WaybillStatusReceipt).Last(&waybillLogistics).Error
+			if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
+				e.Log.Errorf("db error: %s", err)
+				return global.UpdateFailedErr
+			}
+			if waybillLogistics.Id > 0 {
+				waybillLogistics.CreatedAt = task.EndTime
+				err = tx.Save(&waybillLogistics).Error
+				if err != nil {
+					e.Log.Errorf("db error: %s", err)
+					return global.UpdateFailedErr
+				}
+				// 修改运单签收时间
+				var waybillModel = model.Waybill{}
+				// 查询运单是否存在
+				err = tx.Scopes(actions.Permission(waybillModel.TableName(), p)).
+					Where("waybill_no = ?", waybillLogistics.WaybillNo).
+					First(&waybillModel).Error
+				if err != nil {
+					e.Log.Errorf("db error: %s", err)
+					if errors.Is(err, gorm.ErrRecordNotFound) {
+						return errors.New(fmt.Sprintf("运单号%s不存在", waybillLogistics.WaybillNo))
+					}
+					return errors.New(fmt.Sprintf("运单号%s查询失败", waybillLogistics.WaybillNo))
+				}
+				waybillModel.ReceiptTime = task.StartTime
+				err = tx.Save(&waybillModel).Error
+				if err != nil {
+					e.Log.Errorf("db error: %s", err)
+					return errors.New(fmt.Sprintf("保存运单信息失败:%s", err))
+				}
+			}
+		}
 
 		waybillTaskModel.StartTime = task.StartTime
 		waybillTaskModel.EndTime = task.EndTime
@@ -504,6 +597,7 @@ func (e *WaybillTask) Update(c *dto.WaybillTaskUpdateReq, p *actions.DataPermiss
 			e.Log.Errorf("db error: %s", err)
 			return global.UpdateFailedErr
 		}
+
 	}
 
 	return nil