bill_add.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package charge
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "gorm.io/gorm"
  6. "property-garden/errors"
  7. "property-garden/impl/v1/charge_utils"
  8. dbmodel "property-garden/model"
  9. "time"
  10. )
  11. type BillAddParam struct {
  12. HouseInfo *dbmodel.THouseUnitBuilding
  13. PowerInfo *dbmodel.TChargePower
  14. VehicleInfo *dbmodel.TVehicle
  15. SpaceInfo *dbmodel.TSpacePark
  16. ChargeInfo *dbmodel.TChargeConf
  17. BindInfo *dbmodel.TChargeBind
  18. ObjType int32
  19. Months int64
  20. }
  21. func getTimeMonthTimestamp(t time.Time) int64 {
  22. return time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, t.Location()).Unix()
  23. }
  24. func getTimeDayTimestamp(t time.Time) int64 {
  25. return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location()).Unix()
  26. }
  27. func HouseOnceShouldPay(chargeInfo *dbmodel.TChargeConf, houseInfo *dbmodel.THouseUnitBuilding, bindInfo *dbmodel.TChargeBind) (int64, string) {
  28. // 固定费用
  29. chargeDesc := dbmodel.ChargeDesc{
  30. ChargeBasis: chargeInfo.ChargeBasis,
  31. UnitPrice: chargeInfo.UnitPrice,
  32. FixAmount: chargeInfo.FixAmount,
  33. FixAmountName: chargeInfo.FixAmountName,
  34. }
  35. if chargeInfo.ChargeBasis == charge_utils.ChargeBasisFix {
  36. amount := chargeInfo.FixAmount
  37. bytes, _ := json.Marshal(chargeDesc)
  38. return amount, string(bytes)
  39. }
  40. // 单价计费
  41. amount := int64(0)
  42. if chargeInfo.ChargeBasis == charge_utils.ChargeBasisArea {
  43. chargeDesc.ObjArea = houseInfo.HouseArea
  44. amount = int64(float64(chargeInfo.UnitPrice) * houseInfo.HouseArea)
  45. }
  46. if chargeInfo.ChargeBasis == charge_utils.ChargeBasisUsedArea {
  47. chargeDesc.ObjArea = houseInfo.HouseUsedArea
  48. amount = int64(float64(chargeInfo.UnitPrice) * houseInfo.HouseUsedArea)
  49. }
  50. if chargeInfo.ChargeBasis == charge_utils.ChargeBasisSelf {
  51. chargeDesc.CustomFee = bindInfo.CustomFee
  52. amount = bindInfo.CustomFee
  53. }
  54. if chargeInfo.FixAmount > 0 {
  55. amount += chargeInfo.FixAmount
  56. }
  57. bytes, _ := json.Marshal(chargeDesc)
  58. return amount, string(bytes)
  59. }
  60. func HousePowerShouldPay(chargeInfo *dbmodel.TChargeConf, powerInfo *dbmodel.TChargePower) (int64, string) {
  61. chargeDesc := dbmodel.ChargeDesc{
  62. ChargeBasis: chargeInfo.ChargeBasis,
  63. UnitPrice: chargeInfo.UnitPrice,
  64. FixAmount: chargeInfo.FixAmount,
  65. FixAmountName: chargeInfo.FixAmountName,
  66. }
  67. amount := int64((powerInfo.CurrentAmount - powerInfo.LastAmount) * float64(chargeInfo.UnitPrice))
  68. chargeDesc.PowerLastUsed = powerInfo.LastAmount
  69. chargeDesc.PowerCurrentUsed = powerInfo.CurrentAmount
  70. if chargeInfo.FixAmount > 0 {
  71. months := charge_utils.GetMonths(powerInfo.Start, powerInfo.End)
  72. amount += amount * months
  73. }
  74. bytes, _ := json.Marshal(chargeDesc)
  75. return amount, string(bytes)
  76. }
  77. func BillAddHouseOnce(param *BillAddParam, db *gorm.DB, dbname string) error {
  78. amount, desc := HouseOnceShouldPay(param.ChargeInfo, param.HouseInfo, param.BindInfo)
  79. bill := dbmodel.TChargeBill{
  80. ObjType: charge_utils.ObjTypeHouse,
  81. ObjId: param.HouseInfo.ID,
  82. ChargeId: param.ChargeInfo.ID,
  83. ChargeBindId: param.BindInfo.ID,
  84. ChargeTimeType: param.ChargeInfo.ChargeTimeType,
  85. ChargeDesc: desc,
  86. Comment: "",
  87. Amount: amount,
  88. ChargePowerId: 0,
  89. ChargeType: param.ChargeInfo.ChargeType,
  90. ChargeName: param.ChargeInfo.ChargeName,
  91. Uid: 0,
  92. PayMode: 0,
  93. PackageId: 0,
  94. ChargeEnd: param.BindInfo.Start,
  95. ChargeStart: param.BindInfo.Start,
  96. OrderId: "",
  97. PayTime: 0,
  98. HouseId: param.HouseInfo.ID,
  99. ObjName: fmt.Sprintf("%v-%v-%v", param.HouseInfo.BuildingNumber, param.HouseInfo.UnitNumber, param.HouseInfo.HouseNumber),
  100. LateFee: 0,
  101. Status: charge_utils.PayStatusUnPay,
  102. }
  103. bill.SetTable(dbname)
  104. err := bill.Insert(db)
  105. if err != nil {
  106. return errors.DataBaseError
  107. }
  108. bind := dbmodel.NewChargeBind(dbname)
  109. where := map[string]interface{}{
  110. "id": param.BindInfo.ID,
  111. }
  112. values := map[string]interface{}{
  113. "bill_last_time": param.BindInfo.Start,
  114. }
  115. err = bind.Update(db, where, values)
  116. if err != nil {
  117. return errors.DataBaseError
  118. }
  119. return nil
  120. }
  121. func BillAddHousePower(param *BillAddParam, db *gorm.DB, dbname string) error {
  122. //now := time.Now()
  123. amount, desc := HousePowerShouldPay(param.ChargeInfo, param.PowerInfo)
  124. if amount <= 0 {
  125. return nil
  126. }
  127. bill := dbmodel.TChargeBill{
  128. ObjType: charge_utils.ObjTypeHouse,
  129. ObjId: param.HouseInfo.ID,
  130. ChargeId: param.ChargeInfo.ID,
  131. ChargeBindId: param.BindInfo.ID,
  132. ChargeTimeType: param.ChargeInfo.ChargeTimeType,
  133. ChargeDesc: desc,
  134. Comment: "",
  135. Amount: amount,
  136. ChargePowerId: param.PowerInfo.ID,
  137. ChargeType: param.ChargeInfo.ChargeType,
  138. ChargeName: param.ChargeInfo.ChargeName,
  139. Uid: 0,
  140. PayMode: 0,
  141. PackageId: 0,
  142. ChargeEnd: param.PowerInfo.End,
  143. ChargeStart: param.PowerInfo.Start,
  144. OrderId: "",
  145. PayTime: 0,
  146. HouseId: param.HouseInfo.ID,
  147. ObjName: fmt.Sprintf("%v-%v-%v", param.HouseInfo.BuildingNumber, param.HouseInfo.UnitNumber, param.HouseInfo.HouseNumber),
  148. LateFee: 0,
  149. Status: charge_utils.PayStatusUnPay,
  150. }
  151. bill.SetTable(dbname)
  152. err := bill.Insert(db)
  153. if err != nil {
  154. return errors.DataBaseError
  155. }
  156. bind := dbmodel.NewChargeBind(dbname)
  157. where := map[string]interface{}{
  158. "id": param.BindInfo.ID,
  159. }
  160. values := map[string]interface{}{
  161. "bill_last_time": param.PowerInfo.End,
  162. }
  163. err = bind.Update(db, where, values)
  164. if err != nil {
  165. return errors.DataBaseError
  166. }
  167. power := dbmodel.NewChargePower(dbname)
  168. where = map[string]interface{}{
  169. "id": param.PowerInfo.ID,
  170. }
  171. values = map[string]interface{}{
  172. "bill_id": bill.ID,
  173. }
  174. err = power.Update(db, where, values)
  175. if err != nil {
  176. return errors.DataBaseError
  177. }
  178. return nil
  179. }