作者 tangxuhui

添加 字典查询接口

  1 +package query
  2 +
  3 +import (
  4 + "fmt"
  5 +
  6 + "github.com/beego/beego/v2/core/validation"
  7 +)
  8 +
  9 +type GetDictionaryByCodeQuery struct {
  10 + // 字典编码
  11 + DictCodes []string `json:"dictCodes" valid:"Required"`
  12 +}
  13 +
  14 +func (getDictionaryQuery *GetDictionaryByCodeQuery) Valid(validation *validation.Validation) {
  15 +}
  16 +
  17 +func (getDictionaryQuery *GetDictionaryByCodeQuery) ValidateQuery() error {
  18 + valid := validation.Validation{}
  19 + b, err := valid.Valid(getDictionaryQuery)
  20 + if err != nil {
  21 + return err
  22 + }
  23 + if !b {
  24 + for _, validErr := range valid.Errors {
  25 + return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
  26 + }
  27 + }
  28 + return nil
  29 +}
@@ -253,6 +253,47 @@ func (dictionaryService *DictionaryService) UpdateDictionary(updateDictionaryCom @@ -253,6 +253,47 @@ func (dictionaryService *DictionaryService) UpdateDictionary(updateDictionaryCom
253 } 253 }
254 } 254 }
255 255
  256 +// 返回数据字典设置
  257 +func (dictionaryService *DictionaryService) GetDictionaryByCode(getDictionaryQuery *query.GetDictionaryByCodeQuery) (interface{}, error) {
  258 + if err := getDictionaryQuery.ValidateQuery(); err != nil {
  259 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  260 + }
  261 + transactionContext, err := factory.CreateTransactionContext(nil)
  262 + if err != nil {
  263 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  264 + }
  265 + if err := transactionContext.StartTransaction(); err != nil {
  266 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  267 + }
  268 + defer func() {
  269 + transactionContext.RollbackTransaction()
  270 + }()
  271 + var dictionaryRepository domain.DictionaryRepository
  272 + if value, err := factory.CreateDictionaryRepository(map[string]interface{}{
  273 + "transactionContext": transactionContext,
  274 + }); err != nil {
  275 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  276 + } else {
  277 + dictionaryRepository = value
  278 + }
  279 + queryCondition := make(map[string]interface{})
  280 + if len(getDictionaryQuery.DictCodes) > 0 {
  281 + queryCondition["dictCodeIn"] = getDictionaryQuery.DictCodes
  282 + }
  283 + _, dictionarys, err := dictionaryRepository.Find(queryCondition)
  284 + if err != nil {
  285 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  286 + }
  287 +
  288 + if err := transactionContext.CommitTransaction(); err != nil {
  289 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  290 + }
  291 + return map[string]interface{}{
  292 + "dictionarys": dictionarys,
  293 + }, nil
  294 +
  295 +}
  296 +
256 func NewDictionaryService(options map[string]interface{}) *DictionaryService { 297 func NewDictionaryService(options map[string]interface{}) *DictionaryService {
257 newDictionaryService := &DictionaryService{} 298 newDictionaryService := &DictionaryService{}
258 return newDictionaryService 299 return newDictionaryService
@@ -118,6 +118,9 @@ func (repository *DictionaryRepository) Find(queryOptions map[string]interface{} @@ -118,6 +118,9 @@ func (repository *DictionaryRepository) Find(queryOptions map[string]interface{}
118 query := sqlbuilder.BuildQuery(tx.Model(&dictionaryModels), queryOptions) 118 query := sqlbuilder.BuildQuery(tx.Model(&dictionaryModels), queryOptions)
119 query.SetOffsetAndLimit(20) 119 query.SetOffsetAndLimit(20)
120 query.SetOrderDirect("dictionary_id", "DESC") 120 query.SetOrderDirect("dictionary_id", "DESC")
  121 + if v, ok := queryOptions["dictCodeIn"]; ok {
  122 + query.Where("dict_code IN (?)", pg.Array(v))
  123 + }
121 if count, err := query.SelectAndCount(); err != nil { 124 if count, err := query.SelectAndCount(); err != nil {
122 return 0, dictionarys, err 125 return 0, dictionarys, err
123 } else { 126 } else {
@@ -48,9 +48,8 @@ func (controller *DictionaryController) ListDictionary() { @@ -48,9 +48,8 @@ func (controller *DictionaryController) ListDictionary() {
48 48
49 func (controller *DictionaryController) GetDictionaryByCode() { 49 func (controller *DictionaryController) GetDictionaryByCode() {
50 dictionaryService := service.NewDictionaryService(nil) 50 dictionaryService := service.NewDictionaryService(nil)
51 - getDictionaryQuery := &query.GetDictionaryQuery{}  
52 - dictionaryCode := controller.GetString(":dictionaryCode")  
53 - getDictionaryQuery.DictCode = dictionaryCode  
54 - data, err := dictionaryService.GetDictionary(getDictionaryQuery) 51 + getDictionaryQuery := &query.GetDictionaryByCodeQuery{}
  52 + _ = controller.Unmarshal(getDictionaryQuery)
  53 + data, err := dictionaryService.GetDictionaryByCode(getDictionaryQuery)
55 controller.Response(data, err) 54 controller.Response(data, err)
56 } 55 }
@@ -10,5 +10,5 @@ func init() { @@ -10,5 +10,5 @@ func init() {
10 web.Router("/dictionarys/:dictionaryId", &controllers.DictionaryController{}, "Put:UpdateDictionary") 10 web.Router("/dictionarys/:dictionaryId", &controllers.DictionaryController{}, "Put:UpdateDictionary")
11 web.Router("/dictionarys/:dictionaryId", &controllers.DictionaryController{}, "Get:GetDictionary") 11 web.Router("/dictionarys/:dictionaryId", &controllers.DictionaryController{}, "Get:GetDictionary")
12 web.Router("/dictionarys/search", &controllers.DictionaryController{}, "Post:ListDictionary") 12 web.Router("/dictionarys/search", &controllers.DictionaryController{}, "Post:ListDictionary")
13 - web.Router("/dictionarys/dictionary-code/:dictionaryCode", &controllers.DictionaryController{}, "Get:GetDictionaryByCode") 13 + web.Router("/dictionarys/dictionary-code", &controllers.DictionaryController{}, "Post:GetDictionaryByCode")
14 } 14 }
  1 +package dictionary
  2 +
  3 +import (
  4 + "net/http"
  5 +
  6 + "github.com/gavv/httpexpect"
  7 + "github.com/go-pg/pg/v10"
  8 + . "github.com/onsi/ginkgo"
  9 + . "github.com/onsi/gomega"
  10 + pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-basic/pkg/infrastructure/pg"
  11 +)
  12 +
  13 +var _ = Describe("返回数据字典设置列表", func() {
  14 + var dictionaryId int64
  15 + BeforeEach(func() {
  16 + items := `[{"itemCode":"xxx","itemLabel":"123","itemValue":"abc","isShow":1,"sort":1}]`
  17 + _, err := pG.DB.QueryOne(
  18 + pg.Scan(&dictionaryId),
  19 + "INSERT INTO dictionarys (dictionary_id, dict_code, dict_name, describe, is_show, dict_items) VALUES (?, ?, ?, ?, ?, ?) RETURNING dictionary_id",
  20 + 10089, "testDictCode", "testDictName", "testDesc", 1, items)
  21 + Expect(err).NotTo(HaveOccurred())
  22 + })
  23 + Describe("根据参数返回字典列表", func() {
  24 + Context("传入有效的参数", func() {
  25 + It("返回字典数据列表", func() {
  26 + body := map[string]interface{}{
  27 + "dictCodes": []string{"testDictCode"},
  28 + }
  29 + httpExpect := httpexpect.New(GinkgoT(), server.URL)
  30 + httpExpect.POST("/dictionarys/dictionary-code").
  31 + WithJSON(body).
  32 + Expect().
  33 + Status(http.StatusOK).
  34 + JSON().
  35 + Object().
  36 + ContainsKey("code").ValueEqual("code", 0).
  37 + ContainsKey("msg").ValueEqual("msg", "ok").
  38 + ContainsKey("data").Value("data").Object().
  39 + ContainsKey("dictionarys").Value("dictionarys").Array()
  40 + })
  41 + })
  42 + })
  43 + AfterEach(func() {
  44 + _, err := pG.DB.Exec("DELETE FROM dictionarys WHERE true")
  45 + Expect(err).NotTo(HaveOccurred())
  46 + })
  47 +})