good_pharmacy_service.go 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package service
  2. import (
  3. "XT_New/models"
  4. "XT_New/utils"
  5. "github.com/astaxie/beego/config"
  6. "github.com/jinzhu/gorm"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. func GetTodayGood(stime, etime, orgid, is_medicine int64, keyword string) (finlly []models.ListOfDrugs, err error) {
  12. if is_medicine == 0 {
  13. keyword = "%" + keyword + "%"
  14. err = XTReadDB().Raw("select pp.project_id as id, good.good_name as name, good.specification_name as specifications,good.sum_count as stock from his_prescription_project pp join xt_good_information good on pp.project_id = good.id and good.org_id = ? where pp.type =3 and pp.user_org_id = ? and pp.record_date >= ? and pp.record_date <= ? and is_medicine = ? and good.good_name like ? Group by pp.project_id",
  15. orgid, orgid, stime, etime, is_medicine, keyword).Scan(&finlly).Error
  16. } else {
  17. keyword = "%" + keyword + "%"
  18. err = XTReadDB().Raw("select pp.project_id as id, good.good_name as name, good.specification_name as specifications,good.sum_count as stock from his_prescription_project pp join xt_good_information good on pp.project_id = good.id and good.org_id = ? where pp.type =3 and pp.user_org_id = ? and pp.record_date >= ? and pp.record_date <= ? and is_medicine = ? and good.good_name like ? Group by pp.project_id",
  19. orgid, orgid, stime, etime, is_medicine, keyword).Scan(&finlly).Error
  20. }
  21. return
  22. }
  23. func FindGoodMedicationList(orgid, drug_id, stime, etime, is_medicine int64) (pp []*models.PatientInformation, err error) { ///////////////
  24. InitDrugidIsNil(orgid, stime, etime)
  25. var tmp []*models.HisPrescriptionProject
  26. err = XTReadDB().Model(&models.HisPrescriptionProject{}).Where(
  27. "status = 1 and record_date >= ? and record_date <= ? and user_org_id = ? and project_id = ? and is_medicine = ?", stime, etime, orgid, drug_id, is_medicine).Find(&tmp).Error
  28. if err != nil {
  29. return pp, err
  30. }
  31. for _, v := range tmp {
  32. num, _ := strconv.ParseInt(v.Count, 10, 64)
  33. pp = append(pp, &models.PatientInformation{
  34. Id: "h" + config.ToString(v.ID),
  35. Name: FindUserName(v.PatientId),
  36. PatientId: v.PatientId,
  37. SingleDosage: config.ToString(v.SingleDose) + v.Unit,
  38. Usage: v.DeliveryWay,
  39. Frequency: v.ExecutionFrequency,
  40. Days: config.ToString(v.Day) + "天",
  41. Total: config.ToString(v.Count) + v.Unit,
  42. DataSources: "his处方",
  43. Quantity: num,
  44. Unit: v.Unit,
  45. DrugCode: v.DrugCode,
  46. ID: v.ID,
  47. })
  48. }
  49. return
  50. }
  51. func GoodDeparture(ids string, creater, orgid int64) (err error) {
  52. //开事务
  53. tx := XTWriteDB().Begin()
  54. defer func() {
  55. if err != nil {
  56. utils.ErrorLog("事务失败,原因为: %v", err)
  57. tx.Rollback()
  58. } else {
  59. tx.Commit()
  60. }
  61. }()
  62. //处理下字符串
  63. t_ids := ""
  64. if ids[len(ids)-1] == 44 {
  65. t_ids = ids[:len(ids)-1]
  66. } else {
  67. t_ids = ids
  68. }
  69. time_now := time.Now().Unix()
  70. tmp_id := strings.Split(t_ids, ",")
  71. patient_id := make(map[int64]int64)
  72. for _, v := range tmp_id {
  73. front := v[:1]
  74. after := v[1:]
  75. if front == "h" {
  76. var advice_info []*models.HisPrescriptionProject
  77. err = tx.Model(&models.HisPrescriptionProject{}).Where("id = ? and status = 1", after).Find(&advice_info).Error
  78. if err != nil {
  79. return
  80. }
  81. for _, v := range advice_info {
  82. if _, ok := patient_id[v.PatientId]; !ok {
  83. patient_id[v.PatientId] = v.PatientId
  84. }
  85. }
  86. //修改状态
  87. errs := XTWriteDB().Model(&models.HisPrescriptionProject{}).Where("id = ?", after).Updates(map[string]interface{}{
  88. "is_medicine": 1,
  89. "people": creater,
  90. "dispensing_time": time_now,
  91. }).Error
  92. if errs != nil {
  93. return errs
  94. }
  95. err1 := ChangeGoodHisPrescriptionid(after)
  96. if err1 != nil {
  97. return err1
  98. }
  99. }
  100. }
  101. for _, v := range patient_id {
  102. //生成明细记录
  103. tmp_ph := models.Pharmary{
  104. UserOrgId: orgid,
  105. PatientId: v,
  106. Ctime: time.Now().Unix(),
  107. Mtime: time.Now().Unix(),
  108. Status: 1,
  109. RecordDate: time_now,
  110. }
  111. err = tx.Create(&tmp_ph).Error
  112. if err != nil {
  113. return err
  114. }
  115. }
  116. return
  117. }
  118. // 改变处方状态
  119. func ChangeGoodHisPrescriptionid(id string) (err error) {
  120. var advice models.HisPrescriptionProject
  121. err = XTReadDB().Model(&models.HisPrescriptionProject{}).Where("id = ? and status = 1", id).Find(&advice).Error
  122. if err != nil {
  123. if err == gorm.ErrRecordNotFound {
  124. return nil
  125. }
  126. return
  127. }
  128. err = XTWriteDB().Model(&models.HisPrintPrescription{}).Where("id = ?", advice.PrescriptionId).Updates(map[string]interface{}{
  129. "is_medicine": 1,
  130. "mtime": time.Now().Unix(),
  131. }).Error
  132. if err != nil {
  133. return
  134. }
  135. return
  136. }