his_project_service.go 32KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. package service
  2. import (
  3. "time"
  4. "XT_New/models"
  5. "github.com/jinzhu/gorm"
  6. )
  7. func GetHisProjectIsExist(projectName string, orgid int64) (*models.XtHisProject, error) {
  8. var project models.XtHisProject
  9. var err error
  10. err = XTReadDB().Model(&project).Where("project_name = ? and user_org_id = ? and status = 1", projectName, orgid).Find(&project).Error
  11. if err == gorm.ErrRecordNotFound {
  12. return nil, err
  13. }
  14. if err != nil {
  15. return nil, err
  16. }
  17. return &project, nil
  18. }
  19. func CreateHisProject(project *models.XtHisProject) error {
  20. err := XTWriteDB().Model(&project).Create(&project).Error
  21. return err
  22. }
  23. func GetHisProjectList(orgid int64, limit int64, page int64, charge int64, start int64, keyword string, isMark int64) (hisproject []*models.XtHisProject, total int64, err error) {
  24. offset := (page - 1) * limit
  25. likeKey := "%" + keyword + "%"
  26. db := XTReadDB().Table("xt_his_project as x").Where("x.status = 1")
  27. if orgid > 0 {
  28. db = db.Where("x.user_org_id = ?", orgid)
  29. }
  30. if start > 0 {
  31. db = db.Where("x.medical_status = ?", start)
  32. }
  33. if charge > 0 {
  34. db = db.Where("x.is_estimate = ? ", charge)
  35. }
  36. if len(keyword) > 0 {
  37. db = db.Where("x.project_name like ?", likeKey)
  38. }
  39. if isMark > 0 {
  40. db = db.Where("x.is_mark = ?", isMark)
  41. }
  42. err = db.Select("x.id,x.project_name,x.pinyin,x.wubi,x.price,x.unit,x.cost_classify,x.executive_section,x.medical_coverage,x.statistical_classification,x.disease_directory,x.is_record,x.medical_code,x.tube_color,x.medical_status,x.remark,x.sign,x.default_number,x.is_default,x.is_charge,x.is_estimate,x.is_workload,x.sort,x.doctor_advice,x.user_org_id,x.status,x.created_time,x.is_mark,x.social_security_directory_code").Count(&total).Offset(offset).Limit(limit).Order("created_time desc").Find(&hisproject).Error
  43. return hisproject, total, err
  44. }
  45. func GetProjectDetail(id int64) (models.XtHisProject, error) {
  46. project := models.XtHisProject{}
  47. err := XTReadDB().Model(&project).Where("id=? and status = 1", id).Find(&project).Error
  48. return project, err
  49. }
  50. func UpdatedProject(id int64, pro *models.XtHisProject) error {
  51. err := XTWriteDB().Model(&pro).Where("id=?", id).Updates(map[string]interface{}{"project_name": pro.ProjectName, "pinyin": pro.Pinyin, "wubi": pro.Pinyin, "price": pro.Price, "unit": pro.Unit, "cost_classify": pro.CostClassify, "executive_section": pro.ExecutiveSection, "medical_coverage": pro.MedicalCoverage, "statistical_classification": pro.StatisticalClassification, "disease_directory": pro.DiseaseDirectory, "is_record": pro.IsRecord, "medical_code": pro.MedicalCode, "tube_color": pro.TubeColor, "medical_status": pro.MedicalStatus, "remark": pro.Remark, "sign": pro.Sign, "default_number": pro.DefaultNumber, "is_default": pro.IsDefault, "is_charge": pro.IsCharge, "is_estimate": pro.IsEstimate, "is_workload": pro.IsWorkload, "sort": pro.Sort, "doctor_advice": pro.DoctorAdvice, "single_dose": pro.SingleDose, "execution_frequency": pro.ExecutionFrequency, "delivery_way": pro.DeliveryWay, "number_days": pro.NumberDays, "total": pro.Total, "updated_time": pro.UpdatedTime, "category": pro.Category, "specail_project": pro.SpecailProject, "social_security_directory_code": pro.SocialSecurityDirectoryCode, "record_date": pro.RecordDate, "is_print": pro.IsPrint, "bbx01": pro.Bbx01, "bby01": pro.Bby01, "zuobiao_id": pro.ZuobiaoId, "bck01b": pro.Bck01b, "is_show": pro.IsShow}).Error
  52. return err
  53. }
  54. func DeleteHisProject(id int64) error {
  55. project := models.XtHisProject{}
  56. err := XTWriteDB().Model(&project).Where("id=?", id).Updates(map[string]interface{}{"status": 0}).Error
  57. return err
  58. }
  59. func GetHisProjectByNameOne(projectName string, orgid int64) (*models.XtHisProjectTeam, error) {
  60. projectTeam := models.XtHisProjectTeam{}
  61. err := XTReadDB().Model(&projectTeam).Where("project_team = ? and user_org_id = ? and status = 1", projectName, orgid).First(&projectTeam).Error
  62. if err != nil {
  63. if err == gorm.ErrRecordNotFound {
  64. return nil, err
  65. } else {
  66. return nil, err
  67. }
  68. }
  69. return &projectTeam, nil
  70. }
  71. func CreatedProjectTeam(team *models.XtHisProjectTeam) error {
  72. err := XTWriteDB().Model(&team).Create(&team).Error
  73. return err
  74. }
  75. func GetProjectTeamList(limit int64, page int64, orgid int64, keyword string) (projectTeam []*models.XtHisProjectTeam, total int64, err error) {
  76. offset := (page - 1) * limit
  77. likeKey := "%" + keyword + "%"
  78. db := XTReadDB().Model(&projectTeam).Where("status=1")
  79. if len(keyword) > 0 {
  80. db = db.Where("project_team like ?", likeKey)
  81. }
  82. if orgid > 0 {
  83. db = db.Where("user_org_id = ?", orgid)
  84. }
  85. err = db.Count(&total).Offset(offset).Limit(limit).Order("sort asc").Find(&projectTeam).Error
  86. return projectTeam, total, err
  87. }
  88. func GetProjectTeamDetail(id int64) (models.XtHisProjectTeam, error) {
  89. projectTeam := models.XtHisProjectTeam{}
  90. err := XTReadDB().Model(&projectTeam).Where("id= ? and status = 1", id).Find(&projectTeam).Error
  91. return projectTeam, err
  92. }
  93. func UpdatedProjectTeam(id int64, team *models.XtHisProjectTeam) error {
  94. err := XTWriteDB().Model(&team).Where("id=? and status = 1", id).Updates(map[string]interface{}{"project_team": team.ProjectTeam, "price": team.Price, "pinyin": team.Pinyin, "wubi": team.Wubi, "tube_color": team.TubeColor, "team_type": team.TeamType, "remark": team.Remark, "project_id": team.ProjectId, "item_id": team.ItemId, "sort": team.Sort}).Error
  95. return err
  96. }
  97. func DeleteProjectTeam(id int64) error {
  98. team := models.XtHisProjectTeam{}
  99. err := XTWriteDB().Model(&team).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  100. return err
  101. }
  102. func CreateDePartment(department *models.XtHisDepartment) error {
  103. err := XTWriteDB().Model(&department).Create(&department).Error
  104. return err
  105. }
  106. func GetDepartMentList(limit int64, page int64, orgid int64) (departMent []*models.XtHisDepartment, total int64, err error) {
  107. offset := (page - 1) * limit
  108. err = XTReadDB().Model(&departMent).Where("user_org_id = ? and status = 1", orgid).Count(&total).Offset(offset).Limit(limit).Order("created_time desc").Find(&departMent).Error
  109. return departMent, total, err
  110. }
  111. func GetDepartMentDetail(id int64) (models.XtHisDepartment, error) {
  112. department := models.XtHisDepartment{}
  113. err := XTReadDB().Model(&department).Where("id=? and status = 1", id).Find(&department).Error
  114. return department, err
  115. }
  116. func UpdatedDepartment(id int64, department *models.XtHisDepartment) error {
  117. err := XTWriteDB().Model(&department).Where("id=? and status = 1", id).Updates(map[string]interface{}{"name": department.Name, "number": department.Number, "updated_time": time.Now().Unix()}).Error
  118. return err
  119. }
  120. func DeleteDepartment(id int64) error {
  121. department := models.XtHisDepartment{}
  122. err := XTWriteDB().Model(&department).Where("id=? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  123. return err
  124. }
  125. func GetBloodPatientList(orgid int64, startime int64) (schedule []*models.SchedulesVm, err error) {
  126. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  127. if orgid > 0 {
  128. db = db.Where("x.user_org_id = ?", orgid)
  129. }
  130. if startime > 0 {
  131. db = db.Where("x.schedule_date = ?", startime)
  132. }
  133. err = db.Select("x.id,x.user_org_id,x.patient_id,x.schedule_date,x.schedule_type,x.schedule_week,x.mode_id,p.name,p.id_card_no").Joins("left join xt_patients as p on p.id = x.patient_id").Scan(&schedule).Error
  134. return schedule, err
  135. }
  136. func GetHisPatient(orgid int64, recorddate int64) (paitent []*models.HisPatient, err error) {
  137. err = XTReadDB().Model(&paitent).Where("user_org_id = ? and record_date = ? and status = 1", orgid, recorddate).Find(&paitent).Error
  138. return paitent, err
  139. }
  140. func GetHisPrescriptionOther(orgid int64, recorddate int64) (prescription []*models.HisPrescriptionVm, err error) {
  141. db := XTReadDB().Table("his_prescription as x").Where("x.status = 1")
  142. if orgid > 0 {
  143. db = db.Where("x.user_org_id = ?", orgid)
  144. }
  145. if recorddate > 0 {
  146. db = db.Where("x.record_date = ? ", recorddate)
  147. }
  148. err = db.Select("x.id,x.user_org_id,x.record_date,x.patient_id,x.his_patient_id,x.number,x.type,x.doctor,x.creator,x.modifier,t.name").Joins("left join his_patient as t on t.id = x.his_patient_id").Scan(&prescription).Error
  149. return prescription, err
  150. }
  151. func GetHisPrescriptionByPatientId(id int64, recordate int64) (prescription []*models.HisPrescription, err error) {
  152. err = XTReadDB().Model(&prescription).Where("his_patient_id = ? and record_date = ? and status= 1", id, recordate).Find(&prescription).Error
  153. return prescription, err
  154. }
  155. func CreateAdditionalCharge(charge *models.HisAdditionalCharge) error {
  156. err = XTWriteDB().Model(&charge).Save(&charge).Error
  157. return err
  158. }
  159. func GetTreatmentList(id int64, startime int64) (prescription []*models.HisPrescription, err error) {
  160. err = XTReadDB().Model(&prescription).Where("id = ? and record_date = ? and status = 1", id, startime).Find(&prescription).Error
  161. return prescription, err
  162. }
  163. //func GetAllProjectList(orgid int64) (project []*models.XtHisProject, err error) {
  164. //
  165. // err = XTReadDB().Model(&project).Where("user_org_id = ? and status = 1", orgid).Find(&project).Error
  166. // return project, err
  167. //}
  168. func CreateProjectList(list *models.XtHisProjectList) error {
  169. err := XTWriteDB().Model(&list).Create(&list).Error
  170. return err
  171. }
  172. func GetHisProjectListByOrgId(orgid int64) (projectlist []*models.HisProjectListVm, err error) {
  173. db := XTReadDB().Table("xt_his_project_list as x").Where("x.status = 1")
  174. if orgid > 0 {
  175. db = db.Where("x.user_org_id = ? ", orgid)
  176. }
  177. err = db.Select("x.id,x.number,x.user_org_id,x.project_id,t.price,t.unit,t.cost_classify,t.project_name").Joins("left join xt_his_project as t on t.id = x.project_id").Scan(&projectlist).Error
  178. return projectlist, err
  179. }
  180. func GetHisPatientInformation(id int64) (models.HisPatient, error) {
  181. patient := models.HisPatient{}
  182. err := XTReadDB().Model(&patient).Where("id= ? and status = 1", id).Find(&patient).Error
  183. return patient, err
  184. }
  185. func DeleteProjectList(id int64) error {
  186. list := models.XtHisProjectList{}
  187. err := XTWriteDB().Model(&list).Where("id=? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  188. return err
  189. }
  190. func GetHisProject(orgid int64) (project []*models.XtHisProject, err error) {
  191. err = XTReadDB().Model(&project).Where("user_org_id = ? and status =1 and medical_status!=1", orgid).Order("sort asc").Find(&project).Error
  192. return project, err
  193. }
  194. func GetProjectTeam(ids []string, orgid int64) (project []*models.XtHisProject, err error) {
  195. err = XTReadDB().Model(&project).Where("user_org_id = ? and id in (?)", orgid, ids).Find(&project).Error
  196. return project, err
  197. }
  198. func GetAllDepartMent(orgid int64) (department []*models.XtHisDepartment, err error) {
  199. err = XTReadDB().Model(&department).Where("user_org_id = ? and status =1", orgid).Find(&department).Error
  200. return department, err
  201. }
  202. func CreateHisPatient(patient *models.XtHisPatient) error {
  203. err := XTWriteDB().Model(&patient).Create(&patient).Error
  204. return err
  205. }
  206. func UpdateHisPrescriptionHisID(his_patient_id int64, patient_id int64, record_time int64, org_id int64) {
  207. XTWriteDB().Model(&models.HisPrescription{}).Where("record_date = ? AND user_org_id = ? AND patient_id = ? AND his_patient_id = 0", record_time, org_id, patient_id).Updates(map[string]interface{}{"his_patient_id": his_patient_id})
  208. XTWriteDB().Model(&models.HisPrescriptionInfo{}).Where("record_date = ? AND user_org_id = ? AND patient_id = ? AND his_patient_id = 0", record_time, org_id, patient_id).Updates(map[string]interface{}{"his_patient_id": his_patient_id})
  209. return
  210. }
  211. func UpdateHisPrescriptionHisIDByPID(his_patient_id int64, patient_id int64, record_time int64, org_id int64, ids []string) error {
  212. err := XTWriteDB().Model(&models.HisPrescription{}).Where("id in (?) and record_date = ? AND user_org_id = ? AND patient_id = ? ", ids, record_time, org_id, patient_id).Updates(map[string]interface{}{"his_patient_id": his_patient_id}).Error
  213. //XTWriteDB().Model(&models.HisPrescriptionInfo{}).Where("record_date = ? AND user_org_id = ? AND patient_id = ? AND his_patient_id = 0", record_time, org_id, patient_id).Updates(map[string]interface{}{"his_patient_id": his_patient_id})
  214. return err
  215. }
  216. func UpdateHisPrescriptionHisIDTwo(his_patient_id int64, patient_id int64, record_time int64, org_id int64, start_time int64) {
  217. XTWriteDB().Model(&models.HisPrescription{}).Where("record_date <= ? AND record_date >= ? AND user_org_id = ? AND patient_id = ? AND his_patient_id = 0 AND order_status <> 2", record_time, start_time, org_id, patient_id).Updates(map[string]interface{}{"his_patient_id": his_patient_id})
  218. XTWriteDB().Model(&models.HisPrescriptionInfo{}).Where("record_date = ? AND user_org_id = ? AND patient_id = ? AND his_patient_id = 0 AND order_status <> 2", record_time, start_time, org_id, patient_id).Updates(map[string]interface{}{"his_patient_id": his_patient_id})
  219. return
  220. }
  221. func GetBloodPatientByIdCard(idcard string, orgid int64) (*models.XtPatients, error) {
  222. var patients models.XtPatients
  223. var err error
  224. err = XTReadDB().Model(&patients).Where("id_card_no = ? and user_org_id = ? and status = 1", idcard, orgid).Find(&patients).Error
  225. if err != nil {
  226. if err == gorm.ErrRecordNotFound {
  227. return nil, err
  228. } else {
  229. return nil, err
  230. }
  231. }
  232. return &patients, nil
  233. }
  234. func GetAllProjectTeam(orgid int64) (project []*models.XtHisProjectTeam, err error) {
  235. err = XTReadDB().Model(&project).Where("user_org_id = ? and status = 1", orgid).Find(&project).Error
  236. return project, err
  237. }
  238. func GetProjectListById(orgid int64, ids []string) (project []*models.XtHisProjectList, err error) {
  239. err = XTReadDB().Model(&project).Preload("XtHisProject", "status = 1").Preload("GoodInfo", "status=1").Where("user_org_id = ? and id in (?) and status = 1", orgid, ids).Find(&project).Error
  240. return project, err
  241. }
  242. func GetHisPatientHistory(keyword string, startime int64, endtime int64, registtype int64, limit int64, page int64, orgid int64) (hisPatient []*models.XtHisPatient, total int64, err error) {
  243. offset := (page - 1) * limit
  244. likeKey := "%" + keyword + "%"
  245. db := XTReadDB().Table("his_patient as x")
  246. if len(keyword) > 0 {
  247. db = db.Where("x.name like ?", likeKey)
  248. }
  249. if startime > 0 {
  250. db = db.Where("x.record_date >= ?", startime)
  251. }
  252. if endtime > 0 {
  253. db = db.Where("x.record_date<=?", endtime)
  254. }
  255. if registtype > 0 {
  256. db = db.Where("x.register_type = ?", registtype)
  257. }
  258. if orgid > 0 {
  259. db = db.Where("x.user_org_id = ?", orgid)
  260. }
  261. err = db.Select("x.id,x.balance_accounts_type,x.medical_insurance_number,x.name,x.gender,x.id_type,x.medical_treatment_type,x.birthday,x.record_date,x.age,x.phone_number,x.id_card_no,x.register_type,x.admin_user_id,x.departments,x.is_need_cost_of_production,x.register_cost,x.treatment_cost,x.cost_of_production,x.total,x.user_org_id,x.patient_id,x.number,x.is_return,x.doctor,x.ctime,x.social_type,x.phone,x.status").Count(&total).Offset(offset).Limit(limit).Order("x.id desc").Find(&hisPatient).Error
  262. return hisPatient, total, err
  263. }
  264. func ChangePatient(id int64) error {
  265. patient := models.XtHisPatient{}
  266. err := XTWriteDB().Model(&patient).Where("id = ?", id).Updates(map[string]interface{}{"is_return": 2}).Error
  267. return err
  268. }
  269. func GetHisPrescriptionTwo(patientid int64, orgid int64, recorddate int64) (*models.HisPrescription, error) {
  270. prescription := models.HisPrescription{}
  271. err := XTReadDB().Model(&prescription).Where("his_patient_id = ? and user_org_id = ? and record_date = ? and status = 1", patientid, orgid, recorddate).Find(&prescription).Error
  272. if err != nil {
  273. if err == gorm.ErrRecordNotFound {
  274. return nil, err
  275. } else {
  276. return nil, err
  277. }
  278. }
  279. return &prescription, nil
  280. }
  281. func UpdatedHisPatient(hispatientid int64, orgid int64, recorddate int64) error {
  282. hisPatient := models.HisPatient{}
  283. err := XTWriteDB().Model(&hisPatient).Where("id = ? and user_org_id = ? and record_date = ?", hispatientid, orgid, recorddate).Updates(map[string]interface{}{"is_return": 3}).Error
  284. return err
  285. }
  286. func GetBloodPatientInfoById(patientid int64) (models.XtPatients, error) {
  287. patients := models.XtPatients{}
  288. err := XTReadDB().Model(&patients).Where("id = ? and status = 1", patientid).Find(&patients).Error
  289. return patients, err
  290. }
  291. func GetPatientCaseHistory(patientid int64, record_date int64) (models.HisPatientCaseHistory, error) {
  292. history := models.HisPatientCaseHistory{}
  293. db := XTReadDB().Model(&history).Where("status=1")
  294. if patientid > 0 {
  295. db = db.Where("patient_id =?", patientid)
  296. }
  297. if record_date > 0 {
  298. db = db.Where("record_date = ?", record_date)
  299. }
  300. err := db.Last(&history).Error
  301. return history, err
  302. }
  303. func GetBloodPatientByPatient(id int64) (models.XtPatients, error) {
  304. patients := models.XtPatients{}
  305. err := XTReadDB().Model(&patients).Where("id = ? and status = 1", id).Find(&patients).Error
  306. return patients, err
  307. }
  308. func GetHisPatientByIdFour(patientid int64) (models.XtHisPatient, error) {
  309. patient := models.XtHisPatient{}
  310. err := XTReadDB().Model(&patient).Where("patient_id = ? and status=1", patientid).Find(&patient).Error
  311. return patient, err
  312. }
  313. func GetHisPatientById(patientid int64) (models.VMHisPatient, error) {
  314. patient := models.VMHisPatient{}
  315. err := XTReadDB().Model(&patient).Where("patient_id = ? and status = 1", patientid).Find(&patient).Error
  316. return patient, err
  317. }
  318. func GetHisPatientByIdTwo(patientid int64) (models.VMHisPatient, error) {
  319. patient := models.VMHisPatient{}
  320. err := XTReadDB().Model(&patient).Where("patient_id = ? and status = 1", patientid).Limit(&patient).Error
  321. return patient, err
  322. }
  323. func GetTemplateDetail(id int64) (models.HisCaseHistoryTemplate, error) {
  324. template := models.HisCaseHistoryTemplate{}
  325. err := XTReadDB().Model(&template).Where("id= ? and status = 1", id).Find(&template).Error
  326. return template, err
  327. }
  328. func UpdateCaseHistoryTemplate(tem *models.HisCaseHistoryTemplate, id int64) error {
  329. err := XTWriteDB().Model(&tem).Where("id = ? ", id).Updates(map[string]interface{}{"history_of_present_illness": tem.HistoryOfPresentIllness, "past_history": tem.PastHistory, "chief_conplaint": tem.ChiefConplaint, "personal_history": tem.PersonalHistory, "family_history": tem.FamilyHistory, "diagnostic": tem.Diagnostic, "template_name": tem.TemplateName, "template_remark": tem.TemplateRemark, "modifier": tem.Modifier, "doctor_advice": tem.DoctorAdvice, "remark": tem.Remark}).Error
  330. return err
  331. }
  332. func GetHistPatient(orgid int64, keyword string) (hisPatient []*models.HisPatient, err error) {
  333. likeKey := "%" + keyword + "%"
  334. err = XTReadDB().Model(&hisPatient).Where("user_org_id = ? and name like ?", orgid, likeKey).Find(&hisPatient).Error
  335. return hisPatient, err
  336. }
  337. func GetDoctorAdvicePrint(his_patient_id int64, recorddate int64, schIDs []string, orgid int64, p_type int64) (prescription []*models.HisPrescription, err error) {
  338. err = XTReadDB().Model(&prescription).Where("patient_id = ? and record_date = ? and id in(?) and status = 1 and p_type = ? ", his_patient_id, recorddate, schIDs, p_type).Preload("HisPatient", "status = 1 and user_org_id = ?", orgid).Preload("Patients", "status = 1 and user_org_id = ?", orgid).Preload("HisDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
  339. return db.Where("status = 1 and user_org_id = ?", orgid).Preload("Drug", "status = 1")
  340. }).Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
  341. return db.Where("status = 1 AND user_org_id = ?", orgid).Preload("HisProject").Preload("GoodInfo", "status=1").Preload("XtHisProjectTeam", "status = 1")
  342. }).Preload("HisPrescriptionInfo", "status =1 and user_org_id = ? and p_type = ? AND record_date = ?", orgid, p_type, recorddate).Preload("HisAdditionalCharge", "status = 1 and user_org_id = ?", orgid).Find(&prescription).Error
  343. return prescription, err
  344. }
  345. func GetProjectPrint(his_patient_id int64) (models.HisPrescriptionProject, error) {
  346. project := models.HisPrescriptionProject{}
  347. err := XTReadDB().Model(&project).Where("his_patient_id = ? and status = 1", his_patient_id).Find(&project).Error
  348. return project, err
  349. }
  350. func GetHisTemplateId(templateid int64, orgid int64) (*models.XtHisTemplate, error) {
  351. template := models.XtHisTemplate{}
  352. err := XTReadDB().Model(&template).Where("template_id = ? and user_org_id = ? and status = 1", templateid, orgid).Find(&template).Error
  353. if err != nil {
  354. if err == gorm.ErrRecordNotFound {
  355. return nil, err
  356. } else {
  357. return nil, err
  358. }
  359. }
  360. return &template, nil
  361. }
  362. func CreateHisTemplate(template *models.XtHisTemplate) error {
  363. err := XTWriteDB().Create(&template).Error
  364. return err
  365. }
  366. func UpdateHisTemplate(template *models.XtHisTemplate) error {
  367. err := XTWriteDB().Save(&template).Error
  368. return err
  369. }
  370. func GetHisPrintTemplate(orgid int64) (models.XtHisTemplate, error) {
  371. template := models.XtHisTemplate{}
  372. err := XTReadDB().Model(&template).Where("user_org_id = ? and status = 1", orgid).Find(&template).Error
  373. return template, err
  374. }
  375. func GetPrescriptionTemplate(templateid int64, orgid int64) (*models.XtHisAdviceTemplate, error) {
  376. template := models.XtHisAdviceTemplate{}
  377. err := XTReadDB().Model(&template).Where("template_id = ? and user_org_id = ? and status = 1", templateid, orgid).Find(&template).Error
  378. if err != nil {
  379. if err == gorm.ErrRecordNotFound {
  380. return nil, err
  381. } else {
  382. return nil, err
  383. }
  384. }
  385. return &template, nil
  386. }
  387. func CreatePrescriptionTemplate(template *models.XtHisAdviceTemplate) error {
  388. err = XTWriteDB().Create(&template).Error
  389. return err
  390. }
  391. func UpdatePrescriptionTemplate(template *models.XtHisAdviceTemplate) error {
  392. err := XTWriteDB().Save(&template).Error
  393. return err
  394. }
  395. func GetPrescriptionTemplateById(orgid int64) (models.XtHisAdviceTemplate, error) {
  396. template := models.XtHisAdviceTemplate{}
  397. err := XTReadDB().Model(&template).Where("user_org_id = ? and status = 1", orgid).Find(&template).Error
  398. return template, err
  399. }
  400. func GetTreatPrintTemplate(templateid int64, orgid int64) (*models.XtHisTreatmentTemplate, error) {
  401. template := models.XtHisTreatmentTemplate{}
  402. err := XTReadDB().Model(&template).Where("template_id = ? and user_org_id = ? and status = 1", templateid, orgid).Find(&template).Error
  403. if err != nil {
  404. if err == gorm.ErrRecordNotFound {
  405. return nil, err
  406. } else {
  407. return nil, err
  408. }
  409. }
  410. return &template, nil
  411. }
  412. func CreateTreatPrintTemplate(template *models.XtHisTreatmentTemplate) error {
  413. err := XTWriteDB().Create(&template).Error
  414. return err
  415. }
  416. func UpdatedTreateTemplate(template *models.XtHisTreatmentTemplate) error {
  417. err := XTWriteDB().Save(&template).Error
  418. return err
  419. }
  420. func GetTreatTtreatPrintTemplate(orgid int64) (models.XtHisTreatmentTemplate, error) {
  421. template := models.XtHisTreatmentTemplate{}
  422. err := XTReadDB().Where("user_org_id = ? and status =1", orgid).Find(&template).Error
  423. return template, err
  424. }
  425. func GetChargeTemplate(templateid int64, orgid int64) (*models.XtHisChargeTemplate, error) {
  426. template := models.XtHisChargeTemplate{}
  427. err := XTReadDB().Model(&template).Where("template_id = ? and user_org_id = ? and status = 1", templateid, orgid).Find(&template).Error
  428. if err != nil {
  429. if err == gorm.ErrRecordNotFound {
  430. return nil, err
  431. } else {
  432. return nil, err
  433. }
  434. }
  435. return &template, nil
  436. }
  437. func CreateChargePrintTemplate(template *models.XtHisChargeTemplate) error {
  438. err := XTWriteDB().Create(&template).Error
  439. return err
  440. }
  441. func UpdateChargePrintTemplate(template *models.XtHisChargeTemplate) error {
  442. err := XTWriteDB().Save(&template).Error
  443. return err
  444. }
  445. func GetChargePrintTemplate(orgid int64) (models.XtHisChargeTemplate, error) {
  446. template := models.XtHisChargeTemplate{}
  447. err := XTReadDB().Model(&template).Where("user_org_id = ? and status = 1", orgid).Find(&template).Error
  448. return template, err
  449. }
  450. func GetDoctorProjectItem(patientid int64, record_date int64) (models.HisPrescriptionProject, error) {
  451. project := models.HisPrescriptionProject{}
  452. err := XTReadDB().Model(&project).Where("patient_id = ? and record_date = ? and status = 1", patientid, record_date).Preload("HisProject").Find(&project).Error
  453. return project, err
  454. }
  455. func GetChargePrint(record int64, patient_id int64, user_org_id int64) (order XtHisOrder, err error) {
  456. err = readDb.Model(&HisOrder{}).Where("status = 1 AND user_org_id = ? AND patient_id = ? AND settle_accounts_date=?", user_org_id, patient_id, record).
  457. Preload("HisOrderInfo", "status = 1 AND user_org_id = ?", user_org_id).
  458. Preload("HisPatient", "status = 1 AND user_org_id = ?", user_org_id).
  459. Preload("HisPrescriptionInfo", "status = 1 AND user_org_id = ?", user_org_id).
  460. Order("ctime desc").
  461. Find(&order).Error
  462. return
  463. }
  464. func GetLastPatient(orgid int64) (models.HisPatient, error) {
  465. patient := models.HisPatient{}
  466. err := XTReadDB().Model(&patient).Where("user_org_id = ? and status = 1", orgid).Last(&patient).Error
  467. return patient, err
  468. }
  469. func UpdateHisPatient(id int64, patient models.HisPatient) error {
  470. err := XTWriteDB().Model(&patient).Where("id = ? and status = 1", id).Update(map[string]interface{}{"number": patient.Number}).Error
  471. return err
  472. }
  473. func GetTodaySchedulePatient(orgid int64, scheduledata int64) (schedule []*models.Schedules, err error) {
  474. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  475. if orgid > 0 {
  476. db = db.Where("x.user_org_id = ?", orgid)
  477. }
  478. if scheduledata > 0 {
  479. db = db.Where("x.schedule_date = ?", scheduledata)
  480. }
  481. err = db.Select("x.id,x.patient_id,x.schedule_date,t.name").Joins("left join xt_patients as t on t.id = x.patient_id").Scan(&schedule).Error
  482. return schedule, err
  483. }
  484. func GetPrscriptionInfo(patientid int64, recorddata int64) (info []*models.HisPrescriptionInfo, err error) {
  485. err = XTReadDB().Model(&info).Where("patient_id = ? and status = 1 and record_date = ?", patientid, recorddata).Find(&info).Error
  486. return info, err
  487. }
  488. func GetPrescriptionByPatientId(patient_id int64, startime int64, p_type int64) (prescription []*models.HisPrescription, err error) {
  489. err = readDb.Model(&models.HisPrescription{}).
  490. Preload("HisAdditionalCharge", func(db *gorm.DB) *gorm.DB {
  491. return db.Where("status = 1").Preload("XtHisAddtionConfig", "status=1")
  492. }).
  493. Preload("HisDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
  494. return db.Where("status = 1 ").Preload("Drug", "status=1")
  495. }).
  496. Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
  497. return db.Where("status = 1 ").Preload("HisProject").Preload("GoodInfo", "status=1")
  498. }).
  499. Where("patient_id = ? AND record_date =? AND p_type = ? AND status = 1", patient_id, startime, p_type).
  500. Find(&prescription).Error
  501. return
  502. }
  503. // func GetPrescriptionByPatientId(patient_id int64, startime int64, p_type int64) (prescritpion []*models.HisPrescription, err error) {
  504. // // err = XTReadDB().Model(&prescritpion).Preload("HisDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
  505. // // return db.Where("status = 1 and user_org_id = ?", orgid).Preload("Drug", "status = 1")
  506. // // }).Where("patient_id = ? AND record_date =? AND p_type = ? AND status = 1", patient_id, startime, p_type).Find(&prescritpion).Error
  507. // // return prescritpion, err
  508. // }
  509. func GetTodayHisPatient(recod_date int64, patientid int64, user_org_id int64) (*models.XtHisPatient, error) {
  510. patient := models.XtHisPatient{}
  511. err := XTReadDB().Model(&patient).Where("record_date = ? and patient_id = ? and user_org_id = ? and is_return = 1 and status = 1", recod_date, patientid, user_org_id).Find(&patient).Error
  512. if err != nil {
  513. if err == gorm.ErrRecordNotFound {
  514. return nil, err
  515. } else {
  516. return nil, err
  517. }
  518. }
  519. return &patient, nil
  520. }
  521. func CreateHisMedicalTemplate(template *models.XtHisMedicalTemplate) error {
  522. err := XTWriteDB().Create(&template).Error
  523. return err
  524. }
  525. func GetMedicalTemplateList(orgid int64) (template []*models.XtHisMedicalTemplate, err error) {
  526. err = XTReadDB().Model(&template).Where("user_org_id =? and status = 1 and template_id = 1", orgid).Find(&template).Error
  527. return template, err
  528. }
  529. func GetMedicalTemplateListOne(orgid int64) (template []*models.XtHisMedicalTemplate, err error) {
  530. err = XTReadDB().Model(&template).Where("user_org_id =? and status = 1 and template_id = 2", orgid).Find(&template).Error
  531. return template, err
  532. }
  533. func GetMedicalTemplateListTwo(orgid int64) (template []*models.XtHisMedicalTemplate, err error) {
  534. err = XTReadDB().Model(&template).Where("user_org_id =? and status = 1 and template_id = 3", orgid).Find(&template).Error
  535. return template, err
  536. }
  537. func GetMedicalTemplateListThree(orgid int64) (template []*models.XtHisMedicalTemplate, err error) {
  538. err = XTReadDB().Model(&template).Where("user_org_id =? and status = 1 and template_id = 4", orgid).Find(&template).Error
  539. return template, err
  540. }
  541. func GetMedicalTemplateListFour(orgid int64) (template []*models.XtHisMedicalTemplate, err error) {
  542. err = XTReadDB().Model(&template).Where("user_org_id =? and status = 1 and template_id = 5", orgid).Find(&template).Error
  543. return template, err
  544. }
  545. func GetMedicalTemplateListFive(orgid int64) (template []*models.XtHisMedicalTemplate, err error) {
  546. err = XTReadDB().Model(&template).Where("user_org_id =? and status = 1 and template_id = 6", orgid).Find(&template).Error
  547. return template, err
  548. }
  549. func GetMedicalTemplateListSix(orgid int64) (template []*models.XtHisMedicalTemplate, err error) {
  550. err = XTReadDB().Model(&template).Where("user_org_id =? and status = 1 and template_id = 7", orgid).Find(&template).Error
  551. return template, err
  552. }
  553. func GetMedicalTemplateListSeven(orgid int64) (template []*models.XtHisMedicalTemplate, err error) {
  554. err = XTReadDB().Model(&template).Where("user_org_id =? and status = 1 and template_id = 8", orgid).Find(&template).Error
  555. return template, err
  556. }
  557. func GetMainTemplateById(id int64) (models.XtHisMedicalTemplate, error) {
  558. template := models.XtHisMedicalTemplate{}
  559. err := XTReadDB().Model(&template).Where("id=? and status = 1", id).Find(&template).Error
  560. return template, err
  561. }
  562. func UpdateMainTemplate(template *models.XtHisMedicalTemplate, id int64) error {
  563. err := XTWriteDB().Model(&template).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"title": template.Title, "content": template.Content, "remark": template.Remark, "mtime": time.Now().Unix()}).Error
  564. return err
  565. }
  566. func DeleteMainTemplateById(id int64) error {
  567. template := models.XtHisMedicalTemplate{}
  568. err := XTWriteDB().Model(&template).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  569. return err
  570. }
  571. func GetProjectHisList(orgid int64) (hisProjectList []*models.XtHisProjectList, error error) {
  572. error = XTReadDB().Model(&hisProjectList).Where("user_org_id = ? and status = 1", orgid).Group("project_id").Find(&hisProjectList).Error
  573. return hisProjectList, error
  574. }
  575. func GetLastItem(orgid int64) (models.XtHisProjectList, error) {
  576. list := models.XtHisProjectList{}
  577. err := XTReadDB().Model(&list).Where("user_org_id = ? and status = 1", orgid).Last(&list).Error
  578. return list, err
  579. }
  580. func SaveProjectTeam(team *models.XtHisProjectTeam) error {
  581. err := XTWriteDB().Model(&team).Save(&team).Error
  582. return err
  583. }
  584. func GetProjectHisListByIds(ids []string) (hisProjectList []*models.XtHisProjectList, error error) {
  585. error = XTReadDB().Model(&hisProjectList).Preload("XtHisProject").Preload("GoodInfo", "status = 1").Where(" status = 1 and id in (?)", ids).Find(&hisProjectList).Error
  586. return hisProjectList, error
  587. }
  588. func GetHisPatientByIdThree(id int64) (models.HisPatient, error) {
  589. patient := models.HisPatient{}
  590. err := XTReadDB().Model(&patient).Where("id = ? and status = 1", id).First(&patient).Error
  591. return patient, err
  592. }
  593. func GetHisPatientByIdThreeTX(id int64, tx *gorm.DB) (models.HisPatient, error) {
  594. patient := models.HisPatient{}
  595. err := tx.Model(&patient).Where("id = ? and status = 1", id).First(&patient).Error
  596. return patient, err
  597. }
  598. func GetLastHisPatient(patient_id int64, org_id int64) (his models.HisPatient, err error) {
  599. err = XTReadDB().Model(&models.HisPatient{}).Where("user_org_id = ? AND patient_id = ? and status = 1 and patient_info <> ''", org_id, patient_id).First(&his).Error
  600. return
  601. }
  602. func GetNewAdminUserES(orgID int64) (es []*models.AdminUserElectronicSignature, err error) {
  603. err = readUserDb.Model(&models.AdminUserElectronicSignature{}).Where("org_id=? and status=1", orgID).Find(&es).Error
  604. return
  605. }
  606. func GetPatientVascularAccess(patient_id int64) (models.XtPatientVascularAccess, error) {
  607. access := models.XtPatientVascularAccess{}
  608. err = XTReadDB().Where("patient_id = ? and status = 1", patient_id).Last(&access).Error
  609. return access, err
  610. }
  611. func GetPatientVascularAccessOne(patient_id int64) (models.XtPatientVascularAccess, error) {
  612. access := models.XtPatientVascularAccess{}
  613. err = XTReadDB().Where("patient_id = ? and status = 1 and user_status =1", patient_id).Last(&access).Error
  614. return access, err
  615. }
  616. func CreateHisProjectTh11o(project models.HisProject) error {
  617. err := XTWriteDB().Model(&project).Create(&project).Error
  618. return err
  619. }