zhangqian
2024-07-01 73b6baf6af3d88cdcb0e2df7932a9bd96b0b85c5
月度统计出入库按类型汇总报表定时任务和手动跑任务接口
1个文件已添加
3个文件已修改
588 ■■■■■ 已修改文件
docs/docs.go 188 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
docs/swagger.json 188 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
docs/swagger.yaml 116 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
task/warehouse_month_stats.go 96 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
docs/docs.go
@@ -773,6 +773,58 @@
                }
            }
        },
        "/api-wms/v1/forms/doWarehouseMonthStats": {
            "post": {
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "报表"
                ],
                "summary": "手动跑月度统计库存报表",
                "parameters": [
                    {
                        "description": "查询参数",
                        "name": "object",
                        "in": "body",
                        "required": true,
                        "schema": {
                            "$ref": "#/definitions/request.DoWarehouseMonthStats"
                        }
                    },
                    {
                        "type": "string",
                        "description": "token",
                        "name": "Authorization",
                        "in": "header",
                        "required": true
                    }
                ],
                "responses": {
                    "200": {
                        "description": "成功",
                        "schema": {
                            "allOf": [
                                {
                                    "$ref": "#/definitions/util.ResponseList"
                                },
                                {
                                    "type": "object",
                                    "properties": {
                                        "data": {
                                            "type": "array",
                                            "items": {
                                                "$ref": "#/definitions/models.MonthStats"
                                            }
                                        }
                                    }
                                }
                            ]
                        }
                    }
                }
            }
        },
        "/api-wms/v1/forms/downloadHistory": {
            "post": {
                "produces": [
@@ -1146,6 +1198,58 @@
                    "报表"
                ],
                "summary": "月度统计库存报表",
                "parameters": [
                    {
                        "description": "查询参数",
                        "name": "object",
                        "in": "body",
                        "required": true,
                        "schema": {
                            "$ref": "#/definitions/request.GetMonthStats"
                        }
                    },
                    {
                        "type": "string",
                        "description": "token",
                        "name": "Authorization",
                        "in": "header",
                        "required": true
                    }
                ],
                "responses": {
                    "200": {
                        "description": "成功",
                        "schema": {
                            "allOf": [
                                {
                                    "$ref": "#/definitions/util.ResponseList"
                                },
                                {
                                    "type": "object",
                                    "properties": {
                                        "data": {
                                            "type": "array",
                                            "items": {
                                                "$ref": "#/definitions/models.MonthStats"
                                            }
                                        }
                                    }
                                }
                            ]
                        }
                    }
                }
            }
        },
        "/api-wms/v1/forms/warehouseMonthStats": {
            "post": {
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "报表"
                ],
                "summary": "仓库月度统计库存报表",
                "parameters": [
                    {
                        "description": "查询参数",
@@ -1946,7 +2050,22 @@
                    "200": {
                        "description": "成功",
                        "schema": {
                            "$ref": "#/definitions/util.Response"
                            "allOf": [
                                {
                                    "$ref": "#/definitions/util.ResponseList"
                                },
                                {
                                    "type": "object",
                                    "properties": {
                                        "data": {
                                            "type": "array",
                                            "items": {
                                                "$ref": "#/definitions/models.Operation"
                                            }
                                        }
                                    }
                                }
                            ]
                        }
                    }
                }
@@ -2183,9 +2302,9 @@
                        "in": "query"
                    },
                    {
                        "type": "integer",
                        "type": "string",
                        "description": "仓库id",
                        "name": "warehouseId",
                        "name": "warehouseIds",
                        "in": "query"
                    }
                ],
@@ -3794,6 +3913,21 @@
                "BaseOperationTypeAdjust"
            ]
        },
        "constvar.BoolType": {
            "type": "integer",
            "enum": [
                1,
                2
            ],
            "x-enum-comments": {
                "BoolTypeFalse": "false",
                "BoolTypeTrue": "true"
            },
            "x-enum-varnames": [
                "BoolTypeTrue",
                "BoolTypeFalse"
            ]
        },
        "constvar.CostingMethod": {
            "type": "integer",
            "enum": [
@@ -3965,17 +4099,20 @@
            "enum": [
                1,
                2,
                3
                3,
                4
            ],
            "x-enum-comments": {
                "StockoutType": "出库类型",
                "StorageType": "入库类型",
                "TakeStock": "盘点类型",
                "TransferType": "调拨类型"
            },
            "x-enum-varnames": [
                "TransferType",
                "StorageType",
                "StockoutType"
                "StockoutType",
                "TransferType",
                "TakeStock"
            ]
        },
        "constvar.OperationSource": {
@@ -5094,6 +5231,9 @@
                "updateTime": {
                    "type": "string"
                },
                "warehouse": {
                    "$ref": "#/definitions/models.Warehouse"
                },
                "warehouseId": {
                    "type": "integer"
                },
@@ -5125,6 +5265,10 @@
                    "type": "number"
                },
                "createTime": {
                    "type": "string"
                },
                "dealerType": {
                    "description": "出入库类型",
                    "type": "string"
                },
                "fromLocation": {
@@ -5418,6 +5562,14 @@
                    "description": "仓库名称",
                    "type": "string"
                },
                "openMonthStats": {
                    "description": "是否开启月度统计",
                    "allOf": [
                        {
                            "$ref": "#/definitions/constvar.BoolType"
                        }
                    ]
                },
                "outboundTransportation": {
                    "description": "出库运输",
                    "type": "integer"
@@ -5645,6 +5797,9 @@
                    }
                },
                "id": {
                    "type": "integer"
                },
                "inventoryDealerType": {
                    "type": "integer"
                },
                "locationId": {
@@ -5879,6 +6034,14 @@
                }
            }
        },
        "request.DoWarehouseMonthStats": {
            "type": "object",
            "properties": {
                "token": {
                    "type": "string"
                }
            }
        },
        "request.EditMiniDict": {
            "type": "object",
            "properties": {
@@ -6097,6 +6260,12 @@
                },
                "pageSize": {
                    "description": "每页大小",
                    "type": "integer"
                },
                "preload": {
                    "type": "boolean"
                },
                "warehouseID": {
                    "type": "integer"
                }
            }
@@ -6665,6 +6834,9 @@
                "id": {
                    "type": "integer"
                },
                "inventoryDealerType": {
                    "type": "integer"
                },
                "locationId": {
                    "description": "源位置id",
                    "type": "integer"
@@ -6723,6 +6895,10 @@
                    "description": "目标位置id",
                    "type": "integer"
                },
                "warehouseId": {
                    "description": "仓库id",
                    "type": "integer"
                },
                "waybillNumber": {
                    "description": "运单号",
                    "type": "string"
docs/swagger.json
@@ -762,6 +762,58 @@
                }
            }
        },
        "/api-wms/v1/forms/doWarehouseMonthStats": {
            "post": {
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "报表"
                ],
                "summary": "手动跑月度统计库存报表",
                "parameters": [
                    {
                        "description": "查询参数",
                        "name": "object",
                        "in": "body",
                        "required": true,
                        "schema": {
                            "$ref": "#/definitions/request.DoWarehouseMonthStats"
                        }
                    },
                    {
                        "type": "string",
                        "description": "token",
                        "name": "Authorization",
                        "in": "header",
                        "required": true
                    }
                ],
                "responses": {
                    "200": {
                        "description": "成功",
                        "schema": {
                            "allOf": [
                                {
                                    "$ref": "#/definitions/util.ResponseList"
                                },
                                {
                                    "type": "object",
                                    "properties": {
                                        "data": {
                                            "type": "array",
                                            "items": {
                                                "$ref": "#/definitions/models.MonthStats"
                                            }
                                        }
                                    }
                                }
                            ]
                        }
                    }
                }
            }
        },
        "/api-wms/v1/forms/downloadHistory": {
            "post": {
                "produces": [
@@ -1135,6 +1187,58 @@
                    "报表"
                ],
                "summary": "月度统计库存报表",
                "parameters": [
                    {
                        "description": "查询参数",
                        "name": "object",
                        "in": "body",
                        "required": true,
                        "schema": {
                            "$ref": "#/definitions/request.GetMonthStats"
                        }
                    },
                    {
                        "type": "string",
                        "description": "token",
                        "name": "Authorization",
                        "in": "header",
                        "required": true
                    }
                ],
                "responses": {
                    "200": {
                        "description": "成功",
                        "schema": {
                            "allOf": [
                                {
                                    "$ref": "#/definitions/util.ResponseList"
                                },
                                {
                                    "type": "object",
                                    "properties": {
                                        "data": {
                                            "type": "array",
                                            "items": {
                                                "$ref": "#/definitions/models.MonthStats"
                                            }
                                        }
                                    }
                                }
                            ]
                        }
                    }
                }
            }
        },
        "/api-wms/v1/forms/warehouseMonthStats": {
            "post": {
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "报表"
                ],
                "summary": "仓库月度统计库存报表",
                "parameters": [
                    {
                        "description": "查询参数",
@@ -1935,7 +2039,22 @@
                    "200": {
                        "description": "成功",
                        "schema": {
                            "$ref": "#/definitions/util.Response"
                            "allOf": [
                                {
                                    "$ref": "#/definitions/util.ResponseList"
                                },
                                {
                                    "type": "object",
                                    "properties": {
                                        "data": {
                                            "type": "array",
                                            "items": {
                                                "$ref": "#/definitions/models.Operation"
                                            }
                                        }
                                    }
                                }
                            ]
                        }
                    }
                }
@@ -2172,9 +2291,9 @@
                        "in": "query"
                    },
                    {
                        "type": "integer",
                        "type": "string",
                        "description": "仓库id",
                        "name": "warehouseId",
                        "name": "warehouseIds",
                        "in": "query"
                    }
                ],
@@ -3783,6 +3902,21 @@
                "BaseOperationTypeAdjust"
            ]
        },
        "constvar.BoolType": {
            "type": "integer",
            "enum": [
                1,
                2
            ],
            "x-enum-comments": {
                "BoolTypeFalse": "false",
                "BoolTypeTrue": "true"
            },
            "x-enum-varnames": [
                "BoolTypeTrue",
                "BoolTypeFalse"
            ]
        },
        "constvar.CostingMethod": {
            "type": "integer",
            "enum": [
@@ -3954,17 +4088,20 @@
            "enum": [
                1,
                2,
                3
                3,
                4
            ],
            "x-enum-comments": {
                "StockoutType": "出库类型",
                "StorageType": "入库类型",
                "TakeStock": "盘点类型",
                "TransferType": "调拨类型"
            },
            "x-enum-varnames": [
                "TransferType",
                "StorageType",
                "StockoutType"
                "StockoutType",
                "TransferType",
                "TakeStock"
            ]
        },
        "constvar.OperationSource": {
@@ -5083,6 +5220,9 @@
                "updateTime": {
                    "type": "string"
                },
                "warehouse": {
                    "$ref": "#/definitions/models.Warehouse"
                },
                "warehouseId": {
                    "type": "integer"
                },
@@ -5114,6 +5254,10 @@
                    "type": "number"
                },
                "createTime": {
                    "type": "string"
                },
                "dealerType": {
                    "description": "出入库类型",
                    "type": "string"
                },
                "fromLocation": {
@@ -5407,6 +5551,14 @@
                    "description": "仓库名称",
                    "type": "string"
                },
                "openMonthStats": {
                    "description": "是否开启月度统计",
                    "allOf": [
                        {
                            "$ref": "#/definitions/constvar.BoolType"
                        }
                    ]
                },
                "outboundTransportation": {
                    "description": "出库运输",
                    "type": "integer"
@@ -5634,6 +5786,9 @@
                    }
                },
                "id": {
                    "type": "integer"
                },
                "inventoryDealerType": {
                    "type": "integer"
                },
                "locationId": {
@@ -5868,6 +6023,14 @@
                }
            }
        },
        "request.DoWarehouseMonthStats": {
            "type": "object",
            "properties": {
                "token": {
                    "type": "string"
                }
            }
        },
        "request.EditMiniDict": {
            "type": "object",
            "properties": {
@@ -6086,6 +6249,12 @@
                },
                "pageSize": {
                    "description": "每页大小",
                    "type": "integer"
                },
                "preload": {
                    "type": "boolean"
                },
                "warehouseID": {
                    "type": "integer"
                }
            }
@@ -6654,6 +6823,9 @@
                "id": {
                    "type": "integer"
                },
                "inventoryDealerType": {
                    "type": "integer"
                },
                "locationId": {
                    "description": "源位置id",
                    "type": "integer"
@@ -6712,6 +6884,10 @@
                    "description": "目标位置id",
                    "type": "integer"
                },
                "warehouseId": {
                    "description": "仓库id",
                    "type": "integer"
                },
                "waybillNumber": {
                    "description": "运单号",
                    "type": "string"
docs/swagger.yaml
@@ -60,6 +60,17 @@
    - BaseOperationTypeInternal
    - BaseOperationTypeDisuse
    - BaseOperationTypeAdjust
  constvar.BoolType:
    enum:
    - 1
    - 2
    type: integer
    x-enum-comments:
      BoolTypeFalse: "false"
      BoolTypeTrue: "true"
    x-enum-varnames:
    - BoolTypeTrue
    - BoolTypeFalse
  constvar.CostingMethod:
    enum:
    - 1
@@ -200,15 +211,18 @@
    - 1
    - 2
    - 3
    - 4
    type: integer
    x-enum-comments:
      StockoutType: 出库类型
      StorageType: 入库类型
      TakeStock: 盘点类型
      TransferType: 调拨类型
    x-enum-varnames:
    - TransferType
    - StorageType
    - StockoutType
    - TransferType
    - TakeStock
  constvar.OperationSource:
    enum:
    - 1
@@ -1006,6 +1020,8 @@
        type: integer
      updateTime:
        type: string
      warehouse:
        $ref: '#/definitions/models.Warehouse'
      warehouseId:
        type: integer
      waybillNumber:
@@ -1029,6 +1045,9 @@
        description: 成本单价
        type: number
      createTime:
        type: string
      dealerType:
        description: 出入库类型
        type: string
      fromLocation:
        allOf:
@@ -1223,6 +1242,10 @@
      name:
        description: 仓库名称
        type: string
      openMonthStats:
        allOf:
        - $ref: '#/definitions/constvar.BoolType'
        description: 是否开启月度统计
      outboundTransportation:
        description: 出库运输
        type: integer
@@ -1377,6 +1400,8 @@
          $ref: '#/definitions/request.OperationDetails'
        type: array
      id:
        type: integer
      inventoryDealerType:
        type: integer
      locationId:
        description: 源位置id
@@ -1540,6 +1565,11 @@
      token:
        type: string
    type: object
  request.DoWarehouseMonthStats:
    properties:
      token:
        type: string
    type: object
  request.EditMiniDict:
    properties:
      id:
@@ -1688,6 +1718,10 @@
        type: integer
      pageSize:
        description: 每页大小
        type: integer
      preload:
        type: boolean
      warehouseID:
        type: integer
    type: object
  request.GetProductList:
@@ -2077,6 +2111,8 @@
        type: array
      id:
        type: integer
      inventoryDealerType:
        type: integer
      locationId:
        description: 源位置id
        type: integer
@@ -2118,6 +2154,9 @@
        description: 状态
      toLocationId:
        description: 目标位置id
        type: integer
      warehouseId:
        description: 仓库id
        type: integer
      waybillNumber:
        description: 运单号
@@ -2771,6 +2810,37 @@
      summary: 手动跑月度统计库存报表
      tags:
      - 报表
  /api-wms/v1/forms/doWarehouseMonthStats:
    post:
      parameters:
      - description: 查询参数
        in: body
        name: object
        required: true
        schema:
          $ref: '#/definitions/request.DoWarehouseMonthStats'
      - description: token
        in: header
        name: Authorization
        required: true
        type: string
      produces:
      - application/json
      responses:
        "200":
          description: 成功
          schema:
            allOf:
            - $ref: '#/definitions/util.ResponseList'
            - properties:
                data:
                  items:
                    $ref: '#/definitions/models.MonthStats'
                  type: array
              type: object
      summary: 手动跑月度统计库存报表
      tags:
      - 报表
  /api-wms/v1/forms/downloadHistory:
    post:
      parameters:
@@ -3017,6 +3087,37 @@
                  type: array
              type: object
      summary: 月度统计库存报表
      tags:
      - 报表
  /api-wms/v1/forms/warehouseMonthStats:
    post:
      parameters:
      - description: 查询参数
        in: body
        name: object
        required: true
        schema:
          $ref: '#/definitions/request.GetMonthStats'
      - description: token
        in: header
        name: Authorization
        required: true
        type: string
      produces:
      - application/json
      responses:
        "200":
          description: 成功
          schema:
            allOf:
            - $ref: '#/definitions/util.ResponseList'
            - properties:
                data:
                  items:
                    $ref: '#/definitions/models.MonthStats'
                  type: array
              type: object
      summary: 仓库月度统计库存报表
      tags:
      - 报表
  /api-wms/v1/location/addLocation:
@@ -3482,7 +3583,14 @@
        "200":
          description: 成功
          schema:
            $ref: '#/definitions/util.Response'
            allOf:
            - $ref: '#/definitions/util.ResponseList'
            - properties:
                data:
                  items:
                    $ref: '#/definitions/models.Operation'
                  type: array
              type: object
      summary: 入库/出库列表
      tags:
      - 入库/出库
@@ -3631,8 +3739,8 @@
        type: integer
      - description: 仓库id
        in: query
        name: warehouseId
        type: integer
        name: warehouseIds
        type: string
      produces:
      - application/json
      responses:
task/warehouse_month_stats.go
New file
@@ -0,0 +1,96 @@
package task
import (
    "fmt"
    "gorm.io/gorm"
    "time"
    "wms/constvar"
    "wms/models"
    "wms/pkg/logx"
    "wms/service"
)
func WarehouseMonthStats() (err error) {
    //加锁,只需要一个进程运行此任务
    var (
        lockName  = "WarehouseMonthStats"
        serviceID = "wms"
    )
    err = models.NewLockSearch().AcquireLock(lockName, serviceID)
    if err != nil {
        logx.Errorf("WarehouseMonthStats AcquireLock err:%v", err)
        return
    }
    defer func() {
        if err != nil {
            service.SendAlarm("月度统计执行失败", "报错: "+err.Error())
        }
        err = models.NewLockSearch().ReleaseLock(lockName, serviceID)
        if err != nil {
            logx.Errorf("WarehouseMonthStats ReleaseLock err:%v", err)
            service.SendAlarm("月度统计执行成功解锁失败", err.Error())
        }
    }()
    date := time.Now().Format("2006-01")
    lastDate := time.Now().AddDate(0, -1, 0).Format("2006-01")
    warehouses, err := models.NewWarehouseSearch().SetOpenMonthStats(constvar.BoolTypeTrue).FindNotTotal()
    if len(warehouses) == 0 {
        return
    }
    for _, warehouse := range warehouses {
        warehouseId := warehouse.Id
        oldRecords, err := models.NewWarehouseMonthStatsSearch().
            SetDate(lastDate).SetWarehouseId(warehouseId).SetFields("id, product_id").FindNotTotal()
        if err != nil {
            logx.Errorf("WarehouseMonthStats get last date record err:%v", err)
            continue
        }
        oldRecordsMap := models.WarehouseMonthStatsMap(oldRecords)
        //本月期初数量/上月结余数量
        statsRecords, err := service.GetCurrentWarehouseStats(date, warehouseId, nil)
        if err != nil {
            continue
        }
        err = models.NewWarehouseMonthStatsSearch().SetDate(date).Delete()
        if err != nil {
            continue
        }
        for _, record := range statsRecords {
            record.BeginAmount = record.EndAmount
            err = models.WithTransaction(func(db *gorm.DB) error {
                err = models.NewWarehouseMonthStatsSearch().SetOrm(db).Create(record)
                if err != nil {
                    logx.Errorf("NewWarehouseMonthStatsSearch Create err:%v, record: %+v", err, record)
                    service.SendAlarm("按仓库月度统计创建本月失败", fmt.Sprintf("NewWarehouseMonthStatsSearch Create err:%v, record: %+v", err, record))
                }
                if oldRecordsMap[record.ProductId] != nil && oldRecordsMap[record.ProductId].Id != 0 {
                    oldRecordsMap[record.ProductId].EndAmount = record.EndAmount
                    if !record.InputAmount.IsZero() || !record.InputAmount.IsZero() {
                        oldRecordsMap[record.ProductId].InputAmount = record.InputAmount
                        oldRecordsMap[record.ProductId].InputItems = record.InputItems
                        oldRecordsMap[record.ProductId].OutputAmount = record.OutputAmount
                        oldRecordsMap[record.ProductId].OutputItems = record.OutputItems
                    }
                    err = models.NewWarehouseMonthStatsSearch().SetOrm(db).SetID(oldRecordsMap[record.ProductId].Id).Save(record)
                    if err != nil {
                        logx.Errorf("NewWarehouseMonthStatsSearch Save err:%v, record:%v", err, oldRecordsMap[record.ProductId])
                        service.SendAlarm("月度统计更改上月失败", fmt.Sprintf("NewWarehouseMonthStatsSearch Create err:%v, record: %+v", err, record))
                    }
                }
                return nil
            })
        }
    }
    if err == nil {
        service.SendAlarm("按仓库月度统计执行成功", "")
    }
    return
}