Patient.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. package Patient
  2. import (
  3. "FollowUp_Notice/lib"
  4. "FollowUp_Notice/models/Illness"
  5. "FollowUp_Notice/models/Surgical"
  6. "FollowUp_Notice/models/Tag"
  7. "fmt"
  8. "github.com/astaxie/beego/logs"
  9. "github.com/beego/beego/v2/adapter/orm"
  10. orm2 "github.com/beego/beego/v2/client/orm"
  11. "strconv"
  12. "strings"
  13. "time"
  14. )
  15. // 患者
  16. type Patient struct {
  17. Id int `orm:"column(ID);size(11);auto;pk"`
  18. T_uuid string `orm:"size(32);null"` //
  19. T_uid int `orm:"size(200);null"` // 关联的用户id
  20. T_number string `orm:"size(256);null"` // 病历号
  21. T_name string `orm:"size(256);null"` // 姓名
  22. T_age int `orm:"size(200);null"` // 年轻
  23. T_tag string `orm:"size(200);null"` // 标签
  24. T_illness int `orm:"size(200);null"` // 诊断
  25. T_surgical int `orm:"size(200);null"` // 术式
  26. T_phone string `orm:"size(256);null"` // 电话号码
  27. T_next_time string `orm:"size(200);null"` // 下次复诊时间
  28. T_notice_phone int `orm:"size(200);null"` // 手机 1通知 0不通知
  29. T_notice_message int `orm:"size(200);null"` // 短信 1通知 0不通知
  30. T_notice_interval string `orm:"size(200);null"` // 0,0,0,0 提前1天,提前2天,提前3天,提前7天
  31. T_notice int `orm:"size(200);null"` // 通知状态 1待通知 2已通知
  32. T_follow_up int `orm:"size(200);null"` // 复诊状态 1正常 2超时 3结束
  33. T_record string `orm:"type(text);null"` // 2022-07-27,15|2022-07-27,15| 时间,间隔天数|
  34. T_State int `orm:"size(200);default(1)"` // 0删除 1正常
  35. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now 每次 model 保存时都会对时间自动更新
  36. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now_add 第一次保存时才设置时间
  37. }
  38. type Patient_R struct {
  39. Id int
  40. T_uuid string //
  41. T_uid int // 关联的用户id
  42. T_number string // 病历号
  43. T_name string // 姓名
  44. T_age int // 年轻
  45. T_tag_str string // 标签
  46. T_tag []int // 标签
  47. T_tag_List []string // 标签
  48. T_illness int // 疾病
  49. T_illness_name string // 疾病名称
  50. T_surgical int // 术式
  51. T_surgical_name string // 术式名称
  52. T_phone string // 电话号码
  53. T_next_time string // 下次复诊时间
  54. T_next_interval int // 下次复诊间隔天数
  55. T_notice_phone int // 手机 1通知 0不通知
  56. T_notice_message int // 短信 1通知 0不通知
  57. T_notice_interval string // 0,0,0,0 提前1天,提前2天,提前3天,提前7天
  58. T_notice int // 待通知 已通知
  59. T_follow_up int // 复诊 超时 结束
  60. T_record string // 2022-07-27,15|2022-07-27,15| 时间,间隔天数|
  61. T_State int // 0删除 1正常
  62. }
  63. func (t *Patient) TableName() string {
  64. return "patient" // 数据库名称 // ************** 替换 FormulaList **************
  65. }
  66. func init() {
  67. //注册模型
  68. orm.RegisterModel(new(Patient))
  69. }
  70. func PatientToPatient_R(r Patient) (m Patient_R) {
  71. m.Id = r.Id
  72. m.T_uuid = r.T_uuid
  73. m.T_uid = r.T_uid
  74. m.T_number = r.T_number
  75. m.T_name = r.T_name
  76. m.T_age = r.T_age
  77. m.T_tag_str = r.T_tag
  78. if len(r.T_tag) > 0 {
  79. T_tag := strings.Split(strings.Trim(r.T_tag, "|"), "|")
  80. for _, v := range T_tag {
  81. t, _ := strconv.Atoi(v)
  82. m.T_tag = append(m.T_tag, t)
  83. m.T_tag_List = append(m.T_tag_List, Tag.Read_Tag_Get(t))
  84. }
  85. }
  86. m.T_illness = r.T_illness
  87. m.T_illness_name = Illness.Read_Illness_Get(r.T_illness)
  88. m.T_surgical = r.T_surgical
  89. m.T_surgical_name = Surgical.Read_Surgical_Get(r.T_surgical)
  90. m.T_phone = r.T_phone
  91. m.T_next_time = r.T_next_time
  92. m.T_notice_phone = r.T_notice_phone
  93. m.T_notice_message = r.T_notice_message
  94. m.T_notice_interval = r.T_notice_interval
  95. m.T_notice = r.T_notice
  96. m.T_follow_up = r.T_follow_up
  97. m.T_record = r.T_record
  98. m.T_State = r.T_State
  99. return
  100. }
  101. // 添加
  102. func Add_Patient(r Patient) (id int64, err error) {
  103. o := orm.NewOrm()
  104. //生成编号
  105. rand_x := 0
  106. for true {
  107. r.T_uuid = lib.GetRandstring(32, "", int64(rand_x))
  108. err = o.Read(&r, "T_uuid")
  109. if err != nil {
  110. break
  111. }
  112. rand_x += 1
  113. }
  114. r.T_State = 1
  115. id, err = o.Insert(&r)
  116. if err != nil {
  117. logs.Error(lib.FuncName(), err)
  118. }
  119. return id, err
  120. }
  121. // 获取 ById
  122. func Read_Patient_ByT_uuid(T_uuid string) (r Patient, err error) {
  123. o := orm.NewOrm()
  124. qs := o.QueryTable(new(Patient))
  125. err = qs.Filter("T_uuid", T_uuid).One(&r)
  126. if err != nil {
  127. logs.Error(lib.FuncName(), err)
  128. }
  129. return
  130. }
  131. // 修改
  132. func Update_Patient(m Patient, cols ...string) error {
  133. o := orm.NewOrm()
  134. num, err := o.Update(&m, cols...)
  135. if err != nil {
  136. logs.Error(lib.FuncName(), err)
  137. return err
  138. }
  139. fmt.Println("Number of records updated in database:", num)
  140. return nil
  141. }
  142. // 删除
  143. func Delete_Patient(v Patient) error {
  144. o := orm.NewOrm()
  145. v.T_State = 0
  146. _, err := o.Update(&v, "T_State")
  147. if err != nil {
  148. logs.Error(lib.FuncName(), err)
  149. }
  150. return err
  151. }
  152. // 获取列表
  153. func Read_Patient_List(T_uid int, T_number, T_name string, T_tag, T_illness, T_surgical, T_notice, T_follow_up, T_age_sort, T_next_time_sort, page, page_z int) (r_ []Patient_R, cnt int64) {
  154. o := orm.NewOrm()
  155. // 也可以直接使用 Model 结构体作为表名
  156. qs := o.QueryTable(new(Patient))
  157. var offset int64
  158. if page <= 1 {
  159. offset = 0
  160. } else {
  161. offset = int64((page - 1) * page_z)
  162. }
  163. // 过滤
  164. cond := orm.NewCondition()
  165. cond = cond.And("T_State__gt", 0)
  166. if T_uid > 0 {
  167. cond = cond.And("T_uid", T_uid)
  168. }
  169. if len(T_number) > 0 {
  170. cond = cond.And("T_number__icontains", T_number)
  171. }
  172. if len(T_name) > 0 {
  173. cond = cond.And("T_name__icontains", T_name)
  174. }
  175. if T_tag > 0 {
  176. cond = cond.And("T_tag__icontains", fmt.Sprintf("|%d|", T_tag))
  177. }
  178. if T_illness > 0 {
  179. cond = cond.And("T_illness", T_illness)
  180. }
  181. if T_surgical > 0 {
  182. cond = cond.And("T_surgical", T_surgical)
  183. }
  184. if T_notice > 0 {
  185. cond = cond.And("T_notice", T_notice)
  186. }
  187. if T_follow_up > 0 {
  188. cond = cond.And("T_follow_up", T_follow_up)
  189. }
  190. var order []string
  191. if T_age_sort == 1 {
  192. order = append(order, "T_age")
  193. } else if T_age_sort == 2 {
  194. order = append(order, "-T_age")
  195. }
  196. if T_next_time_sort == 1 {
  197. order = append(order, "T_next_time")
  198. } else if T_next_time_sort == 2 {
  199. order = append(order, "-T_next_time")
  200. }
  201. order = append(order, "-Id")
  202. // 查询
  203. var r []Patient
  204. var err error
  205. if page_z == 9999 {
  206. _, err = qs.SetCond((*orm2.Condition)(cond)).OrderBy(order...).All(&r)
  207. } else {
  208. _, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond)).OrderBy(order...).All(&r)
  209. }
  210. if err != nil {
  211. logs.Error(lib.FuncName(), err)
  212. return
  213. }
  214. cnt, err = qs.SetCond((*orm2.Condition)(cond)).Count()
  215. if err != nil {
  216. logs.Error(lib.FuncName(), err)
  217. return
  218. }
  219. for _, v := range r {
  220. r_ = append(r_, PatientToPatient_R(v))
  221. }
  222. return r_, cnt
  223. }