DataController.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. sort := c.GetString("sort")
  119. //log.Println(sort, "排序==========================")
  120. var cnt int64
  121. var pageHelper = lib.PageHelper{}
  122. pageHelper.List, cnt = Device.Read_DeviceData_By_T_snid_List(T_snid, Time_start, Time_end, page, page_z, sort)
  123. pageSize := math.Ceil(float64(cnt) / float64(page_z))
  124. pageHelper.CurrentPage = page
  125. pageHelper.TotalPage = int(pageSize)
  126. pageHelper.TotalCount = int(cnt)
  127. pageHelper.NextPage = page < int(pageSize)
  128. pageHelper.PreviousPage = page > 1
  129. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: pageHelper}
  130. c.ServeJSON()
  131. return
  132. }
  133. // Device_Sensor_List_Delete 删除设备数据
  134. func (c *DataController) Device_Sensor_List_Delete() {
  135. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  136. if !b_ {
  137. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  138. c.ServeJSON()
  139. return
  140. }
  141. var datas = make([]Device.DeviceData_R, 0)
  142. bytes, _ := ioutil.ReadAll(c.Ctx.Request.Body)
  143. fmt.Println("body获取得到数据:", string(bytes))
  144. json.Unmarshal(bytes, &datas)
  145. fmt.Println("jsonUnmarshal获取得到数据:", datas)
  146. Device.DeleteDeviceDataByDeviceDataRList(datas)
  147. c.Data["json"] = lib.JSONS{200, "删除成功!", nil}
  148. c.ServeJSON()
  149. return
  150. }
  151. // Device_Sensor_List_Delete_Time 删除选择时间范围
  152. func (c *DataController) Device_Sensor_List_Delete_Time() {
  153. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  154. if !b_ {
  155. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  156. c.ServeJSON()
  157. return
  158. }
  159. type R_JSONS struct {
  160. T_snid []string `json:"t_sn"`
  161. T_id []string `json:"t_id"`
  162. Time_start string `json:"time_start"`
  163. Time_end string `json:"time_end"`
  164. }
  165. all, _ := io.ReadAll(c.Ctx.Request.Body)
  166. var r_json R_JSONS
  167. err := json.Unmarshal(all, &r_json)
  168. if err != nil {
  169. return
  170. }
  171. var rows sql.Result
  172. var affected int64
  173. for i := 0; i < len(r_json.T_snid); i++ {
  174. rows, err = Device.DeleteDeviceDataByTime(r_json.T_snid[i], r_json.T_id[i], r_json.Time_start, r_json.Time_end)
  175. affected, _ = rows.RowsAffected()
  176. affected += affected
  177. }
  178. if err == nil {
  179. c.Data["json"] = lib.JSONS{Code: 200, Msg: "删除成功", Data: affected}
  180. c.ServeJSON()
  181. }
  182. c.Data["json"] = lib.JSONS{Code: 201, Msg: "删除失败", Data: affected}
  183. c.ServeJSON()
  184. }
  185. // Device_Sensor_Update /Data/Device_Sensor_List_Update 更新设备数据
  186. func (c *DataController) Device_Sensor_Update() {
  187. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  188. if !b_ {
  189. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  190. c.ServeJSON()
  191. return
  192. }
  193. T_sn := c.GetString("t_sn")
  194. T_sp := c.GetString("t_sp")
  195. T_id, _ := c.GetInt("t_id")
  196. T_t, _ := c.GetFloat("t_t")
  197. T_rh, _ := c.GetFloat("t_rh")
  198. T_site := c.GetString("t_site")
  199. CreateTime := c.GetString("create_time")
  200. fieldName := c.GetString("type")
  201. val := c.GetString("value")
  202. i, _ := strconv.ParseInt(T_sp, 10, 64)
  203. data := Device.DeviceData_R{
  204. T_sp: int(i),
  205. T_sn: T_sn,
  206. T_id: T_id,
  207. T_t: float32(T_t),
  208. T_rh: float32(T_rh),
  209. T_site: T_site,
  210. Create_Time: CreateTime,
  211. }
  212. log.Println(T_sn, T_id, T_t, T_rh, T_site, fieldName, val, "修改值")
  213. log.Println(data, "修改值")
  214. err := Device.Update_DeviceSensorData(data, fieldName, val)
  215. if err != nil {
  216. c.Data["json"] = lib.JSONS{
  217. 201, "更新失败", nil,
  218. }
  219. c.ServeJSON()
  220. return
  221. }
  222. c.Data["json"] = lib.JSONS{
  223. 200, "更新成功", nil,
  224. }
  225. c.ServeJSON()
  226. return
  227. }
  228. // Device_Sensor_Record 数据记录
  229. func (c *DataController) Device_Sensor_Record() {
  230. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  231. if !b_ {
  232. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  233. c.ServeJSON()
  234. return
  235. }
  236. sn := c.GetString("sn")
  237. tId := c.GetString("tId")
  238. createTime := c.GetString("createTime")
  239. fmt.Println("时间:", createTime)
  240. list := Device.ReadDeviceOldBySnTid(sn, tId)
  241. c.Data["json"] = lib.JSONS{200, "操作成功", list}
  242. c.ServeJSON()
  243. return
  244. }
  245. // ImportData 数据导入 excel
  246. func (c *DataController) ImportData() {
  247. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  248. if !b_ {
  249. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  250. c.ServeJSON()
  251. return
  252. }
  253. file, header, err := c.GetFile("file")
  254. defer file.Close()
  255. if err != nil {
  256. panic(any(fmt.Sprintf("%s 获取上传文件错误", err.Error())))
  257. }
  258. fmt.Println("文件名称:", header.Filename)
  259. sn := strings.Split(header.Filename, ".")[0]
  260. fmt.Printf("sn:%s\n", sn)
  261. reader, err := excelize.OpenReader(file)
  262. if err != nil {
  263. panic(any(fmt.Sprintf("%s 打开上传文件excel错误", err.Error())))
  264. }
  265. data := Device.ImportDeviceData(reader, sn)
  266. c.Data["json"] = lib.JSONS{200, "操作成功", data}
  267. c.ServeJSON()
  268. return
  269. }
  270. // Device_Sensor_Data_Excel 导出传感器数据列表
  271. func (c *DataController) Device_Sensor_Data_Excel() {
  272. b_, _ := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  273. if !b_ {
  274. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  275. c.ServeJSON()
  276. return
  277. }
  278. T_snid := c.GetString("T_snid")
  279. Time_start := c.GetString("Time_start")
  280. Time_end := c.GetString("Time_end")
  281. if len(T_snid) < 10 {
  282. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  283. c.ServeJSON()
  284. return
  285. }
  286. var DeviceSensor_data []Device.DeviceData_R
  287. DeviceSensor_data, _ = Device.Read_DeviceData_By_T_snid_List(T_snid, Time_start, Time_end, 0, 9999, "DESC")
  288. f := excelize.NewFile() // 设置单元格的值
  289. // 这里设置表头
  290. f.SetCellValue("Sheet1", "A1", "探头编号")
  291. f.SetCellValue("Sheet1", "B1", "温度")
  292. f.SetCellValue("Sheet1", "C1", "湿度℃")
  293. f.SetCellValue("Sheet1", "D1", "坐标")
  294. f.SetCellValue("Sheet1", "E1", "记录时间")
  295. // 设置列宽
  296. f.SetColWidth("Sheet1", "A", "A", 10)
  297. f.SetColWidth("Sheet1", "B", "B", 15)
  298. f.SetColWidth("Sheet1", "C", "D", 20)
  299. f.SetColWidth("Sheet1", "E", "E", 20)
  300. line := 1
  301. headStyleLower, _ := f.NewStyle(
  302. &excelize.Style{
  303. Font: &excelize.Font{Size: 11, Color: "#ff8585", Family: "arial"},
  304. })
  305. headStyleUpper, _ := f.NewStyle(
  306. &excelize.Style{
  307. Font: &excelize.Font{Size: 11, Color: "#ff8585", Family: "arial"},
  308. })
  309. // 定义时间格式
  310. // 循环写入数据
  311. for _, v := range DeviceSensor_data {
  312. line++
  313. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), v.T_id)
  314. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.T_t)
  315. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.T_rh)
  316. //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))
  317. if v.T_t < v.T_tl || v.T_t > v.T_tu {
  318. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("G%d", line), headStyleLower)
  319. }
  320. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_site)
  321. //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))
  322. if v.T_rh < v.T_rhl || v.T_rh > v.T_rhu {
  323. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("G%d", line), headStyleUpper)
  324. }
  325. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_time)
  326. }
  327. //timeStr := time.Now().Format("20060102150405")
  328. filename := strings.Split(T_snid, ",")[0]
  329. // 保存文件
  330. if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  331. fmt.Println(err)
  332. }
  333. url := "/ofile/" + filename + ".xlsx"
  334. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  335. c.ServeJSON()
  336. return
  337. }
  338. // GetCompanyBySn 根据SN查询对应公司
  339. func (c *DataController) GetCompanyBySn() {
  340. b_, admin := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
  341. if !b_ {
  342. c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
  343. c.ServeJSON()
  344. return
  345. }
  346. if admin.T_name == "宝智达-魏公梅" {
  347. sn := c.GetString("sn")
  348. company, err := Device.GetCompanyBySn(sn)
  349. if err != nil {
  350. c.Data["json"] = lib.JSONS{Code: 201, Msg: "查询失败"}
  351. c.ServeJSON()
  352. return
  353. }
  354. log.Println(admin.T_name)
  355. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: company}
  356. c.ServeJSON()
  357. return
  358. }
  359. c.Data["json"] = lib.JSONS{Code: 403, Msg: "无权限"}
  360. c.ServeJSON()
  361. }