DataController.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. package controllers
  2. import (
  3. "ColdP_server/conf"
  4. "ColdP_server/controllers/lib"
  5. "ColdP_server/models/Company"
  6. "ColdP_server/models/Device"
  7. "encoding/json"
  8. "fmt"
  9. beego "github.com/beego/beego/v2/server/web"
  10. "github.com/xuri/excelize/v2"
  11. "io/ioutil"
  12. "math"
  13. "strconv"
  14. "strings"
  15. )
  16. type DataController struct {
  17. beego.Controller
  18. }
  19. func (c *DataController) DataList_html() {
  20. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  21. if !b_ {
  22. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  23. c.ServeJSON()
  24. return
  25. }
  26. // 验证登录
  27. b_, R_u := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  28. if !b_ {
  29. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  30. c.ServeJSON()
  31. return
  32. }
  33. Name := c.GetString("Name")
  34. c.Data["Class_List"] = Company.Read_CompanyClass_All(R_u.T_pid, "")
  35. c.Data["Name"] = Name
  36. c.TplName = "Data/DataList.html"
  37. }
  38. // Device_Sensor_List 传感器列表
  39. func (c *DataController) Device_Sensor_List() {
  40. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  41. if !b_ {
  42. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  43. c.ServeJSON()
  44. return
  45. }
  46. // 验证登录
  47. b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  48. if !b_ {
  49. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  50. c.ServeJSON()
  51. return
  52. }
  53. type R_JSONS struct {
  54. //必须的大写开头
  55. DeviceSensor_lite []Device.DeviceSensor_
  56. Num int
  57. Page int
  58. Page_size int
  59. Pages []lib.Page_T
  60. }
  61. var r_jsons R_JSONS
  62. page, _ := c.GetInt("page")
  63. println(page)
  64. if page < 1 {
  65. page = 1
  66. }
  67. page_z, _ := c.GetInt("page_z")
  68. println(page_z)
  69. if page_z == 0 {
  70. page_z = conf.Page_size
  71. }
  72. T_sn := c.GetString("T_sn")
  73. T_Calss_id, _ := c.GetInt("T_class_id")
  74. T_name := c.GetString("T_name")
  75. SN_type := c.GetString("SN_type")
  76. //c.Data["Class_List"] = Device.Read_Class_All_1()
  77. var cnt int64
  78. fmt.Printf("当前登录用户的PID是:%d\n", admin_r.T_pid)
  79. num, _ := strconv.ParseInt(SN_type, 10, 64)
  80. r_jsons.DeviceSensor_lite, cnt = Device.Read_DeviceSensor_List_T_Class(admin_r.T_pid, T_Calss_id, T_sn, T_name, int(num), page, page_z)
  81. page_size := math.Ceil(float64(cnt) / float64(page_z))
  82. r_jsons.Page = int(page)
  83. r_jsons.Page_size = int(page_size)
  84. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  85. r_jsons.Num = int(cnt)
  86. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  87. c.ServeJSON()
  88. return
  89. }
  90. // Device_Sensor_Data_More 列表 - 接口
  91. func (c *DataController) Device_Sensor_Data_More() {
  92. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  93. if !b_ {
  94. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  95. c.ServeJSON()
  96. return
  97. }
  98. page, _ := c.GetInt("page")
  99. page_z, _ := c.GetInt("page_z")
  100. println(page)
  101. if page < 1 {
  102. page = 1
  103. }
  104. if page_z < 1 {
  105. page_z = conf.Page_size
  106. }
  107. T_snid := c.GetString("T_snid")
  108. Time_start := c.GetString("Time_start")
  109. Time_end := c.GetString("Time_end")
  110. if len(T_snid) < 10 {
  111. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  112. c.ServeJSON()
  113. return
  114. }
  115. var cnt int64
  116. var pageHelper = lib.PageHelper{}
  117. pageHelper.List, cnt = Device.Read_DeviceData_By_T_snid_List(T_snid, Time_start, Time_end, page, page_z)
  118. page_size := math.Ceil(float64(cnt) / float64(page_z))
  119. pageHelper.CurrentPage = page
  120. pageHelper.TotalPage = int(page_size)
  121. pageHelper.TotalCount = int(cnt)
  122. pageHelper.NextPage = page < int(page_size)
  123. pageHelper.PreviousPage = page > 1
  124. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: pageHelper}
  125. c.ServeJSON()
  126. return
  127. }
  128. // Device_Sensor_List_Delete 删除设备数据
  129. func (c *DataController) Device_Sensor_List_Delete() {
  130. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  131. if !b_ {
  132. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  133. c.ServeJSON()
  134. return
  135. }
  136. var datas = make([]Device.DeviceData_R, 0)
  137. bytes, _ := ioutil.ReadAll(c.Ctx.Request.Body)
  138. fmt.Println("body获取得到数据:", string(bytes))
  139. json.Unmarshal(bytes, &datas)
  140. fmt.Println("jsonUnmarshal获取得到数据:", datas)
  141. Device.DeleteDeviceDataByDeviceDataRList(datas)
  142. c.Data["json"] = lib.JSONS{200, "删除成功!", nil}
  143. c.ServeJSON()
  144. return
  145. }
  146. // Device_Sensor_Update /Data/Device_Sensor_List_Update 更新设备数据
  147. func (c *DataController) Device_Sensor_Update() {
  148. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  149. if !b_ {
  150. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  151. c.ServeJSON()
  152. return
  153. }
  154. T_sn := c.GetString("t_sn")
  155. T_id, _ := c.GetInt("t_id")
  156. T_t, _ := c.GetFloat("t_t")
  157. T_rh, _ := c.GetFloat("t_rh")
  158. T_site := c.GetString("t_site")
  159. fieldName := c.GetString("type")
  160. val := c.GetString("value")
  161. data := Device.DeviceData_R{
  162. T_sn: T_sn,
  163. T_id: T_id,
  164. T_t: float32(T_t),
  165. T_rh: float32(T_rh),
  166. T_site: T_site,
  167. }
  168. Device.Update_DeviceSensorData(data, fieldName, val)
  169. c.Data["json"] = lib.JSONS{
  170. 200, "更新成功", nil,
  171. }
  172. c.ServeJSON()
  173. return
  174. }
  175. // Device_Sensor_Record 数据记录
  176. func (c *DataController) Device_Sensor_Record() {
  177. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  178. if !b_ {
  179. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  180. c.ServeJSON()
  181. return
  182. }
  183. sn := c.GetString("sn")
  184. tId := c.GetString("tId")
  185. createTime := c.GetString("createTime")
  186. fmt.Println("时间:", createTime)
  187. list := Device.ReadDeviceOldBySnTid(sn, tId)
  188. c.Data["json"] = lib.JSONS{200, "操作成功", list}
  189. c.ServeJSON()
  190. return
  191. }
  192. // ImportData 数据导入 excel
  193. func (c *DataController) ImportData() {
  194. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  195. if !b_ {
  196. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  197. c.ServeJSON()
  198. return
  199. }
  200. file, header, err := c.GetFile("file")
  201. defer file.Close()
  202. if err != nil {
  203. panic(any(fmt.Sprintf("%s 获取上传文件错误", err.Error())))
  204. }
  205. fmt.Println("文件名称:", header.Filename)
  206. sn := strings.Split(header.Filename, ".")[0]
  207. fmt.Printf("sn:%s\n", sn)
  208. reader, err := excelize.OpenReader(file)
  209. if err != nil {
  210. panic(any(fmt.Sprintf("%s 打开上传文件excel错误", err.Error())))
  211. }
  212. data := Device.ImportDeviceData(reader, sn)
  213. c.Data["json"] = lib.JSONS{200, "操作成功", data}
  214. c.ServeJSON()
  215. return
  216. }
  217. // 导出传感器数据列表
  218. func (c *DataController) Device_Sensor_Data_Excel() {
  219. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  220. if !b_ {
  221. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  222. c.ServeJSON()
  223. return
  224. }
  225. T_snid := c.GetString("T_snid")
  226. Time_start := c.GetString("Time_start")
  227. Time_end := c.GetString("Time_end")
  228. if len(T_snid) < 10 {
  229. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  230. c.ServeJSON()
  231. return
  232. }
  233. var DeviceSensor_data []Device.DeviceData_R
  234. DeviceSensor_data, _ = Device.Read_DeviceData_By_T_snid_List(T_snid, Time_start, Time_end, 0, 9999)
  235. f := excelize.NewFile() // 设置单元格的值
  236. // 这里设置表头
  237. f.SetCellValue("Sheet1", "A1", "探头编号")
  238. f.SetCellValue("Sheet1", "B1", "温度")
  239. f.SetCellValue("Sheet1", "C1", "湿度℃")
  240. f.SetCellValue("Sheet1", "D1", "坐标")
  241. f.SetCellValue("Sheet1", "E1", "记录时间")
  242. // 设置列宽
  243. f.SetColWidth("Sheet1", "A", "A", 10)
  244. f.SetColWidth("Sheet1", "B", "B", 15)
  245. f.SetColWidth("Sheet1", "C", "D", 20)
  246. f.SetColWidth("Sheet1", "E", "E", 20)
  247. line := 1
  248. headStyleLower, _ := f.NewStyle(
  249. &excelize.Style{
  250. Font: &excelize.Font{Size: 11, Color: "#ff8585", Family: "arial"},
  251. })
  252. headStyleUpper, _ := f.NewStyle(
  253. &excelize.Style{
  254. Font: &excelize.Font{Size: 11, Color: "#ff8585", Family: "arial"},
  255. })
  256. // 定义时间格式
  257. // 循环写入数据
  258. for _, v := range DeviceSensor_data {
  259. line++
  260. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), v.T_id)
  261. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.T_t)
  262. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.T_rh)
  263. //f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), strconv.FormatFloat(float64(v.T_tl), 'f', 2, 64)+"~"+strconv.FormatFloat(float64(v.T_tu), 'f', 2, 64))
  264. if v.T_t < v.T_tl || v.T_t > v.T_tu {
  265. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("G%d", line), headStyleLower)
  266. }
  267. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_site)
  268. //f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), strconv.FormatFloat(float64(v.T_rhl), 'f', 2, 64)+"~"+strconv.FormatFloat(float64(v.T_rhu), 'f', 2, 64))
  269. if v.T_rh < v.T_rhl || v.T_rh > v.T_rhu {
  270. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("G%d", line), headStyleUpper)
  271. }
  272. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_time)
  273. }
  274. //timeStr := time.Now().Format("20060102150405")
  275. filename := strings.Split(T_snid, ",")[0]
  276. // 保存文件
  277. if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  278. fmt.Println(err)
  279. }
  280. url := "/ofile/" + filename + ".xlsx"
  281. //// 上传 OSS
  282. //url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx")
  283. //if !is {
  284. // c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  285. // c.ServeJSON()
  286. // return
  287. //}
  288. //删除目录
  289. //err := os.Remove("ofile/" + timeStr + ".xlsx")
  290. //if err != nil {
  291. // fmt.Println(err)
  292. //}
  293. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  294. c.ServeJSON()
  295. return
  296. }