publicity.go 1.9 KB

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