charge_order_pay.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. "gorm.io/gorm"
  13. "property-garden/errors"
  14. "property-garden/impl/v1/charge_utils"
  15. dbmodel "property-garden/model"
  16. pb_v1 "property-garden/pb/v1"
  17. "property-garden/utils"
  18. "strings"
  19. "time"
  20. )
  21. func checkChargeOrderPayParam(req *pb_v1.ChargeOrderPayRequest) error {
  22. switch {
  23. case req.GardenId == 0:
  24. return status.Error(10003, "小区不能为空")
  25. case req.OrderId == "":
  26. return status.Error(10003, "订单号不能为空")
  27. case req.TransactionId == "":
  28. return status.Error(10003, "交易号不能为空")
  29. }
  30. return nil
  31. }
  32. func changeBindTimestamp(start, end, bindId int64, chargeType int32, db *gorm.DB, dbname string) error {
  33. p := dbmodel.NewChargeBind(dbname)
  34. where := map[string]interface{}{
  35. "id": bindId,
  36. "bill_last_time <": end,
  37. }
  38. values := map[string]interface{}{
  39. "bill_last_time": end,
  40. }
  41. if chargeType == charge_utils.ChargeTypeVehicle {
  42. values["start"] = start
  43. values["end"] = end
  44. }
  45. err := p.Update(db, where, values)
  46. if err != nil {
  47. return errors.DataBaseError
  48. }
  49. return nil
  50. }
  51. func ChargeOrderConfirm(ctx context.Context, req *pb_v1.ChargeOrderPayRequest) (reply *pb_v1.ChargeOrderPayReply, err error) {
  52. switch {
  53. case req.GardenId == 0:
  54. return nil, status.Error(10003, "小区不能为空")
  55. case req.OrderId == "":
  56. return nil, status.Error(10003, "订单号不能为空")
  57. }
  58. reply = &pb_v1.ChargeOrderPayReply{}
  59. // 捕获各个task中的异常并返回给调用者
  60. defer func() {
  61. if r := recover(); r != nil {
  62. err = fmt.Errorf("%+v", r)
  63. e := &status.Status{}
  64. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  65. logger.Error("err",
  66. zap.String("system_err", err.Error()),
  67. zap.Stack("stacktrace"))
  68. }
  69. }
  70. }()
  71. dbname := utils.GetGardenDbName(req.GardenId)
  72. // 查看订单
  73. order := dbmodel.NewChargeOrder(dbname)
  74. where := map[string]interface{}{
  75. "order_id": req.OrderId,
  76. }
  77. err = order.Find(database.DB(), where)
  78. if err != nil && err != gorm.ErrRecordNotFound {
  79. return nil, errors.DataBaseError
  80. }
  81. if order.OrderId == "" {
  82. return nil, errors.ErrRecordNotFound
  83. }
  84. if req.PayTime == 0 {
  85. req.PayTime = time.Now().Unix()
  86. }
  87. db := database.DB().Begin()
  88. defer func() {
  89. if err != nil {
  90. db.Rollback()
  91. }
  92. }()
  93. onlineBill := dbmodel.TChargeBill{}
  94. if order.OnlinePay == 1 {
  95. json.Unmarshal([]byte(order.OnlinePayBill), &onlineBill)
  96. }
  97. // 插入已支付账单表
  98. bill := dbmodel.NewChargeBill(dbname)
  99. where = map[string]interface{}{
  100. "order_id": req.OrderId,
  101. }
  102. var list []dbmodel.TChargeBill
  103. if order.OnlinePay == 1 {
  104. list = []dbmodel.TChargeBill{onlineBill}
  105. } else {
  106. list, err = bill.List(database.DB(), where, nil, -1, -1, "")
  107. if err != nil {
  108. return nil, errors.DataBaseError
  109. }
  110. }
  111. if len(list) > 0 {
  112. for i, _ := range list {
  113. list[i].ID = 0
  114. list[i].PayTime = req.PayTime
  115. list[i].Status = charge_utils.PayStatusPayed
  116. }
  117. bill.SetTablePayed(dbname)
  118. err = bill.InsertMulti(db, &list)
  119. if err != nil {
  120. if strings.Contains(strings.ToLower(err.Error()), "duplicate") {
  121. return nil, status.Error(10003, "已存在相同已支付账单(费项和时间相同)")
  122. }
  123. return nil, errors.DataBaseError
  124. }
  125. // 删除待支付表中的信息
  126. if order.OnlinePay == 0 {
  127. bill.SetTable(dbname)
  128. err = bill.Delete(db, where)
  129. if err != nil {
  130. return nil, errors.DataBaseError
  131. }
  132. }
  133. }
  134. billIds := []string{}
  135. for _, v := range list {
  136. billIds = append(billIds, fmt.Sprintf("%d", v.ID))
  137. }
  138. // 更新订单状态
  139. values := map[string]interface{}{
  140. "status": charge_utils.PayStatusPayed,
  141. "pay_time": req.PayTime,
  142. "bill_ids": strings.Join(billIds, ","),
  143. "comment": order.Comment + "(手动确认支付)",
  144. "deleted": 0,
  145. }
  146. if order.OnlinePay == 1 {
  147. onlineBill.Status = charge_utils.PayStatusPayed
  148. bytes, _ := json.Marshal(onlineBill)
  149. values["online_pay_bill"] = string(bytes)
  150. }
  151. err = order.Update(db, where, values)
  152. if err != nil {
  153. return nil, errors.DataBaseError
  154. }
  155. // 线上预存费用要修改关系表时间戳
  156. if order.OnlinePay == 1 {
  157. err = changeBindTimestamp(onlineBill.ChargeStart, onlineBill.ChargeEnd, onlineBill.ChargeBindId, onlineBill.ChargeType, db, dbname)
  158. if err != nil {
  159. return nil, err
  160. }
  161. }
  162. if err = charge_utils.CompanyDealIncrease(req.GardenId, order.PayAmount, req.PayTime); err != nil {
  163. return nil, err
  164. }
  165. db.Commit()
  166. return reply, nil
  167. }
  168. func ChargeOrderPay(ctx context.Context, req *pb_v1.ChargeOrderPayRequest) (reply *pb_v1.ChargeOrderPayReply, err error) {
  169. reply = &pb_v1.ChargeOrderPayReply{}
  170. // 捕获各个task中的异常并返回给调用者
  171. defer func() {
  172. if r := recover(); r != nil {
  173. err = fmt.Errorf("%+v", r)
  174. e := &status.Status{}
  175. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  176. logger.Error("err",
  177. zap.String("system_err", err.Error()),
  178. zap.Stack("stacktrace"))
  179. }
  180. }
  181. }()
  182. // 参数检查
  183. if req.IsComfirm {
  184. return ChargeOrderConfirm(ctx, req)
  185. }
  186. err = checkChargeOrderPayParam(req)
  187. if err != nil {
  188. return nil, err
  189. }
  190. dbname := utils.GetGardenDbName(req.GardenId)
  191. // 查看订单
  192. order := dbmodel.NewChargeOrder(dbname)
  193. where := map[string]interface{}{
  194. "order_id": req.OrderId,
  195. }
  196. err = order.Find(database.DB(), where)
  197. if err != nil && err != gorm.ErrRecordNotFound {
  198. return nil, errors.DataBaseError
  199. }
  200. if order.OrderId == "" {
  201. return nil, errors.ErrRecordNotFound
  202. }
  203. if req.PayTime == 0 {
  204. req.PayTime = time.Now().Unix()
  205. }
  206. onlineBill := dbmodel.TChargeBill{}
  207. if order.OnlinePay == 1 {
  208. json.Unmarshal([]byte(order.OnlinePayBill), &onlineBill)
  209. }
  210. db := database.DB().Begin()
  211. defer func() {
  212. if err != nil {
  213. db.Rollback()
  214. }
  215. }()
  216. // 插入已支付账单表
  217. bill := dbmodel.NewChargeBill(dbname)
  218. where = map[string]interface{}{
  219. "order_id": req.OrderId,
  220. }
  221. var list []dbmodel.TChargeBill
  222. if order.OnlinePay == 1 {
  223. list = []dbmodel.TChargeBill{onlineBill}
  224. } else {
  225. list, err = bill.List(database.DB(), where, nil, -1, -1, "")
  226. if err != nil {
  227. return nil, errors.DataBaseError
  228. }
  229. }
  230. if len(list) > 0 {
  231. for i, _ := range list {
  232. list[i].ID = 0
  233. list[i].PayTime = req.PayTime
  234. list[i].Status = charge_utils.PayStatusPayed
  235. }
  236. bill.SetTablePayed(dbname)
  237. err = bill.InsertMulti(db, &list)
  238. if err != nil {
  239. if strings.Contains(strings.ToLower(err.Error()), "duplicate") {
  240. return nil, status.Error(10003, "已存在相同已支付账单(费项和时间相同)")
  241. }
  242. return nil, errors.DataBaseError
  243. }
  244. // 删除待支付表中的信息
  245. if order.OnlinePay == 0 {
  246. bill.SetTable(dbname)
  247. err = bill.Delete(db, where)
  248. if err != nil {
  249. return nil, errors.DataBaseError
  250. }
  251. }
  252. }
  253. billIds := []string{}
  254. for _, v := range list {
  255. billIds = append(billIds, fmt.Sprintf("%d", v.ID))
  256. }
  257. // 更新订单状态
  258. values := map[string]interface{}{
  259. "status": charge_utils.PayStatusPayed,
  260. "pay_time": req.PayTime,
  261. "bill_ids": strings.Join(billIds, ","),
  262. "transaction_id": req.TransactionId,
  263. }
  264. if order.OnlinePay == 1 {
  265. onlineBill.Status = charge_utils.PayStatusPayed
  266. bytes, _ := json.Marshal(onlineBill)
  267. values["online_pay_bill"] = string(bytes)
  268. }
  269. if req.Amount != order.Amount {
  270. values["comment"] = fmt.Sprintf("%s", order.Comment) + fmt.Sprintf("三方订单已支付,金额不一致%d", req.Amount)
  271. }
  272. err = order.Update(db, where, values)
  273. if err != nil {
  274. return nil, errors.DataBaseError
  275. }
  276. // 线上预存费用要修改关系表时间戳
  277. if order.OnlinePay == 1 {
  278. err = changeBindTimestamp(onlineBill.ChargeStart, onlineBill.ChargeEnd, onlineBill.ChargeBindId, onlineBill.ChargeType, db, dbname)
  279. if err != nil {
  280. return nil, err
  281. }
  282. }
  283. if err = charge_utils.CompanyDealIncrease(req.GardenId, order.PayAmount, req.PayTime); err != nil {
  284. return nil, err
  285. }
  286. db.Commit()
  287. return reply, nil
  288. }