DeviceTask.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package Device
  2. import (
  3. "Cold_Api/controllers/lib"
  4. "github.com/beego/beego/v2/adapter/orm"
  5. orm2 "github.com/beego/beego/v2/client/orm"
  6. "github.com/beego/beego/v2/core/logs"
  7. _ "github.com/go-sql-driver/mysql"
  8. "time"
  9. )
  10. // 模板
  11. type DeviceTask struct {
  12. Id int `orm:"column(ID);size(11);auto;pk"`
  13. T_sn string `orm:"size(256);null"` // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机
  14. T_Ut_start time.Time `orm:"type(timestamp);null;"` // 采集时间
  15. T_Ut_end time.Time `orm:"type(timestamp);null;"` // 采集时间
  16. T_Ut time.Time `orm:"type(timestamp);null;"` // 采集时间
  17. T_State int `orm:"size(2);default(1)"`
  18. // 1 start:开始监测任务,
  19. // 2 stop:结束监测任务,
  20. // 3 print:中途打印任务
  21. }
  22. type DeviceTask_R struct {
  23. Id int
  24. T_sn string // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机
  25. T_Ut_start string // 采集时间
  26. T_Ut_end string // 采集时间
  27. T_Ut string // 采集时间
  28. T_State int
  29. }
  30. func DeviceTaskToDeviceTask_R(r DeviceTask) (t DeviceTask_R) {
  31. t.Id = r.Id
  32. t.T_sn = r.T_sn
  33. t.T_State = r.T_State
  34. if !r.T_Ut_start.IsZero() {
  35. t.T_Ut_start = r.T_Ut_start.Format("2006-01-02 15:04:05")
  36. }
  37. if !r.T_Ut_end.IsZero() {
  38. t.T_Ut_end = r.T_Ut_end.Format("2006-01-02 15:04:05")
  39. }
  40. if !r.T_Ut.IsZero() {
  41. t.T_Ut = r.T_Ut.Format("2006-01-02 15:04:05")
  42. }
  43. return
  44. }
  45. func (t *DeviceTask) TableName() string {
  46. return "device_task" // 数据库名称 // ************** 替换 FormulaList **************
  47. }
  48. func init() {
  49. //注册模型
  50. orm.RegisterModel(new(DeviceTask))
  51. }
  52. // 获取全部
  53. func Read_DeviceTask_All_Limit_1(T_sn string) (DeviceTask, bool) {
  54. o := orm.NewOrm()
  55. qs := o.QueryTable(new(DeviceTask))
  56. var r_l []DeviceTask
  57. _, err := qs.Limit(1, 0).Filter("T_sn", T_sn).OrderBy("-ID").All(&r_l)
  58. if err != nil {
  59. logs.Error(lib.FuncName(), err)
  60. return DeviceTask{}, false
  61. }
  62. if len(r_l) > 0 {
  63. return r_l[0], true
  64. }
  65. return DeviceTask{}, false
  66. }
  67. // 获取全部
  68. func Read_DeviceTask_All(T_sn string) (r_l []DeviceTask_R, cnt int64) {
  69. o := orm.NewOrm()
  70. qs := o.QueryTable(new(DeviceTask))
  71. var maps []DeviceTask
  72. cond := orm.NewCondition()
  73. cond1 := cond.And("T_sn", T_sn)
  74. cnt, err := qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&maps)
  75. if err != nil {
  76. logs.Error(lib.FuncName(), err)
  77. return
  78. }
  79. for _, v := range maps {
  80. r_l = append(r_l, DeviceTaskToDeviceTask_R(v))
  81. }
  82. return
  83. }
  84. // 获取全部
  85. func Read_DeviceTask_List(T_sn string, Time_start string, Time_end string, page int, page_z int) (r_l []DeviceTask, cnt int64) {
  86. o := orm.NewOrm()
  87. qs := o.QueryTable(new(DeviceTask))
  88. var offset int64
  89. if page <= 1 {
  90. offset = 0
  91. } else {
  92. offset = int64((page - 1) * page_z)
  93. }
  94. cond := orm.NewCondition()
  95. cond1 := cond.And("T_sn", T_sn)
  96. if len(Time_start) > 1 {
  97. cond1 = cond1.And("T_Ut_start__gte", Time_start)
  98. }
  99. if len(Time_end) > 1 {
  100. cond1 = cond1.And("T_Ut_end__lte", Time_end)
  101. }
  102. _, err := qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r_l)
  103. if err != nil {
  104. logs.Error(lib.FuncName(), err)
  105. return
  106. }
  107. cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
  108. if err != nil {
  109. logs.Error(lib.FuncName(), err)
  110. return
  111. }
  112. return
  113. }