pay_service.go 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package service
  2. import (
  3. "Xcx_New/models"
  4. "github.com/jinzhu/gorm"
  5. "strconv"
  6. "time"
  7. )
  8. func FindServiceSubscibeByOrgId(orgId int64) (*models.ServeSubscibe, error) {
  9. subscibe := models.ServeSubscibe{}
  10. err = readUserDb.Model(&models.ServeOrder{}).Preload("Org", "status=1").Where("org_id=? and status=1", orgId).First(&subscibe).Error
  11. var count int64
  12. err = readDb.Model(&models.Patients{}).Where("user_org_id=? and status = 1", orgId).Count(&count).Error
  13. subscibe.Patients = count
  14. return &subscibe, err
  15. }
  16. func FindOrgAppByOrgId(orgId int64) (*models.OrgApp, error) {
  17. app := models.OrgApp{}
  18. err = readUserDb.Model(&models.OrgApp{}).Where("org_id=? and status=1", orgId).First(&app).Error
  19. return &app, err
  20. }
  21. func FindAllProduct() ([]*models.ServeProduct, error) {
  22. product := []*models.ServeProduct{}
  23. err := readUserDb.Model(&models.ServeProduct{}).Where("status=1").Find(&product).Error
  24. return product, err
  25. }
  26. func FindProductByID(id int64) (*models.ServeProduct, error) {
  27. var product models.ServeProduct
  28. err := readUserDb.Where("id = ? and status=1", id).First(&product).Error
  29. if err == gorm.ErrRecordNotFound {
  30. return nil, nil
  31. }
  32. if err != nil {
  33. return nil, err
  34. }
  35. return &product, err
  36. }
  37. func CreateOrderRecord(order *models.ServeOrder) {
  38. writeUserDb.Create(order)
  39. }
  40. func CreateOrderInfomation(info *models.ServeOrderInfomation) {
  41. writeUserDb.Create(info)
  42. }
  43. func FindOrderInfomationByID(orderNumber string, orgId int64) (*models.ServeOrderInfomation, error) {
  44. var order models.ServeOrderInfomation
  45. err := readUserDb.Model(&models.ServeOrderInfomation{}).Where("order_number = ? and org_id=? and status=1", orderNumber, orgId).First(&order).Error
  46. if err == gorm.ErrRecordNotFound {
  47. return nil, nil
  48. }
  49. if err != nil {
  50. return nil, err
  51. }
  52. return &order, err
  53. }
  54. func FindServeOrderByID(orgId, id int64) (*models.ServeOrder, error) {
  55. var order models.ServeOrder
  56. err := readUserDb.Model(&models.ServeOrder{}).Where("id = ? and org_id=? and status=1", id, orgId).First(&order).Error
  57. if err == gorm.ErrRecordNotFound {
  58. return nil, nil
  59. }
  60. if err != nil {
  61. return nil, err
  62. }
  63. return &order, err
  64. }
  65. func UpdateOrderPayType(orgId int64, id int64) error {
  66. err := writeUserDb.Model(&models.ServeOrder{}).Where("id = ? and org_id=? and status=1", id, orgId).Updates(map[string]interface{}{"pay_type": 2}).Error
  67. return err
  68. }
  69. func UpdateOrderStatus(orgId int64, id int64) error {
  70. err := writeUserDb.Model(&models.ServeOrder{}).Where("id = ? and org_id=? and status=1", id, orgId).Updates(map[string]interface{}{"order_status": 3}).Error
  71. return err
  72. }
  73. func GetOrderList(orgId int64) ([]*models.ServeOrder, error) {
  74. var orders []*models.ServeOrder
  75. err := readUserDb.Model(&models.ServeOrder{}).Where("org_id=? and status=1", orgId).Order("created_time desc").Find(&orders).Error
  76. for _, order := range orders {
  77. tm := time.Unix(order.OrderExpireTime, 0)
  78. order.OrderExpireTimeFormat = tm.Format("2006-01-02 15:04:05")
  79. }
  80. return orders, err
  81. }
  82. func FindServeOrderByOrderNumber(orderNumber string) (*models.ServeOrder, error) {
  83. var order models.ServeOrder
  84. err := readUserDb.Model(&models.ServeOrder{}).Where(" status=1 AND order_number = ? ", orderNumber).First(&order).Error
  85. if err == gorm.ErrRecordNotFound {
  86. return nil, nil
  87. }
  88. if err != nil {
  89. return nil, err
  90. }
  91. return &order, err
  92. }
  93. func GetHetong(orgID, orderId int64) (*models.ServeOrderContract, error) {
  94. var contract models.ServeOrderContract
  95. var err error
  96. err = readUserDb.Model(&models.ServeOrderContract{}).Where("order_id=? and org_id=? and status=1", orderId, orgID).First(&contract).Error
  97. if err == gorm.ErrRecordNotFound {
  98. return nil, nil
  99. }
  100. if err != nil {
  101. return nil, err
  102. }
  103. return &contract, nil
  104. }
  105. func CreateHetong(m *models.ServeOrderContract) (err error) {
  106. err = writeUserDb.Create(m).Error
  107. return
  108. }
  109. func UpdateOrder(orderNumber string, Quantity int64, orgId int64, payment_transaction_id string, startTimes int64, endTimes int64) {
  110. var startTime int64
  111. var endTime int64
  112. if startTimes == 0 && endTimes == 0 {
  113. startTime = time.Now().Unix()
  114. endTime = startTime + ((12 * 30 * 24 * 3600) * Quantity)
  115. } else {
  116. endTime = endTimes + ((12 * 30 * 24 * 3600) * Quantity)
  117. }
  118. tm := time.Unix(endTime, 0)
  119. serve_duration := strconv.FormatInt(12*Quantity, 10) + "月" + "到期服务时间" + tm.Format("2006-01-02 15:04:05")
  120. writeUserDb.Model(&models.ServeOrder{}).Where("order_number = ? and status=1", orderNumber).Updates(map[string]interface{}{"order_status": 2, "pay_type": 1, "period_start": startTime, "period_end": endTime, "serve_duration": serve_duration, "pay_time": time.Now().Unix(), "payment_transaction_id": payment_transaction_id})
  121. writeUserDb.Model(&models.ServeSubscibe{}).Where("org_id = ? and status=1", orgId).Updates(map[string]interface{}{"period_start": startTime, "period_end": endTime, "updated_time": time.Now().Unix(), "state": 1})
  122. }