charge_del_bill_list.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package charge
  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. "property-garden/impl/v1/charge_utils"
  14. dbmodel "property-garden/model"
  15. pb_v1 "property-garden/pb/v1"
  16. "property-garden/utils"
  17. "time"
  18. )
  19. func checkChargeDelBillListParam(req *pb_v1.ChargeDelBillListRequest) error {
  20. switch {
  21. case req.GardenId == 0:
  22. return status.Error(10003, "小区不能为空")
  23. }
  24. if req.Page == 0 {
  25. req.Page = 1
  26. }
  27. if req.PageSize == 0 {
  28. req.PageSize = 10
  29. }
  30. return nil
  31. }
  32. type HouseholdInfo struct {
  33. Name string
  34. Phone string
  35. }
  36. func GetHouseholdInfoFromHouse(houseIds []int64, uids []int64, dbname string) (map[int64]HouseholdInfo, error) {
  37. if len(houseIds) == 0 && len(uids) == 0 {
  38. return map[int64]HouseholdInfo{}, nil
  39. }
  40. p := dbmodel.NewHouseApprovedGarden(dbname)
  41. where := map[string]interface{}{
  42. "house_id in": houseIds,
  43. "user_type": 1,
  44. }
  45. if len(uids) > 0 {
  46. where = map[string]interface{}{
  47. "uid in": uids,
  48. }
  49. }
  50. list, err := p.List(database.DB(), where, nil, -1, -1)
  51. if err != nil {
  52. return nil, errors.DataBaseError
  53. }
  54. if len(houseIds) > 0 {
  55. m := map[int64]HouseholdInfo{}
  56. for _, v := range list {
  57. m[v.HouseId] = HouseholdInfo{Name: v.Name, Phone: v.Phone}
  58. }
  59. return m, nil
  60. }
  61. m := map[int64]HouseholdInfo{}
  62. for _, v := range list {
  63. m[v.Uid] = HouseholdInfo{Name: v.Name, Phone: v.Phone}
  64. }
  65. return m, nil
  66. }
  67. func GetHouseholdInfoFromSpace(objIds []int64, dbname string) (map[int64]HouseholdInfo, error) {
  68. if len(objIds) == 0 {
  69. return map[int64]HouseholdInfo{}, nil
  70. }
  71. space := dbmodel.NewParkSpace(dbname)
  72. where := map[string]interface{}{
  73. "id in": objIds,
  74. "house_id >": 0,
  75. }
  76. spaceList, err := space.List(database.DB(), where, nil, -1, -1)
  77. if err != nil {
  78. return nil, errors.DataBaseError
  79. }
  80. spaceHouseM := map[int64]int64{}
  81. houseIds := []int64{}
  82. for _, v := range spaceList {
  83. spaceHouseM[v.ID] = v.HouseId
  84. houseIds = append(houseIds, v.HouseId)
  85. }
  86. householdInfosM, err := GetHouseholdInfoFromHouse(houseIds, nil, dbname)
  87. if err != nil {
  88. return nil, errors.DataBaseError
  89. }
  90. ret := map[int64]HouseholdInfo{}
  91. for spaceId, houseId := range spaceHouseM {
  92. ret[spaceId] = householdInfosM[houseId]
  93. }
  94. return ret, nil
  95. }
  96. func GethouseholdInfoFromVehicle(vehicleIds []int64, dbname string) (map[int64]HouseholdInfo, error) {
  97. if len(vehicleIds) == 0 {
  98. return map[int64]HouseholdInfo{}, nil
  99. }
  100. vehicle := dbmodel.NewVehicle(dbname)
  101. where := map[string]interface{}{
  102. "id in": vehicleIds,
  103. }
  104. vlist, err := vehicle.List(database.DB(), where, nil, -1, -1)
  105. if err != nil {
  106. return nil, errors.DataBaseError
  107. }
  108. vehicleUserM := map[int64]int64{}
  109. uids := []int64{}
  110. for _, v := range vlist {
  111. vehicleIds[v.ID] = v.HouseholdUid
  112. uids = append(uids, v.HouseholdUid)
  113. }
  114. householdInfosM, err := GetHouseholdInfoFromHouse(nil, uids, dbname)
  115. if err != nil {
  116. return nil, errors.DataBaseError
  117. }
  118. ret := map[int64]HouseholdInfo{}
  119. for vehicleId, uid := range vehicleUserM {
  120. ret[vehicleId] = householdInfosM[uid]
  121. }
  122. return ret, nil
  123. }
  124. func ChargeDelBillList(ctx context.Context, req *pb_v1.ChargeDelBillListRequest) (reply *pb_v1.ChargeDelBillListReply, err error) {
  125. reply = &pb_v1.ChargeDelBillListReply{}
  126. // 捕获各个task中的异常并返回给调用者
  127. defer func() {
  128. if r := recover(); r != nil {
  129. err = fmt.Errorf("%+v", r)
  130. e := &status.Status{}
  131. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  132. logger.Error("err",
  133. zap.String("system_err", err.Error()),
  134. zap.Stack("stacktrace"))
  135. }
  136. }
  137. }()
  138. // 参数检查
  139. err = checkChargeDelBillListParam(req)
  140. if err != nil {
  141. return nil, err
  142. }
  143. dbname := utils.GetGardenDbName(req.GardenId)
  144. bill := dbmodel.NewChargeBillDel(dbname)
  145. where := map[string]interface{}{}
  146. if req.ChargeType > 0 {
  147. where["charge_type"] = req.ChargeType
  148. }
  149. if req.ObjName != "" {
  150. where["obj_name"] = req.ObjName
  151. }
  152. if req.ChargeName != "" {
  153. where["charge_name"] = req.ChargeName
  154. }
  155. if req.ObjType > 0 {
  156. where["obj_type"] = req.ObjType
  157. }
  158. // 账单数量
  159. reply.Total, err = bill.Count(database.DB(), where, nil)
  160. if err != nil {
  161. return nil, errors.DataBaseError
  162. }
  163. if reply.Total == 0 {
  164. return reply, nil
  165. }
  166. reply.Page = req.Page
  167. chargeInfoM, err := charge_utils.GetAllChargeInfos(dbname)
  168. if err != nil {
  169. return nil, err
  170. }
  171. // 账单列表
  172. list, err := bill.List(database.DB(), where, nil, int(req.Page), int(req.PageSize))
  173. if err != nil {
  174. return nil, errors.DataBaseError
  175. }
  176. vehicleIds, spaceIds, houseIds := []int64{}, []int64{}, []int64{}
  177. for _, v := range list {
  178. switch v.ObjType {
  179. case charge_utils.ObjTypeHouse:
  180. houseIds = append(houseIds, v.ObjId)
  181. case charge_utils.ObjTypeSpace:
  182. spaceIds = append(spaceIds, v.ObjId)
  183. case charge_utils.ObjTypeVehicle:
  184. vehicleIds = append(vehicleIds, v.ObjId)
  185. }
  186. }
  187. vHouseholdM, err := GethouseholdInfoFromVehicle(vehicleIds, dbname)
  188. if err != nil {
  189. return nil, errors.DataBaseError
  190. }
  191. sHouseholdM, err := GetHouseholdInfoFromSpace(spaceIds, dbname)
  192. if err != nil {
  193. return nil, errors.DataBaseError
  194. }
  195. hHouseholdM, err := GetHouseholdInfoFromHouse(houseIds, nil, dbname)
  196. if err != nil {
  197. return nil, errors.DataBaseError
  198. }
  199. reply.List = make([]*pb_v1.ChargeDelBillItem, len(list))
  200. for i, v := range list {
  201. chargeDesc := dbmodel.ChargeDesc{}
  202. json.Unmarshal([]byte(v.ChargeDesc), &chargeDesc)
  203. chargeInfo := chargeInfoM[v.ChargeId]
  204. lfee, _ := charge_utils.GetLateFee(v.Amount, v.ChargeEnd, &chargeInfo)
  205. item := &pb_v1.ChargeDelBillItem{
  206. Start: time.Unix(v.ChargeStart, 0).Format("2006-01-02"),
  207. End: time.Unix(v.ChargeEnd, 0).Format("2006-01-02"),
  208. ChargeType: v.ChargeType,
  209. ChargeName: v.ChargeName,
  210. Amount: v.Amount,
  211. LastUsed: chargeDesc.PowerLastUsed,
  212. CurrentUsed: chargeDesc.PowerCurrentUsed,
  213. LateFee: lfee,
  214. BillId: v.ID,
  215. ChargeDesc: charge_utils.GetChargeDesc(chargeDesc),
  216. PayTime: time.Unix(v.PayTime, 0).Format("2006-01-02 15:04:05"),
  217. PayType: v.PayType,
  218. ObjName: v.ObjName,
  219. ObjType: v.ObjType,
  220. DelAt: v.DelAt.Format("2006-01-02 15:04:05"),
  221. DelReason: v.DelReason,
  222. }
  223. switch v.ObjType {
  224. case charge_utils.ObjTypeHouse:
  225. item.HouseholdName = hHouseholdM[v.ObjId].Name
  226. item.HouseholdPhone = hHouseholdM[v.ObjId].Phone
  227. case charge_utils.ObjTypeSpace:
  228. item.HouseholdName = sHouseholdM[v.ObjId].Name
  229. item.HouseholdPhone = sHouseholdM[v.ObjId].Phone
  230. case charge_utils.ObjTypeVehicle:
  231. item.HouseholdName = vHouseholdM[v.ObjId].Name
  232. item.HouseholdPhone = vHouseholdM[v.ObjId].Phone
  233. }
  234. reply.List[i] = item
  235. }
  236. return reply, nil
  237. }