apply.go 8.6 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. "property-household/errors"
  9. "property-household/impl/v1/house"
  10. dbmodel "property-household/model"
  11. "property-household/pb"
  12. pb_v1 "property-household/pb/v1"
  13. "property-household/utils"
  14. "strings"
  15. "time"
  16. "git.getensh.com/common/gopkgs/database"
  17. "git.getensh.com/common/gopkgs/logger"
  18. "go.uber.org/zap"
  19. "google.golang.org/grpc/status"
  20. "property-household/parser"
  21. )
  22. // 认证状态
  23. const (
  24. HouseholdRentStatusWait = 1
  25. HouseholdRentStatusSuccess = 2
  26. HouseholdRentStatusFail = 3
  27. )
  28. const (
  29. SystemMsgCodeHouseholdBindHouse = "1"
  30. SystemMsgCodeRepairOrder = "2"
  31. SystemMsgCodeRepairOrderSended = "3"
  32. SystemMsgCodeSuggestionOrder = "4"
  33. SystemMsgCodeSuggestionOrderSended = "5"
  34. SystemMsgCodeRent = "6"
  35. SystemMsgCodeRentAppointment = "7"
  36. )
  37. func houseRentSync(data dbmodel.THouseRent, insert bool, gardenId int64, increase int) error {
  38. bytes, _ := json.Marshal(data)
  39. mreq := pb_v1.GardenHouseRentSyncRequest{
  40. Datas: bytes,
  41. Insert: insert,
  42. GardenId: gardenId,
  43. Increase: int64(increase),
  44. }
  45. _, err := pb.Garden.GardenHouseRentSync(context.Background(), &mreq)
  46. if err != nil {
  47. return err
  48. }
  49. return nil
  50. }
  51. func isFix(src string) bool {
  52. if strings.Contains(src, parser.Conf.Oss.RentObj) || strings.Contains(src, parser.Conf.Oss.EventObj) ||
  53. strings.Contains(src, parser.Conf.Oss.VoteObj) {
  54. return true
  55. }
  56. return false
  57. }
  58. func checkHouseRentApplyParam(req *pb_v1.HouseRentApplyRequest) error {
  59. switch {
  60. case req.RentPrice == 0:
  61. return status.Error(10003, "租金不能为空")
  62. case req.RentType < 1:
  63. return status.Error(10003, "出租方式不能为空")
  64. case req.ContactPhone == "":
  65. return status.Error(10003, "联系人电话不能为空")
  66. case req.Contacter == "":
  67. return status.Error(10003, "联系人不能为空")
  68. //case req.HouseName == "":
  69. // return status.Error(10003, "房屋不能为空")
  70. case !req.Approve && req.HouseholdUid < 1:
  71. return status.Error(10003, "业主不能为空")
  72. case req.GardenId < 1:
  73. return status.Error(10003, "小区不能为空")
  74. case req.HouseId < 1:
  75. return status.Error(10003, "房屋id不能为空")
  76. }
  77. housePic := []string{}
  78. for _, v := range req.HousePic {
  79. if isFix(v) {
  80. continue
  81. }
  82. housePic = append(housePic, v)
  83. }
  84. req.HousePic = housePic
  85. if req.CertPic == nil {
  86. req.CertPic = []string{}
  87. }
  88. return nil
  89. }
  90. func getGardenInfo(id int64) (*pb_v1.GardenItem, error) {
  91. mreq := pb_v1.GardenInfosRequest{
  92. Ids: []int64{id},
  93. }
  94. mreply, err := pb.System.GardenInfos(context.Background(), &mreq)
  95. if err != nil {
  96. return nil, err
  97. }
  98. if len(mreply.List) == 0 {
  99. return nil, status.Error(10003, "未找到小区")
  100. }
  101. return mreply.List[0], nil
  102. }
  103. func BaseConfToBool(data int64) pb_v1.HouseRentBaseConf {
  104. conf := pb_v1.HouseRentBaseConf{}
  105. one := int64(1)
  106. if data&one > 0 {
  107. conf.Bed = true
  108. }
  109. if data&(one<<1) > 0 {
  110. conf.Gas = true
  111. }
  112. if data&(one<<2) > 0 {
  113. conf.WarmGas = true
  114. }
  115. if data&(one<<3) > 0 {
  116. conf.Broadband = true
  117. }
  118. if data&(one<<4) > 0 {
  119. conf.Refragerator = true
  120. }
  121. if data&(one<<5) > 0 {
  122. conf.Wardobe = true
  123. }
  124. if data&(one<<6) > 0 {
  125. conf.Sofa = true
  126. }
  127. if data&(one<<7) > 0 {
  128. conf.Aircondition = true
  129. }
  130. if data&(one<<8) > 0 {
  131. conf.Tv = true
  132. }
  133. if data&(one<<9) > 0 {
  134. conf.Heater = true
  135. }
  136. if data&(one<<10) > 0 {
  137. conf.Warshing = true
  138. }
  139. return conf
  140. }
  141. func SpecialConfToBool(data int64) pb_v1.HouseRentSpecialConf {
  142. conf := pb_v1.HouseRentSpecialConf{}
  143. one := int64(1)
  144. if data&one > 0 {
  145. conf.IntelligentLock = true
  146. }
  147. if data&(one<<1) > 0 {
  148. conf.Wifi = true
  149. }
  150. if data&(one<<2) > 0 {
  151. conf.Metro = true
  152. }
  153. if data&(one<<3) > 0 {
  154. conf.ParkSpace = true
  155. }
  156. if data&(one<<4) > 0 {
  157. conf.IndependentWc = true
  158. }
  159. if data&(one<<5) > 0 {
  160. conf.PrivateBalcony = true
  161. }
  162. if data&(one<<6) > 0 {
  163. conf.FirstRent = true
  164. }
  165. return conf
  166. }
  167. func BaseConfToBitmap(conf *pb_v1.HouseRentBaseConf) int64 {
  168. ret := int64(0)
  169. one := int64(1)
  170. if conf.Bed {
  171. ret = ret | (one)
  172. }
  173. if conf.Gas {
  174. ret = ret | (one << 1)
  175. }
  176. if conf.WarmGas {
  177. ret = ret | (one << 2)
  178. }
  179. if conf.Broadband {
  180. ret = ret | (one << 3)
  181. }
  182. if conf.Refragerator {
  183. ret = ret | (one << 4)
  184. }
  185. if conf.Wardobe {
  186. ret = ret | (one << 5)
  187. }
  188. if conf.Sofa {
  189. ret = ret | (one << 6)
  190. }
  191. if conf.Aircondition {
  192. ret = ret | (one << 7)
  193. }
  194. if conf.Tv {
  195. ret = ret | (one << 8)
  196. }
  197. if conf.Heater {
  198. ret = ret | (one << 9)
  199. }
  200. if conf.Warshing {
  201. ret = ret | (one << 10)
  202. }
  203. return ret
  204. }
  205. func SpecialConfToBitmap(conf *pb_v1.HouseRentSpecialConf) int64 {
  206. ret := int64(0)
  207. one := int64(1)
  208. if conf.IntelligentLock {
  209. ret = ret | (one)
  210. }
  211. if conf.Wifi {
  212. ret = ret | (one << 1)
  213. }
  214. if conf.Metro {
  215. ret = ret | (one << 2)
  216. }
  217. if conf.ParkSpace {
  218. ret = ret | (one << 3)
  219. }
  220. if conf.IndependentWc {
  221. ret = ret | (one << 4)
  222. }
  223. if conf.PrivateBalcony {
  224. ret = ret | (one << 5)
  225. }
  226. if conf.FirstRent {
  227. ret = ret | (one << 6)
  228. }
  229. return ret
  230. }
  231. //
  232. func HouseRentApply(ctx context.Context, req *pb_v1.HouseRentApplyRequest) (reply *pb_v1.HouseRentApplyReply, err error) {
  233. reply = &pb_v1.HouseRentApplyReply{}
  234. // 捕获各个task中的异常并返回给调用者
  235. defer func() {
  236. if r := recover(); r != nil {
  237. err = fmt.Errorf("%+v", r)
  238. e := &status.Status{}
  239. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  240. logger.Error("err",
  241. zap.String("system_err", err.Error()),
  242. zap.Stack("stacktrace"))
  243. }
  244. }
  245. }()
  246. err = checkHouseRentApplyParam(req)
  247. if err != nil {
  248. return nil, err
  249. }
  250. old := dbmodel.THouseRent{}
  251. oldWhere := map[string]interface{}{
  252. "garden_id": req.GardenId,
  253. "house_id": req.HouseId,
  254. "approve_status <": 4,
  255. }
  256. oldCount, err := old.Count(database.DB(), oldWhere, nil)
  257. if err != nil {
  258. return nil, errors.DataBaseError
  259. }
  260. if oldCount > 0 {
  261. return nil, status.Error(10003, "已存在相同记录")
  262. }
  263. gardenInfo, err := getGardenInfo(req.GardenId)
  264. if err != nil {
  265. return nil, err
  266. }
  267. houseInfo, err := house.GetHouseInfo(req.HouseId, req.GardenId)
  268. if err != nil {
  269. return nil, err
  270. }
  271. now := time.Now()
  272. hasList := int32(1)
  273. if !houseInfo.HasLift {
  274. hasList = 2
  275. }
  276. houseRent := dbmodel.THouseRent{
  277. GardenId: req.GardenId,
  278. GardenName: gardenInfo.GardenName,
  279. HouseName: fmt.Sprintf("%v-%v-%v", houseInfo.BuildingNumber, houseInfo.UnitNumber, houseInfo.HouseNumber),
  280. Layer: houseInfo.Layer,
  281. HouseArea: houseInfo.HouseArea,
  282. Diretion: req.Direction,
  283. RoomCount: houseInfo.RoomCount,
  284. HallCount: houseInfo.HallCount,
  285. WcCount: req.WcCount,
  286. Decorating: req.Decorating,
  287. Contacter: req.Contacter,
  288. ContactPhone: req.ContactPhone,
  289. PayTimeType: req.PayTimeType,
  290. RentType: req.RentType,
  291. RoomType: req.RoomType,
  292. RoomArea: req.RoomArea,
  293. RentPrice: req.RentPrice,
  294. Desposit: req.Desposit,
  295. InTime: req.InTime,
  296. ServicePrice: req.ServicePrice,
  297. IntermediaryPrice: req.IntermediaryPrice,
  298. BaseConf: req.BaseConf,
  299. SpecialConf: req.SpecialConf,
  300. HousePic: utils.StringJoin(req.HousePic, ";"),
  301. CertPic: utils.StringJoin(req.CertPic, ";"),
  302. ProvinceCode: gardenInfo.ProvinceCode,
  303. CityCode: gardenInfo.CityCode,
  304. AreaCode: gardenInfo.AreaCode,
  305. StreetCode: gardenInfo.StreetCode,
  306. HouseholdUid: req.HouseholdUid,
  307. ApproveStatus: HouseholdRentStatusWait,
  308. HasLift: hasList,
  309. //Feedback string `gorm:"column:feedback" json:"feedback"`
  310. CreatedAt: now,
  311. UpdatedAt: now,
  312. Desc: req.Desc,
  313. Lat: gardenInfo.Lat,
  314. Lnt: gardenInfo.Lnt,
  315. HouseId: req.HouseId,
  316. UnitId: houseInfo.UnitId,
  317. }
  318. if req.HasLift {
  319. houseRent.HasLift = 1
  320. }
  321. if req.Approve {
  322. houseRent.ApproveStatus = HouseholdRentStatusSuccess
  323. houseRent.ApprovedAt = now
  324. }
  325. db := database.DB().Begin()
  326. err = houseRent.Insert(db)
  327. if err != nil {
  328. db.Rollback()
  329. if strings.Contains(strings.ToLower(err.Error()), "duplicate") {
  330. return nil, status.Error(10003, "房屋不能重复发布")
  331. }
  332. return nil, errors.DataBaseError
  333. }
  334. reply.Id = houseRent.ID
  335. increase := 0
  336. if req.Approve {
  337. increase = 1
  338. }
  339. err = houseRentSync(houseRent, true, req.GardenId, increase)
  340. if err != nil {
  341. db.Rollback()
  342. return nil, err
  343. }
  344. db.Commit()
  345. if !req.Approve {
  346. mreq := pb_v1.SystemMsgAddRequest{
  347. GardenId: req.GardenId,
  348. Content: "新的房屋租赁申请",
  349. Code: SystemMsgCodeRent,
  350. }
  351. pb.Garden.SystemMsgAdd(ctx, &mreq)
  352. }
  353. return reply, nil
  354. }