fapiao_service.go 4.3KB

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