fapiao_service.go 4.8KB

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