Data.go 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293
  1. package controllers
  2. import (
  3. "Cold_Api/Nats/NatsServer"
  4. "Cold_Api/conf"
  5. "Cold_Api/controllers/lib"
  6. "Cold_Api/models/Account"
  7. "Cold_Api/models/Device"
  8. "encoding/base64"
  9. "errors"
  10. "fmt"
  11. "github.com/beego/beego/v2/core/logs"
  12. beego "github.com/beego/beego/v2/server/web"
  13. "github.com/signintech/gopdf"
  14. "github.com/xuri/excelize/v2"
  15. "math"
  16. "os"
  17. "strconv"
  18. "strings"
  19. "time"
  20. )
  21. type DataController struct {
  22. beego.Controller
  23. Admin_r Account.Admin // 登陆的用户
  24. T_pid int // 公司id
  25. }
  26. func (c *DataController) Prepare() {
  27. GetCookie := c.Ctx.GetCookie("User_tokey")
  28. GetString := c.GetString("User_tokey")
  29. User_tokey := GetCookie
  30. if len(User_tokey) == 0 {
  31. User_tokey = GetString
  32. }
  33. if Account.Admin_r == nil {
  34. return
  35. }
  36. c.Admin_r = *Account.Admin_r
  37. T_pid := c.Admin_r.T_pid
  38. EntryPid, _ := Account.Redis_Tokey_T_pid_Get(User_tokey)
  39. if EntryPid > 0 {
  40. T_pid = EntryPid
  41. }
  42. c.T_pid = T_pid
  43. }
  44. // 获取传感器
  45. func (c *DataController) Device_Sensor_Get() {
  46. T_sn := c.GetString("T_sn")
  47. T_id, _ := c.GetInt("T_id")
  48. //c.Data["Class_List"] = Device.Read_Class_All_1()
  49. DeviceSensor_r, err := Device.Read_DeviceSensor_ByTsn_Tid(T_sn, T_id)
  50. if err != nil {
  51. c.Data["json"] = lib.JSONS{Code: 202, Msg: "获取失败!"}
  52. c.ServeJSON()
  53. return
  54. }
  55. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Device.DeviceSensorToDeviceSensor_R(DeviceSensor_r)}
  56. c.ServeJSON()
  57. return
  58. }
  59. // 传感器列表
  60. func (c *DataController) Device_Sensor_List() {
  61. type R_JSONS struct {
  62. //必须的大写开头
  63. DeviceSensor_lite []Device.DeviceSensor_R
  64. Num int
  65. Page int
  66. Page_size int
  67. Pages []lib.Page_T
  68. }
  69. var r_jsons R_JSONS
  70. page, _ := c.GetInt("page")
  71. if page < 1 {
  72. page = 1
  73. }
  74. page_z, _ := c.GetInt("page_z")
  75. if page_z == 0 {
  76. page_z = conf.Page_size
  77. }
  78. T_Class_id, _ := c.GetInt("T_calssid")
  79. T_type, T_type_err := c.GetInt("T_type")
  80. if T_type_err != nil {
  81. T_type = -1
  82. }
  83. T_name := c.GetString("T_name")
  84. // 数据展示页面
  85. T_RealTime, T_RealTime_err := c.GetInt("T_RealTime")
  86. if T_RealTime_err != nil {
  87. T_RealTime = 0
  88. }
  89. // 轨迹展示页面
  90. T_MapShow, T_MapShow_err := c.GetInt("T_MapShow")
  91. if T_MapShow_err != nil {
  92. T_MapShow = 0
  93. }
  94. T_abandon, _ := c.GetInt("T_abandon")
  95. var cnt int64
  96. //Device.Read_DeviceSensorParameter_All_Map("", 0)
  97. bindSN, err := Account.Read_UserDevice_List(c.Admin_r)
  98. if err != nil {
  99. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  100. c.ServeJSON()
  101. return
  102. }
  103. r_jsons.DeviceSensor_lite, cnt = Device.Read_DeviceSensor_List_For_Data(bindSN, c.T_pid, T_name, T_Class_id, T_type, T_RealTime, T_MapShow, T_abandon, page, page_z)
  104. page_size := math.Ceil(float64(cnt) / float64(page_z))
  105. r_jsons.Page = page
  106. r_jsons.Page_size = int(page_size)
  107. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  108. r_jsons.Num = int(cnt)
  109. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  110. c.ServeJSON()
  111. return
  112. }
  113. func (c *DataController) Company_Device_Sensor_List() {
  114. var r_jsons lib.R_JSONS
  115. page, _ := c.GetInt("page")
  116. if page < 1 {
  117. page = 1
  118. }
  119. page_z, _ := c.GetInt("page_z")
  120. if page_z == 0 {
  121. page_z = conf.Page_size
  122. }
  123. T_Class_id, _ := c.GetInt("T_calssid")
  124. T_type, T_type_err := c.GetInt("T_type")
  125. if T_type_err != nil {
  126. T_type = -1
  127. }
  128. T_name := c.GetString("T_name")
  129. // 数据展示页面
  130. T_RealTime, T_RealTime_err := c.GetInt("T_RealTime")
  131. if T_RealTime_err != nil {
  132. T_RealTime = 0
  133. }
  134. // 轨迹展示页面
  135. T_MapShow, T_MapShow_err := c.GetInt("T_MapShow")
  136. if T_MapShow_err != nil {
  137. T_MapShow = 0
  138. }
  139. // 查询公司
  140. Company_r, err := Account.Read_Company_ById(c.T_pid)
  141. if err != nil {
  142. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  143. c.ServeJSON()
  144. return
  145. }
  146. // 查询公司下面所有子公司id
  147. T_pids := Account.ReadCompanyIds_T_path(Company_r.T_path)
  148. var cnt int64
  149. Account.Read_Company_All_Maps()
  150. r_jsons.Data, cnt = Device.Read_CompanyDeviceSensor_List_For_Data(T_pids, T_name, T_Class_id, T_type, T_RealTime, T_MapShow, page, page_z)
  151. page_size := math.Ceil(float64(cnt) / float64(page_z))
  152. r_jsons.Page = page
  153. r_jsons.Page_size = int(page_size)
  154. r_jsons.Num = cnt
  155. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  156. c.ServeJSON()
  157. return
  158. }
  159. func (c *DataController) Company_Device_Sensor_List_ByKey() {
  160. var r_jsons lib.R_JSONS
  161. key := c.GetString("key")
  162. T_sn := c.GetString("T_sn")
  163. // 查询公司
  164. Company_r, err := Account.Read_Company_ByKey(key)
  165. if err != nil {
  166. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
  167. c.ServeJSON()
  168. return
  169. }
  170. // 查询公司下面所有子公司id
  171. T_pids := Account.ReadCompanyIds_T_path(Company_r.T_path)
  172. Account.Read_Company_All_Maps()
  173. r_jsons.Data, r_jsons.Num = Device.Read_CompanyDeviceSensor_List_For_Data_ByKey(T_pids, T_sn)
  174. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  175. c.ServeJSON()
  176. return
  177. }
  178. // 传感器数据列表
  179. func (c *DataController) Device_Sensor_Data() {
  180. page, _ := c.GetInt("page")
  181. page_z, _ := c.GetInt("page_z")
  182. T_id, _ := c.GetInt("T_id")
  183. SN := c.GetString("T_sn")
  184. Time_start := c.GetString("Time_start")
  185. Time_end := c.GetString("Time_end")
  186. type R_JSONS struct {
  187. //必须的大写开头
  188. DeviceSensor_data []Device.DeviceData_R
  189. Num int
  190. Page int
  191. Page_size int
  192. Pages []lib.Page_T
  193. }
  194. var r_jsons R_JSONS
  195. var cnt int
  196. Device.Read_DeviceSensorParameter_All_Map(SN, T_id)
  197. r_jsons.DeviceSensor_data, cnt = Device.Read_DeviceData_ById_List(SN, T_id, Time_start, Time_end, page, page_z)
  198. page_size := math.Ceil(float64(cnt) / float64(page_z))
  199. r_jsons.Page = page
  200. r_jsons.Page_size = int(page_size)
  201. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  202. r_jsons.Num = cnt
  203. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  204. c.ServeJSON()
  205. return
  206. }
  207. // 实时轨迹
  208. func (c *DataController) Device_Sensor_Data_Real() {
  209. page, _ := c.GetInt("page")
  210. page_z, _ := c.GetInt("page_z")
  211. T_id, _ := c.GetInt("T_id")
  212. T_sn := c.GetString("T_sn")
  213. TaskList, num := Device.Read_DeviceTask_All(T_sn)
  214. if num == 0 {
  215. c.Data["json"] = lib.JSONS{Code: 202, Msg: "暂时没有轨迹信息"}
  216. c.ServeJSON()
  217. return
  218. }
  219. Time_start := TaskList[0].T_Ut_start
  220. Time_end := TaskList[0].T_Ut_end
  221. type R_JSONS struct {
  222. //必须的大写开头
  223. DeviceSensor_data []Device.DeviceData_R
  224. Num int
  225. Page int
  226. Page_size int
  227. Pages []lib.Page_T
  228. }
  229. var r_jsons R_JSONS
  230. var cnt int
  231. Device.Read_DeviceSensorParameter_All_Map(T_sn, T_id)
  232. r_jsons.DeviceSensor_data, cnt = Device.Read_DeviceData_ById_List(T_sn, T_id, Time_start, Time_end, page, page_z)
  233. page_size := math.Ceil(float64(cnt) / float64(page_z))
  234. r_jsons.Page = page
  235. r_jsons.Page_size = int(page_size)
  236. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  237. r_jsons.Num = cnt
  238. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  239. c.ServeJSON()
  240. return
  241. }
  242. // 设备数据
  243. func (c *DataController) Device_Sensor_Data_More() {
  244. page, _ := c.GetInt("page")
  245. if page < 1 {
  246. page = 1
  247. }
  248. page_z, _ := c.GetInt("page_z")
  249. if page_z < 1 {
  250. page_z = conf.Page_size
  251. }
  252. T_snid := c.GetString("T_snid")
  253. Time_start := c.GetString("Time_start")
  254. Time_end := c.GetString("Time_end")
  255. if len(T_snid) < 10 {
  256. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  257. c.ServeJSON()
  258. return
  259. }
  260. type R_JSONS struct {
  261. //必须的大写开头
  262. Data []Device.DeviceData_R
  263. Num int64
  264. Page int
  265. Page_size int
  266. }
  267. var r_jsons R_JSONS
  268. r_jsons.Data, r_jsons.Num = Device.Read_DeviceData_By_T_snid_List(T_snid, Time_start, Time_end, page, page_z)
  269. r_jsons.Page = page
  270. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  271. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  272. c.ServeJSON()
  273. return
  274. }
  275. // -------------------------------------
  276. // 导出传感器数据列表
  277. func (c *DataController) Device_Sensor_Data_Excel() {
  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)
  288. // 按照传感器T_sort排序
  289. //var snids string
  290. //dsList := Device.Read_DeviceSensorList_By_T_snid(T_snid)
  291. //// 创建名称到权重的映射
  292. //orderMap := make(map[int]int)
  293. //for i, v := range dsList {
  294. // snids += fmt.Sprintf("%s,%d|", v.T_sn, v.T_id)
  295. // orderMap[v.T_id] = i
  296. //}
  297. //sort.Slice(DeviceSensor_data, func(i, j int) bool {
  298. // if DeviceSensor_data[i].T_time == DeviceSensor_data[j].T_time {
  299. // // 如果时间相同,则按预设顺序排序
  300. // return orderMap[DeviceSensor_data[i].T_id] < orderMap[DeviceSensor_data[j].T_id]
  301. // }
  302. // return DeviceSensor_data[i].T_time > DeviceSensor_data[j].T_time
  303. //})
  304. f := excelize.NewFile() // 设置单元格的值
  305. // 这里设置表头
  306. f.SetCellValue("Sheet1", "A1", "编号")
  307. f.SetCellValue("Sheet1", "B1", "传感器名称")
  308. f.SetCellValue("Sheet1", "C1", "温度℃")
  309. f.SetCellValue("Sheet1", "D1", "湿度%")
  310. f.SetCellValue("Sheet1", "E1", "温度范围 ℃")
  311. f.SetCellValue("Sheet1", "F1", "湿度范围 %")
  312. f.SetCellValue("Sheet1", "G1", "记录时间")
  313. f.SetCellValue("Sheet1", "H1", "备注")
  314. // 设置列宽
  315. f.SetColWidth("Sheet1", "A", "A", 10)
  316. f.SetColWidth("Sheet1", "B", "B", 15)
  317. f.SetColWidth("Sheet1", "C", "D", 10)
  318. f.SetColWidth("Sheet1", "E", "F", 15)
  319. f.SetColWidth("Sheet1", "G", "G", 22)
  320. f.SetColWidth("Sheet1", "H", "H", 22)
  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. for _, v := range DeviceSensor_data {
  332. line++
  333. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), line-1)
  334. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.T_name+"["+strconv.Itoa(v.T_id)+"]")
  335. if v.T_ist == 1 {
  336. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.T_t)
  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) && v.T_free == 0 {
  339. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("G%d", line), headStyleLower)
  340. }
  341. }
  342. if v.T_ish == 1 {
  343. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_rh)
  344. 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))
  345. if v.T_rh < v.T_rhl || v.T_rh > v.T_rhu && v.T_free == 0 {
  346. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("G%d", line), headStyleUpper)
  347. }
  348. }
  349. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.T_time)
  350. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), v.T_remark)
  351. //if v.T_ist == 1 && (v.T_t < v.T_tl || v.T_t > v.T_tu) {
  352. // f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("G%d", line), headStyleLower)
  353. //}
  354. //if v.T_ish == 1 && (v.T_rh < v.T_rhl || v.T_rh > v.T_rhu) {
  355. // f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("G%d", line), headStyleUpper)
  356. //}
  357. }
  358. timeStr := time.Now().Format("20060102150405")
  359. // 保存文件
  360. if err := f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil {
  361. logs.Error(err)
  362. }
  363. // 上传 OSS
  364. url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx")
  365. if !is {
  366. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  367. c.ServeJSON()
  368. return
  369. }
  370. //删除目录
  371. err := os.Remove("ofile/" + timeStr + ".xlsx")
  372. if err != nil {
  373. logs.Error(err)
  374. }
  375. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  376. c.ServeJSON()
  377. return
  378. }
  379. // 列表 - 接口
  380. func (c *DataController) Device_Sensor_Data_Excel_m() {
  381. T_snid := c.GetString("T_snid")
  382. Time_start := c.GetString("Time_start")
  383. Time_end := c.GetString("Time_end")
  384. t_x, _ := c.GetInt("t_x")
  385. if len(T_snid) < 10 {
  386. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  387. c.ServeJSON()
  388. return
  389. }
  390. var DeviceSensor_data []Device.DeviceData_R
  391. DeviceSensor_data, _ = Device.Read_DeviceData_By_T_snid_List(T_snid, Time_start, Time_end, 0, 9999)
  392. f := excelize.NewFile() // 设置单元格的值
  393. // 这里设置表头
  394. f.SetCellValue("Sheet1", "A1", "编号")
  395. f.SetCellValue("Sheet1", "B1", "传感器名称")
  396. f.SetCellValue("Sheet1", "C1", "温度℃")
  397. f.SetCellValue("Sheet1", "D1", "湿度%")
  398. f.SetCellValue("Sheet1", "E1", "温度范围 ℃")
  399. f.SetCellValue("Sheet1", "F1", "湿度范围 %")
  400. f.SetCellValue("Sheet1", "G1", "记录时间")
  401. // 设置列宽
  402. f.SetColWidth("Sheet1", "A", "A", 10)
  403. f.SetColWidth("Sheet1", "B", "B", 15)
  404. f.SetColWidth("Sheet1", "C", "D", 10)
  405. f.SetColWidth("Sheet1", "E", "F", 15)
  406. f.SetColWidth("Sheet1", "G", "G", 22)
  407. line := 1
  408. headStyleLower, _ := f.NewStyle(
  409. &excelize.Style{
  410. Font: &excelize.Font{Size: 11, Color: "#a8f7ff", Family: "arial"},
  411. })
  412. headStyleUpper, _ := f.NewStyle(
  413. &excelize.Style{
  414. Font: &excelize.Font{Size: 11, Color: "#ff8585", Family: "arial"},
  415. })
  416. atime, _ := time.ParseDuration("-1m") // Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
  417. // 循环写入数据
  418. for v_i, v := range DeviceSensor_data {
  419. line++
  420. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), line-1)
  421. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.T_name+"["+strconv.Itoa(v.T_id)+"]")
  422. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.T_t)
  423. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_rh)
  424. 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))
  425. 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))
  426. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.T_time)
  427. if v.T_t < v.T_tl || v.T_t > v.T_tu {
  428. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("G%d", line), headStyleLower)
  429. }
  430. if v.T_rh < v.T_rhl || v.T_rh > v.T_rhu {
  431. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("G%d", line), headStyleUpper)
  432. }
  433. if len(DeviceSensor_data)-1 == v_i {
  434. break
  435. }
  436. cha_v := ((DeviceSensor_data[v_i+1].T_t - v.T_t) / 3)
  437. println("t_x:", t_x)
  438. for i := 0; i < t_x; i++ {
  439. println("// -------", i)
  440. v.T_t = float32(lib.Decimal(float64(v.T_t + cha_v)))
  441. fTime, _ := time.Parse("2006-01-02 15:04:05", v.T_time)
  442. fTime = fTime.Add(atime)
  443. v.T_time = fTime.Format("2006-01-02 15:04:05")
  444. line++
  445. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), line-1)
  446. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.T_name+"["+strconv.Itoa(v.T_id)+"]")
  447. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.T_t)
  448. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_rh)
  449. 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))
  450. 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))
  451. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.T_time)
  452. if v.T_t < v.T_tl || v.T_t > v.T_tu {
  453. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("G%d", line), headStyleLower)
  454. }
  455. if v.T_rh < v.T_rhl || v.T_rh > v.T_rhu {
  456. f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("G%d", line), headStyleUpper)
  457. }
  458. }
  459. }
  460. timeStr := time.Now().Format("20060102150405")
  461. // 保存文件
  462. if err := f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil {
  463. logs.Error(err)
  464. }
  465. // 上传 OSS
  466. url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx")
  467. if !is {
  468. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  469. c.ServeJSON()
  470. return
  471. }
  472. //删除目录
  473. //err := os.Remove("ofile/" + timeStr + ".xlsx")
  474. //if err != nil {
  475. // logs.Error(err)
  476. //}
  477. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  478. c.ServeJSON()
  479. return
  480. }
  481. // 小程序 - 打印
  482. func (c *DataController) DeviceSensor_Data_Print() {
  483. SN := c.GetString("T_sn")
  484. Time_start := c.GetString("Time_start")
  485. Time_end := c.GetString("Time_end")
  486. type R_JSONS struct {
  487. //必须的大写开头
  488. T_time string
  489. T1 interface{}
  490. T2 interface{}
  491. T3 interface{}
  492. T4 interface{}
  493. }
  494. r_jsons := make(map[string]R_JSONS)
  495. type JSONS struct {
  496. //必须的大写开头
  497. Code int16
  498. Msg string
  499. DeviceSensorNum int
  500. DeviceSensor Device.DeviceSensor_Applet
  501. AdminNane string
  502. Data interface{} // 泛型
  503. }
  504. GROUP_BY_t_time := Device.Read_DeviceData_List_GROUP_BY_t_time(SN, Time_start, Time_end)
  505. // 时间 MAP
  506. for _, vt := range GROUP_BY_t_time {
  507. stamp, _ := time.Parse("2006-1-02 15:04:05", lib.To_string(vt[0]))
  508. T_time := stamp.Format("2006-01-02 15:04:05")
  509. r_jsons[T_time] = R_JSONS{T_time: T_time, T1: nil, T2: nil, T3: nil, T4: nil}
  510. }
  511. dsList := Device.Read_DeviceSensor_List_T_sn_T_datashow(SN)
  512. if len(dsList) == 0 {
  513. c.Data["json"] = JSONS{Code: 200, Msg: "ok!", DeviceSensorNum: len(dsList), Data: r_jsons}
  514. c.ServeJSON()
  515. return
  516. }
  517. company, _ := Account.Read_Company_ById(c.T_pid)
  518. for i, v := range dsList {
  519. if i >= 4 {
  520. break
  521. }
  522. // 记录传感器
  523. maps, num := Device.Read_DeviceData_ById_List(SN, v.T_id, Time_start, Time_end, 1, 9999)
  524. if num > 0 {
  525. for _, v_map := range maps {
  526. ws, ok := r_jsons[v_map.T_time]
  527. if ok {
  528. switch i {
  529. case 0:
  530. ws.T1 = v_map
  531. case 1:
  532. ws.T2 = v_map
  533. case 2:
  534. ws.T3 = v_map
  535. case 3:
  536. ws.T4 = v_map
  537. }
  538. r_jsons[v_map.T_time] = ws
  539. }
  540. }
  541. }
  542. }
  543. c.Data["json"] = JSONS{Code: 200, Msg: "ok!", DeviceSensorNum: len(dsList), DeviceSensor: Device.DeviceSensorToDeviceSensor_Applet(dsList[0]), AdminNane: company.T_name, Data: r_jsons}
  544. c.ServeJSON()
  545. return
  546. }
  547. // 列表 - 接口
  548. func (c *DataController) Device_Sensor_Data_PDF() {
  549. T_snid := c.GetString("T_snid")
  550. Time_start := c.GetString("Time_start")
  551. Time_end := c.GetString("Time_end")
  552. T_ish, _ := c.GetInt("T_ish") // 展示湿度
  553. if len(T_snid) < 10 {
  554. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_snid Err!"}
  555. c.ServeJSON()
  556. return
  557. }
  558. //T_ist // 温度 1开启 2关闭
  559. //T_ish // 湿度 1开启 2关闭
  560. T_ist := 1
  561. //T_ist_list := make([]int, 0)
  562. //T_ish_list := make([]int, 0)
  563. //T_snid_list := strings.Split(strings.Trim(T_snid, "|"), "|")
  564. //for _, v := range T_snid_list {
  565. // sn_id := strings.Split(v, ",")
  566. // if len(sn_id) == 2 {
  567. // device, _ := Device.Read_Device_ByT_sn(sn_id[0])
  568. // T_ist_list = append(T_ist_list, device.T_ist)
  569. // T_ish_list = append(T_ish_list, device.T_ish)
  570. // }
  571. //}
  572. //istHasOne := false
  573. //istHasTwo := false
  574. //
  575. //for _, v := range T_ist_list {
  576. // if v == 1 {
  577. // istHasOne = true
  578. // }
  579. // if v == 2 {
  580. // istHasTwo = true
  581. // }
  582. //}
  583. //// 全部都为2 则不显示温度
  584. //if !istHasOne && istHasTwo {
  585. // T_ist = 2
  586. //}
  587. //
  588. //ishHasOne := false
  589. //ishHasTwo := false
  590. //T_ish := 1
  591. //for _, v := range T_ish_list {
  592. // if v == 1 {
  593. // ishHasOne = true
  594. // }
  595. // if v == 2 {
  596. // ishHasTwo = true
  597. // }
  598. //}
  599. //// 全部都为2 则不显示湿度
  600. //if !ishHasOne && ishHasTwo {
  601. // T_ish = 2
  602. //}
  603. var DeviceSensor_data []Device.DeviceData_R
  604. DeviceSensor_data, _ = Device.Read_DeviceData_By_T_snid_List(T_snid, Time_start, Time_end, 0, 9999)
  605. // 按照传感器T_sort排序
  606. //var snids string
  607. //dsList := Device.Read_DeviceSensorList_By_T_snid(T_snid)
  608. //// 创建名称到权重的映射
  609. //orderMap := make(map[int]int)
  610. //for i, v := range dsList {
  611. // snids += fmt.Sprintf("%s,%d|", v.T_sn, v.T_id)
  612. // orderMap[v.T_id] = i
  613. //}
  614. //sort.Slice(DeviceSensor_data, func(i, j int) bool {
  615. // if DeviceSensor_data[i].T_time == DeviceSensor_data[j].T_time {
  616. // // 如果时间相同,则按预设顺序排序
  617. // return orderMap[DeviceSensor_data[i].T_id] < orderMap[DeviceSensor_data[j].T_id]
  618. // }
  619. // return DeviceSensor_data[i].T_time > DeviceSensor_data[j].T_time
  620. //})
  621. // -------------------获取最高温湿度、最低温湿度、平均温湿度
  622. // 最高温度、最低温度
  623. var maxTemp, minTemp, maxRH, minRH float32
  624. // 最高温度时间、最低温度时间
  625. var maxTempTime, minTempTime, maxRHTime, minRHTime string
  626. // 总温度
  627. var totalTemp, totalRH float32
  628. // 平均温度
  629. var avgTemp, avgRH float32
  630. var TempCount, RHCount int
  631. if len(DeviceSensor_data) > 0 {
  632. // 最高温度及时刻
  633. firstTempFlag := false
  634. //var maxTemp,minTemp float32
  635. //var maxTempTime,minTempTime string
  636. //maxTemp = DeviceSensor_data[0].T_t
  637. //maxTempTime = DeviceSensor_data[0].T_time
  638. //// 最低温度及时刻
  639. //minTemp = DeviceSensor_data[0].T_t
  640. //minTempTime = DeviceSensor_data[0].T_time
  641. // 最高湿度度及时刻
  642. firstRHFlag := false
  643. //maxRH = DeviceSensor_data[0].T_rh
  644. //maxRHTime = DeviceSensor_data[0].T_time
  645. //// 最低湿度及时刻
  646. //minRH = DeviceSensor_data[0].T_rh
  647. //minRHTime = DeviceSensor_data[0].T_time
  648. for _, data := range DeviceSensor_data {
  649. if data.T_ist == 1 {
  650. if firstTempFlag {
  651. if data.T_t > maxTemp {
  652. maxTemp = data.T_t
  653. maxTempTime = data.T_time
  654. }
  655. if data.T_t < minTemp {
  656. minTemp = data.T_t
  657. minTempTime = data.T_time
  658. }
  659. totalTemp += data.T_t
  660. TempCount++
  661. } else {
  662. maxTemp = data.T_t
  663. maxTempTime = data.T_time
  664. // 最低温度及时刻
  665. minTemp = data.T_t
  666. minTempTime = data.T_time
  667. firstTempFlag = true
  668. totalTemp += data.T_t
  669. TempCount = 1
  670. }
  671. }
  672. if data.T_ish == 1 {
  673. if firstRHFlag {
  674. if data.T_rh > maxRH {
  675. maxRH = data.T_rh
  676. maxRHTime = data.T_time
  677. }
  678. if data.T_rh < minRH {
  679. minRH = data.T_rh
  680. minRHTime = data.T_time
  681. }
  682. totalRH += data.T_rh
  683. RHCount++
  684. } else {
  685. maxRH = data.T_rh
  686. maxRHTime = data.T_time
  687. // 最低温度及时刻
  688. minRH = data.T_rh
  689. minRHTime = data.T_time
  690. firstRHFlag = true
  691. totalRH += data.T_rh
  692. RHCount = 1
  693. }
  694. }
  695. }
  696. if TempCount > 0 {
  697. // 平均温度
  698. avgTemp = totalTemp / float32(TempCount)
  699. }
  700. // 平均湿度
  701. if RHCount > 0 {
  702. avgRH = totalRH / float32(RHCount)
  703. }
  704. }
  705. // -------------------获取最高温湿度、最低温湿度、平均温湿度结束
  706. maxTempTime = maxTempTime
  707. minTempTime = minTempTime
  708. maxRHTime = maxRHTime
  709. minRHTime = minRHTime
  710. var err error
  711. pdf := &gopdf.GoPdf{}
  712. pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4
  713. //err = GetFont(pdf, "LiberationSerif-Regular.ttf")
  714. //if err != nil {
  715. // log.Fatalln(err)
  716. //}
  717. //err = pdf.SetFont("Ubuntu-L", "", 14)
  718. //if err != nil {
  719. // log.Fatalln(err)
  720. //}
  721. err = pdf.AddTTFFont("simsun", "static/fonts/MiSans-Medium.ttf")
  722. if err != nil {
  723. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  724. c.ServeJSON()
  725. return
  726. }
  727. err = pdf.SetFont("simsun", "", 24)
  728. if err != nil {
  729. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  730. c.ServeJSON()
  731. return
  732. }
  733. pdf.SetGrayFill(0.5)
  734. pdf.SetMargins(0, 20, 0, 20)
  735. pdf.AddPage()
  736. company, _ := Account.Read_Company_ById(c.T_pid)
  737. textw, _ := pdf.MeasureTextWidth(company.T_name)
  738. pdf.SetX((595 / 2) - (textw / 2))
  739. pdf.SetY(40)
  740. pdf.Text(company.T_name)
  741. // 线
  742. pdf.SetLineWidth(2)
  743. pdf.SetLineType("dashed")
  744. pdf.Line(10, 60, 585, 60)
  745. err = pdf.AddTTFFont("wts", "static/fonts/MiSans-Medium.ttf")
  746. if err != nil {
  747. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  748. c.ServeJSON()
  749. return
  750. }
  751. err = pdf.SetFont("wts", "", 12)
  752. if err != nil {
  753. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  754. c.ServeJSON()
  755. return
  756. }
  757. var s_time, e_time string
  758. if len(DeviceSensor_data) > 0 {
  759. s_time = fmt.Sprintf("%s", DeviceSensor_data[0].T_time)
  760. e_time = fmt.Sprintf("%s", DeviceSensor_data[len(DeviceSensor_data)-1].T_time)
  761. }
  762. lib.RectFillColor(pdf, "历史数据["+e_time+" / "+s_time+"]", 14, 22, 80, 550, 40, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  763. var y float64 = 120
  764. if T_ist == 1 {
  765. lib.RectFillColor(pdf, fmt.Sprintf("最高温度: %.1f ", maxTemp), 12, 22, y, 183, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  766. lib.RectFillColor(pdf, fmt.Sprintf("最低温度: %.1f ", minTemp), 12, 205, y, 183, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  767. lib.RectFillColor(pdf, fmt.Sprintf("平均温度: %.1f ", avgTemp), 12, 388, y, 184, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  768. y += 20
  769. }
  770. if T_ish == 1 {
  771. lib.RectFillColor(pdf, fmt.Sprintf("最高湿度: %.1f ", maxRH), 12, 22, y, 183, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  772. lib.RectFillColor(pdf, fmt.Sprintf("最低湿度: %.1f ", minRH), 12, 205, y, 183, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  773. lib.RectFillColor(pdf, fmt.Sprintf("平均湿度: %.1f ", avgRH), 12, 388, y, 184, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  774. y += 20
  775. }
  776. y += 5
  777. lib.RectFillColor(pdf, "序号", 12, 22, y, 30, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  778. if T_ish == 1 {
  779. lib.RectFillColor(pdf, "传感器名称", 12, 52, y, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  780. lib.RectFillColor(pdf, "温度℃", 12, 152, y, 50, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  781. lib.RectFillColor(pdf, "湿度%", 12, 202, y, 50, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  782. }
  783. if T_ish == 0 {
  784. lib.RectFillColor(pdf, "传感器名称", 12, 52, y, 140, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  785. lib.RectFillColor(pdf, "温度℃", 12, 192, y, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  786. }
  787. if T_ish == 1 {
  788. lib.RectFillColor(pdf, "温度范围", 12, 252, y, 80, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  789. lib.RectFillColor(pdf, "湿度范围", 12, 332, y, 80, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  790. }
  791. if T_ish == 0 {
  792. lib.RectFillColor(pdf, "温度范围", 12, 292, y, 120, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  793. }
  794. lib.RectFillColor(pdf, "记录时间", 12, 412, y, 120, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  795. lib.RectFillColor(pdf, "备注", 12, 532, y, 40, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  796. y += 20
  797. err = pdf.SetFont("wts", "", 10)
  798. if err != nil {
  799. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  800. c.ServeJSON()
  801. return
  802. }
  803. for i, v := range DeviceSensor_data {
  804. text := fmt.Sprintf(" %d ", i+1)
  805. var textH float64 = 25 // if text height is 25px.
  806. pdf.SetNewY(y, textH)
  807. y = pdf.GetY()
  808. // newY = 20
  809. if y < 30 {
  810. lib.RectFillColor(pdf, "序号", 12, 22, y, 30, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  811. if T_ish == 1 {
  812. lib.RectFillColor(pdf, "传感器名称", 12, 52, y, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  813. lib.RectFillColor(pdf, "温度℃", 12, 152, y, 50, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  814. lib.RectFillColor(pdf, "湿度%", 12, 202, y, 50, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  815. }
  816. if T_ish == 0 {
  817. lib.RectFillColor(pdf, "传感器名称", 12, 52, y, 140, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  818. lib.RectFillColor(pdf, "温度℃", 12, 192, y, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  819. }
  820. if T_ish == 1 {
  821. lib.RectFillColor(pdf, "温度范围", 12, 252, y, 80, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  822. lib.RectFillColor(pdf, "湿度范围", 12, 332, y, 80, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  823. }
  824. if T_ish == 0 {
  825. lib.RectFillColor(pdf, "温度范围", 12, 292, y, 120, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  826. }
  827. lib.RectFillColor(pdf, "记录时间", 12, 412, y, 120, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  828. lib.RectFillColor(pdf, "备注", 12, 532, y, 40, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  829. y += 20
  830. }
  831. //pdf.SetX(x) // must after pdf.SetNewY() called.
  832. //err = pdf.Text(text)
  833. //if err != nil {
  834. // log.Fatalln(err)
  835. //}
  836. T_t := fmt.Sprintf(" %.1f ", v.T_t)
  837. T_rh := fmt.Sprintf(" %.1f ", v.T_rh)
  838. T_Tlu := fmt.Sprintf(" %.1f ~ %.1f ", v.T_tl, v.T_tu)
  839. T_Rlu := fmt.Sprintf(" %.1f ~ %.1f ", v.T_rhl, v.T_rhu)
  840. T_time := fmt.Sprintf("%s", v.T_time)
  841. //if v.T_ist == 1 {
  842. // lib.RectFillColor(pdf, T_t, 10, 152, y, 50, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  843. // lib.RectFillColor(pdf, T_Tlu, 10, 252, y, 80, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  844. //} else {
  845. // lib.RectFillColor(pdf, "", 10, 152, y, 50, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  846. // lib.RectFillColor(pdf, "", 10, 252, y, 80, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  847. //}
  848. //if v.T_ish == 1 {
  849. // lib.RectFillColor(pdf, T_rh, 10, 202, y, 50, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  850. // lib.RectFillColor(pdf, T_Rlu, 10, 332, y, 80, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  851. //} else {
  852. // lib.RectFillColor(pdf, "", 10, 202, y, 50, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  853. // lib.RectFillColor(pdf, "", 10, 332, y, 80, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  854. //}
  855. if T_ish == 1 {
  856. lib.RectFillColor(pdf, T_t, 10, 152, y, 50, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  857. lib.RectFillColor(pdf, T_Tlu, 10, 252, y, 80, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  858. lib.RectFillColor(pdf, T_rh, 10, 202, y, 50, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  859. lib.RectFillColor(pdf, T_Rlu, 10, 332, y, 80, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  860. lib.RectFillColor(pdf, v.T_name, 10, 52, y, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  861. }
  862. if T_ish == 0 {
  863. lib.RectFillColor(pdf, v.T_name, 10, 52, y, 140, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  864. lib.RectFillColor(pdf, T_t, 10, 192, y, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  865. lib.RectFillColor(pdf, T_Tlu, 10, 292, y, 120, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  866. }
  867. lib.RectFillColor(pdf, text, 10, 22, y, 30, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  868. lib.RectFillColor(pdf, T_time, 10, 412, y, 120, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  869. lib.RectFillColor(pdf, v.T_remark, 10, 532, y, 40, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  870. y += 20
  871. }
  872. timeStr := "ofile/" + time.Now().Format("20060102150405") + ".pdf"
  873. err = pdf.WritePdf(timeStr)
  874. if err != nil {
  875. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  876. c.ServeJSON()
  877. return
  878. }
  879. // 上传 OSS
  880. url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/"+timeStr, timeStr)
  881. if !is {
  882. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  883. c.ServeJSON()
  884. return
  885. }
  886. //删除目录
  887. err = os.Remove(timeStr)
  888. if err != nil {
  889. logs.Error(err)
  890. }
  891. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  892. c.ServeJSON()
  893. return
  894. }
  895. func (c *DataController) Device_Sensor_Data_ChartShow_PDF() {
  896. T_snid := c.GetString("T_snid")
  897. Time_start := c.GetString("Time_start")
  898. Time_end := c.GetString("Time_end")
  899. T_forwarding_unit := c.GetString("T_forwarding_unit")
  900. T_consignee_unit := c.GetString("T_consignee_unit")
  901. T_remark := c.GetString("T_remark")
  902. T_temp_show, _ := c.GetInt("T_temp_show")
  903. T_Humidity_show, _ := c.GetInt("T_Humidity_show")
  904. T_template := c.GetString("T_template")
  905. if T_temp_show == 0 && T_Humidity_show == 0 {
  906. c.Data["json"] = lib.JSONS{Code: 202, Msg: "温湿度不能同时不显示"}
  907. c.ServeJSON()
  908. return
  909. }
  910. if len(T_snid) < 10 {
  911. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_snid Err!"}
  912. c.ServeJSON()
  913. return
  914. }
  915. // 获取图片信息
  916. base64String := c.GetString("file")
  917. imageBytes, err := base64.StdEncoding.DecodeString(base64String)
  918. if err != nil {
  919. c.Data["json"] = lib.JSONS{Code: 202, Msg: "解析base64失败"}
  920. c.ServeJSON()
  921. return
  922. }
  923. company, _ := Account.Read_Company_ById(c.T_pid)
  924. if T_template == "1" {
  925. url, err := Device_Sensor_Data_ChartShow_PDF1(company.T_name, T_snid, Time_start, Time_end, T_forwarding_unit, T_consignee_unit, T_remark, imageBytes, T_temp_show, T_Humidity_show)
  926. if err != nil {
  927. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  928. c.ServeJSON()
  929. return
  930. }
  931. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  932. c.ServeJSON()
  933. return
  934. }
  935. url, err := Device_Sensor_Data_ChartShow_PDF0(company.T_name, T_snid, Time_start, Time_end, T_forwarding_unit, T_consignee_unit, T_remark, imageBytes, T_temp_show, T_Humidity_show)
  936. if err != nil {
  937. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  938. c.ServeJSON()
  939. return
  940. }
  941. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  942. c.ServeJSON()
  943. return
  944. }
  945. // 图表展示 下载PDF 箱/车
  946. func Device_Sensor_Data_ChartShow_PDF1(companyName, T_snid, Time_start, Time_end, T_forwarding_unit, T_consignee_unit, T_remark string, imageBytes []byte,
  947. T_temp_show, T_Humidity_show int) (url string, err error) {
  948. snidNum := strings.Split(strings.Trim(T_snid, "|"), "|")
  949. if len(snidNum) > 2 {
  950. err = errors.New("车/箱最多只能选择2个探头!")
  951. return
  952. }
  953. var sn1, sn2 string
  954. var id1, id2 int
  955. var r_maps_sn1, r_maps_sn2 []Device.DeviceData_R
  956. var DeviceSensor_data []Device.DeviceData_R
  957. var Pdf_data []Device.DeviceData_Pdf
  958. if len(snidNum) == 1 {
  959. sn_id := strings.Split(snidNum[0], ",")
  960. sn1, id1 = sn_id[0], lib.To_int(sn_id[1])
  961. r_maps_sn1, _ = Device.Read_DeviceData_ById_List_OrderByTimeAes(sn1, id1, Time_start, Time_end, 0, 9999)
  962. DeviceSensor_data = r_maps_sn1
  963. Pdf_data = Device.Read_DeviceData_ByIds_ForPDF(sn1, []int{id1}, Time_start, Time_end)
  964. }
  965. if len(snidNum) == 2 {
  966. sn_id1 := strings.Split(snidNum[0], ",")
  967. sn_id2 := strings.Split(snidNum[1], ",")
  968. sn1, id1 = sn_id1[0], lib.To_int(sn_id1[1])
  969. sn2, id2 = sn_id2[0], lib.To_int(sn_id2[1])
  970. if sn1 != sn2 {
  971. err = errors.New("请选择相同设备编号的探头导出!")
  972. return
  973. }
  974. r_maps_sn1, _ = Device.Read_DeviceData_ById_List_OrderByTimeAes(sn1, id1, Time_start, Time_end, 0, 9999)
  975. r_maps_sn2, _ = Device.Read_DeviceData_ById_List_OrderByTimeAes(sn2, id2, Time_start, Time_end, 0, 9999)
  976. DeviceSensor_data = append(DeviceSensor_data, r_maps_sn1...)
  977. DeviceSensor_data = append(DeviceSensor_data, r_maps_sn2...)
  978. Pdf_data = Device.Read_DeviceData_ByIds_ForPDF(sn1, []int{id1, id2}, Time_start, Time_end)
  979. }
  980. device, err := Device.Read_Device_ByT_sn(sn1)
  981. if err != nil {
  982. err = errors.New("查询主机信息失败!")
  983. return
  984. }
  985. // -------------------获取最高温湿度、最低温湿度、平均温湿度
  986. // 最高温度、最低温度
  987. var maxTemp, minTemp float32
  988. // 最高温度时间、最低温度时间
  989. var maxTempTime, minTempTime string
  990. // 总温度
  991. var totalTemp float32
  992. // 平均温度
  993. var avgTemp float32
  994. if len(DeviceSensor_data) > 0 {
  995. // 最高温度及时刻
  996. maxTemp = DeviceSensor_data[0].T_t
  997. maxTempTime = DeviceSensor_data[0].T_time
  998. // 最低温度及时刻
  999. minTemp = DeviceSensor_data[0].T_t
  1000. minTempTime = DeviceSensor_data[0].T_time
  1001. for _, data := range DeviceSensor_data {
  1002. if data.T_t > maxTemp {
  1003. maxTemp = data.T_t
  1004. maxTempTime = data.T_time
  1005. }
  1006. if data.T_t < minTemp {
  1007. minTemp = data.T_t
  1008. minTempTime = data.T_time
  1009. }
  1010. totalTemp += data.T_t
  1011. }
  1012. // 平均温度
  1013. avgTemp = totalTemp / float32(len(DeviceSensor_data))
  1014. // 平均湿度
  1015. //avgHumidity = totalHumidity / float32(len(DeviceSensor_data))
  1016. }
  1017. // -------------------获取最高温湿度、最低温湿度、平均温湿度结束
  1018. pdf := &gopdf.GoPdf{}
  1019. pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4
  1020. err = pdf.AddTTFFont("wts", "static/fonts/MiSans-Medium.ttf")
  1021. if err != nil {
  1022. return
  1023. }
  1024. err = pdf.SetFont("wts", "", 20)
  1025. if err != nil {
  1026. return
  1027. }
  1028. pdf.SetGrayFill(0.5)
  1029. pdf.SetMargins(0, 20, 0, 20)
  1030. pdf.AddPage()
  1031. title := companyName + "温湿度记录"
  1032. var y float64 = 40
  1033. textw, _ := pdf.MeasureTextWidth(title)
  1034. pdf.SetX((595 / 2) - (textw / 2))
  1035. pdf.SetY(y)
  1036. pdf.Text(title)
  1037. y += 30
  1038. pdf.SetFont("wts", "", 16)
  1039. pdf.SetXY(10, y)
  1040. pdf.Text("实施设备信息")
  1041. // 线
  1042. y += 10
  1043. pdf.SetLineWidth(0.5)
  1044. pdf.SetStrokeColor(169, 169, 169)
  1045. pdf.Line(10, y, 585, y)
  1046. pdf.SetFont("wts", "", 10)
  1047. y += 20
  1048. pdf.SetXY(10, y)
  1049. pdf.Text(fmt.Sprintf("主机名称:%s", device.T_devName))
  1050. pdf.SetXY(300, y)
  1051. pdf.Text(fmt.Sprintf("主机编号:%s", device.T_sn))
  1052. // 0 仓库 1 车厢
  1053. y += 20
  1054. ds1, _ := Device.Read_DeviceSensor_ByT_sn(sn1, id1)
  1055. pdf.SetXY(10, y)
  1056. pdf.Text(fmt.Sprintf("测点信息T1:%s", ds1.T_name))
  1057. if len(snidNum) > 1 {
  1058. ds2, _ := Device.Read_DeviceSensor_ByT_sn(sn2, id2)
  1059. pdf.SetXY(300, y)
  1060. pdf.Text(fmt.Sprintf("测点信息T2:%s", ds2.T_name))
  1061. }
  1062. y += 35
  1063. pdf.SetFont("wts", "", 16)
  1064. pdf.SetXY(10, y)
  1065. pdf.Text("记录概要信息")
  1066. // 线
  1067. y += 10
  1068. pdf.SetLineWidth(0.5)
  1069. pdf.SetStrokeColor(169, 169, 169)
  1070. pdf.Line(10, y, 585, y)
  1071. y += 20
  1072. var s_time, e_time string
  1073. if len(r_maps_sn1) > 0 {
  1074. s_time = fmt.Sprintf("%s", Pdf_data[0].T_time)
  1075. e_time = fmt.Sprintf("%s", Pdf_data[len(Pdf_data)-1].T_time)
  1076. }
  1077. pdf.SetFont("wts", "", 10)
  1078. pdf.SetXY(10, y)
  1079. pdf.Text(fmt.Sprintf("记录开始时间:%s", s_time))
  1080. pdf.SetXY(240, y)
  1081. pdf.Text(fmt.Sprintf("记录结束时间:%s", e_time))
  1082. sTime, _ := lib.TimeStrToTime(s_time)
  1083. eTime, _ := lib.TimeStrToTime(e_time)
  1084. pdf.SetXY(470, y)
  1085. minutes := int(eTime.Sub(sTime).Minutes())
  1086. hours := minutes / 60
  1087. remainingMinutes := minutes % 60
  1088. pdf.Text(fmt.Sprintf("记录总时间:%dh%dmin", hours, remainingMinutes))
  1089. if T_temp_show == 1 {
  1090. y += 15
  1091. pdf.SetXY(10, y)
  1092. pdf.Text(fmt.Sprintf("最高温度:%s【%.1f℃】", maxTempTime, lib.RoundToDecimal(float64(maxTemp), 1)))
  1093. pdf.SetXY(240, y)
  1094. pdf.Text(fmt.Sprintf("最低温度:%s【%.1f℃】", minTempTime, lib.RoundToDecimal(float64(minTemp), 1)))
  1095. pdf.SetXY(470, y)
  1096. pdf.Text(fmt.Sprintf("平均温度:%.2f℃", lib.RoundToDecimal(float64(avgTemp), 1)))
  1097. }
  1098. y += 15
  1099. pdf.SetXY(10, y)
  1100. var start_temp, end_temp string
  1101. if len(r_maps_sn1) > 0 {
  1102. start_temp = fmt.Sprintf("T1【%.1f℃】", r_maps_sn1[0].T_t)
  1103. end_temp = fmt.Sprintf("T1【%.1f℃】", r_maps_sn1[len(r_maps_sn1)-1].T_t)
  1104. }
  1105. if len(r_maps_sn2) > 0 {
  1106. start_temp += fmt.Sprintf(",T2【%.1f℃】", r_maps_sn2[0].T_t)
  1107. end_temp += fmt.Sprintf(",T2【%.1f℃】", r_maps_sn2[len(r_maps_sn2)-1].T_t)
  1108. }
  1109. pdf.Text(fmt.Sprintf("启运温度:%s", start_temp))
  1110. pdf.SetXY(240, y)
  1111. pdf.Text(fmt.Sprintf("到达温度:%s", end_temp))
  1112. var temp_range string
  1113. if len(DeviceSensor_data) > 0 {
  1114. temp_range = fmt.Sprintf("%.1f ~ %.1f℃", DeviceSensor_data[0].T_tl, DeviceSensor_data[0].T_tu)
  1115. }
  1116. pdf.SetXY(470, y)
  1117. pdf.Text(fmt.Sprintf("温度范围:%s", temp_range))
  1118. y += 15
  1119. pdf.SetXY(10, y)
  1120. T_forwarding_unit_temp := []rune(T_forwarding_unit)
  1121. if len(T_forwarding_unit_temp) > 34 {
  1122. err = errors.New("发货单位长度超过限制(34个字符)")
  1123. return
  1124. }
  1125. if len(T_forwarding_unit_temp) > 17 {
  1126. pdf.Text(fmt.Sprintf("发货单位:%s", string(T_forwarding_unit_temp[0:17])))
  1127. pdf.SetXY(60, y+15)
  1128. pdf.Text(fmt.Sprintf("%s", string(T_forwarding_unit_temp[17:])))
  1129. } else {
  1130. pdf.Text(fmt.Sprintf("发货单位:%s", string(T_forwarding_unit_temp)))
  1131. }
  1132. pdf.SetXY(240, y)
  1133. T_consignee_unit_temp := []rune(T_consignee_unit)
  1134. if len(T_consignee_unit_temp) > 34 {
  1135. err = errors.New("收货单位长度超过限制(34个字符)")
  1136. return
  1137. }
  1138. if len(T_consignee_unit_temp) > 17 {
  1139. pdf.Text(fmt.Sprintf("收货单位:%s", string(T_consignee_unit_temp[0:17])))
  1140. pdf.SetXY(290, y+15)
  1141. pdf.Text(fmt.Sprintf("%s", string(T_consignee_unit_temp[17:])))
  1142. } else {
  1143. pdf.Text(fmt.Sprintf("收货单位:%s", string(T_consignee_unit_temp)))
  1144. }
  1145. pdf.SetXY(470, y)
  1146. T_remark_temp := []rune(T_remark)
  1147. if len(T_remark_temp) > 34 {
  1148. err = errors.New("备注长度超过限制(16个字符)")
  1149. return
  1150. }
  1151. if len(T_remark) > 8 {
  1152. pdf.Text(fmt.Sprintf("备注:%s", string(T_remark_temp[0:8])))
  1153. pdf.SetXY(500, y+15)
  1154. pdf.Text(fmt.Sprintf("%s", string(T_remark_temp[8:])))
  1155. } else {
  1156. pdf.Text(fmt.Sprintf("备注: %s", string(T_remark_temp)))
  1157. }
  1158. y += 35
  1159. pdf.SetFont("wts", "", 16)
  1160. pdf.SetXY(10, y)
  1161. pdf.Text("记录曲线信息")
  1162. // 线
  1163. y += 10
  1164. pdf.SetLineWidth(0.5)
  1165. pdf.SetStrokeColor(169, 169, 169)
  1166. pdf.Line(10, y, 585, y)
  1167. y += 1
  1168. // ----------------绘制图片
  1169. //pdf.ImageFrom(img, 10, y, &gopdf.Rect{W: 575, H: 630})
  1170. imgH, err := gopdf.ImageHolderByBytes(imageBytes)
  1171. if T_temp_show == 1 && T_Humidity_show == 1 {
  1172. //pdf.ImageByHolder(imgH, 10, y, &gopdf.Rect{W: 575, H: 630})
  1173. pdf.ImageByHolder(imgH, 32, y, &gopdf.Rect{W: 522, H: 572})
  1174. y += 562
  1175. } else {
  1176. pdf.ImageByHolder(imgH, 10, y, &gopdf.Rect{W: 575, H: 315})
  1177. y += 315
  1178. }
  1179. y += 35
  1180. if y > 841.89 {
  1181. // 图片结束直接分页
  1182. pdf.AddPage()
  1183. y = 40
  1184. }
  1185. pdf.SetFont("wts", "", 16)
  1186. pdf.SetXY(10, y)
  1187. pdf.Text("记录数据信息")
  1188. // 线
  1189. y += 10
  1190. pdf.SetLineWidth(0.5)
  1191. pdf.SetStrokeColor(169, 169, 169)
  1192. pdf.Line(10, y, 585, y)
  1193. y += 10
  1194. pdf.SetFont("wts", "", 10)
  1195. if len(snidNum) == 1 {
  1196. var x float64 = 19
  1197. var w float64 = 112
  1198. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1199. x = x + w
  1200. w = 74
  1201. lib.RectFillColor(pdf, "T1", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1202. x = x + w
  1203. w = 110
  1204. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1205. x = x + w
  1206. w = 74
  1207. lib.RectFillColor(pdf, "T1", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1208. x = x + w
  1209. w = 110
  1210. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1211. x = x + w
  1212. w = 74
  1213. lib.RectFillColor(pdf, "T1", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1214. for i, v := range Pdf_data {
  1215. if i%3 == 0 {
  1216. y += 20
  1217. x, w = 19, 112
  1218. var textH float64 = 25 // if text height is 25px.
  1219. pdf.SetNewY(y, textH)
  1220. y = pdf.GetY()
  1221. lib.RectFillColor(pdf, v.T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1222. x = x + w
  1223. w = 74
  1224. lib.RectFillColor(pdf, lib.Float32_to_string(v.T_id1), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1225. }
  1226. if i%3 == 1 {
  1227. x = x + w
  1228. w = 110
  1229. lib.RectFillColor(pdf, v.T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1230. x = x + w
  1231. w = 74
  1232. lib.RectFillColor(pdf, lib.Float32_to_string(v.T_id1), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1233. }
  1234. if i%3 == 2 {
  1235. x = x + w
  1236. w = 110
  1237. lib.RectFillColor(pdf, v.T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1238. x = x + w
  1239. w = 74
  1240. lib.RectFillColor(pdf, lib.Float32_to_string(v.T_id1), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1241. }
  1242. }
  1243. }
  1244. if len(snidNum) == 2 {
  1245. var x float64 = 19
  1246. var w float64 = 112
  1247. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1248. x = x + w
  1249. w = 37
  1250. lib.RectFillColor(pdf, "T1", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1251. x = x + w
  1252. lib.RectFillColor(pdf, "T2", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1253. x = x + w
  1254. w = 112
  1255. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1256. x = x + w
  1257. w = 37
  1258. lib.RectFillColor(pdf, "T1", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1259. x = x + w
  1260. lib.RectFillColor(pdf, "T2", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1261. x = x + w
  1262. w = 112
  1263. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1264. x = x + w
  1265. w = 37
  1266. lib.RectFillColor(pdf, "T1", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1267. x = x + w
  1268. lib.RectFillColor(pdf, "T2", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1269. for i, v := range Pdf_data {
  1270. if i%3 == 0 {
  1271. y += 20
  1272. var textH float64 = 25 // if text height is 25px.
  1273. pdf.SetNewY(y, textH)
  1274. y = pdf.GetY()
  1275. x, w = 19, 112
  1276. lib.RectFillColor(pdf, v.T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1277. x = x + w
  1278. w = 37
  1279. lib.RectFillColor(pdf, lib.Float32_to_string(v.T_id1), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1280. x = x + w
  1281. lib.RectFillColor(pdf, lib.Float32_to_string(v.T_id2), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1282. }
  1283. if i%3 == 1 {
  1284. x = x + w
  1285. w = 112
  1286. lib.RectFillColor(pdf, v.T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1287. x = x + w
  1288. w = 37
  1289. lib.RectFillColor(pdf, lib.Float32_to_string(v.T_id1), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1290. x = x + w
  1291. lib.RectFillColor(pdf, lib.Float32_to_string(v.T_id2), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1292. }
  1293. if i%3 == 2 {
  1294. x = x + w
  1295. w = 112
  1296. lib.RectFillColor(pdf, v.T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1297. x = x + w
  1298. w = 37
  1299. lib.RectFillColor(pdf, lib.Float32_to_string(v.T_id1), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1300. x = x + w
  1301. lib.RectFillColor(pdf, lib.Float32_to_string(v.T_id2), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1302. }
  1303. }
  1304. }
  1305. timeStr := "ofile/" + time.Now().Format("20060102150405") + ".pdf"
  1306. err = pdf.WritePdf(timeStr)
  1307. if err != nil {
  1308. return
  1309. }
  1310. // 上传 OSS
  1311. url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/"+timeStr, timeStr)
  1312. if !is {
  1313. err = errors.New("oss!")
  1314. return
  1315. }
  1316. //删除目录
  1317. err = os.Remove(timeStr)
  1318. if err != nil {
  1319. logs.Error(err)
  1320. }
  1321. return url, nil
  1322. }
  1323. // 图表展示 下载PDF 库房
  1324. func Device_Sensor_Data_ChartShow_PDF0(companyName, T_snid, Time_start, Time_end, T_forwarding_unit, T_consignee_unit, T_remark string, imageBytes []byte,
  1325. T_temp_show, T_Humidity_show int) (url string, err error) {
  1326. sn, ids, isSameSn := lib.IsSNAllSame(T_snid)
  1327. if !isSameSn {
  1328. err = errors.New("请选择相同设备编号的探头导出!")
  1329. return
  1330. }
  1331. device, err := Device.Read_Device_ByT_sn(sn)
  1332. if err != nil {
  1333. err = errors.New("查询主机信息失败!")
  1334. return
  1335. }
  1336. var DeviceSensor_data []Device.DeviceData_R
  1337. Device.Read_DeviceSensorParameter_All_Map(sn, 0)
  1338. // -------------------获取最高温湿度、温蒂温湿度、平均温湿度
  1339. //DeviceSensor_data, _ = Device.Read_DeviceData_By_T_snid_List(T_snid, Time_start, Time_end, 0, 9999)
  1340. DeviceSensor_data, _ = Device.Read_DeviceData_ByIds(sn, ids, Time_start, Time_end, 0, 9999)
  1341. // 根据传感器T_sort排序
  1342. //dsList := Device.Read_DeviceSensorList_By_T_snid(T_snid)
  1343. //// 创建名称到权重的映射
  1344. //orderMap := make(map[int]int)
  1345. //for i, v := range dsList {
  1346. // orderMap[v.T_id] = i
  1347. //}
  1348. //sort.Slice(DeviceSensor_data, func(i, j int) bool {
  1349. // if DeviceSensor_data[i].T_time == DeviceSensor_data[j].T_time {
  1350. // // 如果时间相同,则按预设顺序排序
  1351. // return orderMap[DeviceSensor_data[i].T_id] < orderMap[DeviceSensor_data[j].T_id]
  1352. // }
  1353. // return DeviceSensor_data[i].T_time > DeviceSensor_data[j].T_time
  1354. //})
  1355. // 最高温度、最低温度、最高湿度、最低湿度
  1356. var maxTemp, minTemp, maxHumidity, minHumidity float32
  1357. // 最高温度时间、最低温度时间、最高湿度时间、最低湿度时间
  1358. var maxTempTime, minTempTime, maxHumidityTime, minHumidityTime string
  1359. // 总温度 总湿度
  1360. var totalTemp, totalHumidity float32
  1361. // 平均温度 平均湿度
  1362. var avgTemp, avgHumidity float32
  1363. // 温度阈值,湿度阈值
  1364. var tempThreshold, humidityThreshold string
  1365. // 记录开始时间,记录结束时间
  1366. var s_time, e_time string
  1367. var lastTime string
  1368. var isFirst, isSecond = true, false
  1369. var first_column, second_column []Device.DeviceData_R
  1370. if len(DeviceSensor_data) > 0 {
  1371. tempThreshold = fmt.Sprintf("%.1f-%.1f", DeviceSensor_data[0].T_tl, DeviceSensor_data[0].T_tu)
  1372. humidityThreshold = fmt.Sprintf("%.1f-%.1f", DeviceSensor_data[0].T_rhl, DeviceSensor_data[0].T_rhu)
  1373. e_time = DeviceSensor_data[0].T_time
  1374. s_time = DeviceSensor_data[len(DeviceSensor_data)-1].T_time
  1375. // 最高温度及时刻
  1376. maxTemp = DeviceSensor_data[0].T_t
  1377. maxTempTime = DeviceSensor_data[0].T_time
  1378. // 最低温度及时刻
  1379. minTemp = DeviceSensor_data[0].T_t
  1380. minTempTime = DeviceSensor_data[0].T_time
  1381. // 最高湿度及时刻
  1382. maxHumidity = DeviceSensor_data[0].T_rh
  1383. maxHumidityTime = DeviceSensor_data[0].T_time
  1384. // 获取最低湿度及时刻
  1385. minHumidity = DeviceSensor_data[0].T_rh
  1386. minHumidityTime = DeviceSensor_data[0].T_time
  1387. for i := 0; i < len(DeviceSensor_data); i++ {
  1388. data := DeviceSensor_data[i]
  1389. if data.T_t > maxTemp {
  1390. maxTemp = data.T_t
  1391. maxTempTime = data.T_time
  1392. }
  1393. if data.T_t < minTemp {
  1394. minTemp = data.T_t
  1395. minTempTime = data.T_time
  1396. }
  1397. totalTemp += data.T_t
  1398. if data.T_rh > maxHumidity {
  1399. maxHumidity = data.T_rh
  1400. maxHumidityTime = data.T_time
  1401. }
  1402. if data.T_rh < minHumidity {
  1403. minHumidity = data.T_rh
  1404. minHumidityTime = data.T_time
  1405. }
  1406. totalHumidity += data.T_rh
  1407. if len(lastTime) > 0 {
  1408. if lastTime != data.T_time && isFirst == true {
  1409. isFirst = false
  1410. isSecond = true
  1411. } else if lastTime != data.T_time && isSecond == true {
  1412. isFirst = true
  1413. isSecond = false
  1414. }
  1415. }
  1416. if isFirst {
  1417. first_column = append(first_column, data)
  1418. lastTime = data.T_time
  1419. }
  1420. if isSecond {
  1421. second_column = append(second_column, data)
  1422. lastTime = data.T_time
  1423. }
  1424. }
  1425. // 平均温度
  1426. avgTemp = totalTemp / float32(len(DeviceSensor_data))
  1427. // 平均湿度
  1428. avgHumidity = totalHumidity / float32(len(DeviceSensor_data))
  1429. }
  1430. // -------------------获取最高温湿度、温蒂温湿度、平均温湿度结束
  1431. pdf := &gopdf.GoPdf{}
  1432. pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4
  1433. err = pdf.AddTTFFont("wts", "static/fonts/MiSans-Medium.ttf")
  1434. if err != nil {
  1435. return
  1436. }
  1437. err = pdf.SetFont("wts", "", 20)
  1438. if err != nil {
  1439. return
  1440. }
  1441. pdf.SetGrayFill(0.5)
  1442. pdf.SetMargins(0, 20, 0, 20)
  1443. pdf.AddPage()
  1444. title := companyName + "温湿度记录"
  1445. var y float64 = 40
  1446. textw, _ := pdf.MeasureTextWidth(title)
  1447. pdf.SetX((595 / 2) - (textw / 2))
  1448. pdf.SetY(y)
  1449. pdf.Text(title)
  1450. y += 30
  1451. pdf.SetFont("wts", "", 16)
  1452. pdf.SetXY(10, y)
  1453. pdf.Text("实施设备信息")
  1454. // 线
  1455. y += 10
  1456. pdf.SetLineWidth(0.5)
  1457. pdf.SetStrokeColor(169, 169, 169)
  1458. pdf.Line(10, y, 585, y)
  1459. pdf.SetFont("wts", "", 10)
  1460. y += 20
  1461. pdf.SetXY(10, y)
  1462. pdf.Text(fmt.Sprintf("主机名称:%s", device.T_devName))
  1463. pdf.SetXY(300, y)
  1464. pdf.Text(fmt.Sprintf("主机编号:%s", device.T_sn))
  1465. y += 35
  1466. pdf.SetFont("wts", "", 16)
  1467. pdf.SetXY(10, y)
  1468. pdf.Text("记录概要信息")
  1469. // 线
  1470. y += 10
  1471. pdf.SetLineWidth(0.5)
  1472. pdf.SetStrokeColor(169, 169, 169)
  1473. pdf.Line(10, y, 585, y)
  1474. y += 20
  1475. pdf.SetFont("wts", "", 10)
  1476. pdf.SetXY(10, y)
  1477. pdf.Text(fmt.Sprintf("记录开始时间:%s", s_time))
  1478. pdf.SetXY(240, y)
  1479. pdf.Text(fmt.Sprintf("记录结束时间:%s", e_time))
  1480. sTime, _ := lib.TimeStrToTime(s_time)
  1481. eTime, _ := lib.TimeStrToTime(e_time)
  1482. pdf.SetXY(470, y)
  1483. minutes := int(eTime.Sub(sTime).Minutes())
  1484. hours := minutes / 60
  1485. remainingMinutes := minutes % 60
  1486. pdf.Text(fmt.Sprintf("记录总时间:%dh%dmin", hours, remainingMinutes))
  1487. // -------------最高温/湿度 最低温/湿度 平均温/湿度
  1488. if T_temp_show == 1 {
  1489. y += 15
  1490. pdf.SetXY(10, y)
  1491. pdf.Text(fmt.Sprintf("最高温度:%.1f℃,%s", lib.RoundToDecimal(float64(maxTemp), 1), maxTempTime))
  1492. pdf.SetXY(240, y)
  1493. pdf.Text(fmt.Sprintf("最低温度:%.1f℃,%s", lib.RoundToDecimal(float64(minTemp), 1), minTempTime))
  1494. pdf.SetXY(470, y)
  1495. pdf.Text(fmt.Sprintf("平均温度:%.1f℃", lib.RoundToDecimal(float64(avgTemp), 1)))
  1496. }
  1497. if T_Humidity_show == 1 {
  1498. y += 15
  1499. pdf.SetXY(10, y)
  1500. pdf.Text(fmt.Sprintf("最高湿度:%.1f%%RH,%s", lib.RoundToDecimal(float64(maxHumidity), 1), maxHumidityTime))
  1501. pdf.SetXY(240, y)
  1502. pdf.Text(fmt.Sprintf("最低湿度:%.1f%%RH,%s", lib.RoundToDecimal(float64(minHumidity), 1), minHumidityTime))
  1503. pdf.SetXY(470, y)
  1504. pdf.Text(fmt.Sprintf("平均湿度:%.1f%%RH", lib.RoundToDecimal(float64(avgHumidity), 1)))
  1505. }
  1506. // -------------温/湿度阈值
  1507. if T_temp_show == 1 {
  1508. y += 15
  1509. pdf.SetXY(10, y)
  1510. pdf.Text(fmt.Sprintf("温度阈值:%s℃", tempThreshold))
  1511. }
  1512. if T_Humidity_show == 1 {
  1513. if T_temp_show != 1 {
  1514. y += 15
  1515. }
  1516. pdf.SetXY(240, y)
  1517. pdf.Text(fmt.Sprintf("湿度阈值:%s%%", humidityThreshold))
  1518. }
  1519. //-------------发货单位,收货单位,备注
  1520. y += 15
  1521. pdf.SetXY(10, y)
  1522. T_forwarding_unit_temp := []rune(T_forwarding_unit)
  1523. if len(T_forwarding_unit_temp) > 34 {
  1524. err = errors.New("发货单位长度超过限制(34个字符)")
  1525. return
  1526. }
  1527. if len(T_forwarding_unit_temp) > 17 {
  1528. pdf.Text(fmt.Sprintf("发货单位:%s", string(T_forwarding_unit_temp[0:17])))
  1529. pdf.SetXY(60, y+15)
  1530. pdf.Text(fmt.Sprintf("%s", string(T_forwarding_unit_temp[17:])))
  1531. } else {
  1532. pdf.Text(fmt.Sprintf("发货单位:%s", string(T_forwarding_unit_temp)))
  1533. }
  1534. pdf.SetXY(240, y)
  1535. T_consignee_unit_temp := []rune(T_consignee_unit)
  1536. if len(T_consignee_unit_temp) > 34 {
  1537. err = errors.New("收货单位长度超过限制(34个字符)")
  1538. return
  1539. }
  1540. if len(T_consignee_unit_temp) > 17 {
  1541. pdf.Text(fmt.Sprintf("收货单位:%s", string(T_consignee_unit_temp[0:17])))
  1542. pdf.SetXY(290, y+15)
  1543. pdf.Text(fmt.Sprintf("%s", string(T_consignee_unit_temp[17:])))
  1544. } else {
  1545. pdf.Text(fmt.Sprintf("收货单位:%s", string(T_consignee_unit_temp)))
  1546. }
  1547. pdf.SetXY(470, y)
  1548. T_remark_temp := []rune(T_remark)
  1549. if len(T_remark_temp) > 34 {
  1550. err = errors.New("备注长度超过限制(16个字符)")
  1551. return
  1552. }
  1553. if len(T_remark) > 8 {
  1554. pdf.Text(fmt.Sprintf("备注:%s", string(T_remark_temp[0:8])))
  1555. pdf.SetXY(500, y+15)
  1556. pdf.Text(fmt.Sprintf("%s", string(T_remark_temp[8:])))
  1557. } else {
  1558. pdf.Text(fmt.Sprintf("备注: %s", string(T_remark_temp)))
  1559. }
  1560. y += 35
  1561. pdf.SetFont("wts", "", 16)
  1562. pdf.SetXY(10, y)
  1563. pdf.Text("记录曲线信息")
  1564. // 线
  1565. y += 10
  1566. pdf.SetLineWidth(0.5)
  1567. pdf.SetStrokeColor(169, 169, 169)
  1568. pdf.Line(10, y, 585, y)
  1569. y += 1
  1570. // ----------------绘制图片
  1571. //pdf.ImageFrom(img, 10, y, &gopdf.Rect{W: 575, H: 630})
  1572. imgH, err := gopdf.ImageHolderByBytes(imageBytes)
  1573. if T_temp_show == 1 && T_Humidity_show == 1 {
  1574. //pdf.ImageByHolder(imgH, 10, y, &gopdf.Rect{W: 575, H: 630})
  1575. pdf.ImageByHolder(imgH, 32, y, &gopdf.Rect{W: 522, H: 572})
  1576. y += 562
  1577. } else {
  1578. pdf.ImageByHolder(imgH, 10, y, &gopdf.Rect{W: 575, H: 315})
  1579. y += 315
  1580. }
  1581. y += 35
  1582. if y > 841.89 {
  1583. // 图片结束直接分页
  1584. pdf.AddPage()
  1585. y = 40
  1586. }
  1587. pdf.SetFont("wts", "", 16)
  1588. pdf.SetXY(10, y)
  1589. pdf.Text("记录数据信息")
  1590. // 线
  1591. y += 10
  1592. pdf.SetLineWidth(0.5)
  1593. pdf.SetStrokeColor(169, 169, 169)
  1594. pdf.Line(10, y, 585, y)
  1595. y += 10
  1596. pdf.SetFont("wts", "", 10)
  1597. var x float64 = 10
  1598. var w float64 = 112
  1599. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1600. x = x + w
  1601. w = 101
  1602. lib.RectFillColor(pdf, "名称", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1603. if T_temp_show == 1 && T_Humidity_show == 1 {
  1604. x = x + w
  1605. w = 37
  1606. lib.RectFillColor(pdf, "温度℃", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1607. x = x + w
  1608. w = 37
  1609. lib.RectFillColor(pdf, "湿度%", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1610. }
  1611. if T_temp_show == 1 && T_Humidity_show == 0 {
  1612. x = x + w
  1613. w = 74
  1614. lib.RectFillColor(pdf, "温度℃", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1615. }
  1616. if T_temp_show == 0 && T_Humidity_show == 1 {
  1617. x = x + w
  1618. w = 74
  1619. lib.RectFillColor(pdf, "湿度%", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1620. }
  1621. x = x + w
  1622. w = 112
  1623. lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1624. x = x + w
  1625. w = 101
  1626. lib.RectFillColor(pdf, "名称", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1627. if T_temp_show == 1 && T_Humidity_show == 1 {
  1628. x = x + w
  1629. w = 37
  1630. lib.RectFillColor(pdf, "温度℃", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1631. x = x + w
  1632. w = 37
  1633. lib.RectFillColor(pdf, "湿度%", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1634. }
  1635. if T_temp_show == 1 && T_Humidity_show == 0 {
  1636. x = x + w
  1637. w = 74
  1638. lib.RectFillColor(pdf, "温度℃", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1639. }
  1640. if T_temp_show == 0 && T_Humidity_show == 1 {
  1641. x = x + w
  1642. w = 74
  1643. lib.RectFillColor(pdf, "湿度%", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1644. }
  1645. y += 20
  1646. var textH float64 = 25 // if text height is 25px.
  1647. for i, v := range first_column {
  1648. pdf.SetNewY(y, textH)
  1649. y = pdf.GetY()
  1650. x, w = 10, 112
  1651. lib.RectFillColor(pdf, v.T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1652. x = x + w
  1653. w = 101
  1654. lib.RectFillColor(pdf, v.T_name, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1655. // 显示温湿度
  1656. if T_temp_show == 1 && T_Humidity_show == 1 {
  1657. x = x + w
  1658. w = 37
  1659. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", v.T_t), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1660. x = x + w
  1661. w = 37
  1662. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", v.T_rh), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1663. }
  1664. // 只显示温度
  1665. if T_temp_show == 1 && T_Humidity_show == 0 {
  1666. x = x + w
  1667. w = 74
  1668. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", v.T_t), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1669. }
  1670. // 只显示湿度
  1671. if T_temp_show == 0 && T_Humidity_show == 1 {
  1672. x = x + w
  1673. w = 74
  1674. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", v.T_rh), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1675. }
  1676. if i < len(second_column) {
  1677. x = x + w
  1678. w = 112
  1679. lib.RectFillColor(pdf, second_column[i].T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1680. x = x + w
  1681. w = 101
  1682. lib.RectFillColor(pdf, second_column[i].T_name, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1683. if T_temp_show == 1 && T_Humidity_show == 1 {
  1684. x = x + w
  1685. w = 37
  1686. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", second_column[i].T_t), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1687. x = x + w
  1688. w = 37
  1689. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", second_column[i].T_rh), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1690. }
  1691. if T_temp_show == 1 && T_Humidity_show == 0 {
  1692. x = x + w
  1693. w = 74
  1694. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", second_column[i].T_t), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1695. }
  1696. if T_temp_show == 0 && T_Humidity_show == 1 {
  1697. x = x + w
  1698. w = 74
  1699. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", second_column[i].T_rh), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1700. }
  1701. }
  1702. y += 20
  1703. }
  1704. if len(second_column) > len(first_column) {
  1705. for i := len(first_column); i < len(second_column); i++ {
  1706. pdf.SetNewY(y, textH)
  1707. y = pdf.GetY()
  1708. x, w = 297, 112
  1709. lib.RectFillColor(pdf, second_column[i].T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1710. x = x + w
  1711. w = 101
  1712. lib.RectFillColor(pdf, second_column[i].T_name, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1713. if T_temp_show == 1 && T_Humidity_show == 1 {
  1714. x = x + w
  1715. w = 37
  1716. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", second_column[i].T_t), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1717. x = x + w
  1718. w = 37
  1719. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", second_column[i].T_rh), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1720. }
  1721. if T_temp_show == 1 && T_Humidity_show == 0 {
  1722. x = x + w
  1723. w = 74
  1724. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", second_column[i].T_t), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1725. }
  1726. if T_temp_show == 0 && T_Humidity_show == 1 {
  1727. x = x + w
  1728. w = 74
  1729. lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", second_column[i].T_rh), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1730. }
  1731. y += 20
  1732. }
  1733. }
  1734. timeStr := "ofile/" + time.Now().Format("20060102150405") + ".pdf"
  1735. err = pdf.WritePdf(timeStr)
  1736. if err != nil {
  1737. return
  1738. }
  1739. // 上传 OSS
  1740. url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/"+timeStr, timeStr)
  1741. if !is {
  1742. err = errors.New("oss!")
  1743. return
  1744. }
  1745. //删除目录
  1746. err = os.Remove(timeStr)
  1747. if err != nil {
  1748. logs.Error(err)
  1749. }
  1750. return url, nil
  1751. }
  1752. // 数据备份
  1753. func (c *DataController) Device_Sensor_Data_BackUp() {
  1754. page, _ := c.GetInt("page")
  1755. if page < 1 {
  1756. page = 1
  1757. }
  1758. page_z, _ := c.GetInt("page_z")
  1759. if page_z < 1 {
  1760. page_z = conf.Page_size
  1761. }
  1762. T_snid := c.GetString("T_snid")
  1763. Time_start := c.GetString("Time_start")
  1764. Time_end := c.GetString("Time_end")
  1765. if len(T_snid) < 10 {
  1766. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_snid Err!"}
  1767. c.ServeJSON()
  1768. return
  1769. }
  1770. if _, is := lib.DateStrToDate(Time_start); !is {
  1771. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  1772. c.ServeJSON()
  1773. return
  1774. }
  1775. if _, is := lib.DateStrToDate(Time_end); !is {
  1776. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  1777. c.ServeJSON()
  1778. return
  1779. }
  1780. Time_start += " 00:00:00"
  1781. Time_end += " 23:59:59"
  1782. var r_jsons lib.R_JSONS
  1783. r_jsons.Data, r_jsons.Num = Device.Read_DeviceData_BackUp_List(T_snid, Time_start, Time_end, page, page_z)
  1784. r_jsons.Page = page
  1785. r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
  1786. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1787. c.ServeJSON()
  1788. return
  1789. }
  1790. // 数据备份pdf
  1791. func (c *DataController) Device_Sensor_Data_BackUp_PDF() {
  1792. T_sn := c.GetString("T_sn")
  1793. T_id, _ := c.GetInt("T_id")
  1794. T_date := c.GetString("T_date")
  1795. T_snid := fmt.Sprintf("%s,%d", T_sn, T_id)
  1796. date, is := lib.DateStrToDate(T_date)
  1797. if !is {
  1798. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  1799. c.ServeJSON()
  1800. return
  1801. }
  1802. Time_start := T_date + " 00:00:00"
  1803. Time_end := T_date + " 23:59:59"
  1804. var DeviceSensor_data []Device.DeviceData_R
  1805. DeviceSensor_data, _ = Device.Read_DeviceData_By_T_snid_List(T_snid, Time_start, Time_end, 0, 9999)
  1806. var err error
  1807. pdf := &gopdf.GoPdf{}
  1808. pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4
  1809. //err = GetFont(pdf, "LiberationSerif-Regular.ttf")
  1810. //if err != nil {
  1811. // log.Fatalln(err)
  1812. //}
  1813. //err = pdf.SetFont("Ubuntu-L", "", 14)
  1814. //if err != nil {
  1815. // log.Fatalln(err)
  1816. //}
  1817. err = pdf.AddTTFFont("simsun", "static/fonts/三极行楷简体-粗.ttf")
  1818. if err != nil {
  1819. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  1820. c.ServeJSON()
  1821. return
  1822. }
  1823. err = pdf.SetFont("simsun", "", 24)
  1824. if err != nil {
  1825. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  1826. c.ServeJSON()
  1827. return
  1828. }
  1829. pdf.SetGrayFill(0.5)
  1830. pdf.SetMargins(0, 20, 0, 20)
  1831. pdf.AddPage()
  1832. //use path
  1833. //pdf.Image("logo.png", 100, 50, &gopdf.Rect{W: 50, H: 50})
  1834. company, _ := Account.Read_Company_ById(c.T_pid)
  1835. textw, _ := pdf.MeasureTextWidth(company.T_name)
  1836. pdf.SetX((595 / 2) - (textw / 2))
  1837. pdf.SetY(40)
  1838. pdf.Text(company.T_name)
  1839. // 线
  1840. pdf.SetLineWidth(2)
  1841. pdf.SetLineType("dashed")
  1842. pdf.Line(10, 60, 585, 60)
  1843. err = pdf.AddTTFFont("wts", "static/fonts/MiSans-Medium.ttf")
  1844. if err != nil {
  1845. c.Data["json"] = lib.JSONS{Code: 202, Msg: "ok!", Data: err}
  1846. c.ServeJSON()
  1847. return
  1848. }
  1849. err = pdf.SetFont("wts", "", 12)
  1850. if err != nil {
  1851. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  1852. c.ServeJSON()
  1853. return
  1854. }
  1855. var s_time, e_time string
  1856. if len(DeviceSensor_data) > 0 {
  1857. s_time = fmt.Sprintf("%s", DeviceSensor_data[0].T_time)
  1858. e_time = fmt.Sprintf("%s", DeviceSensor_data[len(DeviceSensor_data)-1].T_time)
  1859. }
  1860. //fmt.Sprintf(" %.1f ", v.T_t)
  1861. lib.RectFillColor(pdf, "历史数据["+e_time+" / "+s_time+"]", 14, 22, 80, 550, 40, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1862. lib.RectFillColor(pdf, "序号", 12, 22, 120, 30, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1863. lib.RectFillColor(pdf, "传感器名称", 12, 52, 120, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1864. lib.RectFillColor(pdf, "温度℃", 12, 152, 120, 60, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1865. lib.RectFillColor(pdf, "湿度%", 12, 212, 120, 60, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1866. lib.RectFillColor(pdf, "温度范围", 12, 272, 120, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1867. lib.RectFillColor(pdf, "湿度范围", 12, 362, 120, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1868. lib.RectFillColor(pdf, "记录时间", 12, 452, 120, 120, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1869. var y float64 = 140
  1870. err = pdf.SetFont("wts", "", 10)
  1871. if err != nil {
  1872. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  1873. c.ServeJSON()
  1874. return
  1875. }
  1876. for i, v := range DeviceSensor_data {
  1877. text := fmt.Sprintf(" %d ", i+1)
  1878. var textH float64 = 25 // if text height is 25px.
  1879. pdf.SetNewY(y, textH)
  1880. y = pdf.GetY()
  1881. //pdf.SetX(x) // must after pdf.SetNewY() called.
  1882. //err = pdf.Text(text)
  1883. //if err != nil {
  1884. // log.Fatalln(err)
  1885. //}
  1886. T_t := fmt.Sprintf(" %.1f ", v.T_t)
  1887. T_rh := fmt.Sprintf(" %.1f ", v.T_rh)
  1888. T_Tlu := fmt.Sprintf(" %.1f ~ %.1f ", v.T_tl, v.T_tu)
  1889. T_Rlu := fmt.Sprintf(" %.1f ~ %.1f ", v.T_rhl, v.T_rhu)
  1890. T_time := fmt.Sprintf("%s", v.T_time)
  1891. //if user_r.Admin_rh == 0 && strings.Contains(v.T_sn, "YD") {
  1892. // T_rh = "-"
  1893. // T_Rlu = "-"
  1894. //}
  1895. lib.RectFillColor(pdf, text, 10, 22, y, 30, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1896. lib.RectFillColor(pdf, v.T_name, 10, 52, y, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1897. lib.RectFillColor(pdf, T_t, 10, 152, y, 60, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1898. lib.RectFillColor(pdf, T_rh, 10, 212, y, 60, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1899. lib.RectFillColor(pdf, T_Tlu, 10, 272, y, 90, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1900. lib.RectFillColor(pdf, T_Rlu, 10, 362, y, 90, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1901. lib.RectFillColor(pdf, T_time, 10, 452, y, 120, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
  1902. y += 20
  1903. }
  1904. deviceSensor, _ := Device.Read_DeviceSensor_ByT_sn(T_sn, T_id)
  1905. rand_x := int64(lib.Random(0, 10000))
  1906. filenameStr := fmt.Sprintf("backup/sn/%s_%s(%s).pdf", lib.GetRandstring(8, "0123456789", rand_x), deviceSensor.T_name, date.Format("2006-01-02"))
  1907. err = pdf.WritePdf(filenameStr)
  1908. if err != nil {
  1909. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  1910. c.ServeJSON()
  1911. return
  1912. }
  1913. // 上传 OSS
  1914. url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/"+filenameStr, filenameStr)
  1915. if !is {
  1916. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  1917. c.ServeJSON()
  1918. return
  1919. }
  1920. //删除目录
  1921. err = os.Remove(filenameStr)
  1922. if err != nil {
  1923. logs.Error(lib.FuncName(), err)
  1924. }
  1925. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  1926. c.ServeJSON()
  1927. return
  1928. }
  1929. // 对接电信实时数据
  1930. func (c *DataController) Docking_Real_Data() {
  1931. key := c.GetString("T_key")
  1932. // 查询公司
  1933. Company_r, err := Account.Read_Company_ByKey(key)
  1934. if err != nil {
  1935. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_key Err!"}
  1936. c.ServeJSON()
  1937. return
  1938. }
  1939. // 查询公司下面所有子公司id
  1940. T_pids := Account.ReadCompanyIds_T_path(Company_r.T_path)
  1941. type R_JSONS struct {
  1942. //必须的大写开头
  1943. Data []Device.DeviceData_Docking
  1944. }
  1945. var r_jsons R_JSONS
  1946. r_jsons.Data = Device.Read_Docking_Real_Data(T_pids)
  1947. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1948. c.ServeJSON()
  1949. return
  1950. }
  1951. // 对接电信历史数据
  1952. func (c *DataController) Docking_Note_Data() {
  1953. key := c.GetString("T_key")
  1954. // 查询公司,验证T_key
  1955. _, err := Account.Read_Company_ByKey(key)
  1956. if err != nil {
  1957. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_key Err!"}
  1958. c.ServeJSON()
  1959. return
  1960. }
  1961. page, _ := c.GetInt("page")
  1962. if page < 1 {
  1963. page = 1
  1964. }
  1965. page_z, _ := c.GetInt("page_z")
  1966. if page_z < 1 {
  1967. page_z = conf.Page_size
  1968. }
  1969. if page_z > 50 {
  1970. c.Data["json"] = lib.JSONS{Code: 202, Msg: "page_z 超出限制"}
  1971. c.ServeJSON()
  1972. return
  1973. }
  1974. T_sn := c.GetString("T_sn")
  1975. T_id, T_id_Err := c.GetInt("T_id")
  1976. Time_start := c.GetString("Time_start")
  1977. Time_end := c.GetString("Time_end")
  1978. if len(T_sn) == 0 {
  1979. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn 格式错误"}
  1980. c.ServeJSON()
  1981. return
  1982. }
  1983. if T_id_Err != nil {
  1984. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id 格式错误"}
  1985. c.ServeJSON()
  1986. return
  1987. }
  1988. st, is := lib.TimeStrToTime(Time_start)
  1989. if !is {
  1990. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Time_start 格式不正确"}
  1991. c.ServeJSON()
  1992. return
  1993. }
  1994. if st.Add(40 * 24 * time.Hour).Before(time.Now()) {
  1995. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询时间范围超出限制(40天)"}
  1996. c.ServeJSON()
  1997. return
  1998. }
  1999. et, is := lib.TimeStrToTime(Time_end)
  2000. if !is {
  2001. c.Data["json"] = lib.JSONS{Code: 202, Msg: "Time_start 格式不正确"}
  2002. c.ServeJSON()
  2003. return
  2004. }
  2005. if et.Add(40 * 24 * time.Hour).Before(time.Now()) {
  2006. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询时间范围超出限制(40天)"}
  2007. c.ServeJSON()
  2008. return
  2009. }
  2010. type R_JSONS struct {
  2011. //必须的大写开头
  2012. DeviceSensor_data []Device.DeviceData_R
  2013. Num int
  2014. Page int
  2015. Page_size int
  2016. Pages []lib.Page_T
  2017. }
  2018. var r_jsons R_JSONS
  2019. var cnt int
  2020. Device.Read_DeviceSensorParameter_All_Map(T_sn, T_id)
  2021. r_jsons.DeviceSensor_data, cnt = Device.Read_DeviceData_ById_List(T_sn, T_id, Time_start, Time_end, page, page_z)
  2022. page_size := math.Ceil(float64(cnt) / float64(page_z))
  2023. r_jsons.Page = page
  2024. r_jsons.Page_size = int(page_size)
  2025. r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
  2026. r_jsons.Num = cnt
  2027. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  2028. c.ServeJSON()
  2029. return
  2030. }