zhangqian
2023-10-28 cc94e1f7427fa598f3e3758650554e97ef957034
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package v1
 
import (
    "aps_crm/conf"
    "aps_crm/constvar"
    "aps_crm/model"
    "aps_crm/model/request"
    "aps_crm/model/response"
    "aps_crm/pkg/contextx"
    "aps_crm/pkg/ecode"
    "aps_crm/pkg/logx"
    "aps_crm/proto/code"
    "github.com/gin-gonic/gin"
    "github.com/spf13/cast"
    "google.golang.org/grpc"
    "google.golang.org/grpc/credentials/insecure"
)
 
type CodeApi struct{}
 
var (
    codeServiceConn *grpc.ClientConn
)
 
func InitCodeServiceConn() {
    var err error
    codeServiceConn, err = grpc.Dial(conf.Conf.GrpcServiceAddr.Aps, grpc.WithTransportCredentials(insecure.NewCredentials()))
    if err != nil {
        logx.Errorf("grpc dial product service error: %v", err.Error())
        return
    }
}
 
func CloseCodeServiceConn() {
    if codeServiceConn != nil {
        codeServiceConn.Close()
    }
}
 
// GetCodeList
//
// @Tags        编码
// @Summary    获取编码列表
// @Produce    application/json
// @Param        object    query        request.GetCodeList    true    "参数"
// @Success    200    {object}    response.ListResponse
//
//    @Router        /api/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(codeServiceConn)
    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())
        ctx.FailWithMsg(ecode.UnknownErr, "grpc调用错误")
        return
    }
    ctx.OkWithDetailed(response.ListResponse{
        Data:  list.List,
        Count: list.Total,
    })
}
 
// GetAutoCode
//
// @Tags        编码
// @Summary    获取自动编码
// @Produce    application/json
// @Param        object    body        code.CodeStandard    true    "参数"
// @Success    200    {object}    response.ListResponse
//
//    @Router        /api/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.CodeStandardTypeSaleKey:
        id, err = model.NewSaleChanceSearch().MaxAutoIncr()
    case constvar.CodeStandardTypeSaleLead:
        id, err = model.NewSalesLeadsSearch(nil).MaxAutoIncr()
    case constvar.CodeStandardTypeCustom:
        id, err = model.NewClientSearch(nil).MaxAutoIncr()
    case constvar.CodeStandardTypeFollowRecord:
        id, err = model.NewFollowRecordSearch().MaxAutoIncr()
    case constvar.CodeStandardTypeQuotation:
        id, err = model.NewQuotationSearch(nil).MaxAutoIncr()
    case constvar.CodeStandardTypeSaleTotalOrder:
        id, err = model.NewMasterOrderSearch().MaxAutoIncr()
    case constvar.CodeStandardTypeSaleSuborder:
        id, err = model.NewSubOrderSearch(nil).MaxAutoIncr()
    case constvar.CodeStandardTypeSaleDetail:
        id, err = model.NewSalesDetailsSearch().MaxAutoIncr()
    case constvar.CodeStandardTypeSaleReturnGoods:
        id, err = model.NewSalesReturnSearch().MaxAutoIncr()
    case constvar.CodeStandardTypeSaleRefund:
        id, err = model.NewSalesRefundSearch().MaxAutoIncr()
    case constvar.CodeStandardTypeContract:
        id, err = model.NewContractSearch().MaxAutoIncr()
    case constvar.CodeStandardTypeServerContract:
        id, err = model.NewServiceContractSearch().MaxAutoIncr()
    case constvar.CodeStandardTypeCustomServer:
        id, err = model.NewServiceOrderSearch().MaxAutoIncr()
    case constvar.CodeStandardTypeServerFollow:
        id, err = model.NewServiceFollowupSearch().MaxAutoIncr()
    case constvar.CodeStandardTypeSaleInvoice:
        id, err = model.NewInvoiceSearch().MaxAutoIncr()
    default:
        ctx.FailWithMsg(ecode.UnknownErr, "编码规则不存在")
        return
    }
    if err != nil {
        logx.Errorf("GetAutoCode err: %v", err.Error())
        ctx.FailWithMsg(ecode.UnknownErr, "获取最大值失败")
        return
    }
    autoCode := model.GetAutoCode(id, &params)
    ctx.OkWithDetailed(autoCode)
}