bind_vehicle.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. package charge
  2. import (
  3. "git.getensh.com/common/gopkgs/database"
  4. "google.golang.org/grpc/status"
  5. "gorm.io/gorm"
  6. "property-garden/errors"
  7. "property-garden/impl/v1/charge_utils"
  8. "property-garden/impl/v1/vehicle"
  9. dbmodel "property-garden/model"
  10. pb_v1 "property-garden/pb/v1"
  11. "time"
  12. )
  13. type vehicleBindParam struct {
  14. chargeInfo *dbmodel.TChargeConf
  15. vehicleInfo dbmodel.TVehicle
  16. }
  17. type vehiclesBindParam struct {
  18. chargeInfo *dbmodel.TChargeConf
  19. vehicleIds []int64
  20. vehicleInfoM map[int64]dbmodel.TVehicle
  21. }
  22. func bindChargeVehicles(req *pb_v1.ChargeBindRequest, param *vehiclesBindParam, db *gorm.DB, dbname string) (error) {
  23. now := time.Now()
  24. arrayChargeVehicle := []dbmodel.TChargeBind{}
  25. // 去重
  26. olds := map[int64]bool{}
  27. binded := dbmodel.NewChargeBind(dbname)
  28. where := map[string]interface{}{
  29. "obj_id in": param.vehicleIds,
  30. "obj_type": charge_utils.ObjTypeVehicle,
  31. }
  32. where["charge_type"] = param.chargeInfo.ChargeType
  33. bindedList, err := binded.List(database.DB(), where, nil, -1, -1)
  34. if err != nil {
  35. return errors.DataBaseError
  36. }
  37. for _, v := range bindedList {
  38. olds[v.ObjId] = true
  39. }
  40. for _, v := range param.vehicleIds {
  41. // 已存在不重复添加
  42. if olds[v] {
  43. continue
  44. }
  45. if param.vehicleInfoM[v].BindType == 2 {
  46. //continue
  47. return status.Error(10003, "车辆已绑定已售车位,该车不能绑定该费用")
  48. }
  49. tmp := dbmodel.TChargeBind{
  50. ObjId: v,
  51. ChargeId: req.ChargeId,
  52. Start: req.Start,
  53. End: req.End,
  54. CreatedAt: now,
  55. UpdatedAt: now,
  56. ChargeType: param.chargeInfo.ChargeType,
  57. ObjType: charge_utils.ObjTypeVehicle,
  58. //ChargeBasis:param.chargeInfo.ChargeBasis,
  59. ObjName:param.vehicleInfoM[v].PlateNo,
  60. }
  61. if param.chargeInfo.ChargeBasis == charge_utils.ChargeBasisSelf {
  62. tmp.CustomFee = req.VehicleFee
  63. }
  64. tmp.UniqFlag.Int32 = 1
  65. tmp.UniqFlag.Valid = true
  66. arrayChargeVehicle = append(arrayChargeVehicle, tmp)
  67. }
  68. if len(arrayChargeVehicle) == 0 {
  69. return nil
  70. }
  71. p := dbmodel.NewChargeBind(dbname)
  72. err = p.InsertMulti(db, &arrayChargeVehicle)
  73. if err != nil {
  74. return errors.DataBaseError
  75. }
  76. return err
  77. }
  78. func bindChargeVehicle(req *pb_v1.ChargeBindRequest, param *vehicleBindParam, db *gorm.DB, dbname string) error {
  79. now := time.Now()
  80. // 去重
  81. binded := dbmodel.NewChargeBind(dbname)
  82. where := map[string]interface{}{
  83. "obj_id": param.vehicleInfo.ID,
  84. "obj_type": charge_utils.ObjTypeVehicle,
  85. }
  86. where["charge_type"] = param.chargeInfo.ChargeType
  87. count, err := binded.Count(database.DB(), where, nil)
  88. if err != nil {
  89. return errors.DataBaseError
  90. }
  91. if count > 0 {
  92. return status.Error(10003, "操作失败,该车辆已绑定费用")
  93. }
  94. bind := dbmodel.TChargeBind{
  95. ObjId: param.vehicleInfo.ID,
  96. ChargeId: req.ChargeId,
  97. Start: req.Start,
  98. End: req.End,
  99. CreatedAt: now,
  100. UpdatedAt: now,
  101. ChargeType: param.chargeInfo.ChargeType,
  102. ObjType: charge_utils.ObjTypeVehicle,
  103. //ChargeBasis:param.chargeInfo.ChargeBasis,
  104. ObjName:param.vehicleInfo.PlateNo,
  105. }
  106. if param.chargeInfo.ChargeBasis == charge_utils.ChargeBasisSelf {
  107. bind.CustomFee = req.VehicleFee
  108. }
  109. bind.UniqFlag.Int32 = 1
  110. bind.UniqFlag.Valid = true
  111. bind.SetTable(dbname)
  112. err = bind.Insert(db)
  113. if err != nil {
  114. return errors.DataBaseError
  115. }
  116. return err
  117. }
  118. func getVehicleInfo(id int64, dbname string) (dbmodel.TVehicle, error) {
  119. ret := dbmodel.TVehicle{}
  120. if id == 0 {
  121. return ret, nil
  122. }
  123. p := dbmodel.NewVehicle(dbname)
  124. where := map[string]interface{}{
  125. "id": id,
  126. }
  127. err := p.Find(database.DB(), where)
  128. if err != nil && err != gorm.ErrRecordNotFound{
  129. return ret, errors.DataBaseError
  130. }
  131. ret = *p
  132. return ret, nil
  133. }
  134. func getVehicleInfos(ids []int64, dbname string) (map[int64]dbmodel.TVehicle, error) {
  135. ret := map[int64]dbmodel.TVehicle{}
  136. if len(ids) == 0 {
  137. return ret, nil
  138. }
  139. p := dbmodel.NewVehicle(dbname)
  140. where := map[string]interface{}{
  141. "id in": ids,
  142. }
  143. list, err := p.List(database.DB(), where, nil, -1, -1)
  144. if err != nil && err != gorm.ErrRecordNotFound{
  145. return ret, errors.DataBaseError
  146. }
  147. for _, v := range list {
  148. ret[v.ID] = v
  149. }
  150. return ret, nil
  151. }
  152. func addByVehicleId(req *pb_v1.ChargeBindRequest, chargeInfo *dbmodel.TChargeConf, db *gorm.DB, dbname string) (reply *pb_v1.ChargeBindReply, err error) {
  153. if (req.Start > 0 && req.End == 0) || (req.Start == 0 && req.End > 0) {
  154. return nil, status.Error(10003, "开始和结束时间只能同时存在或同时为空")
  155. }
  156. if len(req.ObjIds) == 0 {
  157. return &pb_v1.ChargeBindReply{}, nil
  158. }
  159. vehicleInfo, err := getVehicleInfo(req.ObjIds[0], dbname)
  160. if err != nil {
  161. return nil, err
  162. }
  163. if vehicleInfo.ID == 0 {
  164. return nil, status.Error(10003, "未找到车辆")
  165. }
  166. if vehicleInfo.BindType == vehicle.BindTypeSell {
  167. return nil, status.Error(10003, "该车辆已绑定出售车位,请使用车位费")
  168. }
  169. param := vehicleBindParam{
  170. chargeInfo: chargeInfo,
  171. vehicleInfo: vehicleInfo,
  172. }
  173. err = bindChargeVehicle(req, &param, db, dbname)
  174. return &pb_v1.ChargeBindReply{}, nil
  175. }
  176. func addByVehicleIds(req *pb_v1.ChargeBindRequest, chargeInfo *dbmodel.TChargeConf, db *gorm.DB, dbname string) (reply *pb_v1.ChargeBindReply, err error) {
  177. if (req.Start > 0 && req.End == 0) || (req.Start == 0 && req.End > 0) {
  178. return nil, status.Error(10003, "开始和结束时间只能同时存在或同时为空")
  179. }
  180. if len(req.ObjIds) == 0 {
  181. return &pb_v1.ChargeBindReply{}, nil
  182. }
  183. vehicleInfoM, err := getVehicleInfos(req.ObjIds, dbname)
  184. if err != nil {
  185. return nil, err
  186. }
  187. if len(vehicleInfoM) == 0 {
  188. return nil, status.Error(10003, "未找到车辆")
  189. }
  190. param := vehiclesBindParam{
  191. chargeInfo: chargeInfo,
  192. vehicleInfoM: vehicleInfoM,
  193. }
  194. vehicleIds := []int64{}
  195. for vehicleId, _ := range vehicleInfoM {
  196. vehicleIds = append(vehicleIds, vehicleId)
  197. }
  198. length := len(vehicleIds)
  199. // 分批
  200. for i := 0; i < length; i += 100 {
  201. if i+100 >= length {
  202. param.vehicleIds = vehicleIds[i:]
  203. err = bindChargeVehicles(req, &param, db, dbname)
  204. } else {
  205. param.vehicleIds = vehicleIds[i:i+100]
  206. err = bindChargeVehicles(req, &param, db, dbname)
  207. }
  208. if err != nil {
  209. return nil, err
  210. }
  211. }
  212. return &pb_v1.ChargeBindReply{}, nil
  213. }