his_project_service.go 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. package service
  2. import (
  3. "Xcx_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").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", 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 GetHisPatientByIdTwo(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 GetTemplateDetail(id int64) (models.HisCaseHistoryTemplate, error) {
  304. template := models.HisCaseHistoryTemplate{}
  305. err := XTReadDB().Model(&template).Where("id= ? and status = 1", id).Find(&template).Error
  306. return template, err
  307. }
  308. func UpdateCaseHistoryTemplate(tem *models.HisCaseHistoryTemplate, id int64) error {
  309. 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
  310. return err
  311. }
  312. func GetHistPatient(orgid int64, keyword string) (hisPatient []*models.HisPatient, err error) {
  313. likeKey := "%" + keyword + "%"
  314. err = XTReadDB().Model(&hisPatient).Where("user_org_id = ? and name like ?", orgid, likeKey).Find(&hisPatient).Error
  315. return hisPatient, err
  316. }
  317. func GetDoctorAdvicePrint(his_patient_id int64, recorddate int64, schIDs []string, orgid int64) (prescription []*models.HisPrescription, err error) {
  318. err = XTReadDB().Model(&prescription).Where("patient_id = ? and record_date = ? and id in(?) and status = 1 ", his_patient_id, recorddate, schIDs).Preload("Patients", "status = 1 and user_org_id = ?", orgid).Preload("HisDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
  319. return db.Where("status = 1 and user_org_id = ?", orgid).Preload("Drug", "status = 1")
  320. }).Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
  321. return db.Where("status = 1 AND user_org_id = ?", orgid).Preload("HisProject").Preload("GoodInfo", "status=1")
  322. }).Preload("HisPrescriptionInfo", "status =1 and user_org_id = ?", orgid).Preload("HisAdditionalCharge", "status = 1 and user_org_id = ?", orgid).Find(&prescription).Error
  323. return prescription, err
  324. }
  325. func GetProjectPrint(his_patient_id int64) (models.HisPrescriptionProject, error) {
  326. project := models.HisPrescriptionProject{}
  327. err := XTReadDB().Model(&project).Where("his_patient_id = ? and status = 1", his_patient_id).Find(&project).Error
  328. return project, err
  329. }
  330. func GetHisTemplateId(templateid int64, orgid int64) (*models.XtHisTemplate, error) {
  331. template := models.XtHisTemplate{}
  332. err := XTReadDB().Model(&template).Where("template_id = ? and user_org_id = ? and status = 1", templateid, orgid).Find(&template).Error
  333. if err != nil {
  334. if err == gorm.ErrRecordNotFound {
  335. return nil, err
  336. } else {
  337. return nil, err
  338. }
  339. }
  340. return &template, nil
  341. }
  342. func CreateHisTemplate(template *models.XtHisTemplate) error {
  343. err := XTWriteDB().Create(&template).Error
  344. return err
  345. }
  346. func UpdateHisTemplate(template *models.XtHisTemplate) error {
  347. err := XTWriteDB().Save(&template).Error
  348. return err
  349. }
  350. func GetHisPrintTemplate(orgid int64) (models.XtHisTemplate, error) {
  351. template := models.XtHisTemplate{}
  352. err := XTReadDB().Model(&template).Where("user_org_id = ? and status = 1", orgid).Find(&template).Error
  353. return template, err
  354. }
  355. func GetPrescriptionTemplate(templateid int64, orgid int64) (*models.XtHisAdviceTemplate, error) {
  356. template := models.XtHisAdviceTemplate{}
  357. err := XTReadDB().Model(&template).Where("template_id = ? and user_org_id = ? and status = 1", templateid, orgid).Find(&template).Error
  358. if err != nil {
  359. if err == gorm.ErrRecordNotFound {
  360. return nil, err
  361. } else {
  362. return nil, err
  363. }
  364. }
  365. return &template, nil
  366. }
  367. func CreatePrescriptionTemplate(template *models.XtHisAdviceTemplate) error {
  368. err = XTWriteDB().Create(&template).Error
  369. return err
  370. }
  371. func UpdatePrescriptionTemplate(template *models.XtHisAdviceTemplate) error {
  372. err := XTWriteDB().Save(&template).Error
  373. return err
  374. }
  375. func GetPrescriptionTemplateById(orgid int64) (models.XtHisAdviceTemplate, error) {
  376. template := models.XtHisAdviceTemplate{}
  377. err := XTReadDB().Model(&template).Where("user_org_id = ? and status = 1", orgid).Find(&template).Error
  378. return template, err
  379. }
  380. func GetTreatPrintTemplate(templateid int64, orgid int64) (*models.XtHisTreatmentTemplate, error) {
  381. template := models.XtHisTreatmentTemplate{}
  382. err := XTReadDB().Model(&template).Where("template_id = ? and user_org_id = ? and status = 1", templateid, orgid).Find(&template).Error
  383. if err != nil {
  384. if err == gorm.ErrRecordNotFound {
  385. return nil, err
  386. } else {
  387. return nil, err
  388. }
  389. }
  390. return &template, nil
  391. }
  392. func CreateTreatPrintTemplate(template *models.XtHisTreatmentTemplate) error {
  393. err := XTWriteDB().Create(&template).Error
  394. return err
  395. }
  396. func UpdatedTreateTemplate(template *models.XtHisTreatmentTemplate) error {
  397. err := XTWriteDB().Save(&template).Error
  398. return err
  399. }
  400. func GetTreatTtreatPrintTemplate(orgid int64) (models.XtHisTreatmentTemplate, error) {
  401. template := models.XtHisTreatmentTemplate{}
  402. err := XTReadDB().Where("user_org_id = ? and status =1", orgid).Find(&template).Error
  403. return template, err
  404. }
  405. func GetChargeTemplate(templateid int64, orgid int64) (*models.XtHisChargeTemplate, error) {
  406. template := models.XtHisChargeTemplate{}
  407. err := XTReadDB().Model(&template).Where("template_id = ? and user_org_id = ? and status = 1", templateid, orgid).Find(&template).Error
  408. if err != nil {
  409. if err == gorm.ErrRecordNotFound {
  410. return nil, err
  411. } else {
  412. return nil, err
  413. }
  414. }
  415. return &template, nil
  416. }
  417. func CreateChargePrintTemplate(template *models.XtHisChargeTemplate) error {
  418. err := XTWriteDB().Create(&template).Error
  419. return err
  420. }
  421. func UpdateChargePrintTemplate(template *models.XtHisChargeTemplate) error {
  422. err := XTWriteDB().Save(&template).Error
  423. return err
  424. }
  425. func GetChargePrintTemplate(orgid int64) (models.XtHisChargeTemplate, error) {
  426. template := models.XtHisChargeTemplate{}
  427. err := XTReadDB().Model(&template).Where("user_org_id = ? and status = 1", orgid).Find(&template).Error
  428. return template, err
  429. }
  430. func GetDoctorProjectItem(patientid int64, record_date int64) (models.HisPrescriptionProject, error) {
  431. project := models.HisPrescriptionProject{}
  432. err := XTReadDB().Model(&project).Where("patient_id = ? and record_date = ? and status = 1", patientid, record_date).Preload("HisProject").Find(&project).Error
  433. return project, err
  434. }
  435. func GetChargePrint(record int64, patient_id int64, user_org_id int64) (order XtHisOrder, err error) {
  436. err = readDb.Model(&HisOrder{}).Where("status = 1 AND user_org_id = ? AND patient_id = ? AND settle_accounts_date=?", user_org_id, patient_id, record).
  437. Preload("HisOrderInfo", "status = 1 AND user_org_id = ?", user_org_id).
  438. Preload("HisPatient", "status = 1 AND user_org_id = ?", user_org_id).
  439. Preload("HisPrescriptionInfo", "status = 1 AND user_org_id = ?", user_org_id).
  440. Order("ctime desc").
  441. Find(&order).Error
  442. return
  443. }
  444. func GetLastPatient(orgid int64) (models.HisPatient, error) {
  445. patient := models.HisPatient{}
  446. err := XTReadDB().Model(&patient).Where("user_org_id = ? and status = 1", orgid).Last(&patient).Error
  447. return patient, err
  448. }
  449. func UpdateHisPatient(id int64, patient models.HisPatient) error {
  450. err := XTWriteDB().Model(&patient).Where("id = ? and status = 1", id).Update(map[string]interface{}{"number": patient.Number}).Error
  451. return err
  452. }
  453. func GetTodaySchedulePatient(orgid int64, scheduledata int64) (schedule []*models.Schedules, err error) {
  454. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  455. if orgid > 0 {
  456. db = db.Where("x.user_org_id = ?", orgid)
  457. }
  458. if scheduledata > 0 {
  459. db = db.Where("x.schedule_date = ?", scheduledata)
  460. }
  461. 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
  462. return schedule, err
  463. }
  464. func GetPrscriptionInfo(patientid int64, recorddata int64) (info []*models.HisPrescriptionInfo, err error) {
  465. err = XTReadDB().Model(&info).Where("patient_id = ? and status = 1 and record_date = ?", patientid, recorddata).Find(&info).Error
  466. return info, err
  467. }
  468. func GetPrescriptionByPatientId(patient_id int64, startime int64, p_type int64) (prescription []*models.HisPrescription, err error) {
  469. err = readDb.Model(&models.HisPrescription{}).
  470. Preload("HisAdditionalCharge", func(db *gorm.DB) *gorm.DB {
  471. return db.Where("status = 1").Preload("XtHisAddtionConfig", "status=1")
  472. }).
  473. Preload("HisDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
  474. return db.Where("status = 1 ").Preload("Drug", "status=1")
  475. }).
  476. Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
  477. return db.Where("status = 1 ").Preload("HisProject").Preload("GoodInfo", "status=1")
  478. }).
  479. Where("patient_id = ? AND record_date =? AND p_type = ? AND status = 1", patient_id, startime, p_type).
  480. Find(&prescription).Error
  481. return
  482. }
  483. // func GetPrescriptionByPatientId(patient_id int64, startime int64, p_type int64) (prescritpion []*models.HisPrescription, err error) {
  484. // // err = XTReadDB().Model(&prescritpion).Preload("HisDoctorAdviceInfo", func(db *gorm.DB) *gorm.DB {
  485. // // return db.Where("status = 1 and user_org_id = ?", orgid).Preload("Drug", "status = 1")
  486. // // }).Where("patient_id = ? AND record_date =? AND p_type = ? AND status = 1", patient_id, startime, p_type).Find(&prescritpion).Error
  487. // // return prescritpion, err
  488. // }
  489. func GetTodayHisPatient(recod_date int64, patientid int64, user_org_id int64) (*models.XtHisPatient, error) {
  490. patient := models.XtHisPatient{}
  491. 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
  492. if err != nil {
  493. if err == gorm.ErrRecordNotFound {
  494. return nil, err
  495. } else {
  496. return nil, err
  497. }
  498. }
  499. return &patient, nil
  500. }
  501. func CreateHisMedicalTemplate(template *models.XtHisMedicalTemplate) error {
  502. err := XTWriteDB().Create(&template).Error
  503. return err
  504. }
  505. func GetMedicalTemplateList(orgid int64) (template []*models.XtHisMedicalTemplate, err error) {
  506. err = XTReadDB().Model(&template).Where("user_org_id =? and status = 1 and template_id = 1", orgid).Find(&template).Error
  507. return template, err
  508. }
  509. func GetMedicalTemplateListOne(orgid int64) (template []*models.XtHisMedicalTemplate, err error) {
  510. err = XTReadDB().Model(&template).Where("user_org_id =? and status = 1 and template_id = 2", orgid).Find(&template).Error
  511. return template, err
  512. }
  513. func GetMedicalTemplateListTwo(orgid int64) (template []*models.XtHisMedicalTemplate, err error) {
  514. err = XTReadDB().Model(&template).Where("user_org_id =? and status = 1 and template_id = 3", orgid).Find(&template).Error
  515. return template, err
  516. }
  517. func GetMedicalTemplateListThree(orgid int64) (template []*models.XtHisMedicalTemplate, err error) {
  518. err = XTReadDB().Model(&template).Where("user_org_id =? and status = 1 and template_id = 4", orgid).Find(&template).Error
  519. return template, err
  520. }
  521. func GetMedicalTemplateListFour(orgid int64) (template []*models.XtHisMedicalTemplate, err error) {
  522. err = XTReadDB().Model(&template).Where("user_org_id =? and status = 1 and template_id = 5", orgid).Find(&template).Error
  523. return template, err
  524. }
  525. func GetMedicalTemplateListFive(orgid int64) (template []*models.XtHisMedicalTemplate, err error) {
  526. err = XTReadDB().Model(&template).Where("user_org_id =? and status = 1 and template_id = 6", orgid).Find(&template).Error
  527. return template, err
  528. }
  529. func GetMedicalTemplateListSix(orgid int64) (template []*models.XtHisMedicalTemplate, err error) {
  530. err = XTReadDB().Model(&template).Where("user_org_id =? and status = 1 and template_id = 7", orgid).Find(&template).Error
  531. return template, err
  532. }
  533. func GetMedicalTemplateListSeven(orgid int64) (template []*models.XtHisMedicalTemplate, err error) {
  534. err = XTReadDB().Model(&template).Where("user_org_id =? and status = 1 and template_id = 8", orgid).Find(&template).Error
  535. return template, err
  536. }
  537. func GetMainTemplateById(id int64) (models.XtHisMedicalTemplate, error) {
  538. template := models.XtHisMedicalTemplate{}
  539. err := XTReadDB().Model(&template).Where("id=? and status = 1", id).Find(&template).Error
  540. return template, err
  541. }
  542. func UpdateMainTemplate(template *models.XtHisMedicalTemplate, id int64) error {
  543. 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
  544. return err
  545. }
  546. func DeleteMainTemplateById(id int64) error {
  547. template := models.XtHisMedicalTemplate{}
  548. err := XTWriteDB().Model(&template).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  549. return err
  550. }
  551. func GetProjectHisList(orgid int64) (hisProjectList []*models.XtHisProjectList, error error) {
  552. error = XTReadDB().Model(&hisProjectList).Where("user_org_id = ? and status = 1", orgid).Group("project_id").Find(&hisProjectList).Error
  553. return hisProjectList, error
  554. }
  555. func GetLastItem(orgid int64) (models.XtHisProjectList, error) {
  556. list := models.XtHisProjectList{}
  557. err := XTReadDB().Model(&list).Where("user_org_id = ? and status = 1", orgid).Last(&list).Error
  558. return list, err
  559. }
  560. func SaveProjectTeam(team *models.XtHisProjectTeam) error {
  561. err := XTWriteDB().Model(&team).Save(&team).Error
  562. return err
  563. }
  564. func GetProjectHisListByIds(ids []string) (hisProjectList []*models.XtHisProjectList, error error) {
  565. error = XTReadDB().Model(&hisProjectList).Preload("XtHisProject").Preload("GoodInfo", "status = 1").Where(" status = 1 and id in (?)", ids).Find(&hisProjectList).Error
  566. return hisProjectList, error
  567. }
  568. func GetHisPatientByIdThree(id int64) (models.HisPatient, error) {
  569. patient := models.HisPatient{}
  570. err := XTReadDB().Model(&patient).Where("id = ? and status = 1", id).First(&patient).Error
  571. return patient, err
  572. }
  573. func GetLastHisPatient(patient_id int64, org_id int64) (his models.HisPatient, err error) {
  574. 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
  575. return
  576. }