patientmanage_service.go 68KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447
  1. package service
  2. import (
  3. "XT_New/models"
  4. "fmt"
  5. "github.com/jinzhu/gorm"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. func GetIllnessListTwo() (ills []*models.Illness, err error) {
  11. err = readUserDb.Where("status=1").Find(&ills).Error
  12. return
  13. }
  14. func GetBloodDialysisPatient(orgid int64, page int64, limit int64) (patients []*models.Patients, total int64, err error) {
  15. db := XTReadDB().Table("xt_patients_new as x").Where("x.status = 1")
  16. if orgid > 0 {
  17. db = db.Where("x.user_org_id = ?", orgid)
  18. }
  19. offset := (page - 1) * limit
  20. err = db.Count(&total).Order("x.created_time desc").Offset(offset).Limit(limit).
  21. Select("x.id,x.user_org_id,x.user_id,x.avatar,x.patient_type,x.dialysis_no,x.admission_number,x.source,x.lapseto,x.partition_id,x.bed_id,x.name,x.alias,x.gender,x.marital_status,x.id_card_no,x.birthday,x.reimbursement_way_id,x.health_care_type,x.health_care_no,x.health_care_due_date,x.height,x.blood_type,x.rh,x.health_care_due_alert_date,x.education_level,x.profession,x.phone,x.home_telephone,x.relative_phone,x.relative_relations,x.home_address,x.work_unit,x.unit_address,x.children,x.receiving_date,x.is_hospital_first_dialysis,x.first_dialysis_date,x.first_dialysis_hospital,x.predialysis_condition,x.pre_hospital_dialysis_frequency,x.pre_hospital_dialysis_times,x.hospital_first_dialysis_date,x.induction_period,x.initial_dialysis,x.total_dialysis,x.attending_doctor_id,x.head_nurse_id,x.evaluate,x.diagnose,x.remark,x.registrars_id,x.registrars,x.qr_code,x.binding_state,x.patient_complains,x.present_history,x.past_history,x.temperature,x.pulse,x.respiratory,x.sbp,x.dbp,x.status,x.created_time,x.updated_time,x.nation,x.native_place,x.age,x.blood_patients,x.slow_patients,x.member_patients,x.ecommer_patients,x.blood_id,x.slow_id,x.member_id").Find(&patients).Error
  22. fmt.Println("err是什么", err)
  23. return
  24. }
  25. func GetAllBloodDialysisPatient(orgid int64, page int64, limit int64, lapseto int64, source int64) (patients []*models.XtPatientsNew, total int64, err error) {
  26. db := XTReadDB().Table("xt_patients_new as x").Where("x.status = 1")
  27. if orgid > 0 {
  28. db = db.Where("x.user_org_id = ? and x.blood_patients = 1", orgid)
  29. }
  30. if lapseto > 0 {
  31. db = db.Where("x.lapseto = ?", lapseto)
  32. }
  33. if source > 0 {
  34. db = db.Where("x.source = ?", source)
  35. }
  36. offset := (page - 1) * limit
  37. err = db.Count(&total).Order("x.created_time desc").Offset(offset).Limit(limit).
  38. Select("x.id,x.user_org_id,x.user_id,x.avatar,x.patient_type,x.dialysis_no,x.admission_number,x.source,x.lapseto,x.partition_id,x.bed_id,x.name,x.alias,x.gender,x.marital_status,x.id_card_no,x.birthday,x.reimbursement_way_id,x.health_care_type,x.health_care_no,x.health_care_due_date,x.height,x.blood_type,x.rh,x.health_care_due_alert_date,x.education_level,x.profession,x.phone,x.home_telephone,x.relative_phone,x.relative_relations,x.home_address,x.work_unit,x.unit_address,x.children,x.receiving_date,x.is_hospital_first_dialysis,x.first_dialysis_date,x.first_dialysis_hospital,x.predialysis_condition,x.pre_hospital_dialysis_frequency,x.pre_hospital_dialysis_times,x.hospital_first_dialysis_date,x.induction_period,x.initial_dialysis,x.total_dialysis,x.attending_doctor_id,x.head_nurse_id,x.evaluate,x.diagnose,x.remark,x.registrars_id,x.registrars,x.qr_code,x.binding_state,x.patient_complains,x.present_history,x.past_history,x.temperature,x.pulse,x.respiratory,x.sbp,x.dbp,x.status,x.created_time,x.updated_time,x.nation,x.native_place,x.age,x.blood_patients,x.slow_patients,x.member_patients,x.ecommer_patients,x.blood_id,x.slow_id,x.member_id").Find(&patients).Error
  39. fmt.Println("err是什么", err)
  40. return
  41. }
  42. func GetAllSlowPatient(orgid int64, page int64, limit int64) (patients []*models.XtPatientsNew, total int64, err error) {
  43. db := XTReadDB().Table("xt_patients_new as x").Where("x.status = 1")
  44. if orgid > 0 {
  45. db = db.Where("x.user_org_id = ? and x.slow_patients = 1", orgid)
  46. }
  47. offset := (page - 1) * limit
  48. err = db.Count(&total).Order("x.created_time desc").Offset(offset).Limit(limit).
  49. Select("x.id,x.user_org_id,x.user_id,x.avatar,x.patient_type,x.dialysis_no,x.admission_number,x.source,x.lapseto,x.partition_id,x.bed_id,x.name,x.alias,x.gender,x.marital_status,x.id_card_no,x.birthday,x.reimbursement_way_id,x.health_care_type,x.health_care_no,x.health_care_due_date,x.height,x.blood_type,x.rh,x.health_care_due_alert_date,x.education_level,x.profession,x.phone,x.home_telephone,x.relative_phone,x.relative_relations,x.home_address,x.work_unit,x.unit_address,x.children,x.receiving_date,x.is_hospital_first_dialysis,x.first_dialysis_date,x.first_dialysis_hospital,x.predialysis_condition,x.pre_hospital_dialysis_frequency,x.pre_hospital_dialysis_times,x.hospital_first_dialysis_date,x.induction_period,x.initial_dialysis,x.total_dialysis,x.attending_doctor_id,x.head_nurse_id,x.evaluate,x.diagnose,x.remark,x.registrars_id,x.registrars,x.qr_code,x.binding_state,x.patient_complains,x.present_history,x.past_history,x.temperature,x.pulse,x.respiratory,x.sbp,x.dbp,x.status,x.created_time,x.updated_time,x.nation,x.native_place,x.age,x.blood_patients,x.slow_patients,x.member_patients,x.ecommer_patients,x.blood_id,x.slow_id,x.member_id").Find(&patients).Error
  50. fmt.Println("err是什么", err)
  51. return
  52. }
  53. func GetAllMemberPatient(orgid int64, page int64, limit int64) (patients []*models.XtPatientsNew, total int64, err error) {
  54. db := XTReadDB().Table("xt_patients_new as x").Where("x.status = 1")
  55. if orgid > 0 {
  56. db = db.Where("x.user_org_id = ? and x.member_patients = 1", orgid)
  57. }
  58. offset := (page - 1) * limit
  59. err = db.Count(&total).Order("x.created_time desc").Offset(offset).Limit(limit).
  60. Select("x.id,x.user_org_id,x.user_id,x.avatar,x.patient_type,x.dialysis_no,x.admission_number,x.source,x.lapseto,x.partition_id,x.bed_id,x.name,x.alias,x.gender,x.marital_status,x.id_card_no,x.birthday,x.reimbursement_way_id,x.health_care_type,x.health_care_no,x.health_care_due_date,x.height,x.blood_type,x.rh,x.health_care_due_alert_date,x.education_level,x.profession,x.phone,x.home_telephone,x.relative_phone,x.relative_relations,x.home_address,x.work_unit,x.unit_address,x.children,x.receiving_date,x.is_hospital_first_dialysis,x.first_dialysis_date,x.first_dialysis_hospital,x.predialysis_condition,x.pre_hospital_dialysis_frequency,x.pre_hospital_dialysis_times,x.hospital_first_dialysis_date,x.induction_period,x.initial_dialysis,x.total_dialysis,x.attending_doctor_id,x.head_nurse_id,x.evaluate,x.diagnose,x.remark,x.registrars_id,x.registrars,x.qr_code,x.binding_state,x.patient_complains,x.present_history,x.past_history,x.temperature,x.pulse,x.respiratory,x.sbp,x.dbp,x.status,x.created_time,x.updated_time,x.nation,x.native_place,x.age,x.blood_patients,x.slow_patients,x.member_patients,x.ecommer_patients,x.blood_id,x.slow_id,x.member_id").Find(&patients).Error
  61. fmt.Println("err是什么", err)
  62. return
  63. }
  64. func GetAllPatient(orgid int64) (patients []*models.PatientsNew, err error) {
  65. db := XTReadDB().Table("xt_patients_new as x")
  66. err = db.Raw("select id,name from xt_patients_new where user_org_id = ? and status =1", orgid).Scan(&patients).Error
  67. return
  68. }
  69. func ChechLastDialysisNoTwo(orgID int64) (dialysisNo int64) {
  70. var patient models.Patients
  71. err := readDb.Model(&models.Patients{}).Where("user_org_id=? and status = 1", orgID).Order("dialysis_no desc").First(&patient).Error
  72. if err != nil {
  73. return
  74. }
  75. if patient.ID == 0 {
  76. return
  77. }
  78. dialysisNo, _ = strconv.ParseInt(patient.DialysisNo, 10, 64)
  79. return
  80. }
  81. func GetBloodPatientInfo(orgid int64, phone string) (*models.Patients, error) {
  82. patients := models.Patients{}
  83. var err error
  84. err = XTReadDB().Model(&patients).Where("user_org_id = ? and phone = ? and status = 1", orgid, phone).Find(&patients).Error
  85. if err == gorm.ErrRecordNotFound {
  86. return nil, err
  87. }
  88. if err != nil {
  89. return nil, err
  90. }
  91. return &patients, nil
  92. }
  93. func GetSlowPatientInfo(orgid int64, phone string) (*models.CdmPatients, error) {
  94. patients := models.CdmPatients{}
  95. var err error
  96. err = PatientReadDB().Model(&patients).Where("user_org_id = ? and phone = ? and status = 1", orgid, phone).Find(&patients).Error
  97. if err == gorm.ErrRecordNotFound {
  98. return nil, err
  99. }
  100. if err != nil {
  101. return nil, err
  102. }
  103. return &patients, nil
  104. }
  105. func GetMemberPatientInfo(orgid int64, phone string) (*models.SgjUserCustomer, error) {
  106. customer := models.SgjUserCustomer{}
  107. err := UserReadDB().Model(&customer).Where("user_org_id = ? and mobile = ? and status =1", orgid, phone).Find(&customer).Error
  108. if err == gorm.ErrRecordNotFound {
  109. return nil, err
  110. }
  111. if err != nil {
  112. return nil, err
  113. }
  114. return &customer, nil
  115. }
  116. func CreateOldPatient(patients *models.Patients) error {
  117. err := XTWriteDB().Create(&patients).Error
  118. return err
  119. }
  120. func GetLastOldPatient(orgid int64) (models.Patients, error) {
  121. patients := models.Patients{}
  122. err := XTReadDB().Model(&patients).Where("user_org_id = ? and status =1", orgid).Last(&patients).Error
  123. return patients, err
  124. }
  125. func AddPatientLapseto(lapseto *models.PatientLapseto) error {
  126. err := XTWriteDB().Create(&lapseto).Error
  127. return err
  128. }
  129. func AddContagions(patienid int64, createdtime int64, updatedtime int64, contagions []int64) (err error) {
  130. utx := writeDb.Begin()
  131. if len(contagions) > 0 {
  132. thisSQL := "INSERT INTO xt_patients_infectious_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  133. insertParams := make([]string, 0)
  134. insertData := make([]interface{}, 0)
  135. for _, contagion := range contagions {
  136. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  137. insertData = append(insertData, patienid)
  138. insertData = append(insertData, contagion)
  139. insertData = append(insertData, 1)
  140. insertData = append(insertData, createdtime)
  141. insertData = append(insertData, updatedtime)
  142. }
  143. thisSQL += strings.Join(insertParams, ",")
  144. err = utx.Exec(thisSQL, insertData...).Error
  145. fmt.Println("这个错误err", err)
  146. }
  147. utx.Commit()
  148. return
  149. }
  150. func AddSlowContagions(patienid int64, createdtime int64, updatedtime int64, contagions []int64, orgid int64) (err error) {
  151. utx := PatientWriteDB().Begin()
  152. if len(contagions) > 0 {
  153. thisSQL := "INSERT INTO xt_patients_infectious_diseases (patient_id, disease_id, status, created_time, updated_time,user_org_id) VALUES "
  154. insertParams := make([]string, 0)
  155. insertData := make([]interface{}, 0)
  156. for _, contagion := range contagions {
  157. insertParams = append(insertParams, "(?, ?, ?, ?, ?,?)")
  158. insertData = append(insertData, patienid)
  159. insertData = append(insertData, contagion)
  160. insertData = append(insertData, 1)
  161. insertData = append(insertData, createdtime)
  162. insertData = append(insertData, updatedtime)
  163. insertData = append(insertData, orgid)
  164. }
  165. thisSQL += strings.Join(insertParams, ", ")
  166. err = utx.Exec(thisSQL, insertData...).Error
  167. if err != nil {
  168. utx.Rollback()
  169. utx.Rollback()
  170. return
  171. }
  172. }
  173. utx.Commit()
  174. return
  175. }
  176. func AddSlowDiseases(patienid int64, createdtime int64, updatedtime int64, diseases []int64, orgid int64) (err error) {
  177. utx := PatientWriteDB().Begin()
  178. if len(diseases) > 0 {
  179. thisSQL := "INSERT INTO xt_patients_chronic_diseases (patient_id, disease_id, status, created_time, updated_time,user_org_id) VALUES "
  180. insertParams := make([]string, 0)
  181. insertData := make([]interface{}, 0)
  182. for _, disease := range diseases {
  183. insertParams = append(insertParams, "(?, ?, ?, ?, ?,?)")
  184. insertData = append(insertData, patienid)
  185. insertData = append(insertData, disease)
  186. insertData = append(insertData, 1)
  187. insertData = append(insertData, createdtime)
  188. insertData = append(insertData, updatedtime)
  189. insertData = append(insertData, orgid)
  190. }
  191. thisSQL += strings.Join(insertParams, ", ")
  192. err = utx.Exec(thisSQL, insertData...).Error
  193. if err != nil {
  194. utx.Rollback()
  195. return
  196. }
  197. }
  198. utx.Commit()
  199. return
  200. }
  201. func CreateNewPatient(patientsNew *models.XtPatientsNew) error {
  202. err := XTWriteDB().Create(&patientsNew).Error
  203. return err
  204. }
  205. func CreateCdmPatient(cdmpatient *models.CdmPatients) error {
  206. err := PatientWriteDB().Create(&cdmpatient).Error
  207. return err
  208. }
  209. func CreateMemberPatient(customer *models.SgjUserCustomer) error {
  210. err := UserWriteDB().Create(&customer).Error
  211. return err
  212. }
  213. func GetOldCdmPatient(orgid int64) (models.CdmPatients, error) {
  214. patients := models.CdmPatients{}
  215. err := PatientReadDB().Model(&patients).Where("user_org_id = ? and status =1", orgid).Last(&patients).Error
  216. return patients, err
  217. }
  218. func GetPatientDetailTwo(id int64) (models.XtPatientsNew, error) {
  219. patients := models.XtPatientsNew{}
  220. err := XTReadDB().Where("id=? and status = 1", id).Find(&patients).Error
  221. return patients, err
  222. }
  223. func GetPatientNewId(id int64) (models.XtPatientsNew, error) {
  224. patientsNew := models.XtPatientsNew{}
  225. err := XTReadDB().Where("blood_id = ? and status = 1", id).Find(&patientsNew).Error
  226. return patientsNew, err
  227. }
  228. func GetPatientDiseasesTwo(id int64) []int64 {
  229. var dis []models.XtPatientsInfectiousDiseases
  230. ids := make([]int64, 0)
  231. err := XTReadDB().Model(&models.XtPatientsInfectiousDiseases{}).Where("patient_id = ? and status = 1", id).Find(&dis).Error
  232. if err != nil || len(dis) == 0 {
  233. return ids
  234. }
  235. for _, item := range dis {
  236. ids = append(ids, item.DiseaseId)
  237. }
  238. return ids
  239. }
  240. func GetSlowPatientDiseases(id int64) (infeciousdisease []*models.CdmPatientsInfectiousDiseases, err error) {
  241. //var dis []models.CdmPatientsInfectiousDiseases
  242. //diseases := models.CdmPatientsChronicDiseases{}
  243. //ids := make([]int64, 0)
  244. //err := PatientReadDB().Model(&diseases).Where("patient_id = ? and status =1", id).Find(&ids).Error
  245. //if err != nil || len(dis) == 0 {
  246. // return ids
  247. //}
  248. //for _, item := range dis {
  249. // ids = append(ids, item.DiseaseId)
  250. //}
  251. //return ids
  252. err = PatientReadDB().Model(&infeciousdisease).Where("patient_id = ? and status =1", id).Find(&infeciousdisease).Error
  253. return infeciousdisease, err
  254. }
  255. func GetCronicDiseases(id int64) (chronic []*models.CdmPatientsChronicDiseases, err error) {
  256. //var dis []models.CdmPatientsChronicDiseases
  257. //ids := make([]int64, 0)
  258. //err := PatientReadDB().Model(&models.CdmPatientsChronicDiseases{}).Where("patient_id = ? and status =1", id).Find(&ids).Error
  259. //if err != nil || len(dis) == 0 {
  260. // return ids
  261. //}
  262. //for _, item := range dis {
  263. // ids = append(ids, item.DiseaseId)
  264. //}
  265. //return ids
  266. err = PatientReadDB().Model(&chronic).Where("patient_id = ? and status =1", id).Find(&chronic).Error
  267. return chronic, err
  268. }
  269. func CreatePatientTwo(patient *models.Patients, contagions []int64, diseases []int64) (err error) {
  270. user, _ := GetSgjUserByMobild(patient.Phone)
  271. customer, _ := GetSgjCoustomerByMobile(patient.UserOrgId, patient.Phone)
  272. utx := writeDb.Begin()
  273. btx := writeUserDb.Begin()
  274. if user.ID == 0 {
  275. user.Mobile = patient.Phone
  276. user.Avatar = patient.Avatar
  277. user.AvatarThumb = patient.Avatar
  278. user.Birthday = patient.Birthday
  279. user.Username = patient.Name
  280. user.Gender = patient.Gender
  281. user.Sources = 11
  282. user.Introduce = patient.Remark
  283. user.Status = 1
  284. user.UpdatedTime = patient.UpdatedTime
  285. user.CreatedTime = patient.CreatedTime
  286. err = btx.Create(&user).Error
  287. if err != nil {
  288. utx.Rollback()
  289. btx.Rollback()
  290. return
  291. }
  292. }
  293. patient.UserId = user.ID
  294. if customer == nil {
  295. err = btx.Create(&models.SgjCustomer{
  296. UserOrgId: patient.UserOrgId,
  297. UserId: user.ID,
  298. Mobile: patient.Phone,
  299. Name: patient.Name,
  300. Gender: patient.Gender,
  301. Birthday: patient.Birthday,
  302. Sources: 11,
  303. Status: 1,
  304. CreatedTime: patient.CreatedTime,
  305. UpdatedTime: patient.UpdatedTime,
  306. Avatar: patient.Avatar,
  307. Remark: patient.Remark,
  308. }).Error
  309. if err != nil {
  310. utx.Rollback()
  311. btx.Rollback()
  312. return
  313. }
  314. }
  315. err = utx.Create(patient).Error
  316. if err != nil {
  317. utx.Rollback()
  318. btx.Rollback()
  319. return
  320. }
  321. var lapseto models.PatientLapseto
  322. lapseto.PatientId = patient.ID
  323. lapseto.LapsetoType = patient.Lapseto
  324. lapseto.CreatedTime = patient.CreatedTime
  325. lapseto.UpdatedTime = patient.CreatedTime
  326. lapseto.Status = 1
  327. lapseto.LapsetoTime = patient.CreatedTime
  328. err = utx.Create(&lapseto).Error
  329. if err != nil {
  330. utx.Rollback()
  331. btx.Rollback()
  332. return
  333. }
  334. if len(contagions) > 0 {
  335. thisSQL := "INSERT INTO xt_patients_infectious_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  336. insertParams := make([]string, 0)
  337. insertData := make([]interface{}, 0)
  338. for _, contagion := range contagions {
  339. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  340. insertData = append(insertData, patient.ID)
  341. insertData = append(insertData, contagion)
  342. insertData = append(insertData, 1)
  343. insertData = append(insertData, patient.CreatedTime)
  344. insertData = append(insertData, patient.UpdatedTime)
  345. }
  346. thisSQL += strings.Join(insertParams, ", ")
  347. err = utx.Exec(thisSQL, insertData...).Error
  348. if err != nil {
  349. utx.Rollback()
  350. btx.Rollback()
  351. return
  352. }
  353. }
  354. if len(diseases) > 0 {
  355. thisSQL := "INSERT INTO xt_patients_chronic_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  356. insertParams := make([]string, 0)
  357. insertData := make([]interface{}, 0)
  358. for _, disease := range diseases {
  359. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  360. insertData = append(insertData, patient.ID)
  361. insertData = append(insertData, disease)
  362. insertData = append(insertData, 1)
  363. insertData = append(insertData, patient.CreatedTime)
  364. insertData = append(insertData, patient.UpdatedTime)
  365. }
  366. thisSQL += strings.Join(insertParams, ", ")
  367. err = utx.Exec(thisSQL, insertData...).Error
  368. if err != nil {
  369. utx.Rollback()
  370. btx.Rollback()
  371. return
  372. }
  373. }
  374. utx.Commit()
  375. btx.Commit()
  376. return
  377. }
  378. func FindPatientByMobileFive(mobile string, orgID int64) (patient models.XtPatientsNew, err error) {
  379. err = readDb.Model(&models.Patients{}).Where("phone=? and user_org_id=? and status=1", mobile, orgID).First(&patient).Error
  380. return
  381. }
  382. func FindPatientByDialysisNoFive(orgID int64, dialysisNo string) (patient models.Patients, err error) {
  383. err = readDb.Model(&models.Patients{}).Where("status=1 and user_org_id=? and dialysis_no=?", orgID, dialysisNo).First(&patient).Error
  384. return
  385. }
  386. func FindPatientByIdCardNoFive(orgID int64, idCardNo string) (patient models.XtPatientsNew, err error) {
  387. err = readDb.Model(&models.Patients{}).Where("status=1 and user_org_id=? and id_card_no=?", orgID, idCardNo).First(&patient).Error
  388. return
  389. }
  390. func FindPatientByIdCardNoOne(orgID int64, idCardNo string, id int64) (patient models.XtPatientsNew, err error) {
  391. err = readDb.Model(&models.Patients{}).Where("status=1 and user_org_id=? and id_card_no=? and id<>?", orgID, idCardNo, id).First(&patient).Error
  392. return
  393. }
  394. func FindPatientByDialysisNoOne(orgID int64, dialysisNo string, id int64) (patient models.Patients, err error) {
  395. err = readDb.Model(&models.Patients{}).Where("status=1 and user_org_id=? and dialysis_no=? and id<>?", orgID, dialysisNo, id).First(&patient).Error
  396. return
  397. }
  398. func FindPatientByMobileOne(mobile string, orgID int64, id int64) (patient models.XtPatientsNew, err error) {
  399. //err = readDb.Model(&models.Patients{}).Where(" status=1 and user_org_id=? and phone=? and id<>?", orgID, mobile, id).First(&patient).Error
  400. //return
  401. db := readDb.Table("xt_patients_new as x")
  402. err = db.Not("phone = ?", mobile).Where("user_org_id = ? and id = ? and status =1", orgID, id).Find(&patient).Error
  403. return
  404. }
  405. func GetPatientData(phone string, orgid int64) (*models.XtPatientsNew, error) {
  406. var patientnew models.XtPatientsNew
  407. var err error
  408. err = XTReadDB().Model(&patientnew).Where("user_org_id = ? and phone = ? and status =? and blood_patients = ?", orgid, phone, 1, 1).Find(&patientnew).Error
  409. if err == gorm.ErrRecordNotFound {
  410. return nil, err
  411. }
  412. if err != nil {
  413. return nil, err
  414. }
  415. return &patientnew, nil
  416. }
  417. func GetSlowPatientData(phone string, orgid int64) (*models.CdmPatients, error) {
  418. var patientnew models.CdmPatients
  419. err = PatientReadDB().Model(&patientnew).Where("user_org_id = ? and phone = ? and status = ?", orgid, phone, 1).Find(&patientnew).Error
  420. if err == gorm.ErrRecordNotFound {
  421. return nil, err
  422. }
  423. if err != nil {
  424. return nil, err
  425. }
  426. return &patientnew, nil
  427. }
  428. func GetMemberNewPatient(phone string, orgid int64) (*models.XtPatientsNew, error) {
  429. var patientnew models.XtPatientsNew
  430. err = XTReadDB().Model(&patientnew).Where("user_org_id = ? and phone = ? and status = ? and member_patients = ?", orgid, phone, 1, 1).Find(&patientnew).Error
  431. if err == gorm.ErrRecordNotFound {
  432. return nil, err
  433. }
  434. if err != nil {
  435. return nil, err
  436. }
  437. return &patientnew, nil
  438. }
  439. func GetLastMemberPatient(orgid int64) (models.SgjUserCustomer, error) {
  440. customer := models.SgjUserCustomer{}
  441. err := UserReadDB().Model(&customer).Where("user_org_id = ? and status = 1", orgid).Last(&customer).Error
  442. return customer, err
  443. }
  444. func GetLastNewSlowPatient(phone string, orgid int64) (*models.XtPatientsNew, error) {
  445. var patientnew models.XtPatientsNew
  446. err = XTReadDB().Model(&patientnew).Where("user_org_id = ? and phone = ? and status = ? and slow_patients = ?", orgid, phone, 1, 1).Find(&patientnew).Error
  447. if err == gorm.ErrRecordNotFound {
  448. return nil, err
  449. }
  450. if err != nil {
  451. return nil, err
  452. }
  453. return &patientnew, nil
  454. }
  455. func GetNewDoctorAdvice(patientID int64, advice_type int64, start int64, end int64, limit int64, page int64, orgID int64) (doctoradvice []*models.DoctorAdvices, total int64, err error) {
  456. db := readDb.Table("xt_doctor_advice as x").Where("x.status = 1")
  457. table := UserReadDB().Table("sgj_user_admin_role as r")
  458. fmt.Print("table", table)
  459. if orgID > 0 {
  460. db = db.Where("x.user_org_id=?", orgID)
  461. }
  462. if patientID > 0 {
  463. db = db.Where("x.patient_id = ?", patientID)
  464. }
  465. if advice_type == 1 {
  466. db = db.Where("x.advice_type = ?", advice_type)
  467. }
  468. if advice_type == 3 {
  469. db = db.Where("x.advice_type <> 1")
  470. }
  471. if start > 0 {
  472. db = db.Where("x.start_time>=?", start)
  473. }
  474. if end > 0 {
  475. db = db.Where("x.start_time<=?", end)
  476. }
  477. //offset := (page - 1) * limit
  478. err = db.Group("x.id").Count(&total).Select("x.id, x.user_org_id, x.patient_id, x.advice_type, x.advice_date, x.record_date, x.start_time, x.advice_name,x.advice_desc, x.reminder_date, x.drug_spec, x.drug_spec_unit, x.single_dose, x.single_dose_unit, x.prescribing_number, x.prescribing_number_unit, x.delivery_way, x.execution_frequency, x.advice_doctor, x.status, x.created_time,x.updated_time, x.advice_affirm, x.remark, x.stop_time, x.stop_reason, x.stop_doctor, x.stop_state, x.parent_id, x.execution_time, x.execution_staff, x.execution_state, x.checker, x.check_state, x.check_time, x.groupno,x.remind_type,x.frequency_type,x.day_count,x.week_day,x.parent_id,r.user_name, IF(x.parent_id > 0, x.parent_id, x.id) as advice_order").Joins("Left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.advice_doctor").Order("start_time desc, groupno desc, advice_order desc, id asc").Scan(&doctoradvice).Error
  479. fmt.Print("err", err)
  480. return
  481. }
  482. func GetNewDoctorAdviceOne(patientID int64, advice_type int64, start int64, end int64, limit int64, page int64, orgID int64) (doctoradvice []*models.DoctorAdvice, total int64, err error) {
  483. db := readDb.Table("xt_doctor_advice as x").Where("x.status = 1")
  484. table := UserReadDB().Table("sgj_user_admin_role as r")
  485. fmt.Println("table", table)
  486. if orgID > 0 {
  487. db = db.Where("x.user_org_id=?", orgID)
  488. }
  489. if patientID > 0 {
  490. db = db.Where("x.patient_id = ?", patientID)
  491. }
  492. if advice_type == 1 {
  493. db = db.Where("x.advice_type = ?", advice_type)
  494. }
  495. if advice_type == 3 {
  496. db = db.Where("x.advice_type <> 1")
  497. }
  498. if start > 0 {
  499. db = db.Where("x.start_time>=?", start)
  500. }
  501. if end > 0 {
  502. db = db.Where("x.start_time<=?", end)
  503. }
  504. offset := (page - 1) * limit
  505. err = db.Order("x.start_time desc").Group("x.start_time").Count(&total).Offset(offset).Limit(limit).Select("x.id, x.user_org_id, x.patient_id, x.advice_type, x.advice_date, x.record_date, x.start_time, x.advice_name,x.advice_desc, x.reminder_date, x.drug_spec, x.drug_spec_unit, x.single_dose, x.single_dose_unit, x.prescribing_number, x.prescribing_number_unit, x.delivery_way, x.execution_frequency, x.advice_doctor, x.status, x.created_time,x.updated_time, x.advice_affirm, x.remark, x.stop_time, x.stop_reason, x.stop_doctor, x.stop_state, x.parent_id, x.execution_time, x.execution_staff, x.execution_state, x.checker, x.check_state, x.check_time, x.groupno,x.remind_type,x.frequency_type,x.day_count,x.week_day,x.parent_id,r.user_name, IF(x.parent_id > 0, x.parent_id, x.id) as advice_order").Joins("Left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.advice_doctor").Scan(&doctoradvice).Error
  506. fmt.Print("错误是什么", err)
  507. return
  508. }
  509. func GetDryWeight(patientid int64, startime int64, endtime int64, limit int64, page int64, orgId int64) (dryWeight []*models.XtPatientDryweight, total int64, err error) {
  510. db := XTReadDB().Table("xt_patient_dryweight as x").Where("x.status = 1")
  511. if orgId > 0 {
  512. db = db.Where("x.user_org_id = ?", orgId)
  513. }
  514. if patientid > 0 {
  515. db = db.Where("x.patient_id = ?", patientid)
  516. }
  517. if startime > 0 {
  518. db = db.Where("x.ctime >= ?", startime)
  519. }
  520. if endtime > 0 {
  521. db = db.Where("x.ctime <=?", endtime)
  522. }
  523. offset := (page - 1) * limit
  524. err = db.Count(&total).Order("x.ctime desc").Offset(offset).Limit(limit).Group("x.id").
  525. Select("x.id,x.dry_weight,x.creator,x.remakes,x.patient_id,x.ctime,x.adjusted_value,x.user_id,x.user_org_id,r.user_name").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.creator").Scan(&dryWeight).Error
  526. return dryWeight, total, err
  527. }
  528. func ToSearch(orgId int64, name string) (patient []*models.XtPatientsNew, err error) {
  529. likeKey := "%" + name + "%"
  530. err = XTReadDB().Where("name like ? and user_org_id = ?", likeKey, orgId).Find(&patient).Error
  531. return patient, err
  532. }
  533. func GetCourseManagement(patientid int64, startime int64, endtime int64, limit int64, page int64, orgid int64) (patientCourse []*models.PatientCourseOfDiseases, total int64, err error) {
  534. db := XTReadDB().Table("xt_patient_course_of_disease as x").Where("x.status = 1")
  535. if patientid > 0 {
  536. db = db.Where("x.patient_id = ?", patientid)
  537. }
  538. if orgid > 0 {
  539. db = db.Where("x.org_id = ?", orgid)
  540. }
  541. if startime > 0 {
  542. db = db.Where("x.record_time >=?", startime)
  543. }
  544. if endtime > 0 {
  545. db = db.Where("x.record_time <= ?", endtime)
  546. }
  547. offset := (page - 1) * limit
  548. err = db.Count(&total).Order("x.ctime desc").Offset(offset).Limit(limit).Group("x.id").
  549. Select("x.id,x.org_id,x.patient_id,x.recorder,x.record_time,x.content,x.title,r.user_name").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.recorder").Scan(&patientCourse).Error
  550. return patientCourse, total, err
  551. }
  552. func DeleteCouseManagement(patientid int64) error {
  553. err := XTWriteDB().Model(models.PatientCourseOfDisease{}).Where("id = ?", patientid).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  554. return err
  555. }
  556. func GetCouseManagentDetail(id int64) (models.PatientCourseOfDiseasess, error) {
  557. disease := models.PatientCourseOfDiseasess{}
  558. db := XTReadDB().Table("xt_patient_course_of_disease as x")
  559. err := db.Select("x.id,x.org_id,x.patient_id,x.recorder,x.record_time,x.content,x.title,s.name,r.user_name").Joins("left join xt_patients as s on s.id = x.patient_id ").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.recorder").Where("x.id = ?", id).Scan(&disease).Error
  560. return disease, err
  561. }
  562. func DeleteDryWeight(id int64) error {
  563. err := XTWriteDB().Model(models.SgjPatientDryweight{}).Where("id=?", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  564. return err
  565. }
  566. func GetDryWeightDetail(id int64) (models.SgjPatientDryweights, error) {
  567. dryweight := models.SgjPatientDryweights{}
  568. db := XTReadDB().Table("xt_patient_dryweight as x")
  569. err := db.Select("x.id,x.dry_weight,x.creator,x.remakes,x.patient_id,x.adjusted_value,x.user_org_id,x.user_id,x.ctime,s.name,r.user_name").Joins("left join xt_patients as s on s.id = x.patient_id").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.user_id").Where("x.id = ?", id).Scan(&dryweight).Error
  570. return dryweight, err
  571. }
  572. func GetlongDialysisrecord(patientid int64, startime int64, endtime int64, limit int64, page int64, orgid int64) (prescription []*models.XtDialysisSolution, total int64, err error) {
  573. db := XTReadDB().Table("xt_dialysis_solution as x").Where("x.status = 1")
  574. if patientid > 0 {
  575. db = db.Where("x.patient_id = ?", patientid)
  576. }
  577. if orgid > 0 {
  578. db = db.Where("x.user_org_id = ?", orgid)
  579. }
  580. if startime > 0 {
  581. db = db.Where("x.created_time >=?", startime)
  582. }
  583. if endtime > 0 {
  584. db = db.Where("x.created_time <= ?", endtime)
  585. }
  586. offset := (page - 1) * limit
  587. err = db.Count(&total).Order("x.created_time desc").Offset(offset).Limit(limit).Group("x.id").
  588. Select("x.id,x.name,x.sub_name,x.user_org_id,x.patient_id,x.parent_id,x.type,x.period,x.times,x.anticoagulant,x.anticoagulant_shouji,x.anticoagulant_weichi,x.anticoagulant_zongliang,x.anticoagulant_gaimingcheng,x.anticoagulant_gaijiliang,x.mode_name,x.mode_id,x.dialysis_duration,x.replacement_way,x.hemodialysis_machine,x.blood_filter,x.perfusion_apparatus,x.blood_flow_volume,x.dewater,x.displace_liqui,x.glucose,x.dry_weight,x.dialysate_flow,x.kalium,x.sodium,x.calcium,x.bicarbonate,x.doctor,x.first_dialysis,x.remark,x.initiate_mode,x.affirm_state,x.use_state,x.status,x.registrars_id,x.created_time,x.updated_time,x.solution_type,x.dialysate_temperature,x.conductivity,x.dialysis_duration_hour,x.dialysis_duration_minute,x.target_ultrafiltration,x.dialysate_formulation,x.dialyzer,x.replacement_total,x.dialyzer_perfusion_apparatus,x.body_fluid,x.special_medicine,x.special_medicine_other,x.displace_liqui_part,x.displace_liqui_value,x.blood_access,x.ultrafiltration,x.body_fluid_other,x.target_ktv").Find(&prescription).Error
  589. return
  590. }
  591. func GetRescuerecord(patientid int64, startime int64, endtime int64, limit int64, page int64, orgid int64) (rescuerecord []*models.XtPatientRescueRecords, total int64, err error) {
  592. db := XTReadDB().Table("xt_patient_rescue_record as x").Where("x.status = 1")
  593. if patientid > 0 {
  594. db = db.Where("x.patient_id = ?", patientid)
  595. }
  596. if orgid > 0 {
  597. db = db.Where("x.org_id = ?", orgid)
  598. }
  599. if startime > 0 {
  600. db = db.Where("x.record_time >=?", startime)
  601. }
  602. if endtime > 0 {
  603. db = db.Where("x.record_time <= ?", endtime)
  604. }
  605. offset := (page - 1) * limit
  606. err = db.Count(&total).Order("x.record_time desc").Offset(offset).Limit(limit).Group("x.id").
  607. Select("x.id,x.org_id,x.patient_id,x.recorder,x.record_time,x.content,x.status,r.user_name").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.recorder").Scan(&rescuerecord).Error
  608. return
  609. }
  610. func GetScheduling(patientid int64, startime int64, endtime int64, limit int64, page int64, orgid int64, modeType int64) (schedules []*models.XtSchedules, total int64, err error) {
  611. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  612. if patientid > 0 {
  613. db = db.Where("x.patient_id = ?", patientid)
  614. }
  615. if orgid > 0 {
  616. db = db.Where("x.user_org_id = ?", orgid)
  617. }
  618. if startime > 0 {
  619. db = db.Where("x.schedule_date >=?", startime)
  620. }
  621. if endtime > 0 {
  622. db = db.Where("x.schedule_date <= ?", endtime)
  623. }
  624. if modeType > 0 {
  625. db = db.Where("x.mode_id = ?", modeType)
  626. }
  627. offset := (page - 1) * limit
  628. err = db.Count(&total).Order("x.schedule_date desc").Offset(offset).Limit(limit).Group("x.id").
  629. Select("x.id,x.user_org_id,x.partition_id,x.bed_id,x.patient_id,x.schedule_date,x.schedule_type,x.schedule_week,x.mode_id,x.status,x.created_time,x.updated_time,s.number,z.name").Joins("left join xt_device_number as s on s.id = x.bed_id").Joins("left join xt_device_zone as z on z.id = x.partition_id").Scan(&schedules).Error
  630. return
  631. }
  632. func GetEducation(patientid int64, startime int64, endtime int64, limit int64, page int64, orgid int64) (treatment []*models.XtTreatmentSummary, total int64, err error) {
  633. db := XTReadDB().Table("xt_treatment_summary as x").Where("x.status = 1")
  634. if patientid > 0 {
  635. db = db.Where("x.patient_id = ?", patientid)
  636. }
  637. if orgid > 0 {
  638. db = db.Where("x.user_org_id = ?", orgid)
  639. }
  640. if startime > 0 {
  641. db = db.Where("x.assessment_date >=?", startime)
  642. }
  643. if endtime > 0 {
  644. db = db.Where("x.assessment_date <= ?", endtime)
  645. }
  646. offset := (page - 1) * limit
  647. err = db.Count(&total).Order("x.assessment_date desc").Offset(offset).Limit(limit).Group("x.id").
  648. Select("x.id,x.user_org_id,x.patient_id,x.assessment_date,x.dialysis_order_id,x.mission,x.dialysis_summary,x.change,x.sj_nurse,x.zl_nurse,x.hd_nurse,x.xj_nurse,x.zl_doctor,x.channel_image,x.puncture,x.puncture_needle,x.puncture_direction,x.status,x.created_time,x.updated_time,x.creater,x.modifier").Scan(&treatment).Error
  649. return
  650. }
  651. func GetSchedulingDetail(id int64) (models.XtScheduless, error) {
  652. schedules := models.XtScheduless{}
  653. db := XTReadDB().Table("xt_schedule as x")
  654. err := db.Select("x.id,x.user_org_id,x.partition_id,x.bed_id,x.patient_id,x.schedule_date,x.schedule_type,x.schedule_week,x.mode_id,n.number,z.name").Joins("left join xt_device_number as n on n.id = x.bed_id").Joins("left join xt_device_zone as z on z.id = x.partition_id").Where("x.id =?", id).Scan(&schedules).Error
  655. return schedules, err
  656. }
  657. func GetSchedulingTwo(id int64) (models.XtScheduless, error) {
  658. schedules := models.XtScheduless{}
  659. db := XTReadDB().Table("xt_schedule as x")
  660. err := db.Select("x.id,x.user_org_id,x.partition_id,x.bed_id,x.patient_id,x.schedule_date,x.schedule_type,x.schedule_week,x.mode_id,z.name").Joins("left join xt_patients as z on z.id = x.patient_id").Where("x.id =?", id).Scan(&schedules).Error
  661. return schedules, err
  662. }
  663. func DeleteScheduling(id int64) error {
  664. err := XTWriteDB().Model(models.XtSchedules{}).Where("id=?", id).Update(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix()}).Error
  665. return err
  666. }
  667. func GetEducationDetailOne(id int64) (models.XtTreatmentSummarys, error) {
  668. summarys := models.XtTreatmentSummarys{}
  669. db := XTReadDB().Table("xt_treatment_summary as x")
  670. err := db.Select("x.id,x.user_org_id,x.patient_id,x.assessment_date,x.dialysis_order_id,x.mission,x.dialysis_summary,x.change,x.sj_nurse,x.zl_nurse,x.hd_nurse,x.xj_nurse,x.zl_doctor,x.channel_image,x.puncture,x.puncture_needle,x.puncture_direction,x.status,x.created_time,x.creater,x.modifier,s.name").Joins("left join xt_patients as s on s.id = x.patient_id").Where("x.id = ?", id).Scan(&summarys).Error
  671. return summarys, err
  672. }
  673. func DeleteEducation(id int64) error {
  674. err := XTWriteDB().Model(models.XtTreatmentSummary{}).Where("id=?", id).Update(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix()}).Error
  675. return err
  676. }
  677. func GetRescueRecordDetail(id int64) (models.XtPatientRescueRecords, error) {
  678. records := models.XtPatientRescueRecords{}
  679. db := XTReadDB().Table("xt_patient_rescue_record as x")
  680. err := db.Select("x.id,x.org_id,x.patient_id,x.recorder,x.record_time,x.content,x.status,r.user_name,s.name").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.recorder").Joins("left join xt_patients as s on s.id = x.patient_id").Where("x.id=?", id).Scan(&records).Error
  681. return records, err
  682. }
  683. func DeleteRescueRecord(id int64) error {
  684. err := XTWriteDB().Model(models.XtPatientRescueRecord{}).Where("id=?", id).Update(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix()}).Error
  685. return err
  686. }
  687. func ToSearchePatient(keyword string, orgId int64) (paitents []*models.XtPatientsNew, err error) {
  688. likeKey := "%" + keyword + "%"
  689. err = XTReadDB().Where("user_org_id = ? and name like ? or dialysis_no like ?", orgId, likeKey, likeKey).Find(&paitents).Error
  690. return paitents, err
  691. }
  692. func SearchallPatient(keyword string, orgid int64) (patients []*models.XtPatientsNew, err error) {
  693. db := XTReadDB().Table("xt_patients_new as x").Where("x.status = 1")
  694. if len(keyword) > 0 {
  695. likeKey := "%" + keyword + "%"
  696. db = db.Where("x.name LIKE ? OR x.dialysis_no LIKE ?", likeKey, likeKey)
  697. }
  698. if orgid > 0 {
  699. db = db.Where("x.user_org_id = ?", orgid)
  700. }
  701. err = db.Select("x.id,x.user_org_id,x.user_id,x.avatar,x.patient_type,x.dialysis_no,x.admission_number,x.source,x.lapseto,x.partition_id,x.bed_id,x.name,x.alias,x.gender,x.marital_status,x.id_card_no,x.birthday,x.reimbursement_way_id,x.health_care_type,x.health_care_no,x.health_care_due_date,x.height,x.blood_type,x.rh,x.health_care_due_alert_date,x.education_level,x.profession,x.phone,x.home_telephone,x.relative_phone,x.relative_relations,x.home_address,x.work_unit,x.unit_address,x.children,x.receiving_date,x.is_hospital_first_dialysis,x.first_dialysis_date,x.first_dialysis_hospital,x.predialysis_condition,x.pre_hospital_dialysis_frequency,x.pre_hospital_dialysis_times,x.hospital_first_dialysis_date,x.induction_period,x.initial_dialysis,x.total_dialysis,x.attending_doctor_id,x.head_nurse_id,x.evaluate,x.diagnose,x.remark,x.registrars_id,x.registrars,x.qr_code,x.binding_state,x.patient_complains,x.present_history,x.past_history,x.temperature,x.pulse,x.respiratory,x.sbp,x.dbp,x.status,x.created_time,x.updated_time,x.nation,x.native_place,x.age,x.infectious_next_record_time,x.is_infectious,x.remind_cycle,x.response_result,x.is_open_remind,x.first_treatment_date,x.dialysis_age,x.expense_kind,x.tell_phone,x.contact_name,x.blood_patients,x.slow_patients,x.member_patients,x.ecommer_patients,x.blood_id,x.slow_id,x.member_id,x.member_fistdate,x.member_patienttype,x.member_treatement,x.equitment_id").Find(&patients).Error
  702. return patients, err
  703. }
  704. func GetSearchallBlood(keyword string, orgid int64) (patients []*models.XtPatientsNew, err error) {
  705. db := XTReadDB().Table("xt_patients_new as x").Where("x.status = 1 and x.blood_patients = 1")
  706. if len(keyword) > 0 {
  707. likeKey := "%" + keyword + "%"
  708. db = db.Where("x.name LIKE ? OR x.dialysis_no LIKE ?", likeKey, likeKey)
  709. }
  710. if orgid > 0 {
  711. db = db.Where("x.user_org_id = ?", orgid)
  712. }
  713. err = db.Select("x.id,x.user_org_id,x.user_id,x.avatar,x.patient_type,x.dialysis_no,x.admission_number,x.source,x.lapseto,x.partition_id,x.bed_id,x.name,x.alias,x.gender,x.marital_status,x.id_card_no,x.birthday,x.reimbursement_way_id,x.health_care_type,x.health_care_no,x.health_care_due_date,x.height,x.blood_type,x.rh,x.health_care_due_alert_date,x.education_level,x.profession,x.phone,x.home_telephone,x.relative_phone,x.relative_relations,x.home_address,x.work_unit,x.unit_address,x.children,x.receiving_date,x.is_hospital_first_dialysis,x.first_dialysis_date,x.first_dialysis_hospital,x.predialysis_condition,x.pre_hospital_dialysis_frequency,x.pre_hospital_dialysis_times,x.hospital_first_dialysis_date,x.induction_period,x.initial_dialysis,x.total_dialysis,x.attending_doctor_id,x.head_nurse_id,x.evaluate,x.diagnose,x.remark,x.registrars_id,x.registrars,x.qr_code,x.binding_state,x.patient_complains,x.present_history,x.past_history,x.temperature,x.pulse,x.respiratory,x.sbp,x.dbp,x.status,x.created_time,x.updated_time,x.nation,x.native_place,x.age,x.infectious_next_record_time,x.is_infectious,x.remind_cycle,x.response_result,x.is_open_remind,x.first_treatment_date,x.dialysis_age,x.expense_kind,x.tell_phone,x.contact_name,x.blood_patients,x.slow_patients,x.member_patients,x.ecommer_patients,x.blood_id,x.slow_id,x.member_id,x.member_fistdate,x.member_patienttype,x.member_treatement,x.equitment_id").Find(&patients).Error
  714. return patients, err
  715. }
  716. func GetSlowSearchePatient(keyword string, orgid int64) (patients []*models.XtPatientsNew, err error) {
  717. db := XTReadDB().Table("xt_patients_new as x").Where("x.status = 1 and x.slow_patients = 1")
  718. if len(keyword) > 0 {
  719. likeKey := "%" + keyword + "%"
  720. db = db.Where("x.name LIKE ? OR x.dialysis_no LIKE ?", likeKey, likeKey)
  721. }
  722. if orgid > 0 {
  723. db = db.Where("x.user_org_id = ?", orgid)
  724. }
  725. err = db.Select("x.id,x.user_org_id,x.user_id,x.avatar,x.patient_type,x.dialysis_no,x.admission_number,x.source,x.lapseto,x.partition_id,x.bed_id,x.name,x.alias,x.gender,x.marital_status,x.id_card_no,x.birthday,x.reimbursement_way_id,x.health_care_type,x.health_care_no,x.health_care_due_date,x.height,x.blood_type,x.rh,x.health_care_due_alert_date,x.education_level,x.profession,x.phone,x.home_telephone,x.relative_phone,x.relative_relations,x.home_address,x.work_unit,x.unit_address,x.children,x.receiving_date,x.is_hospital_first_dialysis,x.first_dialysis_date,x.first_dialysis_hospital,x.predialysis_condition,x.pre_hospital_dialysis_frequency,x.pre_hospital_dialysis_times,x.hospital_first_dialysis_date,x.induction_period,x.initial_dialysis,x.total_dialysis,x.attending_doctor_id,x.head_nurse_id,x.evaluate,x.diagnose,x.remark,x.registrars_id,x.registrars,x.qr_code,x.binding_state,x.patient_complains,x.present_history,x.past_history,x.temperature,x.pulse,x.respiratory,x.sbp,x.dbp,x.status,x.created_time,x.updated_time,x.nation,x.native_place,x.age,x.infectious_next_record_time,x.is_infectious,x.remind_cycle,x.response_result,x.is_open_remind,x.first_treatment_date,x.dialysis_age,x.expense_kind,x.tell_phone,x.contact_name,x.blood_patients,x.slow_patients,x.member_patients,x.ecommer_patients,x.blood_id,x.slow_id,x.member_id,x.member_fistdate,x.member_patienttype,x.member_treatement,x.equitment_id").Find(&patients).Error
  726. return patients, err
  727. }
  728. func GetMemberSearchPatient(keyword string, orgid int64) (patients []*models.XtPatientsNew, err error) {
  729. db := XTReadDB().Table("xt_patients_new as x").Where("x.status = 1 and x.member_patients = 1")
  730. if len(keyword) > 0 {
  731. likeKey := "%" + keyword + "%"
  732. db = db.Where("x.name LIKE ? OR x.dialysis_no LIKE ?", likeKey, likeKey)
  733. }
  734. if orgid > 0 {
  735. db = db.Where("x.user_org_id = ?", orgid)
  736. }
  737. err = db.Select("x.id,x.user_org_id,x.user_id,x.avatar,x.patient_type,x.dialysis_no,x.admission_number,x.source,x.lapseto,x.partition_id,x.bed_id,x.name,x.alias,x.gender,x.marital_status,x.id_card_no,x.birthday,x.reimbursement_way_id,x.health_care_type,x.health_care_no,x.health_care_due_date,x.height,x.blood_type,x.rh,x.health_care_due_alert_date,x.education_level,x.profession,x.phone,x.home_telephone,x.relative_phone,x.relative_relations,x.home_address,x.work_unit,x.unit_address,x.children,x.receiving_date,x.is_hospital_first_dialysis,x.first_dialysis_date,x.first_dialysis_hospital,x.predialysis_condition,x.pre_hospital_dialysis_frequency,x.pre_hospital_dialysis_times,x.hospital_first_dialysis_date,x.induction_period,x.initial_dialysis,x.total_dialysis,x.attending_doctor_id,x.head_nurse_id,x.evaluate,x.diagnose,x.remark,x.registrars_id,x.registrars,x.qr_code,x.binding_state,x.patient_complains,x.present_history,x.past_history,x.temperature,x.pulse,x.respiratory,x.sbp,x.dbp,x.status,x.created_time,x.updated_time,x.nation,x.native_place,x.age,x.infectious_next_record_time,x.is_infectious,x.remind_cycle,x.response_result,x.is_open_remind,x.first_treatment_date,x.dialysis_age,x.expense_kind,x.tell_phone,x.contact_name,x.blood_patients,x.slow_patients,x.member_patients,x.ecommer_patients,x.blood_id,x.slow_id,x.member_id,x.member_fistdate,x.member_patienttype,x.member_treatement,x.equitment_id").Find(&patients).Error
  738. return patients, err
  739. }
  740. func GetLongDialysisDetail(id int64) (models.XtDialysisSolution, error) {
  741. solution := models.XtDialysisSolution{}
  742. err := XTReadDB().Model(&solution).Where("id=? and status =1", id).Find(&solution).Error
  743. return solution, err
  744. }
  745. func GetPatientName(id int64) (models.Patients, error) {
  746. patientsNew := models.Patients{}
  747. err := XTReadDB().Model(&patientsNew).Where("id=? and status = 1", id).Find(&patientsNew).Error
  748. return patientsNew, err
  749. }
  750. func DeleteLongDialysis(id int64) error {
  751. err := XTWriteDB().Model(models.XtDialysisSolution{}).Where("id=?", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  752. return err
  753. }
  754. func GetDialysisrecord(patientID int64, start int64, end int64, limit int64, page int64, orgID int64, mode_id int64) ([]*models.PatientDialysisRecord, int64, error) {
  755. offset := (page - 1) * limit
  756. var total int64
  757. var err error
  758. var orders []*models.PatientDialysisRecord
  759. db := readDb.Table("xt_dialysis_order as do").
  760. Preload("DialysisPrescription", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  761. Preload("PredialysisEvaluation", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  762. Preload("DialysisPrescription", func(db *gorm.DB) *gorm.DB {
  763. return readDb.Where("patient_id=? and user_org_id=? and status=1", patientID, orgID).Preload("UserAdminRole", func(db *gorm.DB) *gorm.DB {
  764. return readUserDb.Where("status = 1")
  765. })
  766. }).
  767. Preload("AssessmentAfterDislysis", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  768. Preload("TreatmentSummary", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  769. Preload("Device", "org_id=? and status=1", orgID).
  770. Preload("UserAdminRole", func(db *gorm.DB) *gorm.DB {
  771. return readUserDb.Where("org_id=? and status = 1", orgID)
  772. }).
  773. Joins("JOIN xt_schedule as s ON s.patient_id=? and FROM_UNIXTIME(s.schedule_date, '%Y-%m-%d')=FROM_UNIXTIME(do.dialysis_date, '%Y-%m-%d')", patientID).
  774. Joins("JOIN xt_device_zone as dz ON dz.org_id = ? and dz.id=s.partition_id", orgID).
  775. Where("do.patient_id=? and do.user_org_id=? and do.stage = 2 and do.status=1", patientID, orgID).Group("s.schedule_date")
  776. if start > 0 {
  777. db = db.Where("do.dialysis_date>=?", start)
  778. }
  779. if end > 0 {
  780. db = db.Where("do.dialysis_date<=?", end)
  781. }
  782. if mode_id > 0 {
  783. db = db.Joins("JOIN xt_dialysis_prescription as dp ON dp.record_id=do.id")
  784. db = db.Where("dp.mode_id=?", mode_id)
  785. }
  786. err = db.Count(&total).Offset(offset).Limit(limit).Order("do.dialysis_date desc").Select("do.bed_id, do.id, do.dialysis_date, do.user_org_id, do.patient_id, do.prescription_id, do.stage, do.remark, do.status, do.created_time, do.updated_time,do.start_nurse,do.finish_nurse ,s.schedule_type, s.partition_id, dz.name as partition_name").Find(&orders).Error
  787. if len(orders) > 0 {
  788. ids := make([]int64, 0)
  789. for _, order := range orders {
  790. dialyzer := order.DialysisPrescription.Dialyzer
  791. ids = append(ids, dialyzer)
  792. }
  793. if len(ids) > 0 {
  794. var dialyzers []*models.DeviceNumber
  795. err = readDb.Model(&models.DeviceNumber{}).Where("id IN (?) and org_id=? and status=1", ids, orgID).Find(&dialyzers).Error
  796. if err != nil {
  797. return nil, 0, err
  798. }
  799. dialyzerMap := make(map[int64]models.DeviceNumber, 0)
  800. for _, item := range dialyzers {
  801. dialyzerMap[item.ID] = *item
  802. }
  803. for orderIndex, order := range orders {
  804. if _, exist := dialyzerMap[order.DialysisPrescription.Dialyzer]; exist {
  805. orders[orderIndex].DeviceNumber = dialyzerMap[order.DialysisPrescription.Dialyzer].Number
  806. }
  807. }
  808. }
  809. }
  810. return orders, total, err
  811. }
  812. func GetAllDoctorTwo(orgid int64, appid int64) (appRole []*models.SgjUserAdminRole, err error) {
  813. err = UserReadDB().Where("org_id = ? and app_id = ? and status = ? ", orgid, appid, 1).Find(&appRole).Error
  814. return appRole, err
  815. }
  816. func GetDoctorAdviceDetail(state int64, id int64, orgid int64) (advices []*models.DoctorAdvices, err error) {
  817. db := XTReadDB().Table("xt_doctor_advice as x")
  818. err = db.Order("x.id desc").Select("x.id,x.user_org_id,x.patient_id,x.advice_type,x.advice_date,x.start_time,x.advice_name,x.advice_desc,x.reminder_date,x.single_dose,x.single_dose_unit,x.prescribing_number,x.prescribing_number_unit,x.delivery_way,x.execution_frequency,x.advice_doctor,x.status,x.created_time,x.updated_time,x.advice_affirm,x.remark,x.stop_time,x.stop_reason,x.stop_doctor,x.stop_state,x.parent_id,x.execution_time,x.execution_staff,x.execution_state,x.checker,x.record_date,x.dialysis_order_id,x.check_time,x.check_state,x.drug_spec,x.drug_spec_unit,x.groupno,x.remind_type,x.frequency_type,x.day_count,x.week_day,x.template_id,x.modifier,s.name").Joins("left join xt_patients as s on s.id=x.patient_id").Where("x.start_time = ? and x.patient_id = ? and x.user_org_id = ? and x.status = 1", state, id, orgid).Scan(&advices).Error
  819. return advices, err
  820. }
  821. func DeleteManagement(groupid int64, orgid int64) error {
  822. err := XTWriteDB().Model(models.DoctorAdvice{}).Where("start_time =? and user_org_id = ?", groupid, orgid).Update(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix()}).Error
  823. return err
  824. }
  825. func DeleteChild(id int64) error {
  826. err := XTWriteDB().Model(models.DoctorAdvice{}).Where("id=?", id).Update(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix()}).Error
  827. return err
  828. }
  829. func DeletePatient(id int64) error {
  830. err = XTWriteDB().Model(models.DoctorAdvice{}).Where("id=? or parent_id = ?", id, id).Update(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix()}).Error
  831. return err
  832. }
  833. func GetNewPatient(id int64) (models.XtPatientsNew, error) {
  834. patientsNew := models.XtPatientsNew{}
  835. err := XTReadDB().Model(&patientsNew).Where("id=?", id).Find(&patientsNew).Error
  836. return patientsNew, err
  837. }
  838. func UpdatedPatient(patients models.Patients, id int64) error {
  839. err := XTWriteDB().Model(&patients).Where("id=?", id).Update(map[string]interface{}{"name": patients.Name, "gender": patients.Gender, "birthday": patients.Birthday, "age": patients.Age, "phone": patients.Phone, "lapseto": patients.Lapseto, "admission_number": patients.AdmissionNumber, "first_dialysis_date": patients.FirstDialysisDate, "diagnose": patients.Diagnose, "source": patients.Source, "dialysis_no": patients.DialysisNo, "avatar": patients.Avatar, "id_card_no": patients.IdCardNo, "is_infectious": patients.IsInfectious}).Error
  840. return err
  841. }
  842. func UpdateXtPatientNew(patients models.XtPatientsNew, id int64) error {
  843. err := XTWriteDB().Model(&patients).Where("id=?", id).Update(map[string]interface{}{"name": patients.Name, "gender": patients.Gender, "birthday": patients.Birthday, "age": patients.Age, "phone": patients.Phone, "lapseto": patients.Lapseto, "admission_number": patients.AdmissionNumber, "first_dialysis_date": patients.FirstDialysisDate, "diagnose": patients.Diagnose, "source": patients.Source, "dialysis_no": patients.DialysisNo, "avatar": patients.Avatar, "id_card_no": patients.IdCardNo, "is_infectious": patients.IsInfectious}).Error
  844. return err
  845. }
  846. func UpdateXtPatientNewOne(patients models.XtPatientsNew, id int64) error {
  847. err := XTWriteDB().Model(&patients).Where("id=?", id).Update(map[string]interface{}{"name": patients.Name, "gender": patients.Gender, "birthday": patients.Birthday, "age": patients.Age, "phone": patients.Phone, "lapseto": patients.Lapseto, "adminssion_number": patients.AdmissionNumber, "first_dialysis_date": patients.FirstDialysisDate, "diagnose": patients.Diagnose, "source": patients.Source, "dialysis_no": patients.DialysisNo, "avatar": patients.Avatar, "id_card_no": patients.IdCardNo, "is_infectious": patients.IsInfectious, "member_patienttype": patients.MemberPatienttype, "member_treatement": patients.MemberTreatement}).Error
  848. return err
  849. }
  850. func UpdateContagions(patientid int64) (models.XtPatientsInfectiousDiseases, error) {
  851. diseases := models.XtPatientsInfectiousDiseases{}
  852. err := XTWriteDB().Model(&diseases).Where("patient_id = ?", patientid).Update(map[string]interface{}{"status": 2, "updated_time": time.Now().Unix()}).Error
  853. return diseases, err
  854. }
  855. func UpdateCustomer(cdmpatient models.CdmPatients, id int64) error {
  856. err := PatientWriteDB().Model(&cdmpatient).Where("id=?", id).Update(map[string]interface{}{"name": cdmpatient.Name, "phone": cdmpatient.Phone, "gender": cdmpatient.Gender, "birthday": cdmpatient.Birthday, "diagnose": cdmpatient.Diagnose, "source": cdmpatient.Source, "avatar": cdmpatient.Avatar, "id_card_no": cdmpatient.IdCardNo, "updated_time": cdmpatient.UpdatedTime}).Error
  857. return err
  858. }
  859. func UpdatedCustomer(customer models.SgjUserCustomer, id int64) error {
  860. err := UserWriteDB().Model(&customer).Where("id = ?", id).Update(map[string]interface{}{"name": customer.Name, "mobile": customer.Mobile, "gender": customer.Gender, "birthday": customer.Birthday, "ill_date": customer.IllDate, "avatar": customer.Avatar, "illness_id": customer.IllnessId, "treat_type": customer.TreatType}).Error
  861. return err
  862. }
  863. func GetInspectionByOrgId(orgid int64) (*models.XtInspectionReference, error) {
  864. diseases := models.XtInspectionReference{}
  865. err := XTReadDB().Model(&diseases).Where("org_id = ? and status = 1", orgid).Find(&diseases).Error
  866. if err == gorm.ErrRecordNotFound {
  867. return nil, err
  868. }
  869. if err != nil {
  870. return nil, err
  871. }
  872. return &diseases, nil
  873. }
  874. func GetAllProjectName(date int64, orgid int64, patientid int64) (inspection []*models.XtInspection, err error) {
  875. err = XTReadDB().Model(&inspection).Where("inspect_date = ? and org_id = ? and patient_id = ? and status = 1", date, orgid, patientid).Order("inspect_date desc").Group("project_name").Find(&inspection).Error
  876. return inspection, err
  877. }
  878. func GetAllInspection(orgid int64) (inspection []*models.XtInspectionReference, err error) {
  879. err = XTReadDB().Model(&inspection).Where("org_id = ? and status = 1", orgid).Group("project_name").Find(&inspection).Error
  880. return inspection, err
  881. }
  882. func GetInspection(patientid int64, startime int64, endtime int64, limit int64, page int64, orgid int64, projectid int64) (inspection []*models.Inspection, total int64, err error) {
  883. db := XTReadDB().Table("xt_inspection as x").Where("x.status = 1")
  884. if patientid > 0 {
  885. db = db.Where("x.patient_id = ?", patientid)
  886. }
  887. if startime > 0 {
  888. db = db.Where("x.inspect_date >= ?", startime)
  889. }
  890. if endtime > 0 {
  891. db = db.Where("x.inspect_date<=?", endtime)
  892. }
  893. if orgid > 0 {
  894. db = db.Where("x.org_id = ?", orgid)
  895. }
  896. if projectid > 0 {
  897. db = db.Where("x.project_id = ?", projectid)
  898. }
  899. err = db.Count(&total).Order("x.inspect_date desc").Group("x.id").
  900. Select("x.id,x.patient_id,x.org_id,x.project_id,x.item_id,x.item_name,x.project_name,x.inspect_type,x.inspect_value,x.inspect_date,x.created_time").Find(&inspection).Error
  901. return inspection, total, err
  902. }
  903. func GetInspectionDetail(patientid int64, date int64, orgid int64, projectid int64) (inspection []*models.Inspections, err error) {
  904. db := XTReadDB().Table("xt_inspection as x").Where("x.status =1")
  905. table := XTReadDB().Table("xt_inspection_reference as r")
  906. fmt.Print("table", table)
  907. if patientid > 0 {
  908. db = db.Where("x.patient_id = ?", patientid)
  909. }
  910. if date > 0 {
  911. db = db.Where("x.inspect_date = ?", date)
  912. }
  913. if orgid > 0 {
  914. db = db.Where("x.org_id = ?", orgid)
  915. }
  916. if projectid > 0 {
  917. db = db.Where("x.project_id = ?", projectid)
  918. }
  919. err = db.Group("x.id").Select("x.id,x.patient_id,x.org_id,x.project_id,x.item_id,x.item_name,x.project_name,x.inspect_type,x.inspect_value,x.inspect_date,x.status,x.created_time,x.updated_time,r.range_type,r.range_min,r.range_max,r.range_value,r.range_options,r.unit").Joins("left join xt_inspection_reference as r on r.item_id = x.item_id ").Scan(&inspection).Error
  920. return inspection, err
  921. }
  922. func CreateFeedBack(feedback *models.XtPatientFeedback) error {
  923. err := XTWriteDB().Model(&feedback).Create(&feedback).Error
  924. return err
  925. }
  926. func DeleteInspection(date int64, orgid int64) error {
  927. err := XTWriteDB().Model(models.Inspection{}).Where("inspect_date = ? and org_id = ?", date, orgid).Update(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix()}).Error
  928. return err
  929. }
  930. func DeleteChildInspection(name string, date int64, orgid int64) error {
  931. err := XTWriteDB().Model(models.Inspection{}).Where("project_name = ? and inspect_date = ? and org_id = ?", name, date, orgid).Update(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix()}).Error
  932. return err
  933. }
  934. func SaveMessage(id int64, orgid int64, role *models.SgjUserAdminRole) error {
  935. err := UserWriteDB().Model(models.SgjUserAdminRole{}).Where("admin_user_id = ? and org_id = ?", id, orgid).Update(map[string]interface{}{"message": role.Message, "mtime": time.Now().Unix()}).Error
  936. return err
  937. }
  938. func SaveSex(id int64, role *models.SgjUserAdminRole) error {
  939. err := UserWriteDB().Model(models.SgjUserAdminRole{}).Where("admin_user_id = ?", id).Update(map[string]interface{}{"sex": role.Sex, "mtime": time.Now().Unix()}).Error
  940. return err
  941. }
  942. func SaveBirthday(id int64, role *models.SgjUserAdminRole) error {
  943. err := UserWriteDB().Model(models.SgjUserAdminRole{}).Where("admin_user_id = ?", id).Update(map[string]interface{}{"birthday": role.Birthday, "mtime": time.Now().Unix()}).Error
  944. return err
  945. }
  946. func GetPartitionByName(name string, orgid int64) (*models.DeviceZone, error) {
  947. zone := models.DeviceZone{}
  948. err := XTReadDB().Model(&zone).Where("name = ? and org_id = ? and status = 1", name, orgid).Find(&zone).Error
  949. if err == gorm.ErrRecordNotFound {
  950. return nil, err
  951. }
  952. if err != nil {
  953. return nil, err
  954. }
  955. return &zone, nil
  956. }
  957. func GetPartitionByNameOne(name string, orgid int64) (zone models.DeviceZone, err error) {
  958. err = XTReadDB().Model(&zone).Where("name = ? and org_id = ? and status = 1", name, orgid).First(&zone).Error
  959. return zone, err
  960. }
  961. func CreatedZone(zone *models.DeviceZone) error {
  962. err := XTWriteDB().Model(&zone).Create(&zone).Error
  963. return err
  964. }
  965. func GetAllZone(orgid int64, page int64, limit int64) (zone []*models.DeviceZones, total int64, err error) {
  966. db := XTReadDB().Table("xt_device_zone as x").Where("x.status =1")
  967. table := XTReadDB().Table("xt_device_number as n")
  968. fmt.Println("table", table)
  969. if orgid > 0 {
  970. db = db.Where("x.org_id = ?", orgid)
  971. }
  972. offset := (page - 1) * limit
  973. err = db.Group("x.id").Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Select("x.id,x.org_id,x.name,x.type,n.number").Joins("left join xt_device_number as n on n.zone_id = x.id AND n.status = 1").Scan(&zone).Error
  974. return zone, total, err
  975. }
  976. func DeleteZoneById(id int64) error {
  977. err := XTWriteDB().Model(models.DeviceZone{}).Where("id=?", id).Update(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix()}).Error
  978. return err
  979. }
  980. func GetGroupByName(name string, orgid int64) (*models.DeviceGroup, error) {
  981. group := models.DeviceGroup{}
  982. err := XTReadDB().Model(&group).Where("name = ? and org_id = ? and status = 1", name, orgid).Find(&group).Error
  983. if err == gorm.ErrRecordNotFound {
  984. return nil, err
  985. }
  986. if err != nil {
  987. return nil, err
  988. }
  989. return &group, nil
  990. }
  991. func GetGroupByNameOne(name string, orgid int64) (group models.DeviceGroup, err error) {
  992. err = XTReadDB().Model(&group).Where("name = ? and org_id = ? and status =1", name, orgid).First(&group).Error
  993. return group, err
  994. }
  995. func SaveGroup(group *models.DeviceGroup) error {
  996. err := XTWriteDB().Model(&group).Create(&group).Error
  997. return err
  998. }
  999. func GetAllGroup(orgid int64, page int64, limit int64) (group []*models.DeviceGroups, total int64, err error) {
  1000. db := XTReadDB().Table("xt_device_group as x").Where("x.status = 1")
  1001. table := XTReadDB().Table("xt_device_number as n")
  1002. fmt.Println("table", table)
  1003. if orgid > 0 {
  1004. db = db.Where("x.org_id = ?", orgid)
  1005. }
  1006. offset := (page - 1) * limit
  1007. err = db.Group("x.id").Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Select("x.id,x.org_id,x.name,x.status,x.ctime,x.mtime,n.number").Joins("left join xt_device_number as n on n.group_id = x.id And n.status = 1").Scan(&group).Error
  1008. return group, total, err
  1009. }
  1010. func DeleteGroup(id int64) error {
  1011. err := XTWriteDB().Model(models.DeviceGroup{}).Where("id=?", id).Update(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix()}).Error
  1012. return err
  1013. }
  1014. func GetAllZoneOne(orgid int64) (zone []*models.DeviceZone, err error) {
  1015. err = XTReadDB().Model(&zone).Where("org_id = ?", orgid).Find(&zone).Error
  1016. return zone, err
  1017. }
  1018. func GetAllGroupOne(orgid int64) (group []*models.DeviceGroup, err error) {
  1019. err = XTReadDB().Model(&group).Where("org_id =?", orgid).Find(&group).Error
  1020. return group, err
  1021. }
  1022. func GetBedByName(name string, orgid int64) (*models.DeviceNumber, error) {
  1023. number := models.DeviceNumber{}
  1024. err := XTReadDB().Model(&number).Where("number=? and org_id = ? and status =1", name, orgid).Find(&number).Error
  1025. if err == gorm.ErrRecordNotFound {
  1026. return nil, err
  1027. }
  1028. if err != nil {
  1029. return nil, err
  1030. }
  1031. return &number, nil
  1032. }
  1033. func GetBedByNameOne(name string, orgid int64) (number models.DeviceNumber, err error) {
  1034. err = XTReadDB().Model(&number).Where("number=? and org_id = ? and status = 1", name, orgid).First(&number).Error
  1035. return number, err
  1036. }
  1037. func CreateNumber(number *models.DeviceNumber) error {
  1038. err := XTWriteDB().Model(&number).Create(&number).Error
  1039. return err
  1040. }
  1041. func GetAllNumber(page int64, limit int64, orgid int64) (number []*models.DeviceNumber, total int64, err error) {
  1042. db := XTReadDB().Table("xt_device_number as x").Where("x.status = 1")
  1043. if orgid > 0 {
  1044. db = db.Where("x.org_id = ?", orgid)
  1045. }
  1046. offset := (page - 1) * limit
  1047. err = db.Count(&total).Offset(offset).Limit(limit).Order("x.ctime asc").Select("x.id,x.org_id,x.number,x.group_id,x.zone_id,x.status").Find(&number).Error
  1048. return number, total, err
  1049. }
  1050. func GetSchedu(bedid int64, thetime int64, orgid int64) (*models.XtSchedule, error) {
  1051. schedule := models.XtSchedule{}
  1052. err := XTReadDB().Model(&schedule).Where("bed_id = ? and schedule_date = ? and user_org_id = ? and status = 1", bedid, thetime, orgid).Find(&schedule).Error
  1053. if err == gorm.ErrRecordNotFound {
  1054. return nil, err
  1055. }
  1056. if err != nil {
  1057. return nil, err
  1058. }
  1059. return &schedule, nil
  1060. }
  1061. func DeleteBed(id int64) error {
  1062. err := XTWriteDB().Model(models.DeviceNumber{}).Where("id=?", id).Update(map[string]interface{}{"status": 0, "ctime": time.Now().Unix()}).Error
  1063. return err
  1064. }
  1065. func GetMemberPatient(orgid int64) (patients []*models.XtPatientsNew, err error) {
  1066. err = XTReadDB().Model(&patients).Where("user_org_id = ? AND member_patients = ? AND status = 1", orgid, 1).Find(&patients).Error
  1067. return patients, err
  1068. }
  1069. func GetSlowPatient(orgid int64) (patients []*models.XtPatientsNew, err error) {
  1070. err = XTReadDB().Model(&patients).Where("user_org_id = ? AND slow_patients = ? AND status = 1", orgid, 1).Find(&patients).Error
  1071. return patients, err
  1072. }
  1073. func GetBloodPatient(orgid int64) (patients []*models.XtPatientsNew, err error) {
  1074. err = XTReadDB().Model(&patients).Where("user_org_id = ? AND blood_patients = ? AND status = 1", orgid, 1).Find(&patients).Error
  1075. return patients, err
  1076. }
  1077. func SaveName(id int64, admin *models.SgjUserAdmin) error {
  1078. err = UserWriteDB().Model(models.SgjUserAdmin{}).Where("id=?", id).Update(map[string]interface{}{"name": admin.Name, "mtime": time.Now().Unix()}).Error
  1079. return err
  1080. }
  1081. func GetAdminUserId(id int64) (models.SgjUserAdminRole, error) {
  1082. role := models.SgjUserAdminRole{}
  1083. err := UserReadDB().Where("id=? and status =1", id).Find(&role).Error
  1084. return role, err
  1085. }
  1086. func GetMyOrganaziton(id int64) (org []*models.SgjUserOrg, err error) {
  1087. db := UserReadDB().Table("sgj_user_org as x").Where("x.status = 1")
  1088. table := UserReadDB().Table("sgj_user_admin as a")
  1089. fmt.Println("table", table)
  1090. err = db.Select("x.id,x.creator,x.org_name,x.org_short_name,x.org_logo,x.org_introduction,x.illness,x.province,x.city,x.district,x.address,x.longitude,x.latitude,x.org_type,x.comments,x.evaluate,x.gallery,x.business_week,x.business_time,x.contact_name,x.telephone,x.claim,x.operating_state,x.source,x.ttype,x.ttid,x.tuid,x.reason,x.level,x.website,x.import,x.sortno,a.is_super_admin").Joins("left join sgj_user_admin as a on a.id = x.creator").Where("x.creator = ?", id).Scan(&org).Error
  1091. return org, err
  1092. }
  1093. func GetOrgInformation(id int64) (models.SgjUserOrg, error) {
  1094. org := models.SgjUserOrg{}
  1095. err := UserReadDB().Where("id=? and status = 1", id).Find(&org).Error
  1096. return org, err
  1097. }
  1098. func GetShortName(orgtype int64) (models.SgjUserOrgType, error) {
  1099. orgType := models.SgjUserOrgType{}
  1100. err := UserReadDB().Model(&orgType).Where("id=? and status = 1", orgtype).Find(&orgType).Error
  1101. return orgType, err
  1102. }
  1103. func GetTypeName(id int64) (models.SgjUserOrgType, error) {
  1104. orgType := models.SgjUserOrgType{}
  1105. err := UserReadDB().Model(&orgType).Where("id=? and status = 1", id).Find(&orgType).Error
  1106. return orgType, err
  1107. }
  1108. func GetOrgProvince(id int64) (models.SgjUserDistrict, error) {
  1109. district := models.SgjUserDistrict{}
  1110. err := UserReadDB().Model(&district).Where("id=?", id).Find(&district).Error
  1111. return district, err
  1112. }
  1113. func GetAllOrgType() (orgType []*models.SgjUserOrgType, err error) {
  1114. err = UserReadDB().Model(&models.SgjUserOrgType{}).Where("status= ? AND id = ? OR id = ? OR id = ? OR id = ? OR id = ? OR id = ?", 1, 1, 7, 12, 16, 21, 26).Find(&orgType).Error
  1115. return
  1116. }
  1117. func UpdateOrg(id int64, org models.SgjUserOrg) error {
  1118. err := UserWriteDB().Model(&org).Where("id = ?", id).Update(map[string]interface{}{"org_name": org.OrgName, "org_short_name": org.OrgShortName, "province": org.Province, "city": org.City, "district": org.District, "address": org.Address, "org_type": org.OrgType, "telephone": org.Telephone, "contact_name": org.ContactName, "mtime": time.Now().Unix()}).Error
  1119. return err
  1120. }
  1121. func GetMyInfoName(id int64) (models.SgjUserAdminRole, error) {
  1122. role := models.SgjUserAdminRole{}
  1123. err := UserReadDB().Model(&role).Where("id=? and status = 1", id).Find(&role).Error
  1124. return role, err
  1125. }
  1126. func GetAdminName(id int64, orgid int64) (models.AdminUsers, error) {
  1127. user := models.AdminUsers{}
  1128. //err := UserReadDB().Model(&user).Where("id=? and status =1", id).Find(&user).Error
  1129. //return user, err
  1130. db := UserReadDB().Table("sgj_user_admin as x").Where("x.status = 1")
  1131. table := UserReadDB().Table("sgj_user_admin_role as r")
  1132. fmt.Println("table", table)
  1133. err := db.Group("x.id").Select("x.id,x.mobile,x.password,x.is_super_admin,x.source,x.name,r.admin_user_id,r.org_id,r.app_id,r.role_id,r.user_name,r.avatar,r.message,r.sex,r.birthday").Where("x.id = ?", id).Joins("left join sgj_user_admin_role as r on r.admin_user_id = x.id and r.org_id = ?", orgid).Scan(&user).Error
  1134. return user, err
  1135. }
  1136. func GetCreatByOrgId(orgid int64) (models.SgjUserOrg, error) {
  1137. org := models.SgjUserOrg{}
  1138. err := UserReadDB().Model(&org).Where("id=? and status = 1", orgid).Find(&org).Error
  1139. return org, err
  1140. }
  1141. func GetMyOrgInformationFour(creator int64, orgid int64) (models.SgjUserAdminRole, error) {
  1142. role := models.SgjUserAdminRole{}
  1143. err := UserReadDB().Model(&role).Where("admin_user_id = ? and org_id = ? and status = 1", creator, orgid).Find(&role).Error
  1144. return role, err
  1145. }
  1146. func GetPartionDetail(id int64) (models.DeviceZone, error) {
  1147. zone := models.DeviceZone{}
  1148. err := XTReadDB().Model(&zone).Where("id=? and status = 1", id).Find(&zone).Error
  1149. return zone, err
  1150. }
  1151. func UpdatePartition(id int64, zone *models.DeviceZone) error {
  1152. err := XTWriteDB().Model(&zone).Where("id=?", id).Update(map[string]interface{}{"name": zone.Name, "type": zone.Type, "mtime": time.Now().Unix()}).Error
  1153. return err
  1154. }
  1155. func GetGroupDetail(id int64) (models.DeviceGroup, error) {
  1156. group := models.DeviceGroup{}
  1157. err := XTReadDB().Model(&group).Where("id=? and status = 1", id).Find(&group).Error
  1158. return group, err
  1159. }
  1160. func UpdateGroup(id int64, group *models.DeviceGroup) error {
  1161. err := XTWriteDB().Model(&group).Where("id=?", id).Update(map[string]interface{}{"name": group.Name, "mtime": time.Now().Unix()}).Error
  1162. return err
  1163. }
  1164. func GetBedDetail(id int64) (models.DeviceNumber, error) {
  1165. number := models.DeviceNumber{}
  1166. err := XTReadDB().Model(&number).Where("id=? and status =1", id).Find(&number).Error
  1167. return number, err
  1168. }
  1169. func UpdatedBed(id int64, number *models.DeviceNumber) error {
  1170. err = XTWriteDB().Model(&number).Where("id=?", id).Update(map[string]interface{}{"number": number.Number, "zone_id": number.ZoneID, "group_id": number.GroupID, "mtime": time.Now().Unix()}).Error
  1171. return err
  1172. }
  1173. func CreateLoginLod(log *models.SgjUserAdminLoginLog) error {
  1174. err := UserWriteDB().Model(&log).Create(&log).Error
  1175. return err
  1176. }
  1177. func GetAppId(orgid int64) (models.OrgApp, error) {
  1178. app := models.OrgApp{}
  1179. err := UserReadDB().Model(&app).Where("org_id = ? and status = 1 and app_type = 3", orgid).Find(&app).Error
  1180. return app, err
  1181. }
  1182. func GetRoleName(adminid int64, orgid int64, appid int64) (models.Roles, error) {
  1183. role := models.Roles{}
  1184. err := UserReadDB().Model(&role).Where("creator = ? and org_id = ? and app_id = ? and status =1", adminid, orgid, appid).Find(&role).Error
  1185. return role, err
  1186. }
  1187. func GetRole(id int64) (models.Role, error) {
  1188. role := models.Role{}
  1189. err := UserReadDB().Model(&role).Where("id = ? and status =1", id).Find(&role).Error
  1190. return role, err
  1191. }
  1192. func GetPositionName(adminid int64, orgid int64, appid int64) (models.App_Role, error) {
  1193. role := models.App_Role{}
  1194. err := UserReadDB().Model(&role).Where("admin_user_id = ? and org_id = ? and app_id = ?", adminid, orgid, appid).Find(&role).Error
  1195. return role, err
  1196. }
  1197. func GetAdmin(id int64) (models.SgjUserAdmin, error) {
  1198. admin := models.SgjUserAdmin{}
  1199. err := UserReadDB().Model(&admin).Where("id=? and status =1", id).Find(&admin).Error
  1200. return admin, err
  1201. }
  1202. func GetPosition(id int64) (models.SgjUserAdminRole, error) {
  1203. roles := models.SgjUserAdminRole{}
  1204. err = UserReadDB().Model(&roles).Where("id=? and status = 1", id).Find(&roles).Error
  1205. return roles, err
  1206. }
  1207. func GetPositionOne(id int64) (models.SgjUserAdmin, error) {
  1208. admin := models.SgjUserAdmin{}
  1209. err = UserReadDB().Model(&admin).Where("id=? and status = 1", id).Find(&admin).Error
  1210. return admin, err
  1211. }
  1212. func GetMyOrgInformation(id int64, appid int64) (models.SgjUserAdminRole, error) {
  1213. admin := models.SgjUserAdminRole{}
  1214. err := UserReadDB().Model(&admin).Where("admin_user_id=? and app_id = ? and status = 1", id, appid).First(&admin).Error
  1215. return admin, err
  1216. }
  1217. func SaveNameTwo(id int64, orgid int64, role *models.SgjUserAdminRole) error {
  1218. err := UserWriteDB().Model(models.SgjUserAdminRole{}).Where("admin_user_id =? and org_id = ?", id, orgid).Update(map[string]interface{}{"user_name": role.UserName, "mtime": time.Now().Unix()}).Error
  1219. return err
  1220. }