manage_service.go 51KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  1. package service
  2. import (
  3. "XT_New/models"
  4. "fmt"
  5. "github.com/jinzhu/gorm"
  6. "strings"
  7. "time"
  8. )
  9. func GetEquitmentName(unit_type string, orgid int64) (*models.DeviceEquimentname, error) {
  10. var equimentname models.DeviceEquimentname
  11. var err error
  12. err = UserReadDB().Model(&equimentname).Where("equitment_name = ? and user_org_id = ? and status = 1", unit_type, orgid).Find(&equimentname).Error
  13. if err == gorm.ErrRecordNotFound {
  14. return nil, err
  15. }
  16. if err != nil {
  17. return nil, err
  18. }
  19. return &equimentname, nil
  20. }
  21. func CreateEquimentName(equimentname models.DeviceEquimentname) error {
  22. //err := XTWriteDB().Create(&equimentname).Error
  23. //return err
  24. err := writeUserDb.Create(&equimentname).Error
  25. return err
  26. }
  27. func GetEquitmentByBedID(bedid int64, orgid int64) (models.DeviceAddmacher, error) {
  28. addmacher := models.DeviceAddmacher{}
  29. err := UserReadDB().Model(&addmacher).Where("bed_id = ? and user_org_id = ? and status = 1", bedid, orgid).First(&addmacher).Error
  30. return addmacher, err
  31. }
  32. func CreateMacher(machers *models.DeviceAddmacher) error {
  33. //err := writeDb.Create(&machers).Error
  34. //xt := writeDb.Begin()
  35. // //fmt.Println("hhhhhhhh",xt)
  36. // //err := xt.Model(&models.DeviceAddmacher{}).Create(machers).Error
  37. // //fmt.Println("err",err)
  38. // //if err !=nil{
  39. // // xt.Rollback()
  40. // //}
  41. // //xt.Commit()
  42. // //return err
  43. err := writeUserDb.Create(&machers).Error
  44. return err
  45. }
  46. func UpdateMachine(id int64, orgid int64, addmacher *models.DeviceAddmacher) error {
  47. err := writeUserDb.Model(&addmacher).Where("id = ? AND user_org_id = ? AND status = ?", id, orgid, 1).Updates(map[string]interface{}{"serial_number": addmacher.SerialNumber, "device_type": addmacher.DeviceType, "bed_number": addmacher.BedNumber, "device_name": addmacher.DeviceName, "manufacture_factory": addmacher.ManufactureFactory, "service_manufacturer": addmacher.ServiceManufacturer, "unit_type": addmacher.UnitType, "use_section": addmacher.UseSection, "section_number": addmacher.SectionNumber, "buy_date": addmacher.BuyDate, "install_date": addmacher.InstallDate, "start_date": addmacher.StartDate, "maintenace_engineer": addmacher.MaintenaceEngineer, "telephone": addmacher.Telephone, "guarantee_date": addmacher.GuaranteeDate, "machine_status": addmacher.MachineStatus, "user_total": addmacher.UserTotal, "remarks": addmacher.Remarks, "rubbish_date": addmacher.RubbishDate, "rubbish_reason": addmacher.RubbishReason, "user_year": addmacher.UserYear, "work_time": addmacher.WorkTime, "bed_id": addmacher.BedId, "disinfection_mode": addmacher.DisinfectionMode, "revers_mode": addmacher.ReversMode, "mtime": time.Now().Unix()}).Error
  48. return err
  49. }
  50. func GetZoneName(zoneid int64, orgid int64) (models.DeviceZone, error) {
  51. zone := models.DeviceZone{}
  52. err := XTReadDB().Where("id = ? AND org_id = ?", zoneid, orgid).Find(&zone).Error
  53. return zone, err
  54. }
  55. func UpdateTreatMode(id int64, orgid int64, treatmodes []int64) (err error) {
  56. utx := XTWriteDB().Begin()
  57. err = utx.Model(models.DeviceTreatmentmode{}).Where("machine_id = ? AND user_org_id = ?", id, orgid).Updates(map[string]interface{}{"status": 2, "mtime": time.Now().Unix()}).Error
  58. fmt.Println("错误是什么", err)
  59. if err != nil {
  60. utx.Rollback()
  61. return
  62. }
  63. if len(treatmodes) > 0 {
  64. thisSQL := "INSERT INTO xt_device_treatmentmode(machine_id,treate_mode,status,ctime,user_org_id) VALUES "
  65. insertParams := make([]string, 0)
  66. insertData := make([]interface{}, 0)
  67. for _, treatmode := range treatmodes {
  68. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  69. insertData = append(insertData, id)
  70. insertData = append(insertData, treatmode)
  71. insertData = append(insertData, 1)
  72. insertData = append(insertData, time.Now().Unix())
  73. insertData = append(insertData, orgid)
  74. }
  75. thisSQL += strings.Join(insertParams, ",")
  76. err = utx.Exec(thisSQL, insertData...).Error
  77. if err != nil {
  78. utx.Rollback()
  79. return
  80. }
  81. }
  82. utx.Commit()
  83. return
  84. }
  85. func GetAllEquimentName(orgid int64) (equit []*models.DeviceEquimentname, err error) {
  86. err = readUserDb.Model(&equit).Where("user_org_id = ? AND status = ?", orgid, 1).Group("equitment_name").Find(&equit).Error
  87. return equit, err
  88. }
  89. func GetBed(equitid int64) (models.DeviceAddmacher, error) {
  90. addmacher := models.DeviceAddmacher{}
  91. err := UserReadDB().Where("id=? AND status = ?", equitid, 1).Find(&addmacher).Error
  92. return addmacher, err
  93. }
  94. func QueryPlan(orgid int64, devicetype int64, ids []int64, classids []int64, equitid int64) (*models.DevicePlan, error) {
  95. var pre models.DevicePlan
  96. var err error
  97. //for _, id := range ids {
  98. // for _, classids := range classids {
  99. // err = UserReadDB().Model(&pre).Where("user_org_id = ? AND device_type = ? AND time = ? AND classtime = ? AND equiment_id = ? AND status = 1", orgid, devicetype, id, classids, equitid).Find(&pre).Error
  100. // }
  101. //}
  102. //if err == gorm.ErrRecordNotFound {
  103. // return nil, err
  104. //}
  105. //if err != nil {
  106. // return nil, err
  107. //}
  108. //return &pre, nil
  109. if len(ids) == 1 && len(classids) == 1 {
  110. err = UserWriteDB().Model(&models.DevicePlan{}).Where("user_org_id = ? and device_type = ? and time = ? and classtime = ? and status = 1 ", orgid, devicetype, ids[0], classids[0]).Find(&pre).Error
  111. if err == gorm.ErrRecordNotFound {
  112. return nil, err
  113. }
  114. if err != nil {
  115. return nil, err
  116. }
  117. } else {
  118. err = UserWriteDB().Model(models.DevicePlan{}).Where("time IN(?) and user_org_id = ? and device_type = ? AND classtime IN(?) and status = 1", ids, orgid, devicetype, classids).Find(&pre).Error
  119. if err == gorm.ErrRecordNotFound {
  120. return nil, err
  121. }
  122. if err != nil {
  123. return nil, err
  124. }
  125. }
  126. return &pre, nil
  127. }
  128. func AddPlan(orgid int64, deviceType int64, disinfecTime string, ids []int64, classids []int64, way int64, machinedisinfectant int64, disinfectantway int64, disinfectant int64, equimentid int64, bedid int64) (err error) {
  129. xt := UserWriteDB().Begin()
  130. if len(ids) > 0 {
  131. thisSQL := "INSERT INTO xt_device_plan(device_type,disinfec_time,time,classtime,way,machine_disinfectant,disinfectan_way,disinfectant,status,ctime,user_org_id,equiment_id,bed_id) VALUES "
  132. insertParams := make([]string, 0)
  133. insertData := make([]interface{}, 0)
  134. for _, id := range ids {
  135. for _, classids := range classids {
  136. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?)")
  137. insertData = append(insertData, deviceType)
  138. insertData = append(insertData, disinfecTime)
  139. insertData = append(insertData, id)
  140. insertData = append(insertData, classids)
  141. insertData = append(insertData, way)
  142. insertData = append(insertData, machinedisinfectant)
  143. insertData = append(insertData, disinfectantway)
  144. insertData = append(insertData, disinfectant)
  145. insertData = append(insertData, 1)
  146. insertData = append(insertData, time.Now().Unix())
  147. insertData = append(insertData, orgid)
  148. insertData = append(insertData, equimentid)
  149. insertData = append(insertData, bedid)
  150. }
  151. }
  152. thisSQL += strings.Join(insertParams, ",")
  153. err = xt.Exec(thisSQL, insertData...).Error
  154. if err != nil {
  155. xt.Rollback()
  156. return
  157. }
  158. }
  159. xt.Commit()
  160. return
  161. }
  162. func GetAllPlan(orgid int64) (plan []*models.DevicePlans, err error) {
  163. //db := readUserDb.Table("xt_device_plan as p").Where("p.status = 1")
  164. // //table := readUserDb.Table("xt_device_addmacher as e")
  165. // //fmt.Println("table", table)
  166. // //if orgid > 0 {
  167. // // db = db.Where("p.user_org_id = ?", orgid)
  168. // //}
  169. // //err = db.Select("p.id,p.device_type,p.disinfec_time,p.time,p.classtime,p.way,p.machine_disinfectant,p.disinfectan_way,p.disinfectant,e.unit_type").Group("p.id").Joins("Left JOIN xt_device_addmacher as e On e.id = p.device_type").Order("e.unit_type asc,p.time").Scan(&plan).Error
  170. // //return plan, err
  171. err = readUserDb.Raw("select DISTINCT p.id,p.device_type,p.disinfec_time,p.time,p.classtime,p.way,p.machine_disinfectant,p.disinfectan_way,p.disinfectant,p.equiment_id,p.bed_id,e.device_mode from xt_device_plan as p left join xt_device_mode as e On e.id = p.device_type where p.user_org_id = ? and p.status = 1", orgid).Order("e.device_mode asc,p.time").Scan(&plan).Error
  172. return plan, err
  173. }
  174. func GetDeviceType(unitype string) (models.DeviceMode, error) {
  175. equimentname := models.DeviceMode{}
  176. err := UserReadDB().Where("id = ?", unitype).Find(&equimentname).Error
  177. return equimentname, err
  178. }
  179. func GetAllPlanDetail(id int64, orgid int64) (plan []*models.DevicePlans, err error) {
  180. //db := readUserDb.Table("xt_device_plan as p").Where("p.status = 1")
  181. //table := readUserDb.Table("xt_device_addmacher as e")
  182. //fmt.Println("table", table)
  183. //if orgid > 0 {
  184. // db = db.Where("p.user_org_id = ?", orgid)
  185. //}
  186. //if id > 0 {
  187. // db = db.Where("p.equiment_id = ?", id)
  188. //}
  189. //err = db.Select("p.id,p.device_type,p.disinfec_time,p.time,p.classtime,p.way,p.machine_disinfectant,p.disinfectan_way,p.disinfectant,e.unit_type").Group("p.id").Joins("Left JOIN xt_device_addmacher as e On e.id = p.equiment_id").Order("p.time asc").Scan(&plan).Error
  190. //return plan, err
  191. err = readUserDb.Raw("select DISTINCT p.id,p.device_type,p.disinfec_time,p.time,p.classtime,p.way,p.machine_disinfectant,p.disinfectan_way,p.disinfectant,p.equiment_id,p.bed_id,e.device_mode from xt_device_plan as p left join xt_device_mode as e On e.id = p.device_type where p.user_org_id = ? and p.device_type = ? and p.status = 1", orgid, id).Order("p.time asc,p.classtime asc").Scan(&plan).Error
  192. return plan, err
  193. }
  194. func DeletePlan(id int64) (err error) {
  195. err = UserWriteDB().Model(&models.DevicePlan{}).Where("id=?", id).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  196. return
  197. }
  198. func DeletePlans(orgid int64, ids []int64) (err error) {
  199. if len(ids) == 1 {
  200. err = UserWriteDB().Model(&models.DevicePlan{}).Where("id=? and user_org_id = ?", ids[0], orgid).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  201. } else {
  202. err = UserWriteDB().Model(models.DevicePlan{}).Where("id IN(?) and user_org_id = ?", ids, orgid).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  203. }
  204. return
  205. }
  206. func GetPlanDetailById(id int64, orgid int64) (models.DevicePlanss, error) {
  207. plans := models.DevicePlanss{}
  208. db := UserReadDB().Table("xt_device_plan as x").Where("x.status = 1")
  209. table := UserReadDB().Table("xt_device_addmacher as a").Where("a.status = 1")
  210. fmt.Println("table", table)
  211. if id > 0 {
  212. db = db.Where("x.id = ?", id)
  213. }
  214. if orgid > 0 {
  215. db = db.Where("x.user_org_id = ?", orgid)
  216. }
  217. err := db.Select("x.id,x.device_type,x.disinfec_time,x.time,x.classtime,x.way,x.machine_disinfectant,x.disinfectan_way,x.disinfectant,x.status,x.user_org_id,x.equiment_id,a.unit_type").Joins("Left Join xt_device_addmacher as a ON a.id = x.equiment_id").Scan(&plans).Error
  218. return plans, err
  219. }
  220. func UpdatePlanInfo(id int64, orgid int64, plan models.DevicePlan) error {
  221. err := writeUserDb.Model(&plan).Where("id = ? AND user_org_id = ?", id, orgid).Updates(map[string]interface{}{"device_type": plan.DeviceType, "disinfec_time": plan.DisinfecTime, "time": plan.Time, "classtime": plan.Classtime, "way": plan.Way, "machine_disinfectant": plan.MachineDisinfectant, "disinfectan_way": plan.MachineDisinfectant, "disinfectant": plan.Disinfectant, "mtime": time.Now().Unix()}).Error
  222. return err
  223. }
  224. func GetComprehensive(zone int64, number int64, devicetype int64, year int64, starttime int64, endtime int64, orgid int64) (macher []*models.DeviceAddmachers, err error) {
  225. db := readUserDb.Table("xt_device_addmacher as a").Where("a.status = 1")
  226. table := XTReadDB().Table("xt_device_zone as x")
  227. fmt.Println(table)
  228. if orgid > 0 {
  229. db = db.Where("a.user_org_id = ?", orgid)
  230. }
  231. if zone > 0 {
  232. db = db.Where("a.zone_id = ?", zone)
  233. }
  234. if number > 0 {
  235. db = db.Where("a.bed_id = ?", number)
  236. }
  237. if devicetype > 0 {
  238. db = db.Where("a.device_type = ?", devicetype)
  239. }
  240. if year == 1 {
  241. db = db.Where("a.user_year >= 0 AND a.user_year <= 1")
  242. }
  243. if year == 2 {
  244. db = db.Where("a.user_year >=1 AND a.user_year <= 3")
  245. }
  246. if year == 3 {
  247. db = db.Where("a.user_year >=3 AND a.user_year <= 10")
  248. }
  249. if year == 4 {
  250. db = db.Where("a.user_year >=10")
  251. }
  252. if starttime > 0 {
  253. db = db.Where("a.ctime >= ?", starttime)
  254. }
  255. if endtime > 0 {
  256. db = db.Where("a.ctime <= ?", endtime)
  257. }
  258. err = db.Select("a.id,a.serial_number,a.device_type,a.bed_number,a.device_name,a.manufacture_factory,a.service_manufacturer,a.unit_type,a.use_section,a.section_number,a.buy_date,a.install_date,a.start_date,a.maintenace_engineer,a.telephone,a.guarantee_date,a.machine_status,a.user_total,a.disinfection_mode,a.remarks,a.rubbish_date,a.rubbish_reason,a.user_year,a.work_time,a.revers_mode,a.user_org_id,a.status,a.ctime,a.mtime,a.zone_id,a.bed_id,x.name,m.device_mode").Joins("Left JOIN sgj_xt.xt_device_zone as x On x.id = a.zone_id").Joins("Left JOIN xt_device_mode as m on m.id = a.unit_type").Scan(&macher).Error
  259. return macher, err
  260. }
  261. func GetBedId(id int64) (models.DeviceAddmacher, error) {
  262. addmacher := models.DeviceAddmacher{}
  263. err := UserReadDB().Where("id = ? AND status = 1", id).Find(&addmacher).Error
  264. return addmacher, err
  265. }
  266. func GetPatientInfo(bedid int64, nowtime int64, orgid int64) (schedules []*models.Schedules, err error) {
  267. db := XTReadDB().Table("xt_schedule as s").Where("s.status = 1")
  268. table := XTReadDB().Table("xt_patients as x")
  269. fmt.Println("table", table)
  270. if bedid > 0 {
  271. db = db.Where("s.bed_id = ?", bedid)
  272. }
  273. if nowtime > 0 {
  274. db = db.Where("s.schedule_date = ?", nowtime)
  275. }
  276. if orgid > 0 {
  277. db = db.Where("s.user_org_id = ?", orgid)
  278. }
  279. err = db.Select("s.id,s.user_org_id,s.partition_id,s.bed_id,s.patient_id,s.schedule_date,s.schedule_type,s.schedule_week,s.mode_id,x.name").Joins("Left Join xt_patients as x On x.id = s.patient_id").Scan(&schedules).Error
  280. return schedules, err
  281. }
  282. func GetPatient(bedid int64, orgid int64, classid int64, nowtime int64) (schedules models.Schedules, err error) {
  283. db := XTReadDB().Table("xt_schedule as s").Where("s.status = 1")
  284. table := XTReadDB().Table("xt_patients as x")
  285. fmt.Println("table", table)
  286. err = db.Select("s.id,s.user_org_id,s.partition_id,s.bed_id,s.patient_id,s.schedule_date,s.schedule_type,s.schedule_week,s.mode_id,x.name,x.is_infectious").Joins("Left Join xt_patients as x On x.id = s.patient_id").Where("s.bed_id = ? AND s.user_org_id = ? AND s.schedule_type = ? AND s.schedule_date = ?", bedid, orgid, classid, nowtime).Scan(&schedules).Error
  287. return schedules, err
  288. }
  289. func GetPartitionName(id int64) (models.DeviceZone, error) {
  290. zone := models.DeviceZone{}
  291. err := XTReadDB().Where("id = ? AND status = 1", id).Find(&zone).Error
  292. return zone, err
  293. }
  294. func GetPatientNumber(id int64) (models.DeviceNumber, error) {
  295. number := models.DeviceNumber{}
  296. err := XTReadDB().Where("id = ? AND status = 1", id).Find(&number).Error
  297. return number, err
  298. }
  299. func GetDialysisOrder(time int64, patientid int64, orgid int64) (models.DialysisOrder, error) {
  300. order := models.DialysisOrder{}
  301. err := XTReadDB().Where("dialysis_date = ? AND patient_id = ? AND user_org_id = ? ", time, patientid, orgid).Find(&order).Error
  302. return order, err
  303. }
  304. func GetDialysisWay(time int64, patientid int64, orgid int64) (models.DialysisPrescription, error) {
  305. prescription := models.DialysisPrescription{}
  306. err := XTReadDB().Where("record_date = ? AND patient_id = ? AND user_org_id = ?", time, patientid, orgid).Find(&prescription).Error
  307. return prescription, err
  308. }
  309. func GetDialysisTime(time int64, patientid int64, orgid int64) (models.AssessmentAfterDislysis, error) {
  310. dislysis := models.AssessmentAfterDislysis{}
  311. err := XTReadDB().Where("assessment_date = ? AND patient_id = ? AND user_org_id = ?", time, patientid, orgid).Find(&dislysis).Error
  312. return dislysis, err
  313. }
  314. func GetAllOrganization(orgid int64, appid int64) (approle []*models.App_Role, err error) {
  315. err = UserReadDB().Where("org_id = ? AND app_id = ? AND status = 1", orgid, appid).Find(&approle).Error
  316. return approle, err
  317. }
  318. func CreateUserInformation(information *models.DeviceInformation) error {
  319. err := writeUserDb.Create(&information).Error
  320. return err
  321. }
  322. func GetRegisterInfo(orgid int64, id int64, timenow int64) (models.DeviceInformations, error) {
  323. //information := models.DeviceInformations{}
  324. var information models.DeviceInformations
  325. db := UserReadDB().Table("xt_device_information as x").Where("x.status = 1")
  326. table := XTReadDB().Table("xt_patients as p")
  327. fmt.Println("table", table)
  328. dbs := UserReadDB().Table("sgj_user_admin_role as r")
  329. fmt.Print("dbs", dbs)
  330. err = db.Select("x.id,x.date,x.class,x.zone,x.bed_number,x.patient_id,x.contagion,x.dialysis_mode,x.start_time,x.end_time,x.dialysis_hour,x.hyperfiltratio,x.weight_loss,x.warning_value,x.user_total,x.move,x.failure_stage,x.fault_description,x.code_information,x.disinfect_type,x.disinfectant_type,x.disinfection,x.machine_run,x.fluid_path,x.disinfectant,x.disinfection_status,x.disinfection_residue,x.long_time,x.disinfec_startime,x.disinfec_endtime,x.dialysis_checked,x.dialysis_name,x.norms,x.dialysis_concentration,x.germ_checked,x.germ_name,x.germ_number,x.clean,x.sign_name,x.equiment_id,p.name,r.user_name,r.admin_user_id").Joins("Left join sgj_xt.xt_patients as p On p.id = x.patient_id").Joins("Left Join sgj_user_admin_role as r on r.admin_user_id = x.sign_name").Where("x.user_org_id = ? AND x.equiment_id = ? AND x.date = ?", orgid, id, timenow).Scan(&information).Error
  331. return information, err
  332. }
  333. func GetSignName(id int64, orgid int64, appid int64) (models.App_Role, error) {
  334. role := models.App_Role{}
  335. err := UserReadDB().Model(&role).Where("admin_user_id = ? AND org_id = ? AND app_id = ?", id, orgid, appid).Find(&role).Error
  336. return role, err
  337. }
  338. func GetInformationData(limit int64, page int64, orgId int64) (information []*models.DeviceInformations, total int64, err error) {
  339. db := UserReadDB().Table("xt_device_information as x").Where("x.status = 1")
  340. table := XTReadDB().Table("xt_patients as p")
  341. fmt.Println("table", table)
  342. dbs := UserReadDB().Table("sgj_user_admin_role as r")
  343. fmt.Println("dbs", dbs)
  344. offset := (page - 1) * limit
  345. if orgId > 0 {
  346. db = db.Where("x.user_org_id = ?", orgId)
  347. }
  348. err = db.Group("x.id").Select("x.id,x.date,x.class,x.zone,x.bed_number,x.patient_id,x.contagion,x.dialysis_mode,x.start_time,x.end_time,x.dialysis_hour,x.hyperfiltratio,x.weight_loss,x.warning_value,x.user_total,x.move,x.failure_stage,x.fault_description,x.code_information,x.disinfect_type,x.disinfectant_type,x.disinfection,x.machine_run,x.fluid_path,x.disinfectant,x.disinfection_status,x.disinfection_residue,x.long_time,x.disinfec_startime,x.disinfec_endtime,x.dialysis_checked,x.dialysis_name,x.norms,x.dialysis_concentration,x.germ_checked,x.germ_name,x.germ_number,x.clean,x.sign_name,x.equiment_id,p.name,r.user_name").Count(&total).Joins("Left Join sgj_xt.xt_patients as p on p.id = x.patient_id").Joins("Left Join sgj_user_admin_role as r on r.admin_user_id = x.sign_name").Order("x.ctime desc").Offset(offset).Limit(limit).Scan(&information).Error
  349. if err != nil {
  350. return
  351. }
  352. return
  353. }
  354. func GetOrgName(orgid int64) (models.Org, error) {
  355. org := models.Org{}
  356. err := UserReadDB().Where("id= ? AND status = 1", orgid).Find(&org).Error
  357. return org, err
  358. }
  359. func CreateCulture(culture *models.DeviceCulture) error {
  360. err := writeUserDb.Create(&culture).Error
  361. return err
  362. }
  363. func CreateDialysate(dialysate *models.DeviceDialysate) error {
  364. err := writeUserDb.Create(&dialysate).Error
  365. return err
  366. }
  367. func CreateIon(ion *models.DeviceIon) error {
  368. err := writeUserDb.Create(&ion).Error
  369. return err
  370. }
  371. func GetAllCulture(orgId int64) (cultures []*models.DeviceCultures, err error) {
  372. db := UserReadDB().Table("xt_device_culture as x").Where("x.status = 1")
  373. table := UserReadDB().Table("sgj_user_admin_role as r")
  374. fmt.Println("table", table)
  375. if orgId > 0 {
  376. db = db.Where("x.user_org_id = ?", orgId)
  377. }
  378. err = db.Group("x.id").Select("x.id,x.speling_date,x.specimen,x.concentrate_noa,x.concentrate_nob,x.sampling_locationa,x.detection_unit,x.sampler,x.reporting_date,x.detection_result,x.bed_id,x.bed,r.user_name").Joins("Left Join sgj_user_admin_role as r on r.admin_user_id = x.sampler").Scan(&cultures).Error
  379. return cultures, err
  380. }
  381. func GetAllDialysate(orgId int64) (dialysate []*models.DeviceDialysates, err error) {
  382. db := UserReadDB().Table("xt_device_dialysate as x").Where("x.status = 1")
  383. table := UserReadDB().Table("sgj_user_admin_role as r")
  384. fmt.Println("table", table)
  385. if orgId > 0 {
  386. db = db.Where("x.user_org_id = ?", orgId)
  387. }
  388. err = db.Group("x.id").Select("x.id,x.sampling_date,x.specimenb,x.concentrate_noc,x.concentrateb_nod,x.sampling_locationb,x.detection_unit,x.samplerb,x.reporting_dateb,x.detection_resultb,x.bed_id,x.bed,r.user_name").Joins("Left Join sgj_user_admin_role as r on r.admin_user_id = x.samplerb").Scan(&dialysate).Error
  389. return dialysate, err
  390. }
  391. func GetAllDeviceIon(orgId int64) (ions []*models.DeviceIons, err error) {
  392. db := UserReadDB().Table("xt_device_ion as x").Where("x.status = 1")
  393. table := UserReadDB().Table("sgj_user_admin_role as r")
  394. fmt.Println("table", table)
  395. if orgId > 0 {
  396. db = db.Where("x.user_org_id = ?", orgId)
  397. }
  398. err = db.Group("x.id").Select("x.id,x.sampling_date,x.samplerc,x.detection_unit,x.concentrate_nof,x.concentrate_nog,x.date_reportc,x.actual_na,x.actual_pna,x.actual_k,x.actual_ca,x.actual_ci,x.actual_hco,x.actual_mg,x.actual_ph,x.remakes,x.bed_id,x.bed,r.user_name").Joins("Left Join sgj_user_admin_role as r on r.admin_user_id = x.samplerc").Scan(&ions).Error
  399. return ions, err
  400. }
  401. func GetCultureById(id int64) (models.DeviceCulture, error) {
  402. culture := models.DeviceCulture{}
  403. err := UserReadDB().Model(&culture).Where("id=? AND status = 1", id).Find(&culture).Error
  404. return culture, err
  405. }
  406. func GetSampler(id int64) (models.App_Role, error) {
  407. role := models.App_Role{}
  408. err := UserReadDB().Model(&role).Where("id= ? AND status =1", id).Find(&role).Error
  409. return role, err
  410. }
  411. func UpdateCulture(id int64, orgid int64, culture *models.DeviceCulture) error {
  412. err := writeUserDb.Model(&culture).Where("id= ? AND user_org_id = ? AND status = ?", id, orgid, 1).Updates(map[string]interface{}{"speling_date": culture.SpelingDate, "specimen": culture.Specimen, "concentrate_noa": culture.ConcentrateNoa, "concentrate_nob": culture.ConcentrateNob, "sampling_locationa": culture.SamplingLocationa, "detection_unit": culture.DetectionUnit, "sampler": culture.Sampler, "reporting_date": culture.ReportingDate, "detection_result": culture.DetectionResult, "mtime": time.Now().Unix()}).Error
  413. return err
  414. }
  415. func DeleteCultrues(id int64) (err error) {
  416. err = UserWriteDB().Model(models.DeviceCulture{}).Where("id=?", id).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  417. return
  418. }
  419. func DeleteCultrue(orgid int64, ids []int64) (err error) {
  420. if len(ids) == 1 {
  421. err = UserWriteDB().Model(&models.DeviceCulture{}).Where("id=? and user_org_id = ?", ids[0], orgid).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  422. } else {
  423. err = UserWriteDB().Model(models.DeviceCulture{}).Where("id IN(?) and user_org_id = ?", ids, orgid).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  424. }
  425. return
  426. }
  427. func GetDialystate(id int64) (models.DeviceDialysate, error) {
  428. dialysate := models.DeviceDialysate{}
  429. err := readUserDb.Where("id= ? AND status = 1", id).Find(&dialysate).Error
  430. return dialysate, err
  431. }
  432. func Updatedialystate(id int64, orgid int64, dialysate *models.DeviceDialysate) error {
  433. err := writeUserDb.Model(&dialysate).Where("id=? AND user_org_id = ? AND status = 1", id, orgid).Updates(map[string]interface{}{"sampling_date": dialysate.SamplingDate, "specimenb": dialysate.Specimenb, "concentrate_noc": dialysate.ConcentrateNoc, "concentrateb_nod": dialysate.ConcentratebNod, "sampling_locationb": dialysate.SamplingLocationb, "detection_unit": dialysate.DetectionUnit, "samplerb": dialysate.Samplerb, "reporting_dateb": dialysate.ReportingDateb, "detection_resultb": dialysate.DetectionResultb, "mtime": time.Now().Unix()}).Error
  434. return err
  435. }
  436. func DeleteDialystates(id int64) (err error) {
  437. err = UserWriteDB().Model(&models.DeviceDialysate{}).Where("id=?", id).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  438. return
  439. }
  440. func DeleteDialystate(orgid int64, ids []int64) (err error) {
  441. if len(ids) == 1 {
  442. err = UserWriteDB().Model(&models.DeviceDialysate{}).Where("id=? and user_org_id = ?", ids[0], orgid).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  443. } else {
  444. err = UserWriteDB().Model(models.DeviceDialysate{}).Where("id IN(?) and user_org_id = ?", ids, orgid).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  445. }
  446. return
  447. }
  448. func GetIon(id int64) (models.DeviceIon, error) {
  449. ion := models.DeviceIon{}
  450. err := UserReadDB().Model(&ion).Where("id = ? AND status = 1", id).Find(&ion).Error
  451. return ion, err
  452. }
  453. func UpdateIon(id int64, orgid int64, ion *models.DeviceIon) error {
  454. err := writeUserDb.Model(&ion).Where("id = ? AND user_org_id = ? AND status = 1", id, orgid).Updates(map[string]interface{}{"sampling_date": ion.SamplingDate, "samplerc": ion.Samplerc, "detection_unit": ion.DetectionUnit, "concentrate_nof": ion.ConcentrateNof, "concentrate_nog": ion.ConcentrateNog, "date_reportc": ion.DateReportc, "actual_na": ion.ActualNa, "actual_pna": ion.ActualPna, "actual_k": ion.ActualK, "actual_ca": ion.ActualCa, "actual_ci": ion.ActualCi, "actual_hco": ion.ActualCi, "actual_mg": ion.ActualMg, "actual_ph": ion.ActualPh, "remakes": ion.Remakes, "mtime": time.Now().Unix()}).Error
  455. return err
  456. }
  457. func DeleteIons(id int64) (err error) {
  458. err = UserWriteDB().Model(&models.DeviceIon{}).Where("id=?", id).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  459. return
  460. }
  461. func DeleteIon(orgid int64, ids []int64) (err error) {
  462. if len(ids) == 1 {
  463. err = UserWriteDB().Model(&models.DeviceIon{}).Where("id=? and user_org_id = ?", ids[0], orgid).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  464. } else {
  465. err = UserWriteDB().Model(models.DeviceIon{}).Where("id IN(?) and user_org_id = ?", ids, orgid).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  466. }
  467. return
  468. }
  469. func GetCultureData(bedid int64, start int64, end int64, orgId int64) (cultures []*models.DeviceCultures, err error) {
  470. db := UserReadDB().Table("xt_device_culture as x").Where("x.status = 1")
  471. table := UserReadDB().Table("sgj_user_admin_role as r")
  472. fmt.Println("table", table)
  473. if orgId > 0 {
  474. db = db.Where("x.user_org_id = ?", orgId)
  475. }
  476. if bedid > 0 {
  477. db = db.Where("x.bed_id = ?", bedid)
  478. }
  479. if start > 0 {
  480. db = db.Where("x.speling_date >= ?", start)
  481. }
  482. if end > 0 {
  483. db = db.Where("x.speling_date <= ?", end)
  484. }
  485. err = db.Group("x.id").Select("x.id,x.speling_date,x.specimen,x.concentrate_noa,x.concentrate_nob,x.sampling_locationa,x.detection_unit,x.sampler,x.reporting_date,x.detection_result,x.bed_id,x.bed,r.user_name").Joins("Left Join sgj_user_admin_role as r on r.admin_user_id = x.sampler").Scan(&cultures).Error
  486. return cultures, err
  487. }
  488. func GetDialysateData(bedid int64, start int64, end int64, orgId int64) (dialysate []*models.DeviceDialysates, err error) {
  489. db := UserReadDB().Table("xt_device_dialysate as x").Where("x.status = 1")
  490. table := UserReadDB().Table("sgj_user_admin_role as r")
  491. fmt.Println("table", table)
  492. if orgId > 0 {
  493. db = db.Where("x.user_org_id = ?", orgId)
  494. }
  495. if bedid > 0 {
  496. db = db.Where("x.bed_id = ?", bedid)
  497. }
  498. if start > 0 {
  499. db = db.Where("x.sampling_date >= ?", start)
  500. }
  501. if end > 0 {
  502. db = db.Where("x.sampling_date <= ?", end)
  503. }
  504. err = db.Group("x.id").Select("x.id,x.sampling_date,x.specimenb,x.concentrate_noc,x.concentrateb_nod,x.sampling_locationb,x.detection_unit,x.samplerb,x.reporting_dateb,x.detection_resultb,x.bed_id,x.bed,r.user_name").Joins("Left Join sgj_user_admin_role as r on r.admin_user_id = x.samplerb").Scan(&dialysate).Error
  505. return dialysate, err
  506. }
  507. func GetDeviceIonData(bedid int64, start int64, end int64, orgId int64) (ions []*models.DeviceIons, err error) {
  508. db := UserReadDB().Table("xt_device_ion as x").Where("x.status = 1")
  509. table := UserReadDB().Table("sgj_user_admin_role as r")
  510. fmt.Println("table", table)
  511. if orgId > 0 {
  512. db = db.Where("x.user_org_id = ?", orgId)
  513. }
  514. if bedid > 0 {
  515. db = db.Where("x.bed_id = ?", bedid)
  516. }
  517. if start > 0 {
  518. db = db.Where("x.sampling_date >= ?", start)
  519. }
  520. if end > 0 {
  521. db = db.Where("x.sampling_date <= ?", end)
  522. }
  523. err = db.Group("x.id").Select("x.id,x.sampling_date,x.samplerc,x.detection_unit,x.concentrate_nof,x.concentrate_nog,x.date_reportc,x.actual_na,x.actual_pna,x.actual_k,x.actual_ca,x.actual_ci,x.actual_hco,x.actual_mg,x.actual_ph,x.remakes,x.bed_id,x.bed,r.user_name").Joins("Left Join sgj_user_admin_role as r on r.admin_user_id = x.samplerc").Scan(&ions).Error
  524. return ions, err
  525. }
  526. func CreateRepair(repair *models.DeviceRepair) error {
  527. err = writeUserDb.Create(&repair).Error
  528. return err
  529. }
  530. func GetAllRepair(orgid int64, limit int64, page int64) (repair []*models.DeviceRepairs, total int64, err error) {
  531. db := UserReadDB().Table("xt_device_repair as r").Where("r.status = ?", 1)
  532. table := XTReadDB().Table("xt_device_addmacher as x")
  533. offset := (page - 1) * limit
  534. fmt.Print("table", table)
  535. if orgid > 0 {
  536. db = db.Where("r.user_org_id = ?", orgid)
  537. }
  538. err = db.Count(&total).Select("r.id,r.guarantee_date,r.start_time,r.arrive_time,r.finish_time,r.total_distance,r.failure_stage,r.fault_description,r.cause_analysis,r.treatment_process,r.images,r.exclude,r.reason,r.ctime,r.mtime,r.status,r.user_org_id,r.bed_id,r.equitment_id,r.code_information,x.bed_number").Joins("Left join xt_device_addmacher as x on x.id = r.equitment_id").Offset(offset).Limit(limit).Scan(&repair).Error
  539. // err = UserReadDB().Where("user_org_id = ? AND status = 1", orgid).Find(&repair).Error
  540. return repair, total, err
  541. }
  542. func EditRepair(id int64) (models.DeviceRepair, error) {
  543. repair := models.DeviceRepair{}
  544. err := UserReadDB().Where("id = ? AND status = 1", id).Find(&repair).Error
  545. return repair, err
  546. }
  547. func UpdateRepair(id int64, orgid int64, repair *models.DeviceRepair) error {
  548. err := writeUserDb.Model(&repair).Where("id=? AND user_org_id = ? AND status = 1", id, orgid).Updates(map[string]interface{}{"guarantee_date": repair.GuaranteeDate, "start_time": repair.StartTime, "arrive_time": repair.ArriveTime, "finish_time": repair.FinishTime, "total_distance": repair.TotalDistance, "failure_stage": repair.FailureStage, "fault_description": repair.FaultDescription, "cause_analysis": repair.CauseAnalysis, "treatment_process": repair.TreatmentProcess, "images": repair.Images, "exclude": repair.Exclude, "reason": repair.Reason, "code_information": repair.CodeInformation, "image_name": repair.ImageName, "mtime": time.Now().Unix()}).Error
  549. return err
  550. }
  551. func DeleteRepairs(id int64) (err error) {
  552. err = UserWriteDB().Model(models.DeviceRepair{}).Where("id=?", id).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  553. return
  554. }
  555. func DeleteRepair(orgid int64, ids []int64) (err error) {
  556. if len(ids) == 1 {
  557. err = UserWriteDB().Model(&models.DeviceRepair{}).Where("id=? and user_org_id = ?", ids[0], orgid).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  558. } else {
  559. err = UserWriteDB().Model(models.DeviceRepair{}).Where("id IN(?) and user_org_id = ?", ids, orgid).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  560. }
  561. return
  562. }
  563. func GetRepair(bedid int64, start int64, end int64, orgid int64) (repair []*models.DeviceRepairs, err error) {
  564. db := UserReadDB().Table("xt_device_repair as r").Where("r.status = 1")
  565. sb := UserReadDB().Table("xt_device_addmacher as x")
  566. fmt.Print("sb", sb)
  567. if orgid > 0 {
  568. db = db.Where("r.user_org_id = ?", orgid)
  569. }
  570. if bedid > 0 {
  571. db = db.Where("r.bed_id = ?", bedid)
  572. }
  573. if start > 0 {
  574. db = db.Where("r.guarantee_date >= ?", start)
  575. }
  576. if end > 0 {
  577. db = db.Where("r.guarantee_date <= ?", end)
  578. }
  579. //err = db.Model(&repair).Find(&repair).Error
  580. err = db.Select("r.id,r.guarantee_date,r.start_time,r.arrive_time,r.finish_time,r.total_distance,r.failure_stage,r.fault_description,r.cause_analysis,r.treatment_process,r.images,r.exclude,r.reason,r.ctime,r.mtime,r.status,r.user_org_id,r.bed_id,r.equitment_id,r.code_information,x.bed_number").Joins("Left join xt_device_addmacher as x on x.id = r.equitment_id").Scan(&repair).Error
  581. return repair, err
  582. }
  583. func GetUserInformation(id int64) (models.DeviceInformation, error) {
  584. information := models.DeviceInformation{}
  585. err := UserReadDB().Where("id = ? AND status = 1", id).Find(&information).Error
  586. return information, err
  587. }
  588. func GetPatientInfoMation(patieintid int64) (models.Patients, error) {
  589. patients := models.Patients{}
  590. err := XTReadDB().Where("id = ? AND status = 1", patieintid).Find(&patients).Error
  591. return patients, err
  592. }
  593. func DeleteUserLogin(id int64) (err error) {
  594. err = UserWriteDB().Model(&models.DeviceInformation{}).Where("id=?", id).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  595. return
  596. }
  597. func DeleteForm(orgid int64, ids []int64) (err error) {
  598. if len(ids) == 1 {
  599. err = UserWriteDB().Model(&models.DeviceInformation{}).Where("id=? and user_org_id = ?", ids[0], orgid).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  600. } else {
  601. err = UserWriteDB().Model(models.DeviceInformation{}).Where("id IN(?) and user_org_id = ?", ids, orgid).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  602. }
  603. return
  604. }
  605. func GetLastInformationdata(eid int64, startdate int64, orgid int64) (models.DeviceInformation, error) {
  606. information := models.DeviceInformation{}
  607. err := UserReadDB().Model(&information).Where("equiment_id = ? and date = ? and user_org_id = ? and status = 1", eid, startdate, orgid).Last(&information).Error
  608. return information, err
  609. }
  610. func UpdateAddMacherBedID(id int64, orgid int64, addmacher *models.DeviceAddmacher) error {
  611. err := UserWriteDB().Model(&addmacher).Where("id=? and user_org_id = ? and status =1", id, orgid).Update(map[string]interface{}{"bed_id": addmacher.BedId, "mtime": addmacher.Mtime}).Error
  612. return err
  613. }
  614. func UpdatedZoneID(bedid int64, orgid int64, number *models.DeviceNumber) error {
  615. err := XTWriteDB().Model(&number).Where("id=? and org_id = ?", bedid, orgid).Update(map[string]interface{}{"zone_id": number.ZoneID, "mtime": time.Now().Unix()}).Error
  616. return err
  617. }
  618. func UpadateForm(id int64, orgid int64, infor *models.DeviceInformation) error {
  619. err := UserWriteDB().Model(&infor).Where("id = ? AND user_org_id = ? AND status = 1", id, orgid).Updates(map[string]interface{}{"date": infor.Date, "class": infor.Class, "zone": infor.Zone, "bed_number": infor.BedNumber, "contagion": infor.Contagion, "dialysis_mode": infor.DialysisMode, "start_time": infor.StartTime, "end_time": infor.EndTime, "dialysis_hour": infor.DialysisHour, "hyperfiltratio": infor.Hyperfiltratio, "weight_loss": infor.WeightLoss, "warning_value": infor.WarningValue, "user_total": infor.UserTotal, "move": infor.Move, "failure_stage": infor.FailureStage, "fault_description": infor.FaultDescription, "code_information": infor.CodeInformation, "disinfect_type": infor.DisinfectType, "disinfectant_type": infor.DisinfectType, "disinfection": infor.Disinfection, "machine_run": infor.MachineRun, "fluid_path": infor.FluidPath, "disinfectant": infor.Disinfectant, "disinfection_status": infor.DisinfectionStatus, "disinfection_residue": infor.DisinfectionResidue, "long_time": infor.LongTime, "disinfec_startime": infor.DisinfecStartime, "disinfec_endtime": infor.DisinfecEndtime, "dialysis_checked": infor.DialysisChecked, "dialysis_name": infor.DialysisName, "norms": infor.Norms, "dialysis_concentration": infor.DialysisConcentration,
  620. "germ_checked": infor.GermChecked, "germ_name": infor.GermName, "germ_number": infor.GermNumber, "clean": infor.Clean, "sign_name": infor.SignName, "bed": infor.Bed, "patient_id": infor.PatientId, "mtime": time.Now().Unix()}).Error
  621. return err
  622. }
  623. func QueryInfo(zone int64, bed int64, start int64, end int64, orgId int64, limit int64, page int64) (infor []*models.DeviceInformations, total int64, err error) {
  624. db := UserReadDB().Table("xt_device_information as x").Where("x.status = 1")
  625. table := XTReadDB().Table("xt_patients as p")
  626. fmt.Println("table", table)
  627. dbs := UserReadDB().Table("sgj_user_admin_role as r")
  628. fmt.Println("dbs", dbs)
  629. if orgId > 0 {
  630. db = db.Where("x.user_org_id = ?", orgId)
  631. }
  632. if zone > 0 {
  633. db = db.Where("x.zone = ?", zone)
  634. }
  635. if bed > 0 {
  636. db = db.Where("x.bed_number = ?", bed)
  637. }
  638. if start > 0 {
  639. db = db.Where("x.date >= ?", start)
  640. }
  641. if end > 0 {
  642. db = db.Where("x.date <= ?", end)
  643. }
  644. offset := (page - 1) * limit
  645. err = db.Group("x.id").Select("x.id,x.date,x.class,x.zone,x.bed_number,x.patient_id,x.contagion,x.dialysis_mode,x.start_time,x.end_time,x.dialysis_hour,x.hyperfiltratio,x.weight_loss,x.warning_value,x.user_total,x.move,x.failure_stage,x.fault_description,x.code_information,x.disinfect_type,x.disinfectant_type,x.disinfection,x.machine_run,x.fluid_path,x.disinfectant,x.disinfection_status,x.disinfection_residue,x.long_time,x.disinfec_startime,x.disinfec_endtime,x.dialysis_checked,x.dialysis_name,x.norms,x.dialysis_concentration,x.germ_checked,x.germ_name,x.germ_number,x.clean,x.sign_name,x.equiment_id,x.bed,p.name,r.user_name").Count(&total).Joins("Left Join sgj_xt.xt_patients as p on p.id = x.patient_id").Joins("Left Join sgj_user_admin_role as r on r.admin_user_id = x.sign_name").Order("x.ctime desc").Offset(offset).Limit(limit).Scan(&infor).Error
  646. if err != nil {
  647. return
  648. }
  649. return
  650. }
  651. func GetPatientID(bedid int64, orgId int64, nowtime int64) (dialysis []*models.DialysisOrder, err error) {
  652. err = XTReadDB().Model(&dialysis).Where("bed_id = ? AND user_org_id = ? AND dialysis_date = ?", bedid, orgId, nowtime).Find(&dialysis).Error
  653. return dialysis, err
  654. }
  655. //func GetAllEquitType(orgId int64) (addmacher []*models.DeviceAddmacher, err error) {
  656. //
  657. // db := UserReadDB().Table("xt_device_addmacher as a").Where("a.status = 1")
  658. // if orgId > 0 {
  659. // db = db.Where("a.user_org_id = ?", orgId)
  660. // }
  661. // err = db.Group("a.unit_type").Select("a.id,a.serial_number,a.device_type,a.bed_number,a.device_name,a.manufacture_factory,a.service_manufacturer,a.unit_type,a.use_section,a.section_number,a.buy_date,a.install_date,a.start_date,a.maintenace_engineer,a.telephone,a.guarantee_date,a.machine_status,a.user_total,a.disinfection_mode,a.remarks,a.rubbish_date,a.rubbish_reason,a.user_year,a.work_time,a.revers_mode,a.user_org_id,a.status,a.ctime,a.mtime,a.zone_id,a.bed_id").Where("a.device_type <> 2 AND a.device_type <> 3").Scan(&addmacher).Error
  662. // return addmacher, err
  663. //}
  664. func GetAllEquitType(orgid int64) (equiment []*models.DeviceEquimentname, err error) {
  665. //err = UserReadDB().Raw("select equitment_name,id,user_org_id,equitment_id from xt_device_equimentname where user_org_id = ? GROUP BY equitment_name ",orgid).Scan(&equiment).Error
  666. //err = UserReadDB().Raw("SELECT e.id,e.equitment_name,e.user_org_id,e.equitment_id FROM xt_device_equimentname as e LEFT JOIN xt_device_addmacher as a ON a.id = e.equitment_id WHERE a.status = 1 AND a.device_type <> 2 AND a.device_type <> 3 AND e.user_org_id = ? GROUP BY equitment_name", orgid).Scan(&equiment).Error
  667. //err = UserReadDB().Where("user_org_id = ? and status =1", orgid).Find(&equiment).Error
  668. err = UserReadDB().Raw("select x.equitment_name,x.id,x.user_org_id,x.equitment_id,x.bed_id from xt_device_equimentname as x where x.user_org_id = ? and x.status = ? GROUP BY x.equitment_name", orgid, 1).Scan(&equiment).Error
  669. return equiment, err
  670. }
  671. func GetAllEquitName(orgid int64) (equiment []*models.DeviceEquimentname, err error) {
  672. err = UserReadDB().Raw("select id,equitment_name,status,ctime,mtime,user_org_id,equitment_id from xt_device_equimentname as e Where EXISTS (select id from xt_device_addmacher as a where a.id = e.equitment_id and a.status = 1) AND e.user_org_id = ? AND e.status = 1 ", orgid).Scan(&equiment).Error
  673. return equiment, err
  674. }
  675. func GetUserForm(id int64, orgId int64, limit int64, page int64) (infor []*models.DeviceInformations, total int64, err error) {
  676. db := UserReadDB().Table("xt_device_information as x").Where("x.status = 1")
  677. table := XTReadDB().Table("xt_patients as p")
  678. fmt.Println("table", table)
  679. dbs := UserReadDB().Table("sgj_user_admin_role as r")
  680. fmt.Println("dbs", dbs)
  681. offset := (page - 1) * limit
  682. if id > 0 {
  683. db = db.Where("x.equiment_id = ?", id)
  684. }
  685. if orgId > 0 {
  686. db = db.Where("x.user_org_id = ?", orgId)
  687. }
  688. err = db.Group("x.id").Select("x.id,x.date,x.class,x.zone,x.bed_number,x.patient_id,x.contagion,x.dialysis_mode,x.start_time,x.end_time,x.dialysis_hour,x.hyperfiltratio,x.weight_loss,x.warning_value,x.user_total,x.move,x.failure_stage,x.fault_description,x.code_information,x.disinfect_type,x.disinfectant_type,x.disinfection,x.machine_run,x.fluid_path,x.disinfectant,x.disinfection_status,x.disinfection_residue,x.long_time,x.disinfec_startime,x.disinfec_endtime,x.dialysis_checked,x.dialysis_name,x.norms,x.dialysis_concentration,x.germ_checked,x.germ_name,x.germ_number,x.clean,x.sign_name,x.equiment_id,x.bed,p.name,r.user_name").Count(&total).Joins("Left Join sgj_xt.xt_patients as p on p.id = x.patient_id").Joins("Left Join sgj_user_admin_role as r on r.admin_user_id = x.sign_name").Offset(offset).Limit(limit).Scan(&infor).Error
  689. if err != nil {
  690. return
  691. }
  692. return
  693. }
  694. func GetBedNumber(id int64, orgid int64) (models.DeviceAddmacher, error) {
  695. addmacher := models.DeviceAddmacher{}
  696. err := UserReadDB().Where("id= ? AND user_org_id = ?", id, orgid).Find(&addmacher).Error
  697. return addmacher, err
  698. }
  699. func GetNumbers(id int64, orgid int64) (models.DeviceNumber, error) {
  700. number := models.DeviceNumber{}
  701. err := XTReadDB().Where("id = ? AND org_id = ?", id, orgid).Find(&number).Error
  702. return number, err
  703. }
  704. func GetZoneForm(id int64) (models.DeviceAddmacher, error) {
  705. macher := models.DeviceAddmacher{}
  706. err := UserReadDB().Where("id=? AND status = 1", id).Find(&macher).Error
  707. return macher, err
  708. }
  709. func GetBedForm(id int64) (number []*models.DeviceNumber, err error) {
  710. err = XTReadDB().Where("zone_id = ? AND status = 1", id).Find(&number).Error
  711. return number, err
  712. }
  713. func GetTotalCount(orgid int64) (repair []*models.DeviceRepair, total int64, err error) {
  714. err = UserReadDB().Model(&repair).Where("user_org_id = ? and status = 1", orgid).Find(&repair).Count(&total).Error
  715. return repair, total, err
  716. }
  717. func GetTotalNumber(id int64, orgid int64) (total int64, err error) {
  718. var device models.DeviceInformation
  719. err = UserReadDB().Model(&device).Where("equiment_id = ? AND user_org_id = ? AND status = 1", id, orgid).Find(&device).Count(&total).Error
  720. return total, err
  721. }
  722. func GetTimeWarning(equitid int64, orgId int64) (models.DeviceInformation, error) {
  723. //fmt.Print("equitid是什么码字--------------------------------------------------------------------",equitid)
  724. information := models.DeviceInformation{}
  725. err = UserReadDB().Where(" user_org_id = ? AND status = 1 AND equiment_id = ? AND (dialysis_checked = 1 or dialysis_checked = 0)", orgId, equitid).Last(&information).Error
  726. return information, err
  727. }
  728. func GetTimeLast(equitid int64, orgId int64) (models.DeviceInformation, error) {
  729. information := models.DeviceInformation{}
  730. err := UserReadDB().Model(&information).Where("equiment_id = ? AND user_org_id = ? AND status = 1 AND (germ_checked = 1 or germ_checked = 0)", equitid, orgId).Last(&information).Error
  731. return information, err
  732. }
  733. func GetTimeLastData(equitid int64, orgId int64) (models.DeviceInformation, error) {
  734. information := models.DeviceInformation{}
  735. err := UserReadDB().Model(&information).Where("equiment_id = ? AND user_org_id = ? AND status = 1 AND (clean = 1 or clean = 0)", equitid, orgId).Last(&information).Error
  736. return information, err
  737. }
  738. func GetAllpatient(orgid int64) (patients []*models.Patients, err error) {
  739. err = XTReadDB().Where("user_org_id = ?", orgid).Find(&patients).Error
  740. return patients, err
  741. }
  742. //func GetInformation(id int64, orgid int64) (*models.DeviceInformation, error) {
  743. //// var information models.DeviceInformation
  744. //// var err error
  745. //// err = UserReadDB().Model(&information).Where("id = ? and user_org_id = ? and status = ?", id, orgid, 1).Find(&information).Error
  746. //// fmt.Print("", err)
  747. //// if err == gorm.ErrRecordNotFound {
  748. //// return nil, err
  749. //// }
  750. //// if err != nil {
  751. //// return nil, err
  752. //// }
  753. //// return &information, nil
  754. ////}
  755. func GetInformation(eid int64, startdate int64, orgid int64) (*models.DeviceInformation, error) {
  756. var information models.DeviceInformation
  757. var err error
  758. err = UserReadDB().Model(&information).Where("equiment_id = ? and user_org_id = ? and status = ? and date = ? ", eid, orgid, 1, startdate).Find(&information).Error
  759. fmt.Print("", err)
  760. if err == gorm.ErrRecordNotFound {
  761. return nil, err
  762. }
  763. if err != nil {
  764. return nil, err
  765. }
  766. return &information, nil
  767. }
  768. func ChangeBed(id int64, org int64) (number []*models.DeviceNumber, err error) {
  769. err = XTReadDB().Model(&number).Where("zone_id = ? AND status = 1 AND org_id = ? ", id, org).Find(&number).Error
  770. return number, err
  771. }
  772. func GetPatientDetail(id int64, orgid int64) (models.Patients, error) {
  773. patients := models.Patients{}
  774. err := XTReadDB().Where("id=? and user_org_id = ?", id, orgid).Find(&patients).Error
  775. return patients, err
  776. }
  777. func GetRemanderData(orgid int64) (remander models.DeviceRemander, err error) {
  778. err = UserReadDB().Model(&models.DeviceRemander{}).Where("user_org_id = ? and status = 1", orgid).Find(&remander).Error
  779. return
  780. }
  781. func CreateRemander(remander *models.DeviceRemander) (err error) {
  782. err = UserWriteDB().Create(&remander).Error
  783. return
  784. }
  785. func UpdateRemander(remander *models.DeviceRemander) (err error) {
  786. err = UserWriteDB().Save(&remander).Error
  787. return
  788. }
  789. func GetMode(mode string, orgid int64) (*models.DeviceMode, error) {
  790. var information models.DeviceMode
  791. var err error
  792. err = UserReadDB().Model(&information).Where("device_mode = ? and user_org_id = ? and status = ?", mode, orgid, 1).Find(&information).Error
  793. fmt.Print("", err)
  794. if err == gorm.ErrRecordNotFound {
  795. return nil, err
  796. }
  797. if err != nil {
  798. return nil, err
  799. }
  800. return &information, nil
  801. }
  802. func SaveMode(mode *models.DeviceMode) (err error) {
  803. err = UserWriteDB().Save(&mode).Error
  804. return
  805. }
  806. func GetAllMode(orgid int64) (mode []*models.DeviceMode, err error) {
  807. err = UserReadDB().Where("user_org_id = ? and status = ?", orgid, 1).Find(&mode).Error
  808. return mode, err
  809. }
  810. func GetModeById(id int64) (models.DeviceMode, error) {
  811. mode := models.DeviceMode{}
  812. err := UserReadDB().Where("id = ?", id).Find(&mode).Error
  813. return mode, err
  814. }
  815. func UpdateMode(id int64, mode models.DeviceMode) error {
  816. err := UserWriteDB().Model(&mode).Where("id=?", id).Update(map[string]interface{}{"device_mode": mode.DeviceMode, "mtime": time.Now().Unix()}).Error
  817. return err
  818. }
  819. func QueryDeviceMode(id int64, orgid int64) (*models.DeviceAddmacher, error) {
  820. addmacher := models.DeviceAddmacher{}
  821. var err error
  822. err = UserReadDB().Model(&addmacher).Where("unit_type = ? and user_org_id = ?", id, orgid).Find(&addmacher).Error
  823. if err == gorm.ErrRecordNotFound {
  824. return nil, err
  825. }
  826. if err != nil {
  827. return nil, err
  828. }
  829. return &addmacher, nil
  830. }
  831. func DeleteMode(id int64) error {
  832. err := UserWriteDB().Model(models.DeviceMode{}).Where("id=?", id).Update(map[string]interface{}{"status": 0}).Error
  833. return err
  834. }
  835. func SelectChange(id int64, orgid int64) (number []*models.DeviceNumber, err error) {
  836. //err = XTReadDB().Where("zone_id = ? and org_id = ? and status = ?", id, orgid, 1).Find(&number).Error
  837. db := XTReadDB().Table("xt_device_number as x").Where("x.status = ?", 1)
  838. err = db.Select("org_id,number,group_id,zone_id as id").Where("zone_id = ? and org_id = ?", id, orgid).Find(&number).Error
  839. return number, err
  840. }
  841. func SelectBed(id int64, orgid int64) (models.DeviceNumber, error) {
  842. number := models.DeviceNumber{}
  843. err := XTReadDB().Where("id = ? and org_id = ?", id, orgid).Find(&number).Error
  844. return number, err
  845. }
  846. func GetTimeData(equitid int64, orgid int64, timenow int64) (information []*models.DeviceInformation, err error) {
  847. //db := UserReadDB().Table("xt_device_information as x").Where("x.status = 1")
  848. //err = db.Raw("select id,date,class,zone,bed_number,patient_id,contagion,dialysis_mode,start_time,end_time,dialysis_hour,hyperfiltratio,weight_loss,warning_value,user_total,move,failure_stage,fault_description,code_information,disinfect_type,disinfectant_type,disinfection,machine_run,fluid_path,disinfectant,disinfection_status,disinfection_residue,long_time,disinfec_startime,disinfec_endtime,dialysis_checked,dialysis_name,norms,dialysis_concentration,germ_checked,germ_name,germ_number,clean,sign_name,status,ctime,mtime,user_org_id,equiment_id,bed,stime from xt_device_information where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= ? and equiment_id = ? and user_org_id = ?",timenow,equitid, orgid).Error
  849. day := time.Now().Day()
  850. oldMonth := day - 30
  851. t := time.Date(time.Now().Year(), time.Now().Month(), oldMonth, time.Now().Hour(), time.Now().Minute(), time.Now().Second(), time.Now().Nanosecond(), time.Local)
  852. err = UserReadDB().Model(&information).Where("date>= ? and date <= ? and equiment_id = ? and user_org_id = ? and dialysis_checked = 1 ", t.Unix(), timenow, equitid, orgid).Find(&information).Error
  853. return information, err
  854. }
  855. func GetTimeTwo(equitid int64, orgid int64, timenow int64) (information []*models.DeviceInformation, err error) {
  856. //db := UserReadDB().Table("xt_device_information as x").Where("x.status = 1")
  857. //err = db.Raw("select id,date,class,zone,bed_number,patient_id,contagion,dialysis_mode,start_time,end_time,dialysis_hour,hyperfiltratio,weight_loss,warning_value,user_total,move,failure_stage,fault_description,code_information,disinfect_type,disinfectant_type,disinfection,machine_run,fluid_path,disinfectant,disinfection_status,disinfection_residue,long_time,disinfec_startime,disinfec_endtime,dialysis_checked,dialysis_name,norms,dialysis_concentration,germ_checked,germ_name,germ_number,clean,sign_name,status,ctime,mtime,user_org_id,equiment_id,bed,stime from xt_device_information where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= ? and equiment_id = ? and user_org_id = ?",timenow,equitid, orgid).Error
  858. day := time.Now().Day()
  859. oldMonth := day - 30
  860. t := time.Date(time.Now().Year(), time.Now().Month(), oldMonth, time.Now().Hour(), time.Now().Minute(), time.Now().Second(), time.Now().Nanosecond(), time.Local)
  861. err = UserReadDB().Model(&information).Where("date >= ? and date<= ? and equiment_id = ? and user_org_id = ? and germ_checked = 1 ", t.Unix(), timenow, equitid, orgid).Find(&information).Error
  862. return information, err
  863. }
  864. func GetTimeThree(equitid int64, orgid int64, timenow int64) (information []*models.DeviceInformation, err error) {
  865. //db := UserReadDB().Table("xt_device_information as x").Where("x.status = 1")
  866. //err = db.Raw("select id,date,class,zone,bed_number,patient_id,contagion,dialysis_mode,start_time,end_time,dialysis_hour,hyperfiltratio,weight_loss,warning_value,user_total,move,failure_stage,fault_description,code_information,disinfect_type,disinfectant_type,disinfection,machine_run,fluid_path,disinfectant,disinfection_status,disinfection_residue,long_time,disinfec_startime,disinfec_endtime,dialysis_checked,dialysis_name,norms,dialysis_concentration,germ_checked,germ_name,germ_number,clean,sign_name,status,ctime,mtime,user_org_id,equiment_id,bed,stime from xt_device_information where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= ? and equiment_id = ? and user_org_id = ?",timenow,equitid, orgid).Error
  867. day := time.Now().Day()
  868. oldMonth := day - 30
  869. t := time.Date(time.Now().Year(), time.Now().Month(), oldMonth, time.Now().Hour(), time.Now().Minute(), time.Now().Second(), time.Now().Nanosecond(), time.Local)
  870. err = UserReadDB().Model(&information).Where("date >= ? and date <= ? and equiment_id = ? and user_org_id = ? and clean = 1 ", t.Unix(), timenow, equitid, orgid).Find(&information).Error
  871. return information, err
  872. }
  873. func DeleteImages(id int64) error {
  874. repair := models.DeviceRepair{}
  875. err := UserWriteDB().Model(&repair).Where("id=?", id).Update(map[string]interface{}{"images": "", "image_name": ""}).Error
  876. return err
  877. }