device_service.go 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. package service
  2. import (
  3. "XT_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 CreateDeviceZone(orgID int64, name string, type_ int) (*models.DeviceZone, error) {
  256. now := time.Now().Unix()
  257. zone := models.DeviceZone{
  258. OrgID: orgID,
  259. Name: name,
  260. Type: type_,
  261. Status: 1,
  262. CreateTime: now,
  263. ModifyTime: now,
  264. }
  265. err := writeDb.Model(&models.DeviceZone{}).Create(&zone).Error
  266. if err != nil {
  267. return nil, err
  268. }
  269. return &zone, nil
  270. }
  271. func UpdateDeviceZone(zone *models.DeviceZone) error {
  272. err := writeDb.Save(zone).Error
  273. return err
  274. }
  275. //////// 分组 Group
  276. func GetAllValidDeviceGroups(orgID int64) ([]*models.DeviceGroup, error) {
  277. var groups []*models.DeviceGroup
  278. err := readDb.Model(&models.DeviceGroup{}).Where("org_id = ? AND status = 1", orgID).Find(&groups).Error
  279. if err != nil {
  280. return nil, err
  281. }
  282. return groups, nil
  283. }
  284. func GetDeviceGroupByID(orgID int64, groupID int64) (*models.DeviceGroup, error) {
  285. var group models.DeviceGroup
  286. err := readDb.Model(&models.DeviceGroup{}).Where("org_id = ? AND id = ?", orgID, groupID).First(&group).Error
  287. if err != nil {
  288. if err == gorm.ErrRecordNotFound {
  289. return nil, nil
  290. } else {
  291. return nil, err
  292. }
  293. }
  294. return &group, nil
  295. }
  296. func CreateDeviceGroup(orgID int64, name string) (*models.DeviceGroup, error) {
  297. now := time.Now().Unix()
  298. group := models.DeviceGroup{
  299. OrgID: orgID,
  300. Name: name,
  301. Status: 1,
  302. CreateTime: now,
  303. ModifyTime: now,
  304. }
  305. err := writeDb.Model(&models.DeviceGroup{}).Create(&group).Error
  306. if err != nil {
  307. return nil, err
  308. }
  309. return &group, nil
  310. }
  311. func UpdateDeviceGroup(group *models.DeviceGroup) error {
  312. err := writeDb.Save(group).Error
  313. return err
  314. }
  315. //////// 机号 Number
  316. type DeviceNumberViewModel struct {
  317. models.DeviceNumber
  318. ZoneName string `gorm:"column:zone_name" json:"zone_name"`
  319. GroupName string `gorm:"column:group_name" json:"group_name"`
  320. }
  321. func GetAllValidDeviceNumbers(orgID int64) ([]*DeviceNumberViewModel, error) {
  322. var vms []*DeviceNumberViewModel = make([]*DeviceNumberViewModel, 0)
  323. 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()
  324. defer rows.Close()
  325. if err != nil {
  326. return nil, err
  327. }
  328. for rows.Next() {
  329. var vm DeviceNumberViewModel
  330. readDb.ScanRows(rows, &vm)
  331. vms = append(vms, &vm)
  332. }
  333. return vms, nil
  334. }
  335. func GetAllBedNumber(orgId int64) (devicenumber []*models.XtDeviceNumber, err error) {
  336. 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
  337. return devicenumber, err
  338. }
  339. func GetAllBedNumberTwo(orgId int64, id int64) (devicenumber []*models.XtDeviceNumber, err error) {
  340. 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
  341. return devicenumber, err
  342. }
  343. func GetAllMachineByOrgId(orgid int64) (addmacher []*models.DeviceAddmacher, err error) {
  344. err = UserReadDB().Where("user_org_id = ? AND status = 1", orgid).Find(&addmacher).Error
  345. return addmacher, err
  346. }
  347. func GetAllDeviceNumbers(orgID int64, record_date int64, schedule_type int64) ([]*DeviceNumberViewModel, error) {
  348. var vms []*DeviceNumberViewModel = make([]*DeviceNumberViewModel, 0)
  349. 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()
  350. defer rows.Close()
  351. if err != nil {
  352. return nil, err
  353. }
  354. for rows.Next() {
  355. var vm DeviceNumberViewModel
  356. readDb.ScanRows(rows, &vm)
  357. vms = append(vms, &vm)
  358. }
  359. return vms, nil
  360. }
  361. func GetDeviceNumberByID(orgID int64, numberID int64) (*models.DeviceNumber, error) {
  362. var number models.DeviceNumber
  363. err := readDb.Model(&models.DeviceNumber{}).Where("org_id = ? AND id = ?", orgID, numberID).First(&number).Error
  364. if err != nil {
  365. if err == gorm.ErrRecordNotFound {
  366. return nil, nil
  367. } else {
  368. return nil, err
  369. }
  370. }
  371. return &number, nil
  372. }
  373. func CreateDeviceNumber(orgID int64, number string, zoneID int64, groupID int64) (*models.DeviceNumber, error) {
  374. now := time.Now().Unix()
  375. numberModel := models.DeviceNumber{
  376. OrgID: orgID,
  377. Number: number,
  378. ZoneID: zoneID,
  379. GroupID: groupID,
  380. Status: 1,
  381. CreateTime: now,
  382. ModifyTime: now,
  383. }
  384. err := writeDb.Model(&models.DeviceNumber{}).Create(&numberModel).Error
  385. if err != nil {
  386. return nil, err
  387. }
  388. return &numberModel, nil
  389. }
  390. func UpdateDeviceNumber(number *models.DeviceNumber) error {
  391. now := time.Now().Unix()
  392. number.ModifyTime = now
  393. tx := writeDb.Begin()
  394. updateNumberErr := tx.Save(number).Error
  395. if updateNumberErr != nil {
  396. tx.Rollback()
  397. return updateNumberErr
  398. }
  399. 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
  400. if updateDeviceErr != nil {
  401. tx.Rollback()
  402. return updateDeviceErr
  403. }
  404. tx.Commit()
  405. return nil
  406. }
  407. func GetDeviceNumberCountForZoneID(orgID int64, zoneID int64) (int, error) {
  408. var count int
  409. err := readDb.Model(&models.DeviceNumber{}).Where("zone_id = ? AND org_id = ? AND status = 1", zoneID, orgID).Count(&count).Error
  410. if err != nil {
  411. return 0, err
  412. }
  413. return count, nil
  414. }
  415. func GetDeviceNumberCountForGroupID(orgID int64, groupID int64) (int, error) {
  416. var count int
  417. err := readDb.Model(&models.DeviceNumber{}).Where("group_id = ? AND org_id = ? AND status = 1", groupID, orgID).Count(&count).Error
  418. if err != nil {
  419. return 0, err
  420. }
  421. return count, nil
  422. }
  423. func GetScheduleCountForDeviceNumber(orgID int64, deviceNumberID int64, beginDate int64) (int, error) {
  424. var count int
  425. err := readDb.Model(&models.Schedule{}).Where("bed_id = ? AND user_org_id = ? AND schedule_date >= ? AND status = 1", deviceNumberID, orgID, beginDate).Count(&count).Error
  426. if err != nil {
  427. return 0, err
  428. }
  429. return count, nil
  430. }
  431. func GetScheduleTemplateItemCountForDeviceNumber(orgID int64, deviceNumberID int64) (int, error) {
  432. var count int
  433. err := readDb.Model(&models.PatientScheduleTemplateItem{}).Where("device_number_id = ? AND org_id = ? AND status = 1", deviceNumberID, orgID).Count(&count).Error
  434. if err != nil {
  435. return 0, err
  436. }
  437. return count, nil
  438. }
  439. func GetZoneId(bednumber int64, orgid int64) (models.DeviceNumber, error) {
  440. number := models.DeviceNumber{}
  441. err := XTReadDB().Model(&number).Where("id = ? AND org_id = ?", bednumber, orgid).Find(&number).Error
  442. return number, err
  443. }
  444. //func AddMacher(deviceaddmacher *models.DeviceAddmacher) error{
  445. // fmt.Println("进来了没哟")
  446. // tx := writeDb.Begin()
  447. // err := tx.Model(&models.DeviceAddmachers{}).Create(&deviceaddmacher).Error
  448. // fmt.Println("错误是什么",err)
  449. // if err !=nil{
  450. // tx.Rollback()
  451. // }
  452. // tx.Commit()
  453. // return err
  454. //}
  455. // func CreateMacher(machers *models.DeviceAddmacher) error {
  456. // fmt.Println("进来了米有")
  457. // begin := writeDb.Begin()
  458. // fmt.Println("hhhhhhhh",begin)
  459. // err := begin.Model(&models.DeviceAddmacher{}).Create(machers).Error
  460. // fmt.Println("err",err)
  461. // if err !=nil{
  462. // begin.Rollback()
  463. // }
  464. // begin.Commit()
  465. // fmt.Println("执行了没有")
  466. // return err
  467. // }
  468. func GetLastMacherData(orgid int64) (models.DeviceAddmacher, error) {
  469. addmacher := models.DeviceAddmacher{}
  470. err := UserReadDB().Model(&addmacher).Where("user_org_id = ? AND status = ?", orgid, 1).Last(&addmacher).Error
  471. return addmacher, err
  472. }
  473. func AddTreatMode(machineid int64, orgid int64, treatmodes []int64) (err error) {
  474. utx := XTWriteDB().Begin()
  475. if len(treatmodes) > 0 {
  476. thisSQL := "INSERT INTO xt_device_treatmentmode(machine_id,treate_mode,status,ctime,user_org_id) VALUES "
  477. insertParams := make([]string, 0)
  478. insertData := make([]interface{}, 0)
  479. for _, contagion := range treatmodes {
  480. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  481. insertData = append(insertData, machineid)
  482. insertData = append(insertData, contagion)
  483. insertData = append(insertData, 1)
  484. insertData = append(insertData, time.Now().Unix())
  485. insertData = append(insertData, orgid)
  486. }
  487. thisSQL += strings.Join(insertParams, ",")
  488. err = utx.Exec(thisSQL, insertData...).Error
  489. if err != nil {
  490. utx.Rollback()
  491. return
  492. }
  493. }
  494. utx.Commit()
  495. return
  496. }
  497. //func DeleteBedNumber(id int64,orgid int64)(error){
  498. // //err := writeDb.Model(&models.DeviceNumber{}).Where("id = ? AND org_id = ?", id, orgid).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  499. // err := writeDb.Model(&models.DeviceNumber{}).Where("id = ? AND org_id = ?", id, orgid).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  500. //
  501. //
  502. // fmt.Println("错误是什么",err)
  503. // return err
  504. //
  505. //}
  506. //
  507. //func DeleteZoneId(id int64,orgid int64)(error) {
  508. // err := writeDb.Model(&models.DeviceZone{}).Where("id = ? AND org_id = ?", id, orgid).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  509. // return err
  510. //}
  511. func GetAllMachineInfo(page int64, limit int64, keyword string, zoneid int64, equimentid int64, statusid int64, orgid int64) (addmahcer []*models.DeviceAddmachers, total int64, err error) {
  512. //db := readUserDb().Table("xt_device_addmacher as a").Where("a.status = 1")
  513. db := readUserDb.Table("xt_device_addmacher as a").Where("a.status = 1")
  514. table := XTReadDB().Table("xt_device_zone as x")
  515. d := readUserDb.Table("xt_device_mode as m")
  516. d2 := XTReadDB().Table("xt_device_number as n")
  517. fmt.Print("d", d)
  518. fmt.Println("table", table)
  519. fmt.Print("d2", d2)
  520. if orgid > 0 {
  521. db = db.Where("a.user_org_id = ?", orgid)
  522. }
  523. if zoneid > 0 {
  524. db = db.Where("a.zone_id = ?", zoneid)
  525. }
  526. if equimentid > 0 {
  527. db = db.Where("a.device_type = ?", equimentid)
  528. }
  529. if statusid > 0 {
  530. db = db.Where("a.machine_status = ?", statusid)
  531. }
  532. if len(keyword) > 0 {
  533. keyword = "%" + keyword + "%"
  534. db = db.Where("a.serial_number LIKE ? OR a.device_name LIKE ? OR a.unit_type LIKE ?", keyword, keyword, keyword)
  535. }
  536. offset := (page - 1) * limit
  537. 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,x.name,m.device_mode,n.number").Count(&total).
  538. 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").Offset(offset).Limit(limit).Scan(&addmahcer).Error
  539. if err != nil {
  540. return
  541. }
  542. return
  543. }
  544. func GetAllMachine(zoneid int64, classid int64, deviceid int64, timenow int64, orgid int64) (addmahcer []*models.DeviceAddmachers, err error) {
  545. fmt.Println("timenow", timenow)
  546. db := readUserDb.Table("xt_device_addmacher as a").Where("a.status = 1")
  547. //db := XTReadDB().Table("xt_device_addmacher as a").Where("a.status = 1")
  548. tab := XTReadDB().Table("xt_schedule as x")
  549. fmt.Println("tab", tab)
  550. d := readUserDb.Table("xt_device_mode as m")
  551. fmt.Print("d", d)
  552. table := XTReadDB().Table("xt_device_number as n")
  553. fmt.Print("table", table)
  554. if orgid > 0 {
  555. db = db.Where("a.user_org_id = ?", orgid)
  556. }
  557. if zoneid > 0 {
  558. db = db.Where("a.zone_id = ?", zoneid)
  559. }
  560. if deviceid > 0 {
  561. db = db.Where("a.device_type = ?", deviceid)
  562. }
  563. if classid > 0 {
  564. 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
  565. fmt.Println("err", error)
  566. } else {
  567. 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
  568. fmt.Println("错误是什么", err)
  569. if err != nil {
  570. return
  571. }
  572. }
  573. return
  574. }
  575. func GetMachineDetail(id int64, orgid int64) (models.DeviceAddmachers, error) {
  576. addmacher := models.DeviceAddmachers{}
  577. // // //err := readUserDb.Model(&addmacher).Where("id = ? AND user_org_id = ? AND status = 1", id, orgid).Find(&addmacher).Error
  578. db := readUserDb.Table("xt_device_addmacher as a").Where("a.status =1")
  579. d := readUserDb.Table("xt_device_mode as d")
  580. fmt.Print("d", d)
  581. 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
  582. return addmacher, err
  583. }
  584. func GetTreatModel(id int64, orgid int64) (mode []*models.DeviceTreatmentmode, err error) {
  585. err = XTReadDB().Model(&mode).Where("machine_id = ? AND user_org_id = ? AND status = 1 ", id, orgid).Find(&mode).Error
  586. return mode, err
  587. }