zhangqian
2024-06-06 6b2c4936814854f658b501e87cdcca454937a786
controllers/report_forms_controller.go
@@ -1,7 +1,6 @@
package controllers
import (
   "fmt"
   "github.com/gin-gonic/gin"
   "github.com/shopspring/decimal"
   "net/url"
@@ -12,7 +11,6 @@
   "wms/models"
   "wms/pkg/logx"
   "wms/request"
   "wms/response"
   "wms/service"
   "wms/task"
)
@@ -102,10 +100,11 @@
// GetHistory
// @Tags      报表
// @Summary   获取历史信息
// @Summary   获取出入库明细
// @Produce   application/json
// @Param     Authorization   header string true "token"
// @Param     object  body  request.GetInventoryHistory true  "查询参数"
// @Success   200 {object} util.ResponseList{data=[]response.InventoryHistory}   "成功"
// @Success   200 {object} util.ResponseList{data=[]models.MoveHistory}   "成功"
// @Router    /api-wms/v1/forms/getHistory [post]
func (slf ReportFormsController) GetHistory(c *gin.Context) {
   slf.GetHistoryNew(c)
@@ -195,53 +194,58 @@
      util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
      return
   }
   //获取操作详情
   detailsSearch := models.NewMoveHistorySearch()
   var (
      result []*response.InventoryHistory
      total  uint64
      ids    []int
      err    error
   )
   if params.KeyWord != "" {
      ids, total, err = service.SearchHistoryReport(params.KeyWord, params.BaseOperationType, params.Page, params.PageSize)
      if err != nil {
         util.ResponseFormat(c, code.InternalError, err.Error())
         return
      }
      if len(ids) == 0 {
         util.ResponseFormatList(c, code.Success, result, 0)
         return
      }
   historyFormsService := service.NewHistoryFormsService()
   result, err := historyFormsService.Query(params)
   if err != nil {
      util.ResponseFormat(c, code.InternalError, "内部错误")
      return
   }
   detailsSearch.Orm = detailsSearch.Orm.Model(&models.MoveHistory{}).
      Select("number, updated_at as date, product_name as product_name, from_location_id, operation_id,to_location_id, amount, " +
         "unit, operator as contacted_name, base_operation_type, weight, product_id, from_location, to_location, operation_type_name, weight").Order("id desc")
   if len(ids) > 0 {
      detailsSearch.Orm = detailsSearch.Orm.Where("id in ?", ids)
   }
   if params.BaseOperationType != 0 {
      detailsSearch.Orm = detailsSearch.Orm.Where("base_operation_type = ?", params.BaseOperationType)
   }
   var t int64
   err = detailsSearch.Orm.Count(&t).Error
   total, err := historyFormsService.Count(params)
   if err != nil {
      util.ResponseFormat(c, code.RequestParamError, fmt.Errorf("查询总条数失败: %v", err))
      util.ResponseFormat(c, code.InternalError, "内部错误")
      return
   }
   total = uint64(t)
   if params.Page*params.PageSize > 0 {
      detailsSearch.Orm = detailsSearch.Orm.Offset((params.Page - 1) * params.PageSize).Limit(params.PageSize)
   }
   err = detailsSearch.Orm.Find(&result).Error
   if err != nil {
      util.ResponseFormat(c, code.RequestParamError, fmt.Errorf("查询操作明细失败: %v", err))
      return
   }
   util.ResponseFormatList(c, code.Success, result, int(total))
}
// DownloadHistory
// @Tags      报表
// @Summary   下载出入库明细报表
// @Produce   application/json
// @Param     Authorization   header string true "token"
// @Param     object  body  request.GetInventoryHistory true  "查询参数"
// @Success   200 {object} util.ResponseList{data=[]models.MoveHistory}   "成功"
// @Router    /api-wms/v1/forms/downloadHistory [post]
func (slf ReportFormsController) DownloadHistory(c *gin.Context) {
   var params request.GetInventoryHistory
   if err := c.BindJSON(&params); err != nil {
      util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
      return
   }
   historyFormsService := service.NewHistoryFormsService()
   list, err := historyFormsService.FetchAll(params)
   if err != nil {
      logx.Errorf("DownloadHistory FetchAll err:%v", err)
      util.ResponseFormat(c, code.InternalError, "查询失败")
      return
   }
   filename, err := historyFormsService.Export(list, params)
   if err != nil {
      logx.Errorf("DownloadHistory Export err:%v", err)
      util.ResponseFormat(c, code.InternalError, "导出数据到文件失败")
      return
   }
   fileContentDisposition := "attachment;filename=\"" + url.QueryEscape(filename) + "\""
   c.Header("Content-Type", "application/xlsx")
   c.Header("Content-Disposition", fileContentDisposition)
   c.File(filename)
   defer os.Remove(filename)
}
// GetLocationForms
// @Tags      报表
// @Summary   获取位置报表