device_service.go 25KB

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