cpinfo_service.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package service
  2. import (
  3. "fmt"
  4. "github.com/astaxie/beego/orm"
  5. "rtzh_elec_temperature/logger"
  6. "sync"
  7. )
  8. //设备控制点列表缓存
  9. var cpList = sync.Map{}
  10. //测点相关服务
  11. type CpinfoService struct {
  12. BaseService
  13. }
  14. // 根据模型ID获取对应的控制点信息
  15. // 返回对应的cpid,cpname,zonename等
  16. func (t *CpinfoService) CpList(modelid int) []orm.Params {
  17. key := fmt.Sprintf("%d", modelid)
  18. var list = []orm.Params{}
  19. if info, ok := cpList.Load(key); ok {
  20. return info.([]orm.Params)
  21. } else {
  22. db := orm.NewOrm()
  23. // 多数据源,切换到平台的库上
  24. db.Using("iss")
  25. var sqlCommandText = "select * from dev_cpinfo where appid=? and modelid=?"
  26. _, err := db.Raw(sqlCommandText, RtelecManageApp().RegAppID, modelid).Values(&list)
  27. if err == nil {
  28. if len(list) > 0 {
  29. cpList.Store(key, list)
  30. return list
  31. }
  32. } else {
  33. logger.Logger.Error(err)
  34. logger.Logger.Println(fmt.Sprintf("Error SQL:%s 参数:%s,%s", sqlCommandText, RtelecManageApp().RegAppID, modelid))
  35. }
  36. }
  37. return list
  38. }
  39. // 根据控制点id获取控制点信息
  40. func (t *CpinfoService) GetCpInfoById(cpid int64) (orm.Params, error) {
  41. db := orm.NewOrm()
  42. db.Using("iss")
  43. cpInfo := []orm.Params{}
  44. sqlCommandText := "select * from dev_cpinfo where appid=? and mpid=?"
  45. _, err := db.Raw(sqlCommandText, RtelecManageApp().RegAppID, cpid).Values(&cpInfo)
  46. if err == nil {
  47. if len(cpInfo) > 0 {
  48. return cpInfo[0], nil
  49. }
  50. } else {
  51. logger.Logger.Error(err)
  52. logger.Logger.Println(fmt.Sprintf("Error SQL:%s 参数:%s,%d", sqlCommandText, RtelecManageApp().RegAppID, cpid))
  53. return orm.Params{}, err
  54. }
  55. return orm.Params{}, nil
  56. }