Patient.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. "sync"
  14. "time"
  15. )
  16. // 患者
  17. type Patient struct {
  18. Id int `orm:"column(ID);size(11);auto;pk"`
  19. T_uuid string `orm:"size(32);null"` //
  20. T_uid int `orm:"size(200);null"` // 关联的用户id
  21. T_number string `orm:"size(256);null"` // 病历号
  22. T_name string `orm:"size(256);null"` // 姓名
  23. T_age string `orm:"size(200);null"` // 年轻
  24. T_tag string `orm:"size(200);null"` // 标签
  25. T_illness int `orm:"size(200);null"` // 诊断
  26. T_surgical int `orm:"size(200);null"` // 术式
  27. T_phone string `orm:"size(256);null"` // 电话号码
  28. T_next_time string `orm:"size(200);null"` // 下次复诊时间
  29. T_notice_phone int `orm:"size(200);null"` // 手机 1通知 0不通知
  30. T_notice_message int `orm:"size(200);null"` // 短信 1通知 0不通知
  31. T_notice_interval string `orm:"size(200);null"` // 0,0,0,0 提前1天,提前2天,提前3天,提前7天
  32. T_notice int `orm:"size(200);null"` // 通知状态 1待通知 2已通知
  33. //T_follow_up int `orm:"size(200);null"` // 复诊状态 1正常 2超时 3结束
  34. //T_record string `orm:"type(text);null"` // 2022-07-27,15|2022-07-27,15| 时间,间隔天数|
  35. T_State int `orm:"size(200);default(1)"` // 0删除 1正常
  36. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now 每次 model 保存时都会对时间自动更新
  37. UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now_add 第一次保存时才设置时间
  38. }
  39. type Patient_R struct {
  40. Id int
  41. T_uuid string //
  42. T_uid int // 关联的用户id
  43. T_number string // 病历号
  44. T_name string // 姓名
  45. T_age string // 年轻
  46. T_tag_str string // 标签
  47. T_tag []int // 标签
  48. T_tag_List []string // 标签
  49. T_illness int // 疾病
  50. T_illness_name string // 疾病名称
  51. T_surgical int // 术式
  52. T_surgical_name string // 术式名称
  53. T_phone string // 电话号码
  54. T_next_time string // 下次复诊时间
  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. type Patient_R2 struct {
  64. Id int `json:"id"`
  65. T_uuid string `json:"t_uuid"` //
  66. T_uid int `json:"t_uid"` // 关联的用户id
  67. T_name string `json:"t_name"`
  68. T_phone string `json:"t_phone"` // 电话号码
  69. T_notice_phone int `json:"t_notice_phone"` // 手机 1通知 0不通知
  70. T_notice_message int `json:"t_notice_message"` // 短信 1通知 0不通知
  71. T_notice_interval string `json:"t_notice_interval"` // 0,0,0,0 提前1天,提前2天,提前3天,提前7天
  72. T_patient_revisit_record_id int `json:"t_patient_revisit_record_id"` // 复诊记录id
  73. T_date string `json:"t_date"` // 复诊日期
  74. T_pid int `json:"t_pid"` // 患者id
  75. T_illness_notice int `json:"t_illness_notice"` // 复诊通知内容id
  76. T_remark string `json:"t_remark"` //
  77. }
  78. func (t *Patient) TableName() string {
  79. return "patient" // 数据库名称 // ************** 替换 FormulaList **************
  80. }
  81. var PatientMap *sync.Map // 泛型
  82. func init() {
  83. //注册模型
  84. orm.RegisterModel(new(Patient))
  85. PatientMap = new(sync.Map)
  86. }
  87. func PatientToPatient_R(r Patient) (m Patient_R) {
  88. m.Id = r.Id
  89. m.T_uuid = r.T_uuid
  90. m.T_uid = r.T_uid
  91. m.T_number = r.T_number
  92. m.T_name = r.T_name
  93. m.T_age = r.T_age
  94. m.T_tag_str = r.T_tag
  95. if len(r.T_tag) > 0 {
  96. T_tag := strings.Split(strings.Trim(r.T_tag, "|"), "|")
  97. for _, v := range T_tag {
  98. t, _ := strconv.Atoi(v)
  99. m.T_tag = append(m.T_tag, t)
  100. m.T_tag_List = append(m.T_tag_List, Tag.Read_Tag_Get(t))
  101. }
  102. }
  103. m.T_illness = r.T_illness
  104. m.T_illness_name = Illness.Read_Illness_Get(r.T_illness)
  105. m.T_surgical = r.T_surgical
  106. m.T_surgical_name = Surgical.Read_Surgical_Get(r.T_surgical)
  107. m.T_phone = r.T_phone
  108. m.T_next_time = r.T_next_time
  109. m.T_notice_phone = r.T_notice_phone
  110. m.T_notice_message = r.T_notice_message
  111. m.T_notice_interval = r.T_notice_interval
  112. m.T_notice = r.T_notice
  113. //m.T_follow_up = r.T_follow_up
  114. //m.T_record = r.T_record
  115. m.T_State = r.T_State
  116. return
  117. }
  118. // 添加
  119. func Add_Patient(r Patient) (id int64, err error) {
  120. o := orm.NewOrm()
  121. //生成编号
  122. rand_x := 0
  123. for true {
  124. r.T_uuid = lib.GetRandstring(32, "", int64(rand_x))
  125. err = o.Read(&r, "T_uuid")
  126. if err != nil {
  127. break
  128. }
  129. rand_x += 1
  130. }
  131. r.T_State = 1
  132. id, err = o.Insert(&r)
  133. if err != nil {
  134. logs.Error(lib.FuncName(), err)
  135. }
  136. return id, err
  137. }
  138. // 获取 ById
  139. func Read_Patient_ByT_uuid(T_uuid string) (r Patient, err error) {
  140. o := orm.NewOrm()
  141. qs := o.QueryTable(new(Patient))
  142. err = qs.Filter("T_uuid", T_uuid).Filter("T_State", 1).One(&r)
  143. if err != nil {
  144. logs.Error(lib.FuncName(), err)
  145. }
  146. return
  147. }
  148. // 获取 ById
  149. func Read_Patient_ById(Id int) (r Patient, err error) {
  150. o := orm.NewOrm()
  151. qs := o.QueryTable(new(Patient))
  152. err = qs.Filter("Id", Id).Filter("T_State", 1).One(&r)
  153. if err != nil {
  154. logs.Error(lib.FuncName(), err)
  155. }
  156. return
  157. }
  158. // 获取 ByT_number
  159. func Read_Patient_ByT_number(T_number string, T_uid int) (r Patient, err error) {
  160. o := orm.NewOrm()
  161. qs := o.QueryTable(new(Patient))
  162. err = qs.Filter("T_number", T_number).Filter("T_uid", T_uid).Filter("T_State", 1).One(&r)
  163. if err != nil {
  164. logs.Error(lib.FuncName(), err)
  165. }
  166. return
  167. }
  168. // 修改
  169. func Update_Patient(m Patient, cols ...string) error {
  170. o := orm.NewOrm()
  171. num, err := o.Update(&m, cols...)
  172. if err != nil {
  173. logs.Error(lib.FuncName(), err)
  174. return err
  175. }
  176. fmt.Println("Number of records updated in database:", num)
  177. return nil
  178. }
  179. // 删除
  180. func Delete_Patient(v Patient) error {
  181. o := orm.NewOrm()
  182. v.T_State = 0
  183. _, err := o.Update(&v, "T_State")
  184. if err != nil {
  185. logs.Error(lib.FuncName(), err)
  186. }
  187. return err
  188. }
  189. // 获取列表
  190. func Read_Patient_List(T_uid int, T_number, T_name string, T_tag, T_illness, T_surgical, T_notice, T_age_sort, T_next_time_sort, page, page_z int) (r_ []Patient_R, cnt int64) {
  191. o := orm.NewOrm()
  192. // 也可以直接使用 Model 结构体作为表名
  193. qs := o.QueryTable(new(Patient))
  194. var offset int64
  195. if page <= 1 {
  196. offset = 0
  197. } else {
  198. offset = int64((page - 1) * page_z)
  199. }
  200. // 过滤
  201. cond := orm.NewCondition()
  202. cond = cond.And("T_State__gt", 0)
  203. if T_uid > 0 {
  204. cond = cond.And("T_uid", T_uid)
  205. }
  206. if len(T_number) > 0 {
  207. cond = cond.And("T_number__icontains", T_number)
  208. }
  209. if len(T_name) > 0 {
  210. cond = cond.And("T_name__icontains", T_name)
  211. }
  212. if T_tag > 0 {
  213. cond = cond.And("T_tag__icontains", fmt.Sprintf("|%d|", T_tag))
  214. }
  215. if T_illness > 0 {
  216. cond = cond.And("T_illness", T_illness)
  217. }
  218. if T_surgical > 0 {
  219. cond = cond.And("T_surgical", T_surgical)
  220. }
  221. if T_notice > 0 {
  222. cond = cond.And("T_notice", T_notice)
  223. }
  224. var order []string
  225. if T_age_sort == 1 {
  226. order = append(order, "T_age")
  227. } else if T_age_sort == 2 {
  228. order = append(order, "-T_age")
  229. }
  230. if T_next_time_sort == 1 {
  231. order = append(order, "T_next_time")
  232. } else if T_next_time_sort == 2 {
  233. order = append(order, "-T_next_time")
  234. }
  235. order = append(order, "Id")
  236. // 查询
  237. var r []Patient
  238. var err error
  239. if page_z == 9999 {
  240. _, err = qs.SetCond((*orm2.Condition)(cond)).OrderBy(order...).All(&r)
  241. } else {
  242. _, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond)).OrderBy(order...).All(&r)
  243. }
  244. if err != nil {
  245. logs.Error(lib.FuncName(), err)
  246. return
  247. }
  248. cnt, err = qs.SetCond((*orm2.Condition)(cond)).Count()
  249. if err != nil {
  250. logs.Error(lib.FuncName(), err)
  251. return
  252. }
  253. for _, v := range r {
  254. r_ = append(r_, PatientToPatient_R(v))
  255. }
  256. return r_, cnt
  257. }
  258. func Read_Patient_List_Lt_NextTime(T_uid int, T_next_time string) (r_ []Patient_R, cnt int64) {
  259. o := orm.NewOrm()
  260. // 也可以直接使用 Model 结构体作为表名
  261. qs := o.QueryTable(new(Patient))
  262. // 过滤
  263. cond := orm.NewCondition()
  264. cond = cond.And("T_State__gt", 0)
  265. if T_uid > 0 {
  266. cond = cond.And("T_uid", T_uid)
  267. }
  268. if len(T_next_time) > 0 {
  269. cond = cond.And("T_next_time__lte", T_next_time)
  270. }
  271. // 查询
  272. var r []Patient
  273. var err error
  274. _, err = qs.SetCond((*orm2.Condition)(cond)).All(&r)
  275. if err != nil {
  276. logs.Error(lib.FuncName(), err)
  277. return
  278. }
  279. cnt, err = qs.SetCond((*orm2.Condition)(cond)).Count()
  280. if err != nil {
  281. logs.Error(lib.FuncName(), err)
  282. return
  283. }
  284. for _, v := range r {
  285. r_ = append(r_, PatientToPatient_R(v))
  286. }
  287. return r_, cnt
  288. }
  289. // 患者通知列表
  290. func Read_Patient_List_For_Notice(T_uid int, nowTime string) (r_ []Patient_R2) {
  291. o := orm.NewOrm()
  292. sql := "SELECT p.ID as id,t_uid,t_name,t_phone,t_notice_phone,t_notice_message,t_notice_interval," +
  293. "prr.ID as t_patient_revisit_record_id,t_date,t_illness_notice,t_remark" +
  294. " from patient p INNER JOIN patient_revisit_record prr ON p.id = prr.t_pid WHERE p.t__state > 0 AND p.t_uid = ? AND prr.t_date > '" + nowTime + "'"
  295. _, err := o.Raw(sql, T_uid).QueryRows(&r_)
  296. if err != nil {
  297. logs.Error(lib.FuncName(), err)
  298. }
  299. return r_
  300. }
  301. func Read_Patient_All_Map(T_uid int) {
  302. o := orm.NewOrm()
  303. // 也可以直接使用 Model 结构体作为表名
  304. qs := o.QueryTable(new(Patient))
  305. var maps []Patient
  306. if T_uid > 0 {
  307. qs.Filter("T_uid", T_uid).All(&maps)
  308. } else {
  309. qs.All(&maps)
  310. }
  311. for _, v := range maps {
  312. PatientMap.Store(v.Id, v.T_name)
  313. }
  314. }
  315. func Read_Patient_T_name_Get(T_pid int) string {
  316. v, ok := PatientMap.Load(T_pid)
  317. if ok {
  318. return v.(string)
  319. } else {
  320. return ""
  321. }
  322. }