device_service.go 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. package service
  2. import (
  3. "XT_New/models"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/jinzhu/gorm"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. //////// 设备基本信息
  12. func GetValidDevicesBy(orgID int64, deviceType int, zoneID int64) ([]*models.Device, error) {
  13. var devices []*models.Device
  14. db := readDb.Model(&models.Device{}).Where("org_id = ? AND status = 1", orgID)
  15. if deviceType != 0 {
  16. db = db.Where("device_type = ?", deviceType)
  17. }
  18. if zoneID != 0 {
  19. db = db.Where("device_zone_id = ?", zoneID)
  20. }
  21. err := db.Find(&devices).Error
  22. if err != nil {
  23. return nil, err
  24. }
  25. return devices, nil
  26. }
  27. // 根据设备关联表的 id,获取设备关联信息
  28. func GetDeviceByID(orgID int64, deviceID int64) (*models.Device, error) {
  29. var device models.Device
  30. err := readDb.Model(&models.Device{}).Where("org_id = ? AND id = ?", orgID, deviceID).First(&device).Error
  31. if err != nil {
  32. if err == gorm.ErrRecordNotFound {
  33. return nil, nil
  34. } else {
  35. return nil, err
  36. }
  37. }
  38. return &device, nil
  39. }
  40. func GetDMDeviceByID(orgID int64, id int64) (*models.DeviceDM, error) {
  41. var device models.DeviceDM
  42. err := readDb.Model(&models.DeviceDM{}).Where("org_id = ? AND id = ?", orgID, id).First(&device).Error
  43. if err != nil {
  44. if err == gorm.ErrRecordNotFound {
  45. return nil, nil
  46. } else {
  47. return nil, err
  48. }
  49. }
  50. return &device, nil
  51. }
  52. func GetWTEDeviceByID(orgID int64, id int64) (*models.DeviceWTE, error) {
  53. var device models.DeviceWTE
  54. err := readDb.Model(&models.DeviceWTE{}).Where("org_id = ? AND id = ?", orgID, id).First(&device).Error
  55. if err != nil {
  56. if err == gorm.ErrRecordNotFound {
  57. return nil, nil
  58. } else {
  59. return nil, err
  60. }
  61. }
  62. return &device, nil
  63. }
  64. // type DeviceBaseInfoViewModel struct {
  65. // DeviceType int `json:"device_type"`
  66. // DeviceNumberID int64 `gorm:"column:device_number_id" json:"device_number_id"`
  67. // ID int64 `gorm:"column:id" json:"id"`
  68. // SerialNumber string `json:"serial_number"` // 序列号
  69. // Name string `json:"name"` // 设备名
  70. // Manufacturer string `json:"manufacturer"` // 生产厂家
  71. // RepairFactory string `json:"repair_factory"` // 维修厂家
  72. // Model string `json:"model"` // 型号
  73. // Department string `json:"department"` // 使用科室
  74. // DepartmentNumber string `json:"department_number"` // 科室编号
  75. // PurchaseDate int64 `json:"purchase_date"` // 购买日期
  76. // InstallDate int64 `json:"install_date"` // 安装日期
  77. // CommissioningDate int64 `json:"commissioning_date"` // 启用日期
  78. // Maintainer string `json:"maintainer"` // 维修工程师
  79. // MaintenanceCall string `json:"maintenance_call"` // 维修联系电话
  80. // WarrantyPeriod string `json:"warranty_period"` // 保修期限
  81. // DeviceStatus int `json:"device_status"` // 机器状态 1.使用机 2.备用机 3.急诊机 4.报废机
  82. // InitialUseTimes int `json:"initial_use_times"` // 初始使用次数
  83. // Mark string `json:"mark"` // 备注
  84. // RetirementDate int64 `json:"retirement_date"` // 报废日期
  85. // RetirementReason string `json:"retirement_reason"` // 报废原因
  86. // ServiceLife int `json:"service_life"` // 使用年限
  87. // WorkingTime int `json:"working_time"` // 工作时长
  88. // TreatmentMode string `json:"treatment_mode"` // 治疗模式 DeviceType = 1
  89. // ReverseOsmosisMode int `json:"reverse_osmosis_mode"` // 反渗模式 DeviceType = 2
  90. // }
  91. // // TODO
  92. // func GetDeviceBaseInfoByDeviceID(deviceID int64) (*DeviceBaseInfoViewModel, error) {
  93. // return nil, nil
  94. // }
  95. // 创建透析机
  96. func CreateDMDevice(dmDevice *models.DeviceDM, deviceNumber *models.DeviceNumber) (*models.Device, error) {
  97. now := time.Now().Unix()
  98. dmDevice.Status = 1
  99. dmDevice.CreateTime = now
  100. dmDevice.ModifyTime = now
  101. tx := writeDb.Begin()
  102. createDMErr := tx.Model(&models.DeviceDM{}).Create(dmDevice).Error
  103. if createDMErr != nil {
  104. tx.Rollback()
  105. return nil, createDMErr
  106. }
  107. device := models.Device{
  108. OrgID: dmDevice.OrgID,
  109. DeviceType: 1,
  110. DMID: dmDevice.ID,
  111. SerialNumber: dmDevice.SerialNumber,
  112. Name: dmDevice.Name,
  113. Model: dmDevice.Model,
  114. Status: 1,
  115. CreateTime: now,
  116. ModifyTime: now,
  117. }
  118. if deviceNumber != nil {
  119. device.DeviceNumber = deviceNumber.Number
  120. device.DeviceNumberID = deviceNumber.ID
  121. device.DeviceZoneID = deviceNumber.ZoneID
  122. device.DeviceGroupID = deviceNumber.GroupID
  123. }
  124. createDeviceErr := tx.Model(&models.Device{}).Create(&device).Error
  125. if createDeviceErr != nil {
  126. tx.Rollback()
  127. return nil, createDeviceErr
  128. }
  129. tx.Commit()
  130. return &device, nil
  131. }
  132. // 创建水处理机
  133. func CreateWTEDevice(wteDevice *models.DeviceWTE, deviceNumber *models.DeviceNumber) (*models.Device, error) {
  134. now := time.Now().Unix()
  135. wteDevice.Status = 1
  136. wteDevice.CreateTime = now
  137. wteDevice.ModifyTime = now
  138. tx := writeDb.Begin()
  139. createWTEErr := tx.Model(&models.DeviceWTE{}).Create(wteDevice).Error
  140. if createWTEErr != nil {
  141. tx.Rollback()
  142. return nil, createWTEErr
  143. }
  144. device := models.Device{
  145. OrgID: wteDevice.OrgID,
  146. DeviceType: 2,
  147. WTEID: wteDevice.ID,
  148. SerialNumber: wteDevice.SerialNumber,
  149. Name: wteDevice.Name,
  150. Model: wteDevice.Model,
  151. Status: 1,
  152. CreateTime: now,
  153. ModifyTime: now,
  154. }
  155. if deviceNumber != nil {
  156. device.DeviceNumber = deviceNumber.Number
  157. device.DeviceNumberID = deviceNumber.ID
  158. device.DeviceZoneID = deviceNumber.ZoneID
  159. device.DeviceGroupID = deviceNumber.GroupID
  160. }
  161. createDeviceErr := tx.Model(&models.Device{}).Create(&device).Error
  162. if createDeviceErr != nil {
  163. tx.Rollback()
  164. return nil, createDeviceErr
  165. }
  166. tx.Commit()
  167. return &device, nil
  168. }
  169. func UpdateDMDevice(dmDevice *models.DeviceDM, deviceNumber *models.DeviceNumber, device *models.Device) error {
  170. now := time.Now().Unix()
  171. tx := writeDb.Begin()
  172. dmDevice.ModifyTime = now
  173. updateDMErr := tx.Save(dmDevice).Error
  174. if updateDMErr != nil {
  175. tx.Rollback()
  176. return updateDMErr
  177. }
  178. device.SerialNumber = dmDevice.SerialNumber
  179. device.Name = dmDevice.Name
  180. device.Model = dmDevice.Model
  181. if deviceNumber != nil {
  182. device.DeviceNumberID = deviceNumber.ID
  183. device.DeviceNumber = deviceNumber.Number
  184. device.DeviceZoneID = deviceNumber.ZoneID
  185. device.DeviceGroupID = deviceNumber.GroupID
  186. } else {
  187. device.DeviceNumberID = 0
  188. device.DeviceNumber = ""
  189. device.DeviceZoneID = 0
  190. device.DeviceGroupID = 0
  191. }
  192. device.ModifyTime = now
  193. updateDeviceErr := tx.Save(device).Error
  194. if updateDeviceErr != nil {
  195. tx.Rollback()
  196. return updateDeviceErr
  197. }
  198. tx.Commit()
  199. return nil
  200. }
  201. func UpdateWTEDevice(wteDevice *models.DeviceWTE, deviceNumber *models.DeviceNumber, device *models.Device) error {
  202. now := time.Now().Unix()
  203. tx := writeDb.Begin()
  204. wteDevice.ModifyTime = now
  205. updateWTEErr := tx.Save(wteDevice).Error
  206. if updateWTEErr != nil {
  207. tx.Rollback()
  208. return updateWTEErr
  209. }
  210. device.SerialNumber = wteDevice.SerialNumber
  211. device.Name = wteDevice.Name
  212. device.Model = wteDevice.Model
  213. device.DeviceNumberID = deviceNumber.ID
  214. device.DeviceNumber = deviceNumber.Number
  215. device.ModifyTime = now
  216. updateDeviceErr := tx.Save(device).Error
  217. if updateDeviceErr != nil {
  218. tx.Rollback()
  219. return updateDeviceErr
  220. }
  221. tx.Commit()
  222. return nil
  223. }
  224. func UpdateDevice(device *models.Device) error {
  225. err := writeDb.Save(device).Error
  226. return err
  227. }
  228. func GetDeviceCountForDeviceNumberID(orgID int64, deviceNumberID int64) (int, error) {
  229. var count int
  230. err := readDb.Model(&models.Device{}).Where("device_number_id = ? AND org_id = ? AND status = 1", deviceNumberID, orgID).Count(&count).Error
  231. if err != nil {
  232. return 0, err
  233. }
  234. return count, nil
  235. }
  236. //////// 分区 Zone
  237. func GetAllValidDeviceZones(orgID int64) ([]*models.DeviceZone, error) {
  238. var zones []*models.DeviceZone
  239. err := readDb.Model(&models.DeviceZone{}).Where("org_id = ?", orgID).Find(&zones).Error
  240. if err != nil {
  241. return nil, err
  242. }
  243. return zones, nil
  244. }
  245. func GetAllValidDeviceZonesOne(orgID int64) ([]*models.DeviceZone, error) {
  246. var zones []*models.DeviceZone
  247. err := readDb.Model(&models.DeviceZone{}).Where("org_id = ? and status = 1", orgID).Find(&zones).Error
  248. if err != nil {
  249. return nil, err
  250. }
  251. return zones, nil
  252. }
  253. func GetDeviceZoneByID(orgID int64, zoneID int64) (*models.DeviceZone, error) {
  254. var zone models.DeviceZone
  255. err := readDb.Model(&models.DeviceZone{}).Where("org_id = ? AND id = ?", orgID, zoneID).First(&zone).Error
  256. if err != nil {
  257. if err == gorm.ErrRecordNotFound {
  258. return nil, nil
  259. } else {
  260. return nil, err
  261. }
  262. }
  263. return &zone, nil
  264. }
  265. func GetZoneByName(name string, orgid int64) (*models.DeviceZone, error) {
  266. zones := models.DeviceZone{}
  267. err := XTReadDB().Model(&zones).Where("name = ? and org_id = ? and status = 1", name, orgid).Find(&zones).Error
  268. if err == gorm.ErrRecordNotFound {
  269. return nil, err
  270. }
  271. if err != nil {
  272. return nil, err
  273. }
  274. return &zones, nil
  275. }
  276. func GetZoneByNameOne(name string, orgid int64) (zone models.DeviceZone, err error) {
  277. err = XTReadDB().Model(&zone).Where("name = ? and org_id=? and status = 1", name, orgid).First(&zone).Error
  278. return zone, err
  279. }
  280. func CreateDeviceZone(orgID int64, name string, type_ int) (*models.DeviceZone, error) {
  281. now := time.Now().Unix()
  282. zone := models.DeviceZone{
  283. OrgID: orgID,
  284. Name: name,
  285. Type: type_,
  286. Status: 1,
  287. CreateTime: now,
  288. ModifyTime: now,
  289. }
  290. err := writeDb.Model(&models.DeviceZone{}).Create(&zone).Error
  291. if err != nil {
  292. return nil, err
  293. }
  294. return &zone, nil
  295. }
  296. func UpdateDeviceZone(zone *models.DeviceZone) error {
  297. err := writeDb.Save(zone).Error
  298. return err
  299. }
  300. //////// 分组 Group
  301. func GetAllValidDeviceGroups(orgID int64) ([]*models.DeviceGroup, error) {
  302. var groups []*models.DeviceGroup
  303. err := readDb.Model(&models.DeviceGroup{}).Where("org_id = ? AND status = 1", orgID).Find(&groups).Error
  304. if err != nil {
  305. return nil, err
  306. }
  307. return groups, nil
  308. }
  309. func GetDeviceGroupByID(orgID int64, groupID int64) (*models.DeviceGroup, error) {
  310. var group models.DeviceGroup
  311. err := readDb.Model(&models.DeviceGroup{}).Where("org_id = ? AND id = ?", orgID, groupID).First(&group).Error
  312. if err != nil {
  313. if err == gorm.ErrRecordNotFound {
  314. return nil, nil
  315. } else {
  316. return nil, err
  317. }
  318. }
  319. return &group, nil
  320. }
  321. func GetDeviceGroupName(name string, orgid int64) (*models.DeviceGroup, error) {
  322. group := models.DeviceGroup{}
  323. err := XTReadDB().Model(&group).Where("name = ? and org_id = ? and status =1", name, orgid).Find(&group).Error
  324. if err == gorm.ErrRecordNotFound {
  325. return nil, err
  326. }
  327. if err != nil {
  328. return nil, err
  329. }
  330. return &group, nil
  331. }
  332. func CreateDeviceGroup(orgID int64, name string) (*models.DeviceGroup, error) {
  333. now := time.Now().Unix()
  334. group := models.DeviceGroup{
  335. OrgID: orgID,
  336. Name: name,
  337. Status: 1,
  338. CreateTime: now,
  339. ModifyTime: now,
  340. }
  341. err := writeDb.Model(&models.DeviceGroup{}).Create(&group).Error
  342. if err != nil {
  343. return nil, err
  344. }
  345. return &group, nil
  346. }
  347. func GetUpdateDeviceGroupByName(name string, orgid int64) (group models.DeviceGroup, err error) {
  348. err = XTReadDB().Model(&group).Where("name = ? and org_id = ? and status =1", name, orgid).Find(&group).Error
  349. return group, err
  350. }
  351. func UpdateDeviceGroup(group *models.DeviceGroup) error {
  352. err := writeDb.Save(group).Error
  353. return err
  354. }
  355. //////// 机号 Number
  356. type DeviceNumberViewModel struct {
  357. models.DeviceNumber
  358. ZoneName string `gorm:"column:zone_name" json:"zone_name"`
  359. GroupName string `gorm:"column:group_name" json:"group_name"`
  360. }
  361. func GetAllValidDeviceNumbers(orgID int64) ([]*DeviceNumberViewModel, error) {
  362. var vms []*DeviceNumberViewModel = make([]*DeviceNumberViewModel, 0)
  363. rows, err := readDb.Raw("SELECT n.*, z.name as zone_name, g.name as group_name FROM xt_device_number as n join xt_device_zone as z on z.id = n.zone_id AND z.status = 1 join xt_device_group as g on g.id = n.group_id WHERE (n.org_id = ? AND n.status = 1) order by sort ", orgID).Rows()
  364. defer rows.Close()
  365. if err != nil {
  366. return nil, err
  367. }
  368. for rows.Next() {
  369. var vm DeviceNumberViewModel
  370. readDb.ScanRows(rows, &vm)
  371. vms = append(vms, &vm)
  372. }
  373. return vms, nil
  374. }
  375. func GetAllBedNumber(orgId int64) (devicenumber []*models.XtDeviceNumber, err error) {
  376. err = XTReadDB().Raw("select id as bed_id,org_id,number as bed_number,group_id,zone_id from xt_device_number as n Where NOT EXISTS (select id,bed_id from sgj_users.xt_device_addmacher as a where a.bed_id = n.id and a.status = 1) AND n.org_id = ? AND n.status = 1 ", orgId).Scan(&devicenumber).Error
  377. return devicenumber, err
  378. }
  379. func GetAllBedNumberTwo(orgId int64, id int64) (devicenumber []*models.XtDeviceNumber, err error) {
  380. err = XTReadDB().Raw("select id as bed_id,org_id,number as bed_number,group_id,zone_id from xt_device_number as n Where NOT EXISTS (select id,bed_id from sgj_users.xt_device_addmacher as a where a.bed_id = n.id and a.status = 1 and a.id <> ?) AND n.org_id = ? AND n.status = 1 ", id, orgId).Scan(&devicenumber).Error
  381. return devicenumber, err
  382. }
  383. func GetAllMachineByOrgId(orgid int64) (addmacher []*models.DeviceAddmacher, err error) {
  384. err = UserReadDB().Where("user_org_id = ? AND status = 1", orgid).Find(&addmacher).Error
  385. return addmacher, err
  386. }
  387. func GetAllDeviceNumbers(orgID int64, record_date int64, schedule_type int64) ([]*DeviceNumberViewModel, error) {
  388. var vms []*DeviceNumberViewModel = make([]*DeviceNumberViewModel, 0)
  389. rows, err := readDb.Raw("SELECT n.*, z.name as zone_name, g.name as group_name FROM xt_device_number as n join xt_device_zone as z on z.id = n.zone_id join xt_device_group as g on g.id = n.group_id WHERE (n.org_id = ? AND n.status = 1) AND NOT EXISTS (Select * FROM xt_schedule as s, `xt_dialysis_order` as d Where d.`dialysis_date` = ? AND d.`status` = 1 AND d.`patient_id` = s.`patient_id` AND s.user_org_id = n.org_id AND s.`bed_id` = n.id AND s.`schedule_date` = ? AND s.`schedule_type` = ? AND s.status = 1 )", orgID, record_date, record_date, schedule_type).Rows()
  390. defer rows.Close()
  391. if err != nil {
  392. return nil, err
  393. }
  394. for rows.Next() {
  395. var vm DeviceNumberViewModel
  396. readDb.ScanRows(rows, &vm)
  397. vms = append(vms, &vm)
  398. }
  399. return vms, nil
  400. }
  401. func GetDeviceNumberByID(orgID int64, numberID int64) (*models.DeviceNumber, error) {
  402. var number models.DeviceNumber
  403. err := readDb.Model(&models.DeviceNumber{}).Where("org_id = ? AND id = ? ", orgID, numberID).First(&number).Error
  404. if err != nil {
  405. if err == gorm.ErrRecordNotFound {
  406. return nil, nil
  407. } else {
  408. return nil, err
  409. }
  410. }
  411. return &number, nil
  412. }
  413. func GetCreateDeviceNumber(num string, orgid int64) (*models.DeviceNumber, error) {
  414. number := models.DeviceNumber{}
  415. err := XTReadDB().Model(&number).Where("number = ? and org_id = ? and status = 1", num, orgid).Find(&number).Error
  416. if err == gorm.ErrRecordNotFound {
  417. return nil, err
  418. }
  419. if err != nil {
  420. return nil, err
  421. }
  422. return &number, nil
  423. }
  424. func CreateDeviceNumber(orgID int64, number string, zoneID int64, groupID int64, sort int64) (*models.DeviceNumber, error) {
  425. now := time.Now().Unix()
  426. numberModel := models.DeviceNumber{
  427. OrgID: orgID,
  428. Number: number,
  429. ZoneID: zoneID,
  430. GroupID: groupID,
  431. Status: 1,
  432. CreateTime: now,
  433. ModifyTime: now,
  434. Sort: sort,
  435. }
  436. err := writeDb.Model(&models.DeviceNumber{}).Create(&numberModel).Error
  437. if err != nil {
  438. return nil, err
  439. }
  440. return &numberModel, nil
  441. }
  442. func GetDeviceNumberByName(num string, zoneId int64, orgid int64) (number models.DeviceNumber, err error) {
  443. err = XTReadDB().Model(&number).Where("number = ? and zone_id = ? and org_id = ? and status =1", num, zoneId, orgid).First(&number).Error
  444. return number, err
  445. }
  446. func UpdateDeviceNumber(number *models.DeviceNumber) error {
  447. now := time.Now().Unix()
  448. number.ModifyTime = now
  449. tx := writeDb.Begin()
  450. updateNumberErr := tx.Save(number).Error
  451. if updateNumberErr != nil {
  452. tx.Rollback()
  453. return updateNumberErr
  454. }
  455. updateDeviceErr := tx.Model(&models.Device{}).Where("org_id = ? AND device_number_id = ?", number.OrgID, number.ID).Updates(map[string]interface{}{"device_number": number.Number}).Error
  456. if updateDeviceErr != nil {
  457. tx.Rollback()
  458. return updateDeviceErr
  459. }
  460. tx.Commit()
  461. return nil
  462. }
  463. func GetAddMacherByBedId(bedid int64, user_org_id int64) (*models.DeviceAddmacher, error) {
  464. addmacher := models.DeviceAddmacher{}
  465. err := UserReadDB().Where("bed_id = ? and status = 1 and user_org_id = ?", bedid, user_org_id).Find(&addmacher).Error
  466. if err != nil {
  467. if err == gorm.ErrRecordNotFound {
  468. return nil, nil
  469. } else {
  470. return nil, err
  471. }
  472. }
  473. return &addmacher, nil
  474. }
  475. func UpdateAddmacherByBedId(bedId int64, addmacher models.DeviceAddmacher) error {
  476. tx := UserWriteDB().Begin()
  477. err := tx.Model(&addmacher).Where("bed_id = ? and status = 1", bedId).Updates(map[string]interface{}{"zone_id": addmacher.ZoneId}).Error
  478. if err != nil {
  479. tx.Rollback()
  480. return err
  481. }
  482. tx.Commit()
  483. return nil
  484. }
  485. func GetDeviceNumberCountForZoneID(orgID int64, zoneID int64) (int, error) {
  486. var count int
  487. err := readDb.Model(&models.DeviceNumber{}).Where("zone_id = ? AND org_id = ? AND status = 1", zoneID, orgID).Count(&count).Error
  488. if err != nil {
  489. return 0, err
  490. }
  491. return count, nil
  492. }
  493. func GetDeviceNumberCountForGroupID(orgID int64, groupID int64) (int, error) {
  494. var count int
  495. err := readDb.Model(&models.DeviceNumber{}).Where("group_id = ? AND org_id = ? AND status = 1", groupID, orgID).Count(&count).Error
  496. if err != nil {
  497. return 0, err
  498. }
  499. return count, nil
  500. }
  501. func GetScheduleCountForDeviceNumber(orgID int64, deviceNumberID int64, beginDate int64) (int, error) {
  502. var count int
  503. err := readDb.Model(&models.Schedule{}).Where("bed_id = ? AND user_org_id = ? AND schedule_date >= ? AND status = 1", deviceNumberID, orgID, beginDate).Count(&count).Error
  504. if err != nil {
  505. return 0, err
  506. }
  507. return count, nil
  508. }
  509. func GetScheduleTemplateItemCountForDeviceNumber(orgID int64, deviceNumberID int64) (int, error) {
  510. var count int
  511. err := readDb.Model(&models.PatientScheduleTemplateItem{}).Where("device_number_id = ? AND org_id = ? AND status = 1", deviceNumberID, orgID).Count(&count).Error
  512. if err != nil {
  513. return 0, err
  514. }
  515. return count, nil
  516. }
  517. func GetZoneId(bednumber int64, orgid int64) (models.DeviceNumber, error) {
  518. number := models.DeviceNumber{}
  519. err := XTReadDB().Model(&number).Where("id = ? AND org_id = ?", bednumber, orgid).Find(&number).Error
  520. return number, err
  521. }
  522. func GetLastMacherData(orgid int64) (models.DeviceAddmacher, error) {
  523. addmacher := models.DeviceAddmacher{}
  524. err := UserReadDB().Model(&addmacher).Where("user_org_id = ? AND status = ?", orgid, 1).Last(&addmacher).Error
  525. return addmacher, err
  526. }
  527. func AddTreatMode(machineid int64, orgid int64, treatmodes []int64) (err error) {
  528. utx := XTWriteDB().Begin()
  529. if len(treatmodes) > 0 {
  530. thisSQL := "INSERT INTO xt_device_treatmentmode(machine_id,treate_mode,status,ctime,user_org_id) VALUES "
  531. insertParams := make([]string, 0)
  532. insertData := make([]interface{}, 0)
  533. for _, contagion := range treatmodes {
  534. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  535. insertData = append(insertData, machineid)
  536. insertData = append(insertData, contagion)
  537. insertData = append(insertData, 1)
  538. insertData = append(insertData, time.Now().Unix())
  539. insertData = append(insertData, orgid)
  540. }
  541. thisSQL += strings.Join(insertParams, ",")
  542. err = utx.Exec(thisSQL, insertData...).Error
  543. if err != nil {
  544. utx.Rollback()
  545. return
  546. }
  547. }
  548. utx.Commit()
  549. return
  550. }
  551. //func DeleteBedNumber(id int64,orgid int64)(error){
  552. // //err := writeDb.Model(&models.DeviceNumber{}).Where("id = ? AND org_id = ?", id, orgid).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  553. // err := writeDb.Model(&models.DeviceNumber{}).Where("id = ? AND org_id = ?", id, orgid).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  554. //
  555. //
  556. // fmt.Println("错误是什么",err)
  557. // return err
  558. //
  559. //}
  560. //
  561. //func DeleteZoneId(id int64,orgid int64)(error) {
  562. // err := writeDb.Model(&models.DeviceZone{}).Where("id = ? AND org_id = ?", id, orgid).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  563. // return err
  564. //}
  565. func GetAllMachineInfo(page int64, limit int64, keyword string, zoneid int64, equimentid int64, statusid int64, orgid int64) (addmahcer []*models.DeviceAddmachers, total int64, err error) {
  566. db := readUserDb.Table("xt_device_addmacher as a").Where("a.status = 1")
  567. table := XTReadDB().Table("xt_device_zone as x")
  568. d := readUserDb.Table("xt_device_mode as m")
  569. d2 := XTReadDB().Table("xt_device_number as n")
  570. d3 := XTReadDB().Table("xt_device_information as i")
  571. fmt.Print("d", d)
  572. fmt.Println("table", table)
  573. fmt.Print("d2", d2, d3)
  574. if orgid > 0 {
  575. db = db.Where("a.user_org_id = ?", orgid)
  576. }
  577. if zoneid > 0 {
  578. db = db.Where("a.zone_id = ?", zoneid)
  579. }
  580. if equimentid > 0 {
  581. db = db.Where("a.device_type = ?", equimentid)
  582. }
  583. if statusid > 0 {
  584. db = db.Where("a.machine_status = ?", statusid)
  585. }
  586. if len(keyword) > 0 {
  587. keyword = "%" + keyword + "%"
  588. db = db.Where("a.serial_number LIKE ? OR a.device_name LIKE ? OR a.unit_type LIKE ?", keyword, keyword, keyword)
  589. }
  590. offset := (page - 1) * limit
  591. err = db.Group("a.id").Order("a.ctime asc").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.bed_id,x.name,m.device_mode,n.number,i.equiment_id").Count(&total).
  592. Joins("Left JOIN xt_device_mode as m on m.id = a.unit_type ").Joins("left join sgj_xt.xt_device_number as n on n.id = a.bed_id").Joins("Left JOIN sgj_xt.xt_device_zone as x On x.id = n.zone_id").Joins("left join xt_device_information as i on i.bed_number = a.bed_id").Offset(offset).Limit(limit).Scan(&addmahcer).Error
  593. if err != nil {
  594. return
  595. }
  596. return
  597. }
  598. func GetAllMachine(zoneid int64, classid int64, deviceid int64, timenow int64, orgid int64) (addmahcer []*models.DeviceAddmachers, err error) {
  599. fmt.Println("timenow", timenow)
  600. db := readUserDb.Table("xt_device_addmacher as a").Where("a.status = 1")
  601. //db := XTReadDB().Table("xt_device_addmacher as a").Where("a.status = 1")
  602. tab := XTReadDB().Table("xt_schedule as x")
  603. fmt.Println("tab", tab)
  604. d := readUserDb.Table("xt_device_mode as m")
  605. fmt.Print("d", d)
  606. table := XTReadDB().Table("xt_device_number as n")
  607. fmt.Print("table", table)
  608. if orgid > 0 {
  609. db = db.Where("a.user_org_id = ?", orgid)
  610. }
  611. if zoneid > 0 {
  612. db = db.Where("a.zone_id = ?", zoneid)
  613. }
  614. if deviceid > 0 {
  615. db = db.Where("a.device_type = ?", deviceid)
  616. }
  617. if classid > 0 {
  618. error := db.Joins("LEFT JOIN sgj_xt.xt_schedule as x On x.bed_id = a.bed_id").Joins("LEFT JOIN xt_device_mode as d on d.id = a.unit_type").Joins("lefj join sgj_xt.xt_device_number as n on n.id = a.bed_id").Where("x.user_org_id = ? AND x.schedule_date = ? AND x.schedule_type = ? AND x.status = ?", orgid, timenow, classid, 1).Order("a.ctime desc").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,d.device_mode,n.number").Find(&addmahcer).Error
  619. fmt.Println("err", error)
  620. } else {
  621. err = db.Order("a.ctime asc").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.bed_id,d.device_mode,n.number").Joins("LEFT JOIN xt_device_mode as d on d.id = a.unit_type").Joins("left join sgj_xt.xt_device_number as n on n.id = a.bed_id").Find(&addmahcer).Error
  622. fmt.Println("错误是什么", err)
  623. if err != nil {
  624. return
  625. }
  626. }
  627. return
  628. }
  629. func GetMachineDetail(id int64, orgid int64) (models.DeviceAddmachers, error) {
  630. addmacher := models.DeviceAddmachers{}
  631. // // //err := readUserDb.Model(&addmacher).Where("id = ? AND user_org_id = ? AND status = 1", id, orgid).Find(&addmacher).Error
  632. db := readUserDb.Table("xt_device_addmacher as a").Where("a.status =1")
  633. d := readUserDb.Table("xt_device_mode as d")
  634. fmt.Print("d", d)
  635. 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 as device_mode,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").Joins("left join xt_device_mode as d on d.id = a.unit_type ").Where("a.id = ? and a.user_org_id = ?", id, orgid).Find(&addmacher).Error
  636. return addmacher, err
  637. }
  638. func GetTreatModel(id int64, orgid int64) (mode []*models.DeviceTreatmentmode, err error) {
  639. err = XTReadDB().Model(&mode).Where("machine_id = ? AND user_org_id = ? AND status = 1 ", id, orgid).Find(&mode).Error
  640. return mode, err
  641. }
  642. func GetPatientId(id int64) (models.XtPatientsNew, error) {
  643. patientsNew := models.XtPatientsNew{}
  644. err := XTReadDB().Model(&patientsNew).Where("blood_id = ? and status = 1", id).Find(&patientsNew).Error
  645. return patientsNew, err
  646. }
  647. func GetPatientInfectious(id int64) (infectious []*models.InfectiousDiseases, err error) {
  648. err = XTReadDB().Where("patient_id=? and status=1", id).Find(&infectious).Error
  649. return infectious, err
  650. }
  651. func GetPatientIdOne(patient_id int64, orgId int64) (models.XtPatientsNew, error) {
  652. patientsNew := models.XtPatientsNew{}
  653. redis := RedisClient()
  654. defer redis.Close()
  655. key := strconv.FormatInt(orgId, 10) + ":" + strconv.FormatInt(patient_id, 10) + ":patient_info"
  656. patient_info_str, _ := redis.Get(key).Result()
  657. if len(patient_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  658. err = readDb.Model(&models.Patients{}).Where("blood_id = ? and user_org_id=? and status=1", patient_id, orgId).First(&patientsNew).Error
  659. if err != nil {
  660. if err == gorm.ErrRecordNotFound {
  661. return patientsNew, nil
  662. } else {
  663. return patientsNew, err
  664. }
  665. } else {
  666. if patientsNew.ID > 0 {
  667. //缓存数据
  668. patient_info_json, err := json.Marshal(&patientsNew)
  669. if err == nil {
  670. redis.Set(key, patient_info_json, time.Second*60*60*18)
  671. }
  672. } else {
  673. redis.Set(key, "null", time.Second*60*60*18)
  674. }
  675. return patientsNew, nil
  676. }
  677. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  678. if patient_info_str == "null" {
  679. return patientsNew, nil
  680. } else {
  681. json.Unmarshal([]byte(patient_info_str), &patientsNew)
  682. return patientsNew, nil
  683. }
  684. }
  685. }
  686. func GetAllBedNumberSix(orgId int64) (devicenumber []*models.ScheduleDeviceNumber, err error) {
  687. err = XTReadDB().Where("org_id = ? and status = 1", orgId).Find(&devicenumber).Error
  688. return devicenumber, err
  689. }
  690. func GetAllBedNumberSeven(orgId int64) (devicenumber []*models.SgjDeviceNumber, err error) {
  691. err = XTReadDB().Where("org_id = ? and status= 1", orgId).Preload("Zone", "status = 1").Find(&devicenumber).Error
  692. return devicenumber, err
  693. }