charge_infos.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. package charge_utils
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "git.getensh.com/common/gopkgs/database"
  7. "google.golang.org/grpc/status"
  8. "gorm.io/gorm"
  9. "property-garden/errors"
  10. dbmodel "property-garden/model"
  11. "property-garden/parser"
  12. "property-garden/pb"
  13. pb_v1 "property-garden/pb/v1"
  14. "strings"
  15. "time"
  16. )
  17. func GetGardenInfos(ids []int64) (map[int64]pb_v1.GardenItem, error) {
  18. ret := map[int64]pb_v1.GardenItem{}
  19. if len(ids) == 0 {
  20. return ret, nil
  21. }
  22. mreq := pb_v1.GardenInfosRequest{Ids: ids}
  23. mreply, err := pb.System.GardenInfos(context.Background(), &mreq)
  24. if err != nil {
  25. return ret, err
  26. }
  27. for _, v := range mreply.List {
  28. ret[v.Id] = *v
  29. }
  30. return ret, nil
  31. }
  32. func GetCompanyInfo(id int64) (*pb_v1.CompanyInfoReply, error) {
  33. mreq := pb_v1.CompanyInfoRequest{Id: id}
  34. mreply, err := pb.Company.CompanyInfo(context.Background(), &mreq)
  35. if err != nil {
  36. return nil, err
  37. }
  38. return mreply, nil
  39. }
  40. func CheckMchPayMode(gardenId int64) (string, error) {
  41. infoM, err := GetGardenInfos([]int64{gardenId})
  42. if err != nil {
  43. return "", err
  44. }
  45. info, ok := infoM[gardenId]
  46. if !ok {
  47. return "", status.Error(10003, "获取小区信息失败")
  48. }
  49. if info.PayMode == 1 {
  50. return "", status.Error(10003, "暂不支持线上缴费")
  51. }
  52. if info.PayMode == 2 {
  53. if info.MchId == "" {
  54. return "", status.Error(10003, "暂不支持线上缴费")
  55. }
  56. return info.MchId, nil
  57. }
  58. return parser.Conf.ThirdParty.Wx.AppletMchId, nil
  59. }
  60. func GetHouseInfos(ids []int64, dbname string) (map[int64]dbmodel.THouseUnitBuilding, error) {
  61. ret := map[int64]dbmodel.THouseUnitBuilding{}
  62. if len(ids) == 0 {
  63. return ret, nil
  64. }
  65. p := dbmodel.NewHouseUnitBuilding(dbname)
  66. where := map[string]interface{}{
  67. "id in": ids,
  68. }
  69. list, err := p.List(database.DB(), where, nil, -1, -1)
  70. if err != nil {
  71. return nil, errors.DataBaseError
  72. }
  73. for _, v := range list {
  74. ret[v.ID] = v
  75. }
  76. return ret, nil
  77. }
  78. func GetChargeInfos(ids []int64, dbname string) (map[int64]dbmodel.TChargeConf, error) {
  79. ret := map[int64]dbmodel.TChargeConf{}
  80. if len(ids) == 0 {
  81. return ret, nil
  82. }
  83. p := dbmodel.NewChargeConf(dbname)
  84. where := map[string]interface{}{
  85. "id in": ids,
  86. }
  87. list, err := p.List(database.DB(), where, nil, -1, -1)
  88. if err != nil {
  89. return nil, errors.DataBaseError
  90. }
  91. for _, v := range list {
  92. ret[v.ID] = v
  93. }
  94. return ret, nil
  95. }
  96. func GetAllChargeInfos(dbname string) (map[int64]dbmodel.TChargeConf, error) {
  97. ret := map[int64]dbmodel.TChargeConf{}
  98. p := dbmodel.NewChargeConf(dbname)
  99. list, err := p.List(database.DB(), nil, nil, -1, -1)
  100. if err != nil {
  101. return nil, errors.DataBaseError
  102. }
  103. for _, v := range list {
  104. ret[v.ID] = v
  105. }
  106. return ret, nil
  107. }
  108. func IsChargeBindObj(chargeId int64, dbname string) (bool, error) {
  109. p := dbmodel.NewChargeBind(dbname)
  110. where := map[string]interface{}{
  111. "charge_id": chargeId,
  112. }
  113. count, err := p.Count(database.DB(), where, nil)
  114. if err != nil {
  115. return false, errors.DataBaseError
  116. }
  117. if count > 0 {
  118. return true, nil
  119. }
  120. return false, nil
  121. }
  122. func IsObjBindCharge(objId int64, objType int32, dbname string) (bool, error) {
  123. p := dbmodel.NewChargeBind(dbname)
  124. where := map[string]interface{}{
  125. "obj_id": objId,
  126. "obj_type": objType,
  127. }
  128. count, err := p.Count(database.DB(), where, nil)
  129. if err != nil {
  130. return false, errors.DataBaseError
  131. }
  132. if count > 0 {
  133. return true, nil
  134. }
  135. return false, nil
  136. }
  137. func IsObjsBindCharge(objIds []int64, objType int32, dbname string) (bool, error) {
  138. p := dbmodel.NewChargeBind(dbname)
  139. where := map[string]interface{}{
  140. "obj_id in": objIds,
  141. "obj_type": objType,
  142. }
  143. count, err := p.Count(database.DB(), where, nil)
  144. if err != nil {
  145. return false, errors.DataBaseError
  146. }
  147. if count > 0 {
  148. return true, nil
  149. }
  150. return false, nil
  151. }
  152. func CompanyDealIncrease(gardenId int64, amount int64, timestamp int64) error {
  153. mreq := pb_v1.CompanyDealStatisticSetRequest{GardenId: gardenId, Amount: amount, DateTimestamp: timestamp}
  154. _, err := pb.Company.CompanyDealStatisticSet(context.Background(), &mreq)
  155. return err
  156. }
  157. /*
  158. func IsBillPaying(billId int64, dbname string) (bool, string, error) {
  159. p := dbmodel.NewChargeBill(dbname)
  160. where := map[string]interface{}{
  161. "id":billId,
  162. "status":PayStatusPaying,
  163. }
  164. err := p.Find(database.DB(), where)
  165. if err != nil && err != gorm.ErrRecordNotFound{
  166. return false, "", errors.DataBaseError
  167. }
  168. if p.ID == 0 {
  169. return false, "", nil
  170. }
  171. return true, p.OrderId, nil
  172. }
  173. */
  174. /*
  175. func IsPowerPaying(powerId int64, dbname string) (bool, string, error) {
  176. p := dbmodel.NewChargeBill(dbname)
  177. where := map[string]interface{}{
  178. "charge_power_id":powerId,
  179. "status":PayStatusPaying,
  180. }
  181. err := p.Find(database.DB(), where)
  182. if err != nil && err != gorm.ErrRecordNotFound{
  183. return false, "", errors.DataBaseError
  184. }
  185. if p.ID == 0 {
  186. return false, "", nil
  187. }
  188. return true, p.OrderId, nil
  189. }
  190. */
  191. /*
  192. func IsBillsPaying(billIds []int64, dbname string) (bool, string, error) {
  193. p := dbmodel.NewChargeBill(dbname)
  194. where := map[string]interface{}{
  195. "id in":billIds,
  196. "status":PayStatusPaying,
  197. }
  198. err := p.Find(database.DB(), where)
  199. if err != nil && err != gorm.ErrRecordNotFound{
  200. return false, "", errors.DataBaseError
  201. }
  202. if p.ID == 0 {
  203. return false, "", nil
  204. }
  205. return true, p.OrderId, nil
  206. }
  207. */
  208. /*
  209. func IsChargesPaying(bindIds []int64, dbname string) (bool, string, error) {
  210. p := dbmodel.NewChargeBill(dbname)
  211. where := map[string]interface{}{
  212. "charge_bind_id in":bindIds,
  213. "status":PayStatusPaying,
  214. }
  215. err := p.Find(database.DB(), where)
  216. if err != nil && err != gorm.ErrRecordNotFound{
  217. return false, "", errors.DataBaseError
  218. }
  219. if p.ID == 0 {
  220. return false, "", nil
  221. }
  222. return true, p.OrderId, nil
  223. }
  224. */
  225. /*
  226. func IsObjPaying(objId int64, objType int32, dbname string) (bool, string, error) {
  227. p := dbmodel.NewChargeBill(dbname)
  228. where := map[string]interface{}{
  229. "obj_id":objId,
  230. "obj_type":objType,
  231. "status":PayStatusPaying,
  232. }
  233. err := p.Find(database.DB(), where)
  234. if err != nil && err != gorm.ErrRecordNotFound{
  235. return false, "", errors.DataBaseError
  236. }
  237. if p.ID == 0 {
  238. return false, "", nil
  239. }
  240. return true, p.OrderId, nil
  241. }
  242. */
  243. func ThirdpartyCheckPayingOrder(orderNo string, gardenId int64) (string, string, string, int64, int64) {
  244. //TODO 当前只有微信小程序,后续会有其他方式
  245. orderId := fmt.Sprintf("%d-%s", gardenId, orderNo)
  246. mreq := &pb_v1.WxAppletQueryRequest{Order: orderId}
  247. mreply, err := pb.Thirdparty.WxAppletQuery(context.Background(), mreq)
  248. if err != nil {
  249. return "", "", "", 0, 0
  250. }
  251. return mreply.TradeState, mreply.TradeStateDesc, mreply.TransactionId, mreply.PayTime, mreply.Amount
  252. }
  253. func SetOrderPayingDesc(orderNo string, payingDesc string, dbname string) error {
  254. p := dbmodel.NewChargeOrder(dbname)
  255. where := map[string]interface{}{
  256. "order_id": orderNo,
  257. }
  258. values := map[string]interface{}{
  259. "paying_desc": payingDesc,
  260. }
  261. err := p.Update(database.DB(), where, values)
  262. if err != nil {
  263. return errors.DataBaseError
  264. }
  265. return nil
  266. }
  267. func SetOrderPayed(payTime int64, orderNo string, transId string, dbname string) error {
  268. p := dbmodel.NewChargeOrder(dbname)
  269. var err error
  270. // 更新订单
  271. where := map[string]interface{}{
  272. "order_id": orderNo,
  273. }
  274. values := map[string]interface{}{
  275. "status": PayStatusPayed,
  276. "pay_time": payTime,
  277. "transaction_id": transId,
  278. }
  279. db := database.DB().Begin()
  280. defer func() {
  281. if err != nil {
  282. db.Rollback()
  283. }
  284. }()
  285. err = p.Update(db, where, values)
  286. if err != nil {
  287. return errors.DataBaseError
  288. }
  289. // 插入已付费账单表
  290. bill := dbmodel.NewChargeBill(dbname)
  291. where = map[string]interface{}{
  292. "order": orderNo,
  293. }
  294. var list []dbmodel.TChargeBill
  295. list, err = bill.List(database.DB(), where, nil, -1, -1, "")
  296. if err != nil {
  297. return errors.DataBaseError
  298. }
  299. for i, _ := range list {
  300. list[i].Status = PayStatusPayed
  301. list[i].PayTime = payTime
  302. }
  303. bill.SetTablePayed(dbname)
  304. err = bill.InsertMulti(db, &list)
  305. if err != nil {
  306. return errors.DataBaseError
  307. }
  308. // 删除待缴表中记录
  309. bill.SetTable(dbname)
  310. err = bill.Delete(db, where)
  311. if err != nil {
  312. return errors.DataBaseError
  313. }
  314. db.Commit()
  315. return nil
  316. }
  317. func IsOrderPaying(orders []string, dbname string, db *gorm.DB) (bool, error) {
  318. if len(orders) == 0 {
  319. return false, nil
  320. }
  321. order := dbmodel.NewChargeOrder(dbname)
  322. where := map[string]interface{}{
  323. "order_id in": orders,
  324. "deleted": 0,
  325. }
  326. count, err := order.Count(db, where, nil)
  327. if err != nil {
  328. return false, errors.DataBaseError
  329. }
  330. return count > 0, nil
  331. }
  332. /*
  333. func CheckPayingOrder(orderNo string, gardenId int64, dbname string) error {
  334. p := dbmodel.NewChargeOrder(dbname)
  335. where := map[string]interface{}{
  336. "order_id":orderNo,
  337. }
  338. err := p.Find(database.DB(), where)
  339. if err != nil && err != gorm.ErrRecordNotFound{
  340. return errors.DataBaseError
  341. }
  342. if p.OrderId == "" {
  343. return status.Error(10003, "订单不存在")
  344. }
  345. if p.Status != PayStatusPaying {
  346. return nil
  347. }
  348. if time.Now().Unix() - p.CreatedAt.Unix() < 60 {
  349. return nil
  350. }
  351. state, desc, transId, payTime, amount := ThirdpartyCheckPayingOrder(orderNo, gardenId)
  352. if state == "" {
  353. return nil
  354. }
  355. if state != "SUCCESS" {
  356. return SetOrderPayingDesc(orderNo, fmt.Sprintf("%s-%s", state, desc), dbname)
  357. }
  358. if p.Amount != amount {
  359. return SetOrderPayingDesc(orderNo, fmt.Sprintf("三方状态已支付,金额不一致%d", amount), dbname)
  360. }
  361. return SetOrderPayed(payTime, orderNo, transId, dbname)
  362. }
  363. */
  364. func PrePayInfo(chargeInfo *dbmodel.TChargeConf, bindInfo *dbmodel.TChargeBind, months int64) (int64, string, string) {
  365. array := []string{}
  366. amount := int64(0)
  367. chargeDesc := dbmodel.ChargeDesc{
  368. ChargeBasis: chargeInfo.ChargeBasis,
  369. UnitPrice: chargeInfo.UnitPrice,
  370. FixAmount: chargeInfo.FixAmount,
  371. FixAmountName: chargeInfo.FixAmountName,
  372. CustomFee: bindInfo.CustomFee,
  373. }
  374. switch chargeInfo.ChargeBasis {
  375. case ChargeBasisUsedArea:
  376. amount += int64(float64(chargeInfo.UnitPrice*months) * bindInfo.ObjArea)
  377. array = append(array, fmt.Sprintf("使用面积:%f", bindInfo.ObjArea))
  378. array = append(array, fmt.Sprintf("单价:%.2f元", float64(chargeInfo.UnitPrice)/100.00))
  379. if chargeInfo.FixAmount > 0 {
  380. amount += (chargeInfo.FixAmount * months)
  381. array = append(array, fmt.Sprintf("附加费用(%s)每月:%.2f元", chargeInfo.FixAmountName, float64(chargeInfo.FixAmount)/100.00))
  382. }
  383. case ChargeBasisArea:
  384. amount += int64(float64(chargeInfo.UnitPrice*months) * bindInfo.ObjArea)
  385. array = append(array, fmt.Sprintf("建筑面积:%f", bindInfo.ObjArea))
  386. array = append(array, fmt.Sprintf("单价:%.2f元", float64(chargeInfo.UnitPrice)/100.00))
  387. if chargeInfo.FixAmount > 0 {
  388. amount += (chargeInfo.FixAmount * months)
  389. array = append(array, fmt.Sprintf("附加费用(%s)每月:%.2f元", chargeInfo.FixAmountName, float64(chargeInfo.FixAmount)/100.00))
  390. }
  391. case ChargeBasisSpaceArea:
  392. amount += int64(float64(chargeInfo.UnitPrice*months) * bindInfo.ObjArea)
  393. array = append(array, fmt.Sprintf("车位面积:%f", bindInfo.ObjArea))
  394. array = append(array, fmt.Sprintf("单价:%.2f元", float64(chargeInfo.UnitPrice)/100.00))
  395. if chargeInfo.FixAmount > 0 {
  396. amount += (chargeInfo.FixAmount * months)
  397. array = append(array, fmt.Sprintf("附加费用(%s)每月:%.2f元", chargeInfo.FixAmountName, float64(chargeInfo.FixAmount)/100.00))
  398. }
  399. case ChargeBasisFix:
  400. amount += (chargeInfo.FixAmount * months)
  401. array = append(array, fmt.Sprintf("固定费用(%s)每月:%.2f元", chargeInfo.FixAmountName, float64(chargeInfo.FixAmount)/100.00))
  402. case ChargeBasisSelf:
  403. amount += (bindInfo.CustomFee * months)
  404. array = append(array, fmt.Sprintf("自定义费用每月:%.2f元", float64(bindInfo.CustomFee)/100.00))
  405. }
  406. desc := strings.Join(array, "\n")
  407. bytes, _ := json.Marshal(chargeDesc)
  408. return amount, desc, string(bytes)
  409. }
  410. func GetMonths(start int64, end int64) int64 {
  411. startT := time.Unix(start, 0)
  412. endT := time.Unix(end, 0)
  413. startT = time.Date(startT.Year(), startT.Month(), 1, 0, 0, 0, 0, startT.Location())
  414. endT = time.Date(endT.Year(), startT.Month(), 1, 0, 0, 0, 0, endT.Location())
  415. ret := int64(1)
  416. for i := 0; i < 1200; i++ {
  417. startT = startT.AddDate(0, 1, 0)
  418. if startT.Unix() >= endT.Unix() {
  419. return ret
  420. }
  421. ret++
  422. }
  423. return ret
  424. }
  425. func GetChargeDesc(chargeDesc dbmodel.ChargeDesc) string {
  426. array := []string{}
  427. switch chargeDesc.ChargeBasis {
  428. case ChargeBasisArea:
  429. array = append(array, fmt.Sprintf("建筑面积:%.2f", chargeDesc.ObjArea))
  430. array = append(array, fmt.Sprintf("单价:%.2f", float64(chargeDesc.UnitPrice)/100.00))
  431. if chargeDesc.FixAmount > 0 {
  432. array = append(array, fmt.Sprintf("附加费用(%s)每月:%.2f元", chargeDesc.FixAmountName, float64(chargeDesc.FixAmount)/100.00))
  433. }
  434. case ChargeBasisUsedArea:
  435. array = append(array, fmt.Sprintf("使用面积:%.2f", chargeDesc.ObjArea))
  436. array = append(array, fmt.Sprintf("单价:%.2f", float64(chargeDesc.UnitPrice)/100.00))
  437. if chargeDesc.FixAmount > 0 {
  438. array = append(array, fmt.Sprintf("附加费用(%s)每月:%.2f元", chargeDesc.FixAmountName, float64(chargeDesc.FixAmount)/100.00))
  439. }
  440. case ChargeBasisSpaceArea:
  441. array = append(array, fmt.Sprintf("车位面积:%.2f", chargeDesc.ObjArea))
  442. array = append(array, fmt.Sprintf("单价:%.2f", float64(chargeDesc.UnitPrice)/100.00))
  443. if chargeDesc.FixAmount > 0 {
  444. array = append(array, fmt.Sprintf("附加费用(%s)每月:%.2f元", chargeDesc.FixAmountName, float64(chargeDesc.FixAmount)/100.00))
  445. }
  446. case ChargeBasisFix:
  447. array = append(array, fmt.Sprintf("固定费用(%s)每月:%.2f元", chargeDesc.FixAmountName, float64(chargeDesc.FixAmount)/100.00))
  448. case ChargeBasisUsed:
  449. array = append(array, fmt.Sprintf("上期用量:%.2f", chargeDesc.PowerLastUsed))
  450. array = append(array, fmt.Sprintf("本期用量:%.2f", chargeDesc.PowerCurrentUsed))
  451. array = append(array, fmt.Sprintf("单价:%.2f", float64(chargeDesc.UnitPrice)/100.00))
  452. if chargeDesc.FixAmount > 0 {
  453. array = append(array, fmt.Sprintf("附加费用(%s)每月:%.2f元", chargeDesc.FixAmountName, float64(chargeDesc.FixAmount)/100.00))
  454. }
  455. case ChargeBasisSelf:
  456. array = append(array, fmt.Sprintf("费用每月:%.2f元", float64(chargeDesc.CustomFee)/100.00))
  457. }
  458. return strings.Join(array, "\n")
  459. }
  460. func GetLateFee(amount int64, billTime int64, chargeInfo *dbmodel.TChargeConf) (int64, int64) {
  461. if chargeInfo.LateFeeEnable != 1 {
  462. return 0, 0
  463. }
  464. if chargeInfo.LateFeePercent == 0 {
  465. return 0, 0
  466. }
  467. now := time.Now()
  468. billt := time.Unix(billTime, 0)
  469. // 账单日期
  470. billDay := time.Date(billt.Year(), billt.Month(), billt.Day(), 0, 0, 0, 0, billt.Location())
  471. // 当天日期
  472. nowDay := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
  473. // 超期开始日期
  474. expireDay := billDay.AddDate(0, 0, int(chargeInfo.LateFeeDay))
  475. if expireDay.Unix() >= nowDay.Unix() {
  476. return 0, 0
  477. }
  478. days := (nowDay.Unix() - expireDay.Unix()) / (3600 * 24)
  479. if chargeInfo.LateFeeMaxDays > 0 && days > chargeInfo.LateFeeMaxDays {
  480. days = chargeInfo.LateFeeMaxDays
  481. }
  482. ret := float64(amount*days*chargeInfo.LateFeePercent) / 1000.00
  483. if ret == 0 {
  484. return 0, 0
  485. }
  486. return int64(ret), days
  487. }