pay.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. package shanghu
  2. import (
  3. orm "duoduo/database"
  4. "errors"
  5. "fmt"
  6. "github.com/shopspring/decimal"
  7. "time"
  8. )
  9. type ClientPayTrans struct {
  10. ID int64 `gorm:"column:id;type:bigint(20);primary_key" json:"id"` // 主键
  11. RequestID string `gorm:"column:request_id;type:varchar(255)" json:"request_id"` // 请求id,幂等性
  12. OutTradeNo string `gorm:"column:out_trade_no;type:varchar(255)" json:"out_trade_no"` // 交易id
  13. MerchantCardID int64 `gorm:"column:merchant_card_id;type:bigint(20)" json:"merchant_card_id"` // 商户卡id
  14. ClientOpenID string `gorm:"column:client_open_id;type:varchar(255)" json:"client_open_id"` // 客户端openid
  15. Status int `gorm:"column:status;type:int(11)" json:"status"` // 1-未支付 2-支付成功 3-取消支付 4-退款
  16. Amount decimal.Decimal `gorm:"column:amount;type:decimal(10,2)" json:"amount"` // 交易金额
  17. ThirdTradeNo string `gorm:"column:third_trade_no;type:varchar(255)" json:"third_trade_no"` // 微信交易id
  18. PayTime time.Time `gorm:"column:pay_time;type:datetime(3);default:null" json:"pay_time"` // 支付时间
  19. InvitationCode string `gorm:"column:invitation_code;type:varchar(25)" json:"invitation_code"` // 邀请码
  20. AccountStatus int `gorm:"column:account_status;type:int(11)" json:"account_status"` // 分账状态 99-分账成功 2-分账失败 1-未分账
  21. AccountErrLog string `gorm:"column:account_err_log;type:varchar(255)" json:"account_err_log"` // 分账err日志
  22. CreateBy int64 `gorm:"column:create_by;type:bigint(20)" json:"create_by"` // 创建者
  23. UpdateBy int64 `gorm:"column:update_by;type:bigint(20)" json:"update_by"` // 更新者
  24. CreatedAt time.Time `gorm:"column:created_at;type:datetime(3)" json:"created_at"` // 创建时间
  25. UpdatedAt time.Time `gorm:"column:updated_at;type:datetime(3)" json:"updated_at"` // 最后更新时间
  26. DeletedAt time.Time `gorm:"column:deleted_at;type:datetime(3);default:null" json:"deleted_at"` // 删除时间
  27. }
  28. func (m *ClientPayTrans) TableName() string {
  29. return "client_pay_trans"
  30. }
  31. func (m *ClientPayTrans) GetRequestNum() int {
  32. var count int
  33. tableCount := orm.ShMysql.Table(m.TableName()).Where("request_id = ? ", m.RequestID)
  34. tableCount.Count(&count)
  35. return count
  36. }
  37. func (m *ClientPayTrans) GetNumByCardID() int {
  38. var count int
  39. tableCount := orm.ShMysql.Table(m.TableName()).Where("merchant_card_id = ? and status =2 ", m.MerchantCardID)
  40. tableCount.Count(&count)
  41. return count
  42. }
  43. func (m *ClientPayTrans) GetPayTransByOpenid() (ClientPayTrans, error) {
  44. var doc ClientPayTrans
  45. table := orm.ShMysql.Table(m.TableName())
  46. table = table.Where("merchant_card_id = ? and client_open_id = ? and status = 2 ", m.MerchantCardID, m.ClientOpenID)
  47. if err := table.Select("*").First(&doc).Error; err != nil {
  48. return doc, err
  49. }
  50. return doc, nil
  51. }
  52. func (m *ClientPayTrans) GetPayTransByOpenId() (ClientPayTrans, error) {
  53. var doc ClientPayTrans
  54. table := orm.ShMysql.Table(m.TableName())
  55. table = table.Where("client_open_id = ? and status = 2 ", m.ClientOpenID)
  56. if err := table.Select("*").First(&doc).Error; err != nil {
  57. return doc, err
  58. }
  59. return doc, nil
  60. }
  61. func (m *ClientPayTrans) GetPaySuccessTransByInvitationCode(pageSize int, pageIndex int) ([]ClientPayTrans, int, error) {
  62. var doc []ClientPayTrans
  63. table := orm.ShMysql.Table(m.TableName())
  64. table = table.Where("invitation_code = ? and status = 2 and account_status = 99", m.InvitationCode)
  65. var count int
  66. if err := table.Select("*").Order("id desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
  67. return nil, 0, err
  68. }
  69. table.Count(&count)
  70. return doc, count, nil
  71. }
  72. func (m *ClientPayTrans) GetPayTransById() (ClientPayTrans, error) {
  73. var doc ClientPayTrans
  74. table := orm.ShMysql.Table(m.TableName())
  75. table = table.Where("id = ? ", m.ID)
  76. if err := table.Select("*").First(&doc).Error; err != nil {
  77. return doc, err
  78. }
  79. return doc, nil
  80. }
  81. func (m *ClientPayTrans) GetPayTransByTradeNo() (ClientPayTrans, error) {
  82. var doc ClientPayTrans
  83. table := orm.ShMysql.Table(m.TableName())
  84. table = table.Where("out_trade_no = ? ", m.OutTradeNo)
  85. if err := table.Select("*").First(&doc).Error; err != nil {
  86. return doc, err
  87. }
  88. return doc, nil
  89. }
  90. func (u *ClientPayTrans) Create() (ClientPayTrans, error) {
  91. var doc ClientPayTrans
  92. var err error
  93. doc = *u
  94. err = orm.ShMysql.Table(u.TableName()).Create(&doc).Error
  95. if err != nil {
  96. return doc, err
  97. }
  98. return doc, nil
  99. }
  100. func (m *ClientPayTrans) UpdatePayTransByTradeNo() error {
  101. if err := orm.ShMysql.Table(m.TableName()).Model(&m).Where("out_trade_no = ? ", m.OutTradeNo).Updates(
  102. map[string]interface{}{
  103. "third_trade_no": m.ThirdTradeNo,
  104. "pay_time": m.PayTime,
  105. "status": m.Status,
  106. "account_status": m.AccountStatus,
  107. "updated_at": time.Now()}).Error; err != nil {
  108. return err
  109. }
  110. return nil
  111. }
  112. func (m *ClientPayTrans) GetTransByAccount() (ClientPayTrans, error) {
  113. var doc ClientPayTrans
  114. table := orm.ShMysql.Table(m.TableName())
  115. table = table.Where("account_status = ? ", m.AccountStatus)
  116. if err := table.Select("*").First(&doc).Error; err != nil {
  117. return doc, err
  118. }
  119. return doc, nil
  120. }
  121. func (m *ClientPayTrans) GetPayTransByStatus() (ClientPayTrans, error) {
  122. var doc ClientPayTrans
  123. table := orm.ShMysql.Table(m.TableName())
  124. table = table.Where("status = ? and account_status = ?", m.Status, m.AccountStatus)
  125. if err := table.Select("*").First(&doc).Error; err != nil {
  126. return doc, err
  127. }
  128. return doc, nil
  129. }
  130. func (m *ClientPayTrans) UpdateById(data map[string]interface{}) error {
  131. err := orm.ShMysql.Table(m.TableName()).Where("id = ?", m.ID).Updates(data).Error
  132. if err != nil {
  133. return err
  134. }
  135. return nil
  136. }
  137. func (m *ClientPayTrans) GetPayTransSuccessByOpenID(pageSize int, pageIndex int) ([]ClientPayTrans, int, error) {
  138. var doc []ClientPayTrans
  139. table := orm.ShMysql.Table(m.TableName())
  140. //if m.ActivityStartTime != 0
  141. table = table.Where("client_open_id = ? and status = 2", m.ClientOpenID)
  142. var count int
  143. if err := table.Select("*").Order("id desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
  144. return nil, 0, err
  145. }
  146. table.Count(&count)
  147. return doc, count, nil
  148. }
  149. func (m *ClientPayTrans) GetPayTransSuccessByMerchantID(pageSize int, pageIndex int, merchantId []int64) ([]ClientPayTrans, int, error) {
  150. var doc []ClientPayTrans
  151. table := orm.ShMysql.Table(m.TableName())
  152. table = table.Where("merchant_card_id in (?) and status = 2", merchantId)
  153. var count int
  154. if err := table.Select("*").Order("id desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
  155. return nil, 0, err
  156. }
  157. table.Count(&count)
  158. return doc, count, nil
  159. }
  160. func (m *ClientPayTrans) SettleAdd(merchantAmount, clientAmount decimal.Decimal, merchantOpenId, clientOpenId string, payTransId int64) error {
  161. // 使用事务 添加
  162. var err error
  163. var clientAccount MerchantClientAccount
  164. var merchantAccount MerchantAccount
  165. var amountPreMerchant decimal.Decimal
  166. var amountPreClient decimal.Decimal
  167. tx := orm.ShMysql.Begin()
  168. defer func() {
  169. if err != nil {
  170. tx.Rollback()
  171. } else {
  172. tx.Commit()
  173. }
  174. }()
  175. //查看是否有账号,没有创建
  176. err = tx.Table("merchant_account").Select("*").Where("merchant_open_id = ?", merchantOpenId).Find(&merchantAccount).Error
  177. if err != nil && err.Error() != "record not found" {
  178. return err
  179. }
  180. if merchantAccount.ID == 0 {
  181. merchantAccount.MerchantOpenID = merchantOpenId
  182. merchantAccount.Version = 1
  183. merchantAccount.UpdatedAt = time.Now()
  184. merchantAccount.CreatedAt = time.Now()
  185. err = tx.Table("merchant_account").Create(&merchantAccount).Error
  186. if err != nil {
  187. return err
  188. }
  189. }
  190. amountPreMerchant = merchantAccount.Amount
  191. if clientOpenId != "" { //原始码不抽佣金
  192. err = tx.Table("merchant_client_account").Select("*").Where("client_open_id = ?", clientOpenId).Find(&clientAccount).Error
  193. if err != nil && err.Error() != "record not found" {
  194. return err
  195. }
  196. if clientAccount.ID == 0 {
  197. clientAccount.ClientOpenID = clientOpenId
  198. clientAccount.Version = 1
  199. clientAccount.UpdatedAt = time.Now()
  200. clientAccount.CreatedAt = time.Now()
  201. err = tx.Table("merchant_client_account").Create(&clientAccount).Error
  202. if err != nil {
  203. return err
  204. }
  205. }
  206. amountPreClient = clientAccount.Amount
  207. clientAmountAdd := clientAccount.Amount.Add(clientAmount)
  208. result := tx.Table("merchant_client_account").Model(&clientAccount).Where("client_open_id = ? and version = ?", clientOpenId, clientAccount.Version).Updates(
  209. map[string]interface{}{
  210. "amount": clientAmountAdd,
  211. "version": clientAccount.Version + 1,
  212. "updated_at": time.Now()})
  213. if result.Error != nil {
  214. return result.Error
  215. }
  216. if result.RowsAffected <= 0 {
  217. err = errors.New("rows is zero")
  218. return err
  219. }
  220. var clientAccountLog MerchantClientAccountLog
  221. clientAccountLog.ClientOpenID = clientOpenId
  222. clientAccountLog.UpdatedAt = time.Now()
  223. clientAccountLog.AmountPre = amountPreClient
  224. clientAccountLog.AmountAfter = clientAmountAdd
  225. clientAccountLog.ReviewAmountAfter = clientAccount.ReviewAmount
  226. clientAccountLog.ReviewAmountPre = clientAccount.ReviewAmount
  227. clientAccountLog.Amount = clientAmount
  228. clientAccountLog.PayTransId = payTransId
  229. err = tx.Table("merchant_client_account_log").Create(&clientAccountLog).Error
  230. if err != nil {
  231. return err
  232. }
  233. }
  234. //做金额加减操作并且入日志库
  235. merchantAmountAdd := merchantAccount.Amount.Add(merchantAmount)
  236. result := tx.Table("merchant_account").Model(&merchantAccount).Where("merchant_open_id = ? and version = ?", merchantOpenId, merchantAccount.Version).Updates(
  237. map[string]interface{}{
  238. "amount": merchantAmountAdd,
  239. "version": merchantAccount.Version + 1,
  240. "updated_at": time.Now()})
  241. if result.Error != nil {
  242. return result.Error
  243. }
  244. if result.RowsAffected <= 0 {
  245. err = errors.New("rows is zero")
  246. return err
  247. }
  248. var merchantAccountLog MerchantAccountLog
  249. fmt.Println("amountPreMerchant = ", amountPreMerchant)
  250. fmt.Println("merchantAccount.Amount = ", merchantAccount.Amount)
  251. merchantAccountLog.MerchantOpenID = merchantOpenId
  252. merchantAccountLog.UpdatedAt = time.Now()
  253. merchantAccountLog.AmountPre = amountPreMerchant
  254. merchantAccountLog.AmountAfter = merchantAmountAdd
  255. merchantAccountLog.ReviewAmountAfter = merchantAccount.ReviewAmount
  256. merchantAccountLog.ReviewAmountPre = merchantAccount.ReviewAmount
  257. merchantAccountLog.Amount = merchantAmount
  258. merchantAccountLog.PayTransId = payTransId
  259. err = tx.Table("merchant_account_log").Create(&merchantAccountLog).Error
  260. if err != nil {
  261. return err
  262. }
  263. return nil
  264. }
  265. func (m *ClientPayTrans) SettleAddCashOutFee(amount decimal.Decimal, clientOpenId string, cashOutId int64) error {
  266. // 使用事务 添加
  267. var err error
  268. var clientAccount MerchantClientAccount
  269. var amountPreClient decimal.Decimal
  270. tx := orm.ShMysql.Begin()
  271. defer func() {
  272. if err != nil {
  273. tx.Rollback()
  274. } else {
  275. tx.Commit()
  276. }
  277. }()
  278. err = tx.Table("merchant_client_account").Select("*").Where("client_open_id = ?", clientOpenId).Find(&clientAccount).Error
  279. if err != nil && err.Error() != "record not found" {
  280. return err
  281. }
  282. if clientAccount.ID == 0 {
  283. clientAccount.ClientOpenID = clientOpenId
  284. clientAccount.Version = 1
  285. clientAccount.UpdatedAt = time.Now()
  286. clientAccount.CreatedAt = time.Now()
  287. err = tx.Table("merchant_client_account").Create(&clientAccount).Error
  288. if err != nil {
  289. return err
  290. }
  291. }
  292. amountPreClient = clientAccount.Amount
  293. clientAmountAdd := clientAccount.Amount.Add(amount)
  294. result := tx.Table("merchant_client_account").Model(&clientAccount).Where("client_open_id = ? and version = ?", clientOpenId, clientAccount.Version).Updates(
  295. map[string]interface{}{
  296. "amount": clientAmountAdd,
  297. "version": clientAccount.Version + 1,
  298. "updated_at": time.Now()})
  299. if result.Error != nil {
  300. return result.Error
  301. }
  302. if result.RowsAffected <= 0 {
  303. err = errors.New("rows is zero")
  304. return err
  305. }
  306. var clientAccountLog MerchantClientAccountLog
  307. clientAccountLog.ClientOpenID = clientOpenId
  308. clientAccountLog.UpdatedAt = time.Now()
  309. clientAccountLog.AmountPre = amountPreClient
  310. clientAccountLog.AmountAfter = clientAmountAdd
  311. clientAccountLog.ReviewAmountAfter = clientAccount.ReviewAmount
  312. clientAccountLog.ReviewAmountPre = clientAccount.ReviewAmount
  313. clientAccountLog.Amount = amount
  314. clientAccountLog.PayTransId = cashOutId
  315. clientAccountLog.TransType = 3 //服务费
  316. err = tx.Table("merchant_client_account_log").Create(&clientAccountLog).Error
  317. if err != nil {
  318. return err
  319. }
  320. return nil
  321. }
  322. func (m *ClientPayTrans) SettleSubClient(clientAmount decimal.Decimal, clientOpenId string, cashOutId int64) error {
  323. // 使用事务 添加
  324. var err error
  325. var clientAccount MerchantClientAccount
  326. var cashOut CashOut
  327. var amountPreClient decimal.Decimal
  328. tx := orm.ShMysql.Begin()
  329. defer func() {
  330. if err != nil {
  331. tx.Rollback()
  332. } else {
  333. tx.Commit()
  334. }
  335. }()
  336. err = tx.Table("merchant_client_account").Select("*").Where("client_open_id = ?", clientOpenId).Find(&clientAccount).Error
  337. if err != nil {
  338. return err
  339. }
  340. amountPreClient = clientAccount.Amount
  341. if clientAccount.Amount.Cmp(clientAmount) < 0 {
  342. err = errors.New("账户金额不够")
  343. return err
  344. }
  345. clientAmountSub := clientAccount.Amount.Sub(clientAmount)
  346. result := tx.Table("merchant_client_account").Model(&clientAccount).Where("client_open_id = ? and version = ?", clientOpenId, clientAccount.Version).Updates(
  347. map[string]interface{}{
  348. "amount": clientAmountSub,
  349. "version": clientAccount.Version + 1,
  350. "updated_at": time.Now()})
  351. if result.Error != nil {
  352. return result.Error
  353. }
  354. if result.RowsAffected <= 0 {
  355. err = errors.New("rows is zero")
  356. return err
  357. }
  358. var clientAccountLog MerchantClientAccountLog
  359. clientAccountLog.ClientOpenID = clientOpenId
  360. clientAccountLog.UpdatedAt = time.Now()
  361. clientAccountLog.AmountPre = amountPreClient
  362. clientAccountLog.AmountAfter = clientAmountSub
  363. clientAccountLog.ReviewAmountAfter = clientAccount.ReviewAmount
  364. clientAccountLog.ReviewAmountPre = clientAccount.ReviewAmount
  365. clientAccountLog.Amount = clientAmount
  366. clientAccountLog.PayTransId = cashOutId
  367. clientAccountLog.TransType = 2
  368. err = tx.Table("merchant_client_account_log").Create(&clientAccountLog).Error
  369. if err != nil {
  370. return err
  371. }
  372. result = tx.Table("cash_out").Model(&cashOut).Where("id = ? ", cashOutId).Updates(
  373. map[string]interface{}{
  374. "status": 99, //体现成功
  375. "account_status": 99, //分账成功
  376. "updated_at": time.Now()})
  377. if result.Error != nil {
  378. return result.Error
  379. }
  380. if result.RowsAffected <= 0 {
  381. err = errors.New("rows is zero")
  382. return err
  383. }
  384. return nil
  385. }
  386. func (m *ClientPayTrans) SettleSubMerchant(merchantAmount decimal.Decimal, merchantOpenId string, cashOutId int64) error {
  387. // 使用事务 添加
  388. var err error
  389. var merchantAccount MerchantAccount
  390. var cashOut CashOut
  391. var amountPreMerchant decimal.Decimal
  392. tx := orm.ShMysql.Begin()
  393. defer func() {
  394. if err != nil {
  395. tx.Rollback()
  396. } else {
  397. tx.Commit()
  398. }
  399. }()
  400. err = tx.Table("merchant_account").Select("*").Where("merchant_open_id = ?", merchantOpenId).Find(&merchantAccount).Error
  401. if err != nil {
  402. return err
  403. }
  404. amountPreMerchant = merchantAccount.Amount
  405. if merchantAccount.Amount.Cmp(merchantAmount) < 0 {
  406. err = errors.New("账户金额不够")
  407. return err
  408. }
  409. merchantAmountSub := merchantAccount.Amount.Sub(merchantAmount)
  410. result := tx.Table("merchant_account").Model(&merchantAccount).Where("merchant_open_id = ? and version = ?", merchantOpenId, merchantAccount.Version).Updates(
  411. map[string]interface{}{
  412. "amount": merchantAmountSub,
  413. "version": merchantAccount.Version + 1,
  414. "updated_at": time.Now()})
  415. if result.Error != nil {
  416. return result.Error
  417. }
  418. if result.RowsAffected <= 0 {
  419. err = errors.New("rows is zero")
  420. return err
  421. }
  422. var merchantAccountLog MerchantAccountLog
  423. merchantAccountLog.MerchantOpenID = merchantOpenId
  424. merchantAccountLog.UpdatedAt = time.Now()
  425. merchantAccountLog.AmountPre = amountPreMerchant
  426. merchantAccountLog.AmountAfter = merchantAmountSub
  427. merchantAccountLog.ReviewAmountAfter = merchantAccount.ReviewAmount
  428. merchantAccountLog.ReviewAmountPre = merchantAccount.ReviewAmount
  429. merchantAccountLog.Amount = merchantAmount
  430. merchantAccountLog.PayTransId = cashOutId
  431. merchantAccountLog.TransType = 2
  432. err = tx.Table("merchant_account_log").Create(&merchantAccountLog).Error
  433. if err != nil {
  434. return err
  435. }
  436. result = tx.Table("cash_out").Model(&cashOut).Where("id = ? ", cashOutId).Updates(
  437. map[string]interface{}{
  438. "status": 99, //体现成功
  439. "account_status": 99, //分账成功
  440. "updated_at": time.Now()})
  441. if result.Error != nil {
  442. return result.Error
  443. }
  444. if result.RowsAffected <= 0 {
  445. err = errors.New("rows is zero")
  446. return err
  447. }
  448. return nil
  449. }