Browse Source

update:优化导出温湿度pdf

zoie 5 months ago
parent
commit
0969b699b0

+ 57 - 0
app/admin/controller/waybill.go

@@ -6,6 +6,7 @@ import (
 	"cold-logistics/app/admin/service"
 	"cold-logistics/app/admin/service/dto"
 	"cold-logistics/common/actions"
+	"cold-logistics/common/file_store"
 	"cold-logistics/common/lib"
 	"cold-logistics/common/nats/nats_server"
 	"cold-logistics/conf"
@@ -1190,6 +1191,62 @@ func (e WaybillController) TemperaturePDF(c *gin.Context) {
 
 }
 
+// TemperaturePDFUrl 导出温度记录pdf链接
+// @Summary  导出温度记录pdf链接
+// @Description  导出温度记录pdf链接
+// @Tags 运单
+// @Accept  application/json
+// @Product application/json
+// @Param data body dto.WaybillGetByWaybillPdfReq true "data"
+// @Success 200 {string} string	"{"code": 200, "message": "添加成功"}"
+// @Success 200 {string} string	"{"code": -1, "message": "添加失败"}"
+// @Router /api/waybill/temperature-pdf [post]
+// @Security Bearer
+func (e WaybillController) TemperaturePDFUrl(c *gin.Context) {
+	s := service.Waybill{}
+	companySvc := service.Company{}
+	req := dto.WaybillGetByWaybillPdfReq{}
+	err := e.MakeContext(c).
+		MakeOrm().
+		Bind(&req, binding.Query).
+		MakeService(&s.Service).
+		MakeService(&companySvc.Service).
+		Errors
+	if err != nil {
+		e.Logger.Error(err)
+		e.Error(500, err, err.Error())
+		return
+	}
+	var waybill model.Waybill
+	err = s.GetByWaybillNo(&req, &waybill, nil)
+	if err != nil {
+		e.Error(500, err, err.Error())
+		return
+	}
+	var company model.SysDept
+	err = companySvc.Get(&dto.CompanyGetReq{Id: waybill.DeptId}, &company)
+	if err != nil {
+		e.Error(500, err, err.Error())
+		return
+	}
+	filename, filePath, err := PDF(s, waybill, company, req.HumidityShow)
+	defer func() {
+		os.Remove(filePath)
+	}()
+
+	err = file_store.QiniuFileStore.UpLoad(filename, filePath)
+	if err != nil {
+		c.JSON(400, gin.H{"error": "文件上传失败"})
+		return
+	}
+
+	e.Custom(gin.H{
+		"code": 200,
+		"data": conf.ExtConfig.Qiniu.Endpoint + filename,
+		"msg":  "success",
+	})
+
+}
 func PDF(s service.Waybill, waybill model.Waybill, company model.SysDept, humidityShow bool) (filename, filePath string, err error) {
 
 	DeviceSensor_data, Pdf_data, waybillPDF, err := s.GetTwoDeviceSensorData(&dto.WaybillGetByWaybillNoReq{WaybillNo: waybill.WaybillNo})

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

@@ -4,11 +4,13 @@ import (
 	"cold-logistics/app/admin/model"
 	"cold-logistics/app/admin/service"
 	"cold-logistics/app/admin/service/dto"
+	"cold-logistics/common/actions"
 	"cold-logistics/common/nats/nats_server"
 	"errors"
 	"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"
 )
 
@@ -216,3 +218,38 @@ func (e WaybillTaskController) GetLocus(c *gin.Context) {
 	}
 	e.OK(list, "获取轨迹信息成功")
 }
+
+// Update 修改运单任务
+// @Summary 修改运单任务
+// @Description 修改运单任务
+// @Tags 运单任务
+// @Accept  application/json
+// @Product application/json
+// @Param data body dto.WaybillTaskUpdateReq true "body"
+// @Success 200 {string} string	"{"code": 200, "message": "修改成功"}"
+// @Success 200 {string} string	"{"code": -1, "message": "修改失败"}"
+// @Router /api/waybill [put]
+// @Security Bearer
+func (e WaybillTaskController) Update(c *gin.Context) {
+	s := service.WaybillTask{}
+	req := dto.WaybillTaskUpdateReq{}
+	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, "修改成功")
+}

+ 1 - 1
app/admin/model/sys_user.go

@@ -91,7 +91,7 @@ func GetUserCacheKey(id int) string {
 type SysUserOmit struct {
 	Id       int    `json:"id,omitempty"`       // 主键编码
 	NickName string `json:"nickName,omitempty"` // 昵称
-	Phone    string `json:"phone,omitempty"`    // 昵称
+	Phone    string `json:"phone"`              // 电话
 }
 
 func (SysUserOmit) TableName() string {

+ 2 - 2
app/admin/model/waybill_task.go

@@ -14,8 +14,8 @@ type WaybillTask struct {
 	CoolerBoxId      int                          `json:"coolerBoxId"  gorm:"size:128"` // 保温箱id
 	UserId           int                          `json:"userId"  gorm:"size:128"`      // 司机/仓管id
 	Sn               string                       `json:"sn" gorm:"size:128"`           // sn
-	StartTime        model2.Time                  `json:"startTime"  gorm:"size:128"`   // 签收时间
-	EndTime          model2.Time                  `json:"endTime"  gorm:"size:128"`     // 签收时间
+	StartTime        model2.Time                  `json:"startTime"  gorm:"size:128"`   // 开始时间
+	EndTime          model2.Time                  `json:"endTime"  gorm:"size:128"`     // 结束时间
 	Warehouse        WarehouseOmit                `json:"warehouse"`
 	Car              CarOmit                      `json:"car"`
 	CoolerBox        CoolerBoxOmit                `json:"coolerBox"`

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

@@ -9,6 +9,8 @@ import (
 
 func init() {
 	routerCheckRole = append(routerCheckRole, registerWaybillRouter)
+	routerNoCheckRole = append(routerNoCheckRole, registerWaybillRouter2)
+
 }
 
 // 派费管理
@@ -18,7 +20,7 @@ func registerWaybillRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddle
 	r := v1.Group("/waybill").Use(authMiddleware.MiddlewareFunc()).Use(actions.PermissionAction())
 	{
 		r.GET("", cont.GetPage)
-		r.GET("/:id", cont.Get)
+		r.GET("/:waybillNo", cont.Get)
 		r.POST("", cont.Insert)
 		r.POST("/applet", cont.AppletInsert)
 		r.POST("/delivery", cont.Delivery)
@@ -46,3 +48,11 @@ func registerWaybillRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddle
 		r.POST("/send-mail", cont.SendMail)            // 发送温湿度邮件
 	}
 }
+
+func registerWaybillRouter2(v1 *gin.RouterGroup) {
+	cont := controller.WaybillController{}
+	r := v1.Group("/waybill")
+	{
+		r.GET("/temperature-pdf-url", cont.TemperaturePDFUrl) // 导出温湿度pdf
+	}
+}

+ 11 - 0
app/admin/router/waybill_task.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, registerWaybillTaskRouter)
+	routerCheckRole = append(routerCheckRole, registerWaybillTaskRouter2)
+
 }
 
 // 物流详情
@@ -22,3 +26,10 @@ func registerWaybillTaskRouter(v1 *gin.RouterGroup) {
 		r.POST("/newest-locus", cont.GetNewestData)
 	}
 }
+func registerWaybillTaskRouter2(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) {
+	cont := controller.WaybillTaskController{}
+	r := v1.Group("/waybill-task").Use(authMiddleware.MiddlewareFunc()).Use(actions.PermissionAction())
+	{
+		r.PUT("", cont.Update)
+	}
+}

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

@@ -160,11 +160,7 @@ func (s *WaybillDeliveryReq) GetId() interface{} {
 }
 
 type WaybillGetReq struct {
-	Id int `uri:"id"`
-}
-
-func (s *WaybillGetReq) GetId() interface{} {
-	return s.Id
+	WaybillNo string `uri:"waybillNo"`
 }
 
 type WaybillDeleteReq struct {

+ 15 - 2
app/admin/service/dto/waybill_task.go

@@ -1,6 +1,9 @@
 package dto
 
-import "cold-logistics/common/dto"
+import (
+	"cold-logistics/common/dto"
+	model2 "cold-logistics/common/model"
+)
 
 // 运单
 
@@ -11,7 +14,7 @@ type WaybillTaskGetPageReq struct {
 }
 
 type WaybillTaskOrder struct {
-	CreatedAtOrder string `search:"type:order;column:created_at;table:waybill_task" form:"createdAtOrder" default:"asc"`
+	IDOrder string `search:"type:order;column:id;table:waybill_task" form:"id" default:"asc"`
 }
 
 func (m *WaybillTaskGetPageReq) GetNeedSearch() interface{} {
@@ -44,3 +47,13 @@ func (m *WaybillGetLocusReq) GetNeedSearch() interface{} {
 type WaybillTaskGetNewestDataPageReq struct {
 	WaybillNo string `form:"waybillNo" vd:"len($)>0;msg:'运单编号不能为空'"` // 运单编号-必填
 }
+
+type WaybillTaskUpdateReq struct {
+	WaybillTaskList []struct {
+		Id        int         `json:"id" comment:"编码" swaggerignore:"true"` // 编码
+		StartTime model2.Time `json:"startTime"  gorm:"size:128"`           // 开始时间
+		EndTime   model2.Time `json:"endTime"  gorm:"size:128"`             // 结束时间
+	} `json:"waybillTaskList"`
+	model2.ControlBy `swaggerignore:"true"`
+	model2.DeptBy    `swaggerignore:"true"`
+}

+ 24 - 5
app/admin/service/waybill.go

@@ -194,8 +194,11 @@ func (e *Waybill) GetCustomerPage(c *dto.WaybillGetCustomerPageReq, list *[]mode
 func (e *Waybill) Get(d *dto.WaybillGetReq, waybillModel *model.Waybill, p *actions.DataPermission) error {
 	err := e.Orm.
 		Scopes(actions.Permission(waybillModel.TableName(), p)).
-		Preload("User").
-		First(waybillModel, d.GetId()).Error
+		Where("waybill_no = ?", d.WaybillNo).
+		Preload("PrintUser").
+		Preload("CoolerBox").
+		Preload("Dept").
+		First(waybillModel).Error
 	if err != nil {
 		e.Log.Errorf("db error: %s", err)
 		if errors.Is(err, gorm.ErrRecordNotFound) {
@@ -1873,7 +1876,6 @@ func (e *Waybill) StopRecord(c *dto.WaybillStopRecordReq, p *actions.DataPermiss
 		return err
 	}
 
-	// 保温箱配送 不需要中转
 	if waybillModel.Status == model.WaybillStatusVanning {
 		// 查询保温箱信息
 		var coolerBox = model.CoolerBox{}
@@ -1908,6 +1910,9 @@ func (e *Waybill) StopRecord(c *dto.WaybillStopRecordReq, p *actions.DataPermiss
 			e.Log.Errorf("db error: %s", err)
 			return errors.New(fmt.Sprintf("查询运单任务信息失败:%s", err))
 		}
+		if task.StartTime.Local().After(c.StartTime.Local()) {
+			return errors.New(fmt.Sprintf("停止时间不能早于任务开始时间%s", task.StartTime.String()))
+		}
 		// 未出箱 停止记录
 		if time.Time(task.EndTime).IsZero() {
 			task.EndTime = c.StartTime
@@ -1978,6 +1983,9 @@ func (e *Waybill) StopRecord(c *dto.WaybillStopRecordReq, p *actions.DataPermiss
 			e.Log.Errorf("db error: %s", err)
 			return errors.New(fmt.Sprintf("查询运单任务信息失败:%s", err))
 		}
+		if task.StartTime.Local().After(c.StartTime.Local()) {
+			return errors.New(fmt.Sprintf("停止时间不能早于任务开始时间%s", task.StartTime.String()))
+		}
 		// 未下车 停止记录
 		if time.Time(task.EndTime).IsZero() {
 			task.EndTime = c.StartTime
@@ -2047,6 +2055,9 @@ func (e *Waybill) StopRecord(c *dto.WaybillStopRecordReq, p *actions.DataPermiss
 			e.Log.Errorf("db error: %s", err)
 			return errors.New(fmt.Sprintf("查询运单任务信息失败:%s", err))
 		}
+		if task.StartTime.Local().After(c.StartTime.Local()) {
+			return errors.New(fmt.Sprintf("停止时间不能早于任务开始时间%s", task.StartTime.String()))
+		}
 		// 未出库 停止记录
 		if time.Time(task.EndTime).IsZero() {
 			task.EndTime = c.StartTime
@@ -2357,7 +2368,10 @@ func (e *Waybill) GetTwoDeviceSensorData(c *dto.WaybillGetByWaybillNoReq) ([]nat
 	}
 
 	// 未签收,不返回数据
-	if waybill.Status != model.WaybillStatusReceipt {
+	if waybill.Status != model.WaybillStatusReceipt &&
+		waybill.Status != model.WaybillStatusTruckOut &&
+		waybill.Status != model.WaybillStatusStorageOut &&
+		waybill.Status != model.WaybillStatusVanningOut {
 		return dataList, deviceDataPdf, waybillPDF, nil
 	}
 
@@ -2523,7 +2537,12 @@ func DeviceSensorDataListToDeviceDataPdfList(T_name, T_sn string, list []nats_se
 		key := item.T_time
 		if _, ok := resultMap[key]; !ok {
 			pdf.T_time = item.T_time
-			pdf.T_name = T_name
+			T_name_s := []rune(T_name)
+			if len(T_name_s) > 8 {
+				pdf.T_name = string(T_name_s[:8]) + "..."
+			} else {
+				pdf.T_name = T_name
+			}
 			pdf.T_sn = T_sn
 			resultMap[key] = pdf
 		}

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

@@ -3,6 +3,7 @@ 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"
 	model2 "cold-logistics/common/model"
@@ -135,6 +136,9 @@ func (e *WaybillTask) GetDataPage(c *dto.WaybillTaskGetDataPageReq) (list []nats
 		T_snid += fmt.Sprintf("%s,%d|", task.Sn, id)
 	}
 	list, count, err = nats_server.Cold_ReadDeviceDataListBy_T_snid(T_snid, c.StartTime, c.EndTime, 0, 9999)
+	sort.Slice(list, func(i, j int) bool {
+		return list[i].T_time < list[j].T_time
+	})
 	firstMap := map[int]nats_server.DeviceData_R{}
 	lastMap := map[int]nats_server.DeviceData_R{}
 	if count > 0 {
@@ -277,6 +281,9 @@ func (e *WaybillTask) GetPrintDataPage(c *dto.WaybillTaskGetDataPageReq) (list [
 		T_snid += fmt.Sprintf("%s,%d|", task.Sn, id)
 	}
 	list, count, err = nats_server.Cold_ReadDeviceDataListBy_T_snid(T_snid, c.StartTime, c.EndTime, 0, 9999)
+	sort.Slice(list, func(i, j int) bool {
+		return list[i].T_time < list[j].T_time
+	})
 	firstMap := map[int]nats_server.DeviceData_R{}
 	lastMap := map[int]nats_server.DeviceData_R{}
 	if count > 0 {
@@ -462,3 +469,42 @@ func (e *WaybillTask) GetLocus(c *dto.WaybillGetLocusReq) ([]nats_server.DeviceD
 	}
 	return locusList, nil
 }
+
+// Update 修改Waybill对象
+func (e *WaybillTask) Update(c *dto.WaybillTaskUpdateReq, p *actions.DataPermission) error {
+	var err error
+
+	tx := e.Orm.Begin()
+	defer func() {
+		if err != nil {
+			tx.Rollback()
+		} else {
+			tx.Commit()
+		}
+	}()
+
+	for _, task := range c.WaybillTaskList {
+		var waybillTaskModel = model.WaybillTask{}
+		// 查询运单是否存在
+		err = e.Orm.Scopes(actions.Permission(waybillTaskModel.TableName(), p)).
+			First(&waybillTaskModel, task.Id).Error
+		if err != nil {
+			e.Log.Errorf("db error: %s", err)
+			if errors.Is(err, gorm.ErrRecordNotFound) {
+				return global.UpdateNotFoundOrNoPermissionErr
+			}
+			return global.UpdateFailedErr
+		}
+
+		waybillTaskModel.StartTime = task.StartTime
+		waybillTaskModel.EndTime = task.EndTime
+		waybillTaskModel.UpdateBy = c.UpdateBy
+		err = tx.Save(&waybillTaskModel).Error
+		if err != nil {
+			e.Log.Errorf("db error: %s", err)
+			return global.UpdateFailedErr
+		}
+	}
+
+	return nil
+}