his_project_service.go 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. package service
  2. import (
  3. "XT_New/models"
  4. "github.com/jinzhu/gorm"
  5. "time"
  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}).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)
  79. if len(keyword) > 0 {
  80. db = db.Where("project_team like ?", likeKey)
  81. }
  82. err = db.Where("user_org_id = ? and status = 1", orgid).Count(&total).Offset(offset).Limit(limit).Order("created_time desc").Find(&projectTeam).Error
  83. return projectTeam, total, err
  84. }
  85. func GetProjectTeamDetail(id int64) (models.XtHisProjectTeam, error) {
  86. projectTeam := models.XtHisProjectTeam{}
  87. err := XTReadDB().Model(&projectTeam).Where("id= ? and status = 1", id).Find(&projectTeam).Error
  88. return projectTeam, err
  89. }
  90. func UpdatedProjectTeam(id int64, team *models.XtHisProjectTeam) error {
  91. 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}).Error
  92. return err
  93. }
  94. func DeleteProjectTeam(id int64) error {
  95. team := models.XtHisProjectTeam{}
  96. err := XTWriteDB().Model(&team).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  97. return err
  98. }
  99. func CreateDePartment(department *models.XtHisDepartment) error {
  100. err := XTWriteDB().Model(&department).Create(&department).Error
  101. return err
  102. }
  103. func GetDepartMentList(limit int64, page int64, orgid int64) (departMent []*models.XtHisDepartment, total int64, err error) {
  104. offset := (page - 1) * limit
  105. 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
  106. return departMent, total, err
  107. }
  108. func GetDepartMentDetail(id int64) (models.XtHisDepartment, error) {
  109. department := models.XtHisDepartment{}
  110. err := XTReadDB().Model(&department).Where("id=? and status = 1", id).Find(&department).Error
  111. return department, err
  112. }
  113. func UpdatedDepartment(id int64, department *models.XtHisDepartment) error {
  114. 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
  115. return err
  116. }
  117. func DeleteDepartment(id int64) error {
  118. department := models.XtHisDepartment{}
  119. err := XTReadDB().Model(&department).Where("id=? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  120. return err
  121. }
  122. func GetBloodPatientList(orgid int64, startime int64) (schedule []*models.SchedulesVm, err error) {
  123. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  124. if orgid > 0 {
  125. db = db.Where("x.user_org_id = ?", orgid)
  126. }
  127. if startime > 0 {
  128. db = db.Where("x.schedule_date = ?", startime)
  129. }
  130. 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
  131. return schedule, err
  132. }
  133. func GetHisPatient(orgid int64, recorddate int64) (paitent []*models.HisPatient, err error) {
  134. err = XTReadDB().Model(&paitent).Where("user_org_id = ? and record_date = ? and status = 1", orgid, recorddate).Find(&paitent).Error
  135. return paitent, err
  136. }
  137. func GetHisPrescriptionOther(orgid int64, recorddate int64) (prescription []*models.HisPrescriptionVm, err error) {
  138. db := XTReadDB().Table("his_prescription as x").Where("x.status = 1")
  139. if orgid > 0 {
  140. db = db.Where("x.user_org_id = ?", orgid)
  141. }
  142. if recorddate > 0 {
  143. db = db.Where("x.record_date = ? ", recorddate)
  144. }
  145. 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
  146. return prescription, err
  147. }
  148. func GetHisPrescriptionByPatientId(id int64, recordate int64) (prescription []*models.HisPrescription, err error) {
  149. err = XTReadDB().Model(&prescription).Where("his_patient_id = ? and record_date = ? and status= 1", id, recordate).Find(&prescription).Error
  150. return prescription, err
  151. }
  152. func CreateAdditionalCharge(charge *models.HisAdditionalCharge) error {
  153. err = XTWriteDB().Model(&charge).Save(&charge).Error
  154. return err
  155. }
  156. func GetTreatmentList(id int64, startime int64) (prescription []*models.HisPrescription, err error) {
  157. err = XTReadDB().Model(&prescription).Where("id = ? and record_date = ? and status = 1", id, startime).Find(&prescription).Error
  158. return prescription, err
  159. }
  160. //func GetAllProjectList(orgid int64) (project []*models.XtHisProject, err error) {
  161. //
  162. // err = XTReadDB().Model(&project).Where("user_org_id = ? and status = 1", orgid).Find(&project).Error
  163. // return project, err
  164. //}
  165. func CreateProjectList(list *models.XtHisProjectList) error {
  166. err := XTWriteDB().Model(&list).Create(&list).Error
  167. return err
  168. }
  169. func GetHisProjectListByOrgId(orgid int64) (projectlist []*models.HisProjectListVm, err error) {
  170. db := XTReadDB().Table("xt_his_project_list as x").Where("x.status = 1")
  171. if orgid > 0 {
  172. db = db.Where("x.user_org_id = ? ", orgid)
  173. }
  174. 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
  175. return projectlist, err
  176. }
  177. func GetHisPatientInformation(id int64) (models.HisPatient, error) {
  178. patient := models.HisPatient{}
  179. err := XTReadDB().Model(&patient).Where("id= ? and status = 1", id).Find(&patient).Error
  180. return patient, err
  181. }
  182. func DeleteProjectList(id int64) error {
  183. list := models.XtHisProjectList{}
  184. err := XTWriteDB().Model(&list).Where("id=? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  185. return err
  186. }
  187. func GetHisProject(orgid int64) (project []*models.XtHisProject, err error) {
  188. err = XTReadDB().Model(&project).Where("user_org_id = ? and status =1 and medical_status!=1", orgid).Find(&project).Error
  189. return project, err
  190. }
  191. func GetProjectTeam(ids []string, orgid int64) (project []*models.XtHisProject, err error) {
  192. err = XTReadDB().Model(&project).Where("user_org_id = ? and id in (?)", orgid, ids).Find(&project).Error
  193. return project, err
  194. }
  195. func GetAllDepartMent(orgid int64) (department []*models.XtHisDepartment, err error) {
  196. err = XTReadDB().Model(&department).Where("user_org_id = ? and status =1", orgid).Find(&department).Error
  197. return department, err
  198. }
  199. func CreateHisPatient(patient *models.XtHisPatient) error {
  200. err := XTReadDB().Model(&patient).Create(&patient).Error
  201. return err
  202. }
  203. func UpdateHisPrescriptionHisID(his_patient_id int64, patient_id int64, record_time int64, org_id int64) {
  204. 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})
  205. 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})
  206. return
  207. }
  208. func UpdateHisPrescriptionHisIDTwo(his_patient_id int64, patient_id int64, record_time int64, org_id int64, start_time int64) {
  209. 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})
  210. 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})
  211. return
  212. }
  213. func GetBloodPatientByIdCard(idcard string, orgid int64) (*models.XtPatients, error) {
  214. var patients models.XtPatients
  215. var err error
  216. err = XTReadDB().Model(&patients).Where("id_card_no = ? and user_org_id = ? and status = 1", idcard, orgid).Find(&patients).Error
  217. if err != nil {
  218. if err == gorm.ErrRecordNotFound {
  219. return nil, err
  220. } else {
  221. return nil, err
  222. }
  223. }
  224. return &patients, nil
  225. }
  226. func GetAllProjectTeam(orgid int64) (project []*models.XtHisProjectTeam, err error) {
  227. err = XTReadDB().Model(&project).Where("user_org_id = ? and status = 1", orgid).Find(&project).Error
  228. return project, err
  229. }
  230. func GetProjectListById(orgid int64, ids []string) (project []*models.XtHisProjectList, err error) {
  231. err = XTReadDB().Model(&project).Preload("XtHisProject", "status = 1").Preload("GoodInfo", "status=1").Where("user_org_id = ? and id in (?)", orgid, ids).Find(&project).Error
  232. return project, err
  233. }
  234. func GetHisPatientHistory(keyword string, startime int64, endtime int64, registtype int64, limit int64, page int64, orgid int64) (hisPatient []*models.XtHisPatient, total int64, err error) {
  235. offset := (page - 1) * limit
  236. likeKey := "%" + keyword + "%"
  237. db := XTReadDB().Table("his_patient as x")
  238. if len(keyword) > 0 {
  239. db = db.Where("x.name like ?", likeKey)
  240. }
  241. if startime > 0 {
  242. db = db.Where("x.record_date >= ?", startime)
  243. }
  244. if endtime > 0 {
  245. db = db.Where("x.record_date<=?", endtime)
  246. }
  247. if registtype > 0 {
  248. db = db.Where("x.register_type = ?", registtype)
  249. }
  250. if orgid > 0 {
  251. db = db.Where("x.user_org_id = ?", orgid)
  252. }
  253. 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
  254. return hisPatient, total, err
  255. }
  256. func ChangePatient(id int64) error {
  257. patient := models.XtHisPatient{}
  258. err := XTWriteDB().Model(&patient).Where("id = ?", id).Updates(map[string]interface{}{"is_return": 2}).Error
  259. return err
  260. }
  261. func GetHisPrescriptionTwo(patientid int64, orgid int64, recorddate int64) (*models.HisPrescription, error) {
  262. prescription := models.HisPrescription{}
  263. err := XTReadDB().Model(&prescription).Where("his_patient_id = ? and user_org_id = ? and record_date = ? and status = 1", patientid, orgid, recorddate).Find(&prescription).Error
  264. if err != nil {
  265. if err == gorm.ErrRecordNotFound {
  266. return nil, err
  267. } else {
  268. return nil, err
  269. }
  270. }
  271. return &prescription, nil
  272. }
  273. func UpdatedHisPatient(hispatientid int64, orgid int64, recorddate int64) error {
  274. hisPatient := models.HisPatient{}
  275. err := XTWriteDB().Model(&hisPatient).Where("id = ? and user_org_id = ? and record_date = ?", hispatientid, orgid, recorddate).Updates(map[string]interface{}{"is_return": 3}).Error
  276. return err
  277. }
  278. func GetBloodPatientInfoById(patientid int64) (models.XtPatients, error) {
  279. patients := models.XtPatients{}
  280. err := XTReadDB().Model(&patients).Where("id = ? and status = 1", patientid).Find(&patients).Error
  281. return patients, err
  282. }
  283. func GetPatientCaseHistory(patientid int64) (models.HisPatientCaseHistory, error) {
  284. history := models.HisPatientCaseHistory{}
  285. err := XTReadDB().Model(&history).Where("patient_id = ? and status = 1", patientid).Last(&history).Error
  286. return history, err
  287. }
  288. func GetBloodPatientByPatient(id int64) (models.XtPatients, error) {
  289. patients := models.XtPatients{}
  290. err := XTReadDB().Model(&patients).Where("id = ? and status = 1", id).Find(&patients).Error
  291. return patients, err
  292. }
  293. func GetHisPatientByIdFour(patientid int64) (models.XtHisPatient, error) {
  294. patient := models.XtHisPatient{}
  295. err := XTReadDB().Model(&patient).Where("patient_id = ? and status=1", patientid).Find(&patient).Error
  296. return patient, err
  297. }
  298. func GetHisPatientById(patientid int64) (models.VMHisPatient, error) {
  299. patient := models.VMHisPatient{}
  300. err := XTReadDB().Model(&patient).Where("patient_id = ? and status = 1", patientid).Find(&patient).Error
  301. return patient, err
  302. }
  303. func GetHisPatientByIdTwo(patientid int64) (models.VMHisPatient, error) {
  304. patient := models.VMHisPatient{}
  305. err := XTReadDB().Model(&patient).Where("patient_id = ? and status = 1", patientid).Limit(&patient).Error
  306. return patient, err
  307. }
  308. func GetTemplateDetail(id int64) (models.HisCaseHistoryTemplate, error) {
  309. template := models.HisCaseHistoryTemplate{}
  310. err := XTReadDB().Model(&template).Where("id= ? and status = 1", id).Find(&template).Error
  311. return template, err
  312. }
  313. func UpdateCaseHistoryTemplate(tem *models.HisCaseHistoryTemplate, id int64) error {
  314. 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
  315. return err
  316. }
  317. func GetHistPatient(orgid int64, keyword string) (hisPatient []*models.HisPatient, err error) {
  318. likeKey := "%" + keyword + "%"
  319. err = XTReadDB().Model(&hisPatient).Where("user_org_id = ? and name like ?", orgid, likeKey).Find(&hisPatient).Error
  320. return hisPatient, err
  321. }
  322. func GetDoctorAdvicePrint(his_patient_id int64, recorddate int64, schIDs []string, orgid int64, p_type int64) (prescription []*models.HisPrescription, err error) {
  323. 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("Patients", "status = 1 and user_org_id = ?", orgid).Preload("HisDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
  324. return db.Where("status = 1 and user_org_id = ?", orgid).Preload("Drug", "status = 1")
  325. }).Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
  326. return db.Where("status = 1 AND user_org_id = ?", orgid).Preload("HisProject").Preload("GoodInfo", "status=1").Preload("XtHisProjectTeam", "status = 1")
  327. }).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
  328. return prescription, err
  329. }
  330. func GetProjectPrint(his_patient_id int64) (models.HisPrescriptionProject, error) {
  331. project := models.HisPrescriptionProject{}
  332. err := XTReadDB().Model(&project).Where("his_patient_id = ? and status = 1", his_patient_id).Find(&project).Error
  333. return project, err
  334. }
  335. func GetHisTemplateId(templateid int64, orgid int64) (*models.XtHisTemplate, error) {
  336. template := models.XtHisTemplate{}
  337. err := XTReadDB().Model(&template).Where("template_id = ? and user_org_id = ? and status = 1", templateid, orgid).Find(&template).Error
  338. if err != nil {
  339. if err == gorm.ErrRecordNotFound {
  340. return nil, err
  341. } else {
  342. return nil, err
  343. }
  344. }
  345. return &template, nil
  346. }
  347. func CreateHisTemplate(template *models.XtHisTemplate) error {
  348. err := XTWriteDB().Create(&template).Error
  349. return err
  350. }
  351. func UpdateHisTemplate(template *models.XtHisTemplate) error {
  352. err := XTWriteDB().Save(&template).Error
  353. return err
  354. }
  355. func GetHisPrintTemplate(orgid int64) (models.XtHisTemplate, error) {
  356. template := models.XtHisTemplate{}
  357. err := XTReadDB().Model(&template).Where("user_org_id = ? and status = 1", orgid).Find(&template).Error
  358. return template, err
  359. }
  360. func GetPrescriptionTemplate(templateid int64, orgid int64) (*models.XtHisAdviceTemplate, error) {
  361. template := models.XtHisAdviceTemplate{}
  362. err := XTReadDB().Model(&template).Where("template_id = ? and user_org_id = ? and status = 1", templateid, orgid).Find(&template).Error
  363. if err != nil {
  364. if err == gorm.ErrRecordNotFound {
  365. return nil, err
  366. } else {
  367. return nil, err
  368. }
  369. }
  370. return &template, nil
  371. }
  372. func CreatePrescriptionTemplate(template *models.XtHisAdviceTemplate) error {
  373. err = XTWriteDB().Create(&template).Error
  374. return err
  375. }
  376. func UpdatePrescriptionTemplate(template *models.XtHisAdviceTemplate) error {
  377. err := XTWriteDB().Save(&template).Error
  378. return err
  379. }
  380. func GetPrescriptionTemplateById(orgid int64) (models.XtHisAdviceTemplate, error) {
  381. template := models.XtHisAdviceTemplate{}
  382. err := XTReadDB().Model(&template).Where("user_org_id = ? and status = 1", orgid).Find(&template).Error
  383. return template, err
  384. }
  385. func GetTreatPrintTemplate(templateid int64, orgid int64) (*models.XtHisTreatmentTemplate, error) {
  386. template := models.XtHisTreatmentTemplate{}
  387. err := XTReadDB().Model(&template).Where("template_id = ? and user_org_id = ? and status = 1", templateid, orgid).Find(&template).Error
  388. if err != nil {
  389. if err == gorm.ErrRecordNotFound {
  390. return nil, err
  391. } else {
  392. return nil, err
  393. }
  394. }
  395. return &template, nil
  396. }
  397. func CreateTreatPrintTemplate(template *models.XtHisTreatmentTemplate) error {
  398. err := XTWriteDB().Create(&template).Error
  399. return err
  400. }
  401. func UpdatedTreateTemplate(template *models.XtHisTreatmentTemplate) error {
  402. err := XTWriteDB().Save(&template).Error
  403. return err
  404. }
  405. func GetTreatTtreatPrintTemplate(orgid int64) (models.XtHisTreatmentTemplate, error) {
  406. template := models.XtHisTreatmentTemplate{}
  407. err := XTReadDB().Where("user_org_id = ? and status =1", orgid).Find(&template).Error
  408. return template, err
  409. }
  410. func GetChargeTemplate(templateid int64, orgid int64) (*models.XtHisChargeTemplate, error) {
  411. template := models.XtHisChargeTemplate{}
  412. err := XTReadDB().Model(&template).Where("template_id = ? and user_org_id = ? and status = 1", templateid, orgid).Find(&template).Error
  413. if err != nil {
  414. if err == gorm.ErrRecordNotFound {
  415. return nil, err
  416. } else {
  417. return nil, err
  418. }
  419. }
  420. return &template, nil
  421. }
  422. func CreateChargePrintTemplate(template *models.XtHisChargeTemplate) error {
  423. err := XTWriteDB().Create(&template).Error
  424. return err
  425. }
  426. func UpdateChargePrintTemplate(template *models.XtHisChargeTemplate) error {
  427. err := XTWriteDB().Save(&template).Error
  428. return err
  429. }
  430. func GetChargePrintTemplate(orgid int64) (models.XtHisChargeTemplate, error) {
  431. template := models.XtHisChargeTemplate{}
  432. err := XTReadDB().Model(&template).Where("user_org_id = ? and status = 1", orgid).Find(&template).Error
  433. return template, err
  434. }
  435. func GetDoctorProjectItem(patientid int64, record_date int64) (models.HisPrescriptionProject, error) {
  436. project := models.HisPrescriptionProject{}
  437. err := XTReadDB().Model(&project).Where("patient_id = ? and record_date = ? and status = 1", patientid, record_date).Preload("HisProject").Find(&project).Error
  438. return project, err
  439. }
  440. func GetChargePrint(record int64, patient_id int64, user_org_id int64) (order XtHisOrder, err error) {
  441. err = readDb.Model(&HisOrder{}).Where("status = 1 AND user_org_id = ? AND patient_id = ? AND settle_accounts_date=?", user_org_id, patient_id, record).
  442. Preload("HisOrderInfo", "status = 1 AND user_org_id = ?", user_org_id).
  443. Preload("HisPatient", "status = 1 AND user_org_id = ?", user_org_id).
  444. Preload("HisPrescriptionInfo", "status = 1 AND user_org_id = ?", user_org_id).
  445. Order("ctime desc").
  446. Find(&order).Error
  447. return
  448. }
  449. func GetLastPatient(orgid int64) (models.HisPatient, error) {
  450. patient := models.HisPatient{}
  451. err := XTReadDB().Model(&patient).Where("user_org_id = ? and status = 1", orgid).Last(&patient).Error
  452. return patient, err
  453. }
  454. func UpdateHisPatient(id int64, patient models.HisPatient) error {
  455. err := XTWriteDB().Model(&patient).Where("id = ? and status = 1", id).Update(map[string]interface{}{"number": patient.Number}).Error
  456. return err
  457. }
  458. func GetTodaySchedulePatient(orgid int64, scheduledata int64) (schedule []*models.Schedules, err error) {
  459. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  460. if orgid > 0 {
  461. db = db.Where("x.user_org_id = ?", orgid)
  462. }
  463. if scheduledata > 0 {
  464. db = db.Where("x.schedule_date = ?", scheduledata)
  465. }
  466. 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
  467. return schedule, err
  468. }
  469. func GetPrscriptionInfo(patientid int64, recorddata int64) (info []*models.HisPrescriptionInfo, err error) {
  470. err = XTReadDB().Model(&info).Where("patient_id = ? and status = 1 and record_date = ?", patientid, recorddata).Find(&info).Error
  471. return info, err
  472. }
  473. func GetPrescriptionByPatientId(patient_id int64, startime int64, p_type int64) (prescription []*models.HisPrescription, err error) {
  474. err = readDb.Model(&models.HisPrescription{}).
  475. Preload("HisAdditionalCharge", func(db *gorm.DB) *gorm.DB {
  476. return db.Where("status = 1").Preload("XtHisAddtionConfig", "status=1")
  477. }).
  478. Preload("HisDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
  479. return db.Where("status = 1 ").Preload("Drug", "status=1")
  480. }).
  481. Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
  482. return db.Where("status = 1 ").Preload("HisProject").Preload("GoodInfo", "status=1")
  483. }).
  484. Where("patient_id = ? AND record_date =? AND p_type = ? AND status = 1", patient_id, startime, p_type).
  485. Find(&prescription).Error
  486. return
  487. }
  488. // func GetPrescriptionByPatientId(patient_id int64, startime int64, p_type int64) (prescritpion []*models.HisPrescription, err error) {
  489. // // err = XTReadDB().Model(&prescritpion).Preload("HisDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
  490. // // return db.Where("status = 1 and user_org_id = ?", orgid).Preload("Drug", "status = 1")
  491. // // }).Where("patient_id = ? AND record_date =? AND p_type = ? AND status = 1", patient_id, startime, p_type).Find(&prescritpion).Error
  492. // // return prescritpion, err
  493. // }
  494. func GetTodayHisPatient(recod_date int64, patientid int64, user_org_id int64) (*models.XtHisPatient, error) {
  495. patient := models.XtHisPatient{}
  496. 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
  497. if err != nil {
  498. if err == gorm.ErrRecordNotFound {
  499. return nil, err
  500. } else {
  501. return nil, err
  502. }
  503. }
  504. return &patient, nil
  505. }
  506. func CreateHisMedicalTemplate(template *models.XtHisMedicalTemplate) error {
  507. err := XTWriteDB().Create(&template).Error
  508. return err
  509. }
  510. func GetMedicalTemplateList(orgid int64) (template []*models.XtHisMedicalTemplate, err error) {
  511. err = XTReadDB().Model(&template).Where("user_org_id =? and status = 1 and template_id = 1", orgid).Find(&template).Error
  512. return template, err
  513. }
  514. func GetMedicalTemplateListOne(orgid int64) (template []*models.XtHisMedicalTemplate, err error) {
  515. err = XTReadDB().Model(&template).Where("user_org_id =? and status = 1 and template_id = 2", orgid).Find(&template).Error
  516. return template, err
  517. }
  518. func GetMedicalTemplateListTwo(orgid int64) (template []*models.XtHisMedicalTemplate, err error) {
  519. err = XTReadDB().Model(&template).Where("user_org_id =? and status = 1 and template_id = 3", orgid).Find(&template).Error
  520. return template, err
  521. }
  522. func GetMedicalTemplateListThree(orgid int64) (template []*models.XtHisMedicalTemplate, err error) {
  523. err = XTReadDB().Model(&template).Where("user_org_id =? and status = 1 and template_id = 4", orgid).Find(&template).Error
  524. return template, err
  525. }
  526. func GetMedicalTemplateListFour(orgid int64) (template []*models.XtHisMedicalTemplate, err error) {
  527. err = XTReadDB().Model(&template).Where("user_org_id =? and status = 1 and template_id = 5", orgid).Find(&template).Error
  528. return template, err
  529. }
  530. func GetMedicalTemplateListFive(orgid int64) (template []*models.XtHisMedicalTemplate, err error) {
  531. err = XTReadDB().Model(&template).Where("user_org_id =? and status = 1 and template_id = 6", orgid).Find(&template).Error
  532. return template, err
  533. }
  534. func GetMedicalTemplateListSix(orgid int64) (template []*models.XtHisMedicalTemplate, err error) {
  535. err = XTReadDB().Model(&template).Where("user_org_id =? and status = 1 and template_id = 7", orgid).Find(&template).Error
  536. return template, err
  537. }
  538. func GetMedicalTemplateListSeven(orgid int64) (template []*models.XtHisMedicalTemplate, err error) {
  539. err = XTReadDB().Model(&template).Where("user_org_id =? and status = 1 and template_id = 8", orgid).Find(&template).Error
  540. return template, err
  541. }
  542. func GetMainTemplateById(id int64) (models.XtHisMedicalTemplate, error) {
  543. template := models.XtHisMedicalTemplate{}
  544. err := XTReadDB().Model(&template).Where("id=? and status = 1", id).Find(&template).Error
  545. return template, err
  546. }
  547. func UpdateMainTemplate(template *models.XtHisMedicalTemplate, id int64) error {
  548. 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
  549. return err
  550. }
  551. func DeleteMainTemplateById(id int64) error {
  552. template := models.XtHisMedicalTemplate{}
  553. err := XTWriteDB().Model(&template).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  554. return err
  555. }
  556. func GetProjectHisList(orgid int64) (hisProjectList []*models.XtHisProjectList, error error) {
  557. error = XTReadDB().Model(&hisProjectList).Where("user_org_id = ? and status = 1", orgid).Group("project_id").Find(&hisProjectList).Error
  558. return hisProjectList, error
  559. }
  560. func GetLastItem(orgid int64) (models.XtHisProjectList, error) {
  561. list := models.XtHisProjectList{}
  562. err := XTReadDB().Model(&list).Where("user_org_id = ? and status = 1", orgid).Last(&list).Error
  563. return list, err
  564. }
  565. func SaveProjectTeam(team *models.XtHisProjectTeam) error {
  566. err := XTWriteDB().Model(&team).Save(&team).Error
  567. return err
  568. }
  569. func GetProjectHisListByIds(ids []string) (hisProjectList []*models.XtHisProjectList, error error) {
  570. error = XTReadDB().Model(&hisProjectList).Preload("XtHisProject").Preload("GoodInfo", "status = 1").Where(" status = 1 and id in (?)", ids).Find(&hisProjectList).Error
  571. return hisProjectList, error
  572. }
  573. func GetHisPatientByIdThree(id int64) (models.HisPatient, error) {
  574. patient := models.HisPatient{}
  575. err := XTReadDB().Model(&patient).Where("id = ? and status = 1", id).First(&patient).Error
  576. return patient, err
  577. }
  578. func GetHisPatientByIdThreeTX(id int64, tx *gorm.DB) (models.HisPatient, error) {
  579. patient := models.HisPatient{}
  580. err := tx.Model(&patient).Where("id = ? and status = 1", id).First(&patient).Error
  581. return patient, err
  582. }
  583. func GetLastHisPatient(patient_id int64, org_id int64) (his models.HisPatient, err error) {
  584. 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
  585. return
  586. }
  587. func GetNewAdminUserES(orgID int64) (es []*models.AdminUserElectronicSignature, err error) {
  588. err = readUserDb.Model(&models.AdminUserElectronicSignature{}).Where("org_id=? and status=1", orgID).Find(&es).Error
  589. return
  590. }
  591. func GetPatientVascularAccess(patient_id int64) (models.XtPatientVascularAccess, error) {
  592. access := models.XtPatientVascularAccess{}
  593. err = XTReadDB().Where("patient_id = ? and status = 1", patient_id).Last(&access).Error
  594. return access, err
  595. }