publicity.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2019 githup.com. All rights reserved.
  2. // Use of this source code is governed by githup.com.
  3. package user
  4. import (
  5. "context"
  6. "github.com/jaryhe/gopkgs/database"
  7. "smart-enterprise-management/consts"
  8. "smart-enterprise-management/errors"
  9. dbmodel "smart-enterprise-management/model"
  10. pb_v1 "smart-enterprise-management/pb/v1"
  11. "github.com/jaryhe/gopkgs/logger"
  12. "go.uber.org/zap"
  13. )
  14. func Publicity(ctx context.Context, req *pb_v1.PublicityRequest)(reply *pb_v1.PublicityReply, err error) {
  15. // 设备类型map
  16. pdt := &dbmodel.TDeviceTypeItem{}
  17. m, _, err := pdt.AllMap(database.DB())
  18. if err != nil {
  19. logger.Error("publicity",
  20. zap.String("err", err.Error()))
  21. return nil, errors.DataBaseError
  22. }
  23. // 供应商公示列表
  24. reply = &pb_v1.PublicityReply{}
  25. list, total, err := dbmodel.PublicityList(database.DB(), req.Provider)
  26. if err != nil {
  27. logger.Error("publicity",
  28. zap.String("err", err.Error()))
  29. return nil, errors.DataBaseError
  30. }
  31. if total == 0 {
  32. return reply, nil
  33. }
  34. // 设配类型
  35. pids := make([]int64, len(list))
  36. for i, v := range list {
  37. pids[i] = v.Id
  38. }
  39. pt := dbmodel.TProviderDevice{}
  40. where := map[string]interface{}{
  41. "provider_id in":pids,
  42. "status":consts.DeviceTypeStatusAddAuditted,
  43. }
  44. if req.DeviceCode != 0 {
  45. where["device_code"] = req.DeviceCode
  46. }
  47. dlist, err := pt.List(database.DB(), where)
  48. if err != nil {
  49. logger.Error("publicity",
  50. zap.String("err", err.Error()))
  51. return nil, errors.DataBaseError
  52. }
  53. dmap := map[int64][]dbmodel.TProviderDevice{}
  54. for _, v := range dlist {
  55. dmap[v.ProviderId] = append(dmap[v.ProviderId], v)
  56. }
  57. // 组装结果
  58. reply.List = make([]*pb_v1.PublicityItem, len(dmap))
  59. k := 0
  60. for _, v := range list {
  61. item := &pb_v1.PublicityItem{
  62. Name:v.Name,
  63. SocialCode:v.SocialCode,
  64. }
  65. if len(dmap[v.Id]) == 0 {
  66. continue
  67. }
  68. if dsublist, ok := dmap[v.Id]; ok {
  69. for _, value := range dsublist {
  70. subItem := &pb_v1.PublicityDeviceTypeItem{
  71. TypeCode:value.DeviceCode,
  72. TypeName:m[value.DeviceCode],
  73. }
  74. item.Devices = append(item.Devices, subItem)
  75. }
  76. }
  77. reply.List[k] = item
  78. k++
  79. }
  80. return reply, nil
  81. }