DataController.go 12 KB

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