fapiao_service.go 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package service
  2. import (
  3. "XT_New/models"
  4. "fmt"
  5. "strings"
  6. )
  7. func FindFaPiaoConfigInfo(org_id int64) (config models.FapiaoConfig, err error) {
  8. err = readDb.Model(&models.FapiaoConfig{}).Where("status = 1 AND user_org_id = ?", org_id).First(&config).Error
  9. return
  10. }
  11. func GetFaPiaoSettleList(user_org_id int64, page int64, limit int64, start_time_timestamp int64, end_time_timestamp int64, sort_type int64, start_time string, end_time string, is_open_fapiao int64, patient_id int64) (order []*models.HisOrderByFaPiao, err error, total int64) {
  12. offset := (page - 1) * limit
  13. db := readDb.Model(&models.HisOrderByFaPiao{})
  14. // Join the patients table based on user_org_id
  15. db = db.Joins("join xt_patients as patient on patient.id = his_order.patient_id and patient.user_org_id = ?", user_org_id)
  16. // Handle the is_open_fapiao logic
  17. if is_open_fapiao == 1 {
  18. // is_open_fapiao == 1: Look for orders with a corresponding fa_piao_number
  19. db = db.Joins("left join his_fapiao_order as fo on fo.ticket_sn = his_order.fa_piao_number and fo.user_org_id = ?", user_org_id).
  20. Where("fo.id IS NOT NULL") // Ensure fa_piao_number exists
  21. } else if is_open_fapiao == 2 {
  22. // is_open_fapiao == 2: Look for orders without a corresponding fa_piao_number
  23. db = db.Joins("left join his_fapiao_order as fo on fo.ticket_sn = his_order.fa_piao_number and fo.user_org_id = ?", user_org_id).
  24. Where("fo.id IS NULL") // Ensure fa_piao_number does not exist
  25. }
  26. // Handle sorting logic
  27. if sort_type == 1 {
  28. if start_time_timestamp != 0 {
  29. db = db.Where("his_order.settle_accounts_date >= ?", start_time_timestamp)
  30. }
  31. if end_time_timestamp != 0 {
  32. db = db.Where("his_order.settle_accounts_date <= ?", end_time_timestamp)
  33. }
  34. } else {
  35. if len(start_time) != 0 {
  36. db = db.Where("his_order.setl_time >= ?", start_time+" 00:00:00")
  37. }
  38. if len(end_time) != 0 {
  39. db = db.Where("his_order.setl_time <= ?", end_time+" 23:59:59")
  40. }
  41. }
  42. if patient_id > 0 {
  43. db = db.Where("his_order.patient_id = ?", patient_id)
  44. }
  45. db = db.Where("his_order.status = 1 and his_order.order_status = 2")
  46. // Preload related data
  47. db = db.Preload("Patients", "status = 1 AND user_org_id = ?", user_org_id).
  48. Preload("HisPatient", "status = 1 AND user_org_id = ?", user_org_id).
  49. Preload("HisFaPiaoOrder", "status = 1")
  50. // Count the total number of records
  51. db = db.Count(&total)
  52. // Apply pagination
  53. db = db.Limit(limit).Offset(offset)
  54. // Execute query and return results
  55. err = db.Find(&order).Error
  56. //
  57. //for _, item := range order {
  58. // var hfpo models.HisFaPiaoOrder
  59. // readDb.Model(&models.HisFaPiaoOrder{}).Where("ticket_sn = ?", item.FaPiaoNumber).First(&hfpo)
  60. // item.HisFaPiaoOrder =
  61. //}
  62. return
  63. }
  64. func GetFaPiaoList(user_org_id int64, page int64, limit int64, start_time_timestamp int64, end_time_timestamp int64) (order []*models.HisFaPiaoOrder, err error, total int64) {
  65. offset := (page - 1) * limit
  66. db := readDb.Model(&models.HisFaPiaoOrder{})
  67. db = db.Preload("Patients", "status = 1 AND user_org_id = ?", user_org_id)
  68. db = db.Where("ctime >= ? and ctime <= ? and user_org_id = ? and status = 1 and pdf_url <> ''", start_time_timestamp, end_time_timestamp, user_org_id)
  69. // Count the total number of records
  70. db = db.Count(&total)
  71. // Apply pagination
  72. db = db.Limit(limit).Offset(offset)
  73. // Execute query and return results
  74. err = db.Find(&order).Error
  75. for _, item := range order {
  76. var os []*models.SettleAccountsDateOrder
  77. order_ids := strings.Split(item.OrderIds, ",")
  78. err := readDb.Model(&models.SettleAccountsDateOrder{}).Where("id in (?)", order_ids).Find(&os).Error
  79. if err == nil {
  80. item.SettleAccountsDateOrder = append(item.SettleAccountsDateOrder, os...)
  81. }
  82. }
  83. return
  84. }
  85. func GetFaPiaoOrderByIDS(ids []string) (orders []models.HisOrder, err error) {
  86. err = readDb.Model(&models.HisOrder{}).Where("id in (?) AND status = 1", ids).Find(&orders).Error
  87. return
  88. }
  89. func GetFaPiaoOrderById(id int64) (orders models.HisFaPiaoOrder, err error) {
  90. err = readDb.Model(&models.HisFaPiaoOrder{}).Where("id = ?", id).Find(&orders).Error
  91. return
  92. }
  93. func GetFaPiaoOrderInfoByNumbers(numbers []string) (infos []models.HisOrderInfo, err error) {
  94. err = readDb.Model(&models.HisOrderInfo{}).Where("order_number in (?)", numbers).Find(&infos).Error
  95. return
  96. }
  97. func SaveFaPiaoOrder(fapiao *models.HisFaPiaoOrder) (err error) {
  98. err = writeDb.Save(&fapiao).Error
  99. return
  100. }
  101. func UpdateFaPiaoNumber(number string, ids []string) (err error) {
  102. err = XTWriteDB().Model(&models.HisOrder{}).Where("id in (?)", ids).Updates(map[string]interface{}{"fa_piao_number": number}).Error
  103. return
  104. }
  105. func UpdateFaPiaoSN(number string, ids []string) (err error) {
  106. fmt.Println(ids)
  107. fmt.Println(number)
  108. err = XTWriteDB().Model(&models.HisOrder{}).Where("id in (?)", ids).Updates(map[string]interface{}{"fa_piao_sn": number}).Error
  109. return
  110. }
  111. func UpdateFaPiaoNumberByNumber(number string) (err error) {
  112. err = XTWriteDB().Model(&models.HisOrder{}).Where("fa_piao_number = ?", number).Updates(map[string]interface{}{"fa_piao_number": ""}).Error
  113. return
  114. }