list.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package house
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "git.getensh.com/common/gopkgs/database"
  9. "git.getensh.com/common/gopkgs/logger"
  10. "go.uber.org/zap"
  11. "google.golang.org/grpc/status"
  12. "property-household/errors"
  13. dbmodel "property-household/model"
  14. "property-household/pb"
  15. pb_v1 "property-household/pb/v1"
  16. "property-household/utils"
  17. "strings"
  18. )
  19. func checkHouseholdListParam(req *pb_v1.HouseholdListRequest) error {
  20. switch {
  21. case req.Uid < 1 && req.GardenId < 1:
  22. return status.Error(10003, "id不能为空")
  23. case req.ApproveStatus < 0 || req.ApproveStatus > 4:
  24. return status.Error(10003, "请选择认证状态")
  25. }
  26. if req.Page == 0 {
  27. req.Page = 1
  28. }
  29. if req.PageSize == 0 {
  30. req.PageSize = 10
  31. }
  32. return nil
  33. }
  34. /*
  35. func getUserInfos(uids []int64) (map[int64]dbmodel.TUser, error) {
  36. user := dbmodel.TUser{}
  37. where := map[string]interface{}{
  38. "id in":uids,
  39. }
  40. list, err := user.List(database.DB(), where, nil, -1, -1)
  41. if err != nil {
  42. return nil, errors.DataBaseError
  43. }
  44. ret := map[int64]dbmodel.TUser{}
  45. for _, v := range list {
  46. ret[v.ID] = v
  47. }
  48. return ret, nil
  49. }
  50. */
  51. func getGardenInfos(ids []int64) (map[int64]pb_v1.GardenItem, error) {
  52. ret := map[int64]pb_v1.GardenItem{}
  53. if len(ids) == 0 {
  54. return ret, nil
  55. }
  56. mreq := pb_v1.GardenInfosRequest{
  57. Ids:ids,
  58. }
  59. mreply, err := pb.System.GardenInfos(context.Background(), &mreq)
  60. if err != nil {
  61. return nil, err
  62. }
  63. for _, v := range mreply.List {
  64. ret[v.Id] = *v
  65. }
  66. return ret, nil
  67. }
  68. func householdApprovedList(ctx context.Context, req *pb_v1.HouseholdListRequest)(reply *pb_v1.HouseholdListReply, err error) {
  69. reply = &pb_v1.HouseholdListReply{}
  70. p := dbmodel.THouseApproved{}
  71. where := map[string]interface{}{}
  72. if req.GardenId > 0 {
  73. where["garden_id"] = req.GardenId
  74. }
  75. if req.Uid > 0 {
  76. where["uid"] = req.Uid
  77. }
  78. if req.Name != "" {
  79. where["name"] = req.Name
  80. }
  81. reply.Page = req.Page
  82. reply.Total, err = p.Count(database.DB(), where, nil)
  83. if err != nil {
  84. return nil, errors.DataBaseError
  85. }
  86. if reply.Total == 0 {
  87. return reply, nil
  88. }
  89. list, err := p.ListByJoin(database.DB(), where, nil, int(req.Page), int(req.PageSize))
  90. if err != nil {
  91. return nil, errors.DataBaseError
  92. }
  93. gardenIdsM := map[int64]bool{}
  94. gardenIds := []int64{}
  95. for _, v := range list {
  96. if gardenIdsM[v.GardenId] {
  97. continue
  98. }
  99. gardenIdsM[v.GardenId] = true
  100. gardenIds = append(gardenIds, v.GardenId)
  101. }
  102. gardenInfosM, err := getGardenInfos(gardenIds)
  103. if err != nil {
  104. return nil, err
  105. }
  106. reply.List = make([]*pb_v1.HouseholdItem, len(list))
  107. for i, v := range list {
  108. reply.List[i] = &pb_v1.HouseholdItem{
  109. Id: v.ID,
  110. HouseName: fmt.Sprintf("%v-%v-%v", v.BuildingNumber, v.UnitNumber, v.HouseNumber),
  111. UserType: v.UserType,
  112. ApproveStatus: HouseholdApproveStatusSuccess,
  113. GardenName: gardenInfosM[v.GardenId].GardenName,
  114. IdType:v.IdType,
  115. IdNumber:v.IdNumber,
  116. Phone:v.Phone,
  117. Name:v.Name,
  118. GardenId:v.GardenId,
  119. HouseId:v.HouseId,
  120. Uid:v.Uid,
  121. }
  122. decrypt := utils.CertDecrypt(reply.List[i].IdNumber)
  123. if decrypt != "" {
  124. reply.List[i].IdNumber = decrypt
  125. }
  126. if v.Appendix != "" {
  127. reply.List[i].Appendix = strings.Split(v.Appendix, ";")
  128. }
  129. if !strings.Contains(v.HouseNumber, "-") {
  130. if v.UnitNumber > 0 {
  131. reply.List[i].HouseName = fmt.Sprintf("%s-%d-%s", v.BuildingNumber, v.UnitNumber, v.HouseNumber)
  132. } else {
  133. reply.List[i].HouseName = fmt.Sprintf("%s-%s", v.BuildingNumber, v.HouseNumber)
  134. }
  135. }
  136. }
  137. return reply, err
  138. }
  139. //
  140. func HouseholdList(ctx context.Context, req *pb_v1.HouseholdListRequest) (reply *pb_v1.HouseholdListReply, err error) {
  141. reply = &pb_v1.HouseholdListReply{}
  142. // 捕获各个task中的异常并返回给调用者
  143. defer func() {
  144. if r := recover(); r != nil {
  145. err = fmt.Errorf("%+v", r)
  146. e := &status.Status{}
  147. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  148. logger.Error("err",
  149. zap.String("system_err", err.Error()),
  150. zap.Stack("stacktrace"))
  151. }
  152. }
  153. }()
  154. err = checkHouseholdListParam(req)
  155. if err != nil {
  156. return nil, err
  157. }
  158. if req.ApproveStatus == HouseholdApproveStatusSuccess {
  159. return householdApprovedList(ctx, req)
  160. }
  161. p := dbmodel.THouse{}
  162. where := map[string]interface{}{}
  163. if req.GardenId > 0 {
  164. where["garden_id"] = req.GardenId
  165. }
  166. if req.Uid > 0 {
  167. where["uid"] = req.Uid
  168. }
  169. if req.Name != "" {
  170. where["name"] = req.Name
  171. }
  172. if req.ApproveStatus > 0 && req.ApproveStatus != HouseholdApproveStatusFailAndWait{
  173. where["approve_status"] = req.ApproveStatus
  174. }
  175. reply.Page = req.Page
  176. reply.Total, err = p.Count(database.DB(), where, nil)
  177. if err != nil {
  178. return nil, errors.DataBaseError
  179. }
  180. if reply.Total == 0 {
  181. return reply, nil
  182. }
  183. list, err := p.ListByJoin(database.DB(), where, nil, int(req.Page), int(req.PageSize))
  184. if err != nil {
  185. return nil, errors.DataBaseError
  186. }
  187. gardenIdsM := map[int64]bool{}
  188. gardenIds := []int64{}
  189. for _, v := range list {
  190. if gardenIdsM[v.GardenId] {
  191. continue
  192. }
  193. gardenIdsM[v.GardenId] = true
  194. gardenIds = append(gardenIds, v.GardenId)
  195. }
  196. gardenInfosM, err := getGardenInfos(gardenIds)
  197. if err != nil {
  198. return nil, err
  199. }
  200. reply.List = make([]*pb_v1.HouseholdItem, len(list))
  201. for i, v := range list {
  202. reply.List[i] = &pb_v1.HouseholdItem{
  203. Id: v.ID,
  204. HouseName: fmt.Sprintf("%v-%v-%v", v.BuildingNumber, v.UnitNumber, v.HouseNumber),
  205. UserType: v.UserType,
  206. ApproveStatus: int32(v.ApproveStatus),
  207. GardenName: gardenInfosM[v.GardenId].GardenName,
  208. IdType:v.IdType,
  209. IdNumber:v.IdNumber,
  210. Phone:v.Phone,
  211. Name:v.Name,
  212. GardenId:v.GardenId,
  213. HouseId:v.HouseId,
  214. Uid:v.Uid,
  215. }
  216. decrypt := utils.CertDecrypt(reply.List[i].IdNumber)
  217. if decrypt != "" {
  218. reply.List[i].IdNumber = decrypt
  219. }
  220. if v.Appendix != "" {
  221. reply.List[i].Appendix = strings.Split(v.Appendix, ";")
  222. }
  223. if !strings.Contains(v.HouseNumber, "-") {
  224. if v.UnitNumber > 0 {
  225. reply.List[i].HouseName = fmt.Sprintf("%s-%d-%s", v.BuildingNumber, v.UnitNumber, v.HouseNumber)
  226. } else {
  227. reply.List[i].HouseName = fmt.Sprintf("%s-%s", v.BuildingNumber, v.HouseNumber)
  228. }
  229. }
  230. }
  231. return reply, nil
  232. }