selfcheer
2024-07-19 1572f45e72cc0fa15c029f9ee2a08474104435e6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package test
 
import (
    "github.com/gin-gonic/gin"
    "github.com/spf13/cast"
    "srm/constvar"
    "srm/model/common/response"
    "srm/model/test/request"
    "srm/pkg/contextx"
    "srm/pkg/logx"
    "srm/proto/code"
    "srm/service"
    purchaseService "srm/service/purchase"
    "srm/utils"
)
 
type CodeApi struct {
}
 
var (
    supplierService = service.ServiceGroupApp.TestServiceGroup.SupplierService
)
 
// GetCodeList
//
// @Tags        编码
// @Summary    获取编码列表
// @Produce    application/json
// @Param        object    query        request.GetCodeList    true    "参数"
// @Success    200    {object}    response.Response{data=[]code.CodeStandard}
//
//    @Router /code/getCodeList [get]
func (ca *CodeApi) GetCodeList(c *gin.Context) {
    var params request.GetCodeList
    ctx, ok := contextx.NewContext(c, &params)
    if !ok {
        return
    }
    params.CodeStandID = c.Query("codeStandID")
    params.Name = c.Query("name")
    params.Type = c.Query("type")
    client := code.NewCodeServiceClient(productServiceConn)
    list, err := client.GetCodeList(ctx.GetCtx(), &code.GetCodeListRequest{
        Page:        cast.ToInt32(params.Page),
        PageSize:    cast.ToInt32(params.PageSize),
        CodeStandID: params.CodeStandID,
        Name:        params.Name,
        Type:        params.Type,
    })
    if err != nil {
        logx.Errorf("GetCodeList err: %v", err.Error())
        response.FailWithMessage("产品名称不能为空", c)
        return
    }
    response.OkWithDetailed(response.PageResult{
        List:     list.List,
        Total:    list.Total,
        Page:     params.Page,
        PageSize: params.PageSize,
    }, "获取成功", c)
}
 
// GetAutoCode
//
// @Tags        编码
// @Summary    获取自动编码
// @Produce    application/json
// @Param        object    body        code.CodeStandard    true    "参数"
// @Success    200    {object}    response.Response{data=map[string]interface{}}
//
//    @Router    /code/getAutoCode [post]
func (ca *CodeApi) GetAutoCode(c *gin.Context) {
    var params code.CodeStandard
    ctx, ok := contextx.NewContext(c, &params)
    if !ok {
        return
    }
    var (
        id  = 0
        err error
    )
    switch constvar.CodeStandardType(params.Type) {
    case constvar.CodeStandardType_PurchaseOrder:
        id, err = purchaseService.NewPurchaseService().MaxAutoIncr()
    case constvar.CodeStandardType_Supplier:
        id, err = supplierService.MaxAutoIncr()
    default:
        response.FailWithMessage("编码规则不存在", c)
        return
    }
    if err != nil {
        response.FailWithMessage("获取最大值失败", c)
        return
    }
    m := make(map[string]interface{})
    autoCode := utils.GetAutoCode(id, &params)
    m["id"] = autoCode
    m["codeStandardID"] = params.ID
    m["maxAutoIncr"] = id + 1
    ctx.OkWithDetailed(m)
}