DataController.go 11 KB

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