list.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package house_rent
  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-garden/errors"
  13. dbmodel "property-garden/model"
  14. "property-garden/parser"
  15. "property-garden/pb"
  16. pb_v1 "property-garden/pb/v1"
  17. "property-garden/utils"
  18. "strings"
  19. )
  20. func checkGardenHouseRentListParam(req *pb_v1.GardenHouseRentListRequest) error {
  21. if req.Page == 0 {
  22. req.Page = 1
  23. }
  24. if req.PageSize == 0 {
  25. req.PageSize = 10
  26. }
  27. if req.GardenId < 1 {
  28. return status.Error(10003, "小区不能为空")
  29. }
  30. return nil
  31. }
  32. func BaseConfToBool(data int64) pb_v1.HouseRentBaseConf {
  33. conf := pb_v1.HouseRentBaseConf{}
  34. one := int64(1)
  35. if data&one > 0 {
  36. conf.Bed = true
  37. }
  38. if data&(one<<1) > 0 {
  39. conf.Gas = true
  40. }
  41. if data&(one<<2) > 0 {
  42. conf.WarmGas = true
  43. }
  44. if data&(one<<3) > 0 {
  45. conf.Broadband = true
  46. }
  47. if data&(one<<4) > 0 {
  48. conf.Refragerator = true
  49. }
  50. if data&(one<<5) > 0 {
  51. conf.Wardobe = true
  52. }
  53. if data&(one<<6) > 0 {
  54. conf.Sofa = true
  55. }
  56. if data&(one<<7) > 0 {
  57. conf.Aircondition = true
  58. }
  59. if data&(one<<8) > 0 {
  60. conf.Tv = true
  61. }
  62. if data&(one<<9) > 0 {
  63. conf.Heater = true
  64. }
  65. if data&(one<<10) > 0 {
  66. conf.Warshing = true
  67. }
  68. return conf
  69. }
  70. func SpecialConfToBool(data int64) pb_v1.HouseRentSpecialConf {
  71. conf := pb_v1.HouseRentSpecialConf{}
  72. one := int64(1)
  73. if data&one > 0 {
  74. conf.IntelligentLock = true
  75. }
  76. if data&(one<<1) > 0 {
  77. conf.Wifi = true
  78. }
  79. if data&(one<<2) > 0 {
  80. conf.Metro = true
  81. }
  82. if data&(one<<3) > 0 {
  83. conf.ParkSpace = true
  84. }
  85. if data&(one<<4) > 0 {
  86. conf.IndependentWc = true
  87. }
  88. if data&(one<<5) > 0 {
  89. conf.PrivateBalcony = true
  90. }
  91. if data&(one<<6) > 0 {
  92. conf.FirstRent = true
  93. }
  94. return conf
  95. }
  96. func BaseConfToBitmap(conf *pb_v1.HouseRentBaseConf) int64 {
  97. ret := int64(0)
  98. one := int64(1)
  99. if conf.Bed {
  100. ret = ret | (one)
  101. }
  102. if conf.Gas {
  103. ret = ret | (one << 1)
  104. }
  105. if conf.WarmGas {
  106. ret = ret | (one << 2)
  107. }
  108. if conf.Broadband {
  109. ret = ret | (one << 3)
  110. }
  111. if conf.Refragerator {
  112. ret = ret | (one << 4)
  113. }
  114. if conf.Wardobe {
  115. ret = ret | (one << 5)
  116. }
  117. if conf.Sofa {
  118. ret = ret | (one << 6)
  119. }
  120. if conf.Aircondition {
  121. ret = ret | (one << 7)
  122. }
  123. if conf.Tv {
  124. ret = ret | (one << 8)
  125. }
  126. if conf.Heater {
  127. ret = ret | (one << 9)
  128. }
  129. if conf.Warshing {
  130. ret = ret | (one << 10)
  131. }
  132. return ret
  133. }
  134. func SpecialConfToBitmap(conf *pb_v1.HouseRentSpecialConf) int64 {
  135. ret := int64(0)
  136. one := int64(1)
  137. if conf.IntelligentLock {
  138. ret = ret | (one)
  139. }
  140. if conf.Wifi {
  141. ret = ret | (one << 1)
  142. }
  143. if conf.Metro {
  144. ret = ret | (one << 2)
  145. }
  146. if conf.ParkSpace {
  147. ret = ret | (one << 3)
  148. }
  149. if conf.IndependentWc {
  150. ret = ret | (one << 4)
  151. }
  152. if conf.PrivateBalcony {
  153. ret = ret | (one << 5)
  154. }
  155. if conf.FirstRent {
  156. ret = ret | (one << 6)
  157. }
  158. return ret
  159. }
  160. func getGardenInfos(ids []int64) (map[int64]pb_v1.GardenItem, error) {
  161. ret := map[int64]pb_v1.GardenItem{}
  162. if len(ids) == 0 {
  163. return ret, nil
  164. }
  165. mreq := pb_v1.GardenInfosRequest{
  166. Ids: ids,
  167. }
  168. mreply, err := pb.System.GardenInfos(context.Background(), &mreq)
  169. if err != nil {
  170. return nil, err
  171. }
  172. if len(mreply.List) == 0 {
  173. return ret, nil
  174. }
  175. for _, v := range mreply.List {
  176. ret[v.Id] = *v
  177. }
  178. return ret, nil
  179. }
  180. func getUserInfo(uids []int64, dbname string) (map[int64]dbmodel.THouseholdUser, error) {
  181. p := dbmodel.NewHouseholdUser(dbname)
  182. where := map[string]interface{}{
  183. "id in": uids,
  184. }
  185. list, err := p.List(database.DB(), where, nil, -1, -1)
  186. if err != nil {
  187. return nil, errors.DataBaseError
  188. }
  189. ret := map[int64]dbmodel.THouseholdUser{}
  190. for _, v := range list {
  191. ret[v.ID] = v
  192. }
  193. return ret, nil
  194. }
  195. //
  196. func GardenHouseRentList(ctx context.Context, req *pb_v1.GardenHouseRentListRequest) (reply *pb_v1.GardenHouseRentListReply, err error) {
  197. reply = &pb_v1.GardenHouseRentListReply{}
  198. // 捕获各个task中的异常并返回给调用者
  199. defer func() {
  200. if r := recover(); r != nil {
  201. err = fmt.Errorf("%+v", r)
  202. e := &status.Status{}
  203. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  204. logger.Error("err",
  205. zap.String("system_err", err.Error()),
  206. zap.Stack("stacktrace"))
  207. }
  208. }
  209. }()
  210. err = checkGardenHouseRentListParam(req)
  211. if err != nil {
  212. return nil, err
  213. }
  214. dbname := utils.GetGardenDbName(req.GardenId)
  215. p := dbmodel.NewHouseRent(dbname)
  216. where := map[string]interface{}{}
  217. if req.ApproveStatus > 0 {
  218. where["approve_status"] = req.ApproveStatus
  219. }
  220. if req.SpecialConf > 0 {
  221. where["special_conf & ? > 0 "] = req.SpecialConf
  222. }
  223. if req.BaseConf > 0 {
  224. where["base_conf & ? > 0 "] = req.BaseConf
  225. }
  226. if req.HouseholdUid > 0 {
  227. where["household_uid"] = req.HouseholdUid
  228. }
  229. if req.RoomCount > 0 {
  230. where["room_count"] = req.RoomCount
  231. }
  232. if req.HallCount > 0 {
  233. where["hall_count"] = req.HallCount
  234. }
  235. if req.WcCount > 0 {
  236. where["wc_count"] = req.WcCount
  237. }
  238. if req.StreetCode != "" {
  239. where["street_code"] = req.StreetCode
  240. } else if req.AreaCode != "" {
  241. where["area_code"] = req.AreaCode
  242. } else if req.CityCode != "" {
  243. where["city_code"] = req.CityCode
  244. } else if req.ProvinceCode != "" {
  245. where["province_code"] = req.ProvinceCode
  246. }
  247. if req.RentPriceLess > 0 {
  248. where["rent_price <="] = req.RentPriceLess * 100
  249. }
  250. if req.RentPriceGreater > 0 {
  251. where["rent_price >"] = req.RentPriceGreater * 100
  252. }
  253. if req.Contacter != "" {
  254. where["contacter"] = req.Contacter
  255. }
  256. if req.ContacterPhone != "" {
  257. where["contact_phone"] = req.ContacterPhone
  258. }
  259. reply.Page = req.Page
  260. reply.Total, err = p.Count(database.DB(), where, nil)
  261. if err != nil {
  262. return nil, errors.DataBaseError
  263. }
  264. if reply.Total == 0 {
  265. return reply, nil
  266. }
  267. list, err := p.List(database.DB(), where, nil, int(req.Page), int(req.PageSize))
  268. if err != nil {
  269. return nil, errors.DataBaseError
  270. }
  271. reply.List = make([]*pb_v1.HouseRentItem, len(list))
  272. m := map[int64]bool{}
  273. uidM := map[int64]bool{}
  274. gardenIds := []int64{}
  275. uids := []int64{}
  276. for _, v := range list {
  277. if _, ok := m[v.GardenId]; !ok {
  278. m[v.GardenId] = true
  279. gardenIds = append(gardenIds, v.GardenId)
  280. }
  281. if _, ok := uidM[v.HouseholdUid]; !ok && v.HouseholdUid > 0 {
  282. uidM[v.HouseholdUid] = true
  283. uids = append(uids, v.HouseholdUid)
  284. }
  285. }
  286. userInfos, err := getUserInfo(uids, dbname)
  287. if err != nil {
  288. return nil, err
  289. }
  290. gardenInfos, err := getGardenInfos(gardenIds)
  291. if err != nil {
  292. return nil, errors.DataBaseError
  293. }
  294. for i, v := range list {
  295. item := &pb_v1.HouseRentItem{
  296. HouseName: v.HouseName,
  297. HouseId: v.HouseId,
  298. Layer: v.Layer,
  299. HouseArea: v.HouseArea,
  300. Direction: v.Diretion,
  301. RoomCount: v.RoomCount,
  302. HallCount: v.HallCount,
  303. WcCount: v.WcCount,
  304. Decorating: v.Decorating,
  305. Contacter: v.Contacter,
  306. ContactPhone: v.ContactPhone,
  307. PayTimeType: v.PayTimeType,
  308. RentType: v.RentType,
  309. RoomType: v.RoomType,
  310. RoomArea: v.RoomArea,
  311. RentPrice: v.RentPrice,
  312. Desposit: v.Desposit,
  313. //InTime:0,
  314. ApproveStatus: v.ApproveStatus,
  315. ServicePrice: v.ServicePrice,
  316. IntermediaryPrice: v.IntermediaryPrice,
  317. //BaseConf:,
  318. //SpecialConf
  319. Desc: v.Desc,
  320. HousePic: strings.Split(v.HousePic, ";"),
  321. CertPic: strings.Split(v.CertPic, ";"),
  322. //HasLift:
  323. GardenId: v.GardenId,
  324. Id: v.ID,
  325. GardenName: v.GardenName,
  326. Province: gardenInfos[v.GardenId].Province,
  327. City: gardenInfos[v.GardenId].City,
  328. Street: gardenInfos[v.GardenId].Street,
  329. Area: gardenInfos[v.GardenId].Area,
  330. Lat: v.Lat,
  331. Lnt: v.Lnt,
  332. Addr: gardenInfos[v.GardenId].GardenAddr,
  333. InTime: v.InTime,
  334. GardenDesc: gardenInfos[v.GardenId].GardenDesc,
  335. HouseholdUid: v.HouseholdUid,
  336. RealName: userInfos[v.HouseholdUid].RealName,
  337. Phone: userInfos[v.HouseholdUid].Phone,
  338. }
  339. if v.HasLift == 1 {
  340. item.HasLift = true
  341. }
  342. //bconf := BaseConfToBool(v.BaseConf)
  343. item.BaseConf = v.BaseConf
  344. //sconf := SpecialConfToBool(v.SpecialConf)
  345. item.SpecialConf = v.SpecialConf
  346. if v.HousePic == "" {
  347. item.HousePic = []string{parser.Conf.Oss.Protocol + "://" + parser.Conf.Oss.Endpoint + "/" + parser.Conf.Oss.FixBucket + "/" + parser.Conf.Oss.RentObj}
  348. }
  349. reply.List[i] = item
  350. }
  351. return reply, nil
  352. }