deviceService.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. package device
  2. import (
  3. "fmt"
  4. "strconv"
  5. "xorm.io/xorm"
  6. "dashoo.cn/base_common/utils"
  7. . "dashoo.cn/base_common/utils/db"
  8. "dashoo.cn/mcs_common/business/equipment"
  9. "dashoo.cn/mcs_common/business/organize"
  10. )
  11. type DeviceService struct {
  12. ServiceBase
  13. }
  14. func GetDeviceService(xormEngine *xorm.Engine) *DeviceService {
  15. s := new(DeviceService)
  16. s.DBE = xormEngine
  17. return s
  18. }
  19. // 新增
  20. func (s *DeviceService) GetChannelids(userid string) (ids []string) {
  21. var idList []Id_Str
  22. err := s.DBE.SQL(`select Id FROM Device where CreateUserId= ` + userid).Find(&idList)
  23. LogError(err)
  24. ids = s.GetIdsFromId_StrList(idList)
  25. if len(ids) == 0 {
  26. ids = append(ids, "-1")
  27. }
  28. return
  29. }
  30. func (s *DeviceService) GetEntitiesWithOrderSearch(order, searchstring string) []Device {
  31. sql := `select a.Id,a.Title,a.Serial,a.About,a.Local,a.Latitude,a.Longitude from Device a
  32. where ` + searchstring + ` order by ` + order
  33. List := make([]Device, 0)
  34. utils.DBE.SQL(sql).Find(&List)
  35. return List
  36. }
  37. func (s *DeviceService) GetEntitiesbyVisitLevel(index int) (devices []Device) {
  38. s.DBE.Where("Visitlevel=?", index).Find(&devices)
  39. return
  40. }
  41. func (s *DeviceService) GetEntitiesbyiddtype(id, dtype int) (devices []Device) {
  42. s.DBE.Where("Createuserid=? and Dtype=?", utils.ToStr(id), dtype).Find(&devices)
  43. return
  44. }
  45. func (s *DeviceService) GetEntitiesbyuid(uid string) (devices []Device) {
  46. s.DBE.Where("Createuserid=?", uid).Find(&devices)
  47. return
  48. }
  49. func (s *DeviceService) GetChannelsListOrder(pageIndex, itemsPerPage int64, order string, searchstring, Uid string) (int64, []DeviceChannels) {
  50. var err error
  51. var total int64
  52. if Uid == "" {
  53. Uid = "0"
  54. }
  55. //获取总记录数
  56. sqlCount := " select count(*) from Channels a left join Device b on a.DId=b.Id left join Base_User d on a.CreateUserId=d.Id where " + searchstring + ""
  57. var sql, limitstr string = "", ""
  58. if pageIndex > 0 {
  59. limitstr = ` limit ` + utils.ToStr((pageIndex-1)*itemsPerPage) + "," + utils.ToStr(itemsPerPage)
  60. }
  61. sql = `select a.Id, a.EquipMentId, a.Code,a.Title,a.Tags,a.About,a.Local,a.Serial,a.TagCode,b.Code BCode,b.Title BTitle,a.DeviceState,a.DataItem,a.Description from Channels a left join Device b on a.DId=b.Id
  62. left join ChannelsSort c on a.Id=c.ChannelId and c.UserId=` + Uid + `
  63. left join Base_User d on a.CreateUserId=d.Id
  64. where ` + searchstring + ` order by ` + order + limitstr
  65. List := make([]DeviceChannels, 0)
  66. utils.DBE.SQL(sql).Find(&List)
  67. resultsSlice, err := s.DBE.Query(sqlCount)
  68. LogError(err)
  69. if len(resultsSlice) > 0 {
  70. results := resultsSlice[0]
  71. for _, value := range results {
  72. total, err = strconv.ParseInt(string(value), 10, 64)
  73. LogError(err)
  74. break
  75. }
  76. }
  77. return total, List
  78. }
  79. func (s *DeviceService) GetChannelsList(pageIndex, itemsPerPage int64, searchstring, Uid string) (int64, []DeviceChannels) {
  80. var err error
  81. var total int64
  82. if Uid == "" {
  83. Uid = "0"
  84. }
  85. //获取总记录数
  86. sqlCount := " select count(*) from Channels a where " + searchstring + ""
  87. var sql, limitstr string = "", ""
  88. if pageIndex > 0 {
  89. limitstr = ` limit ` + utils.ToStr((pageIndex-1)*itemsPerPage) + "," + utils.ToStr(itemsPerPage)
  90. }
  91. sql = `select a.* from Channels a
  92. left join EquipMentSort b on a.EquipMentId = b.EquipMentlId and b.CreateUserId=` + Uid + `
  93. where ` + searchstring + ` order by b.SortCode,a.Id desc ` + limitstr
  94. List := make([]DeviceChannels, 0)
  95. utils.DBE.SQL(sql).Find(&List)
  96. resultsSlice, err := s.DBE.Query(sqlCount)
  97. LogError(err)
  98. if len(resultsSlice) > 0 {
  99. results := resultsSlice[0]
  100. for _, value := range results {
  101. total, err = strconv.ParseInt(string(value), 10, 64)
  102. LogError(err)
  103. break
  104. }
  105. }
  106. return total, List
  107. }
  108. func (s *DeviceService) GetIdByCode(code string) int {
  109. var channel Channels
  110. s.DBE.Where("Code=?", code).Get(&channel)
  111. return channel.Id
  112. }
  113. func (s *DeviceService) GetBlackBoxIdByCode(code string) int {
  114. var channel Channels
  115. s.DBE.Where("Code=?", code).Get(&channel) //都得到的是整条语句
  116. return channel.DId //取其中一个字段
  117. }
  118. func (s *DeviceService) GetDidByCode(code string) int {
  119. var channel Channels
  120. s.DBE.Where("Code=?", code).Get(&channel)
  121. return channel.DId
  122. }
  123. func (s *DeviceService) GetIdByDId(did int) int {
  124. var channel Channels
  125. s.DBE.Where("DId=?", did).Get(&channel)
  126. return channel.Id
  127. }
  128. func (s *DeviceService) GetWDidByDid(id int) int {
  129. var device Device
  130. s.DBE.Where("Id=?", id).Get(&device)
  131. return device.Wdid
  132. }
  133. func (s *DeviceService) GetTitleByCode(code string) string {
  134. var channel Channels
  135. s.DBE.Where("Code=?", code).Get(&channel)
  136. return channel.Title
  137. }
  138. func (s *DeviceService) GetDataItemByCode(code string) int {
  139. var channel Channels
  140. s.DBE.Where("Code=?", code).Get(&channel)
  141. return channel.DataItem
  142. }
  143. func (s *DeviceService) Delete(id string) (err error) {
  144. var entity Device
  145. err = s.DeleteEntityById(id, &entity)
  146. if err == nil {
  147. Idint, _ := utils.StrTo(id).Int()
  148. var detail Channels = Channels{DId: Idint}
  149. s.DeleteEntity(&detail)
  150. }
  151. return
  152. }
  153. func (s *DeviceService) GetDeviceChannel(did int, channels interface{}) {
  154. s.DBE.Where("DId=?", did).Find(channels)
  155. }
  156. func (s *DeviceService) GetDeviceTreeByRole(roleid, permissionitem string) (utree []DeviceShowr) {
  157. s.DBE.SQL(`select distinct a.TargetId Id,c.Title FullName
  158. from Base_PermissionScope a
  159. left join Device c on a.TargetId=c.Id
  160. where a.ResourceCategory='Base_Role'
  161. and a.ResourceId =` + roleid + `
  162. and a.TargetCategory='Base_User' and a.PermissionId=` + permissionitem).Find(&utree)
  163. return
  164. }
  165. //验证设备是否存在
  166. func (s *DeviceService) VerifyDevice(did int) bool {
  167. var has bool
  168. var devices Device
  169. s.DBE.Where(`WDid=?`, utils.ToStr(did)).Get(&devices)
  170. if devices.Id == 0 {
  171. has = false
  172. } else {
  173. has = true
  174. }
  175. return has
  176. }
  177. //验证传感器是否存在
  178. func (s *DeviceService) VerifyChannel(code string) bool {
  179. var has bool
  180. var channel Channels
  181. s.DBE.Where(`Code=?`, code).Get(&channel)
  182. if channel.Id == 0 {
  183. has = false
  184. } else {
  185. has = true
  186. }
  187. return has
  188. }
  189. //根据序列号获取设备id
  190. func (s *DeviceService) GetDIdBySerial(serial string) (did int) {
  191. var devices Device
  192. s.DBE.Where(`Serial=?`, serial).Get(&devices)
  193. return devices.Id
  194. }
  195. func (s *DeviceService) GetDTitleBySerial(serial string) string {
  196. var devices Device
  197. s.DBE.Where(`Serial=?`, serial).Get(&devices)
  198. return devices.Title
  199. }
  200. //根据Code获取WDid
  201. func (s *DeviceService) GetWdidByCode(code string) int {
  202. var devices Device
  203. sql := `select a.WDid from Device a left join Channels b on a.Id=b.DId where b.Code='` + code + `'`
  204. s.DBE.SQL(sql).Get(&devices)
  205. return devices.Wdid
  206. }
  207. func (s *DeviceService) GetChannelsCount(searchstring string) int64 {
  208. channel := new(Channels)
  209. total, _ := s.DBE.Where(searchstring).Count(channel)
  210. return total
  211. }
  212. func (s *DeviceService) GetCIds(searchstring string) string {
  213. if searchstring == "" {
  214. searchstring = "1=1"
  215. }
  216. sql := `select Code from Channels
  217. where ` + searchstring
  218. var cids = "-1"
  219. List := make([]Channels, 0)
  220. s.DBE.SQL(sql).Find(&List)
  221. for i, j := 0, len(List); i < j; i++ {
  222. cids += "," + utils.ToStr(List[i].Code)
  223. }
  224. return cids
  225. }
  226. func (s *DeviceService) GetEntitieByCode(code string) *Channels {
  227. entity := new(Channels)
  228. s.DBE.Where("Code='" + code + "'").Get(entity)
  229. return entity
  230. }
  231. func (s *DeviceService) GetDeviceAlertor(code, alerttype, binddatatype string) *DeviceAlertor {
  232. entity := new(DeviceAlertor)
  233. sql := "select a.Id,a.Code,a.Title,a.Serial,a.`Local`,a.Latitude,a.Longitude,a.DataItem,c.Code Ccode,group_concat(b.DataItem) AlertItem" + `
  234. from Device a
  235. inner join Channels b on a.Id=b.DId and b.DataItem in (` + alerttype + `)
  236. inner join Channels c on a.Id=c.DId and c.DataItem=` + binddatatype + `
  237. where a.DataItem=4 and b.Code='` + code + `'
  238. group by a.Id`
  239. s.DBE.SQL(sql).Get(entity)
  240. return entity
  241. }
  242. func (s *DeviceService) GetCode(searchstring, Uid string) string {
  243. if searchstring == "" {
  244. searchstring = "1=1"
  245. }
  246. sql := `select a.Code from Channels a left join Device b on a.DId=b.Id
  247. left join ChannelsSort c on a.Id=c.ChannelId and c.UserId=` + Uid + `
  248. left join Base_User d on a.CreateUserId=d.Id
  249. where ` + searchstring
  250. var cids = "-1"
  251. List := make([]DeviceChannels, 0)
  252. s.DBE.SQL(sql).Find(&List)
  253. for i, j := 0, len(List); i < j; i++ {
  254. cids += "," + utils.ToStr(List[i].Code)
  255. }
  256. return cids
  257. }
  258. //根据条件取列表(不分页)
  259. func (s *DeviceService) Getlist(order string, where, Uid string) []DeviceChannels {
  260. sql := `select a.Id, a.Code,a.Title,a.Tags,a.About,b.Local,a.Serial,a.TagCode,b.Code BCode,b.Title BTitle,a.DeviceState,a.DataItem from Channels a left join Device b on a.DId=b.Id
  261. left join ChannelsSort c on a.Id=c.ChannelId and c.UserId=` + Uid + `
  262. left join Base_User d on a.CreateUserId=d.Id
  263. where ` + where + ` order by ` + order
  264. List := make([]DeviceChannels, 0)
  265. utils.DBE.SQL(sql).Find(&List)
  266. return List
  267. }
  268. //查询异常数据(sensor查询工具也用这个方法)
  269. func (s *DeviceService) GetAbnormal(pageIndex, itemsPerPage int64, order string, searchstring, Uid string) (int64, []Abnormal) {
  270. var err error
  271. var total int64
  272. if Uid == "" {
  273. Uid = "0"
  274. }
  275. //获取总记录数
  276. sqlCount := " select count(*) from Channels a left join Base_User b on a.CreateUserId=b.Id left join Device c on a.DId=c.Id left join Base_Company d on d.Id=b.AccCode where " + searchstring + ""
  277. var sql, limitstr string = "", ""
  278. if pageIndex > 0 {
  279. limitstr = ` limit ` + utils.ToStr((pageIndex-1)*itemsPerPage) + "," + utils.ToStr(itemsPerPage)
  280. }
  281. sql = "select a.Title,a.Serial,a.Tags,a.ChannelState,a.DataItem,a.DeviceState,a.CJTime,a.CSTime,a.CreateOn BindingTime,b.Realname,b.UserName,b.Mobile,b.Telephone,c.`Local` from Channels a left join Base_User b on a.CreateUserId=b.Id left join Device c on a.DId=c.Id left join Base_Company d on d.Id=b.AccCode where " + searchstring + " order by " + order + limitstr
  282. List := make([]Abnormal, 0)
  283. utils.DBE.SQL(sql).Find(&List)
  284. resultsSlice, err := s.DBE.Query(sqlCount)
  285. LogError(err)
  286. if len(resultsSlice) > 0 {
  287. results := resultsSlice[0]
  288. for _, value := range results {
  289. total, err = strconv.ParseInt(string(value), 10, 64)
  290. LogError(err)
  291. break
  292. }
  293. }
  294. return total, List
  295. }
  296. //BB查询工具
  297. func (s *DeviceService) GetBlackboxquery(pageIndex, itemsPerPage int64, order string, searchstring, Uid string) (int64, []Blackboxquery) {
  298. var err error
  299. var total int64
  300. if Uid == "" {
  301. Uid = "0"
  302. }
  303. //获取总记录数
  304. sqlCount := " select count(*) from Channels a left join Base_User b on a.CreateUserId=b.Id left join Base_Company d on d.Id=b.AccCode where " + searchstring + ""
  305. var sql, limitstr string = "", ""
  306. if pageIndex > 0 {
  307. limitstr = ` limit ` + utils.ToStr((pageIndex-1)*itemsPerPage) + "," + utils.ToStr(itemsPerPage)
  308. }
  309. sql = "select a.Title,a.Serial,a.ChannelState,a.CreateOn BindingTime,b.Realname,b.UserName from Channels a left join Base_User b on a.CreateUserId=b.Id left join Base_Company d on d.Id=b.AccCode where " + searchstring + " order by " + order + limitstr
  310. List := make([]Blackboxquery, 0)
  311. utils.DBE.SQL(sql).Find(&List)
  312. resultsSlice, err := s.DBE.Query(sqlCount)
  313. LogError(err)
  314. if len(resultsSlice) > 0 {
  315. results := resultsSlice[0]
  316. for _, value := range results {
  317. total, err = strconv.ParseInt(string(value), 10, 64)
  318. LogError(err)
  319. break
  320. }
  321. }
  322. return total, List
  323. }
  324. func (s *DeviceService) GetDeviceModel(order, searchstring string) []DeviceModel {
  325. sql := `select Id,DItem,Brands,DModel,Structure,Photo from DeviceModel
  326. where ` + searchstring + ` order by ` + order
  327. List := make([]DeviceModel, 0)
  328. utils.DBE.SQL(sql).Find(&List)
  329. return List
  330. }
  331. func (s *DeviceService) GetDeviceModelDItem(searchstring string) []DeviceModel {
  332. sql := `select distinct DItem from DeviceModel
  333. where ` + searchstring + ` order by DItem`
  334. List := make([]DeviceModel, 0)
  335. utils.DBE.SQL(sql).Find(&List)
  336. return List
  337. }
  338. func (s *DeviceService) GetDeviceModelBrands(searchstring string) []DeviceModel {
  339. sql := `select distinct Brands from DeviceModel
  340. where ` + searchstring + ` order by Brands`
  341. List := make([]DeviceModel, 0)
  342. utils.DBE.SQL(sql).Find(&List)
  343. return List
  344. }
  345. func (s *DeviceService) GetDeviceModelDModel(searchstring string) []DeviceModel {
  346. sql := `select distinct DModel,Photo,Id from DeviceModel
  347. where ` + searchstring + ` order by DModel`
  348. List := make([]DeviceModel, 0)
  349. utils.DBE.SQL(sql).Find(&List)
  350. return List
  351. }
  352. func (s *DeviceService) GetDeviceModelStructure(searchstring string) []DeviceModel {
  353. sql := `select distinct Structure from DeviceModel
  354. where ` + searchstring + ` order by Structure`
  355. List := make([]DeviceModel, 0)
  356. utils.DBE.SQL(sql).Find(&List)
  357. return List
  358. }
  359. func (s *DeviceService) GetDeviceModelPhoto(searchstring string) []DeviceModel {
  360. sql := `select distinct Photo from DeviceModel
  361. where ` + searchstring + ` order by Photo`
  362. List := make([]DeviceModel, 0)
  363. utils.DBE.SQL(sql).Find(&List)
  364. return List
  365. }
  366. func (s *DeviceService) GetChannelsTitles(searchstring string) string {
  367. type TitleStr struct {
  368. Title string
  369. }
  370. var entity TitleStr
  371. sql := `select group_concat(Title) Title from Channels
  372. where Code in (` + searchstring + `)`
  373. utils.DBE.SQL(sql).Get(&entity)
  374. return entity.Title
  375. }
  376. func (s *DeviceService) GetTree_Org(whereorg string) (tree []organize.Base_Organizetree) {
  377. s.DBE.SQL(`select Id, ParentId,Fullname, '/static/img/1_open.png' Icon from Base_Organize
  378. where ` + whereorg).Find(&tree)
  379. return
  380. }
  381. func (s *DeviceService) GetTree_OrgAndDevice(whereorg, searchstring, uid string, isoprnorg bool) (tree []organize.Base_Organizetree) {
  382. sql := ""
  383. if isoprnorg {
  384. sql = `select Id, ParentId,Fullname, '' Icon,'-1' Fu from Base_Organize
  385. where ` + whereorg + ` union all
  386. (select a.Id Id, a.OrganizeId ParentId, a.Title Fullname, '' Icon,'-2' Fu from Channels a left join Device b on a.DId=b.Id
  387. left join ChannelsSort c on a.Id=c.ChannelId and c.UserId=` + uid + `
  388. left join Base_User d on a.CreateUserId=d.Id
  389. where ` + searchstring + " order by c.SortCode,a.CreateOn desc)"
  390. } else {
  391. sql = `select a.Id Id, a.OrganizeId ParentId, a.Title Fullname, '' Icon,'-2' Fu from Channels a left join Device b on a.DId=b.Id
  392. left join ChannelsSort c on a.Id=c.ChannelId and c.UserId=` + uid + `
  393. left join Base_User d on a.CreateUserId=d.Id
  394. where ` + searchstring + " order by c.SortCode,a.CreateOn desc"
  395. }
  396. s.DBE.SQL(sql).Find(&tree)
  397. return
  398. }
  399. func (s *DeviceService) GetDistinctLocal(uid int) (list []Channels) {
  400. sql := fmt.Sprintf("select distinct Local,Latitude,Longitude from Channels where CreateUserId=%v and Local<>'' and Local is not null order by CreateOn desc", uid)
  401. s.DBE.SQL(sql).Find(&list)
  402. return
  403. }
  404. func (s *DeviceService) GetChannelSerialStrS(searchstring string) string {
  405. var ids Id_Str
  406. sql := `select group_concat(Serial) Id from Channels a
  407. left join Base_User b on a.CreateUserId=b.Id
  408. where ` + searchstring
  409. utils.DBE.SQL(sql).Get(&ids)
  410. return ids.Id
  411. }
  412. func (s *DeviceService) UpdateChannelBySet(did int) {
  413. if did == 0 {
  414. } else {
  415. sql := fmt.Sprintf(`update Channels set DId=0
  416. where DataItem=0 and Did=%v`, did)
  417. utils.DBE.Exec(sql)
  418. }
  419. }
  420. func (s *DeviceService) GetAllChannelsByOrgTopId(topids []string, uid, ditem string) (clist []Channels) {
  421. svc := organize.GetOrganizeService(s.DBE)
  422. orgidstrs := "-1"
  423. for i, v := range topids {
  424. if i != 0 { // 新增,略去不必要数据
  425. if v != "" {
  426. orgidstrstmp := svc.GetAllChildByTopId(v, uid)
  427. if orgidstrstmp != "" {
  428. orgidstrs += "," + orgidstrstmp
  429. }
  430. }
  431. }
  432. }
  433. svc.GetEntities(&clist, "OrganizeId in ("+orgidstrs+") or DataItem="+ditem+" and CreateUserId="+uid)
  434. // fmt.Println("----------------------------orgidstrsorgidstrs---", orgidstrs)
  435. return
  436. }
  437. // 根据设备ID获取传感器序列号
  438. func (s *DeviceService) GetSerialsByEquipids(equipids string) string {
  439. var model Id_Str
  440. if equipids == "" {
  441. equipids = "-1"
  442. }
  443. sql := `select group_concat(Serial) Id from Channels where EquipMentId in (` + equipids + `)`
  444. s.DBE.SQL(sql).Get(&model)
  445. return model.Id
  446. }
  447. // 根据设备ID获取传感器Code
  448. func (s *DeviceService) GetCodesByEquipids(equipids string) string {
  449. var model Id_Str
  450. if equipids == "" {
  451. equipids = "-1"
  452. }
  453. sql := `select group_concat(Code) Id from Channels where EquipMentId in (` + equipids + `)`
  454. s.DBE.SQL(sql).Get(&model)
  455. return model.Id
  456. }
  457. // 根据ID获取传感器序列号
  458. func (s *DeviceService) GetSerialsByids(ids string) string {
  459. var model Id_Str
  460. if ids == "" {
  461. ids = "-1"
  462. }
  463. sql := `select group_concat(Serial) Id from Channels where Id in (` + ids + `)`
  464. s.DBE.SQL(sql).Get(&model)
  465. return model.Id
  466. }
  467. // 根据ID获取传感器Code
  468. func (s *DeviceService) GetCodeByids(ids string) string {
  469. var model Id_Str
  470. if ids == "" {
  471. ids = "-1"
  472. }
  473. sql := `select group_concat(Code) Id from Channels where Id in (` + ids + `)`
  474. s.DBE.SQL(sql).Get(&model)
  475. return model.Id
  476. }
  477. // 根据组织id获取所有设备id
  478. func (s *DeviceService) GetAllIdsByOrgTopId(topids []string, uid string) (ids []ChannelsIdtitle) {
  479. svc := organize.GetOrganizeService(s.DBE)
  480. orgidstrs := "-1"
  481. for _, v := range topids {
  482. if v != "" {
  483. orgidstrstmp := svc.GetAllChildByTopId(v, uid)
  484. if orgidstrstmp != "" {
  485. orgidstrs += "," + orgidstrstmp
  486. }
  487. }
  488. }
  489. svcequi := equipment.GetEquipmentService(s.DBE)
  490. eids := svcequi.GetEquipmentidsByUid(uid)
  491. where := " (CreateUserId = " + uid + " or Id in (" + eids + "))"
  492. sql := `select a.Id,a.Title from Channels a left join
  493. EquipMentSort b on a.EquipMentId = b.EquipMentlId and b.UserId=` + uid + `
  494. where a.EquipMentId in
  495. (select Id from EquipMent where OrganizeId in (` + orgidstrs + ") and " + where + ") order by b.SortCode,a.Id desc"
  496. s.DBE.SQL(sql).Find(&ids)
  497. return
  498. }
  499. // 根据组织id和设备类型获取所有设备id
  500. func (s *DeviceService) GetAllSerialsByOrgTopIdDitem(topids []string, item, uid string) (ids []ChannelsSerialtitle) {
  501. svc := organize.GetOrganizeService(s.DBE)
  502. svcequi := equipment.GetEquipmentService(s.DBE)
  503. eids := svcequi.GetEquipmentidsByUid(uid)
  504. where := " (a.CreateUserId = " + uid + " or a.Id in (" + eids + "))"
  505. if len(topids) > 0 {
  506. orgidstrs := "-1"
  507. for _, v := range topids {
  508. if v != "" {
  509. orgidstrstmp := svc.GetAllChildByTopId(v, uid)
  510. if orgidstrstmp != "" {
  511. orgidstrs += "," + orgidstrstmp
  512. }
  513. }
  514. }
  515. where += " and a.OrganizeId in (" + orgidstrs + ")"
  516. }
  517. if item != "" && item != "-1" && item != "0" {
  518. where += " and b.DItem =" + item
  519. }
  520. sql := `select a.Serial,a.Title from Channels a left join
  521. EquipMentSort b on a.EquipMentId = b.EquipMentlId and b.UserId=` + uid + `
  522. where a.EquipMentId in
  523. (select a.Id from EquipMent a
  524. inner join EquipMentModel b on a.ModelId=b.Id where ` + where + ` )
  525. order by b.SortCode,a.Id desc`
  526. s.DBE.SQL(sql).Find(&ids)
  527. return
  528. }