dataexports.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  1. package system
  2. import (
  3. "encoding/json"
  4. "strings"
  5. "time"
  6. "dashoo.cn/backend/api/business/device"
  7. "dashoo.cn/backend/api/business/userchannels"
  8. . "dashoo.cn/backend/api/controllers"
  9. "dashoo.cn/utils"
  10. "github.com/signintech/gopdf"
  11. "github.com/tealeg/xlsx"
  12. )
  13. type DataExportsController struct {
  14. BaseController
  15. }
  16. type dataso2 struct {
  17. Timedata string
  18. Temperaturedata string
  19. Humiditydata string
  20. Voltagedata string
  21. O2data string
  22. }
  23. type datasco2 struct {
  24. Timedata string
  25. Temperaturedata string
  26. Humiditydata string
  27. Voltagedata string
  28. CO2data string
  29. }
  30. type dataspower struct {
  31. Timedata string
  32. Power string
  33. Supply string
  34. }
  35. type headnameo2 struct {
  36. Time string
  37. Temperature string
  38. Humidity string
  39. O2 string
  40. Voltage string
  41. Emptystr string
  42. TimeRange string
  43. }
  44. type datas struct {
  45. Timedata string
  46. Temperaturedata string
  47. Humiditydata string
  48. Voltagedata string
  49. }
  50. type headname struct {
  51. Time string
  52. Temperature string
  53. Humidity string
  54. Voltage string
  55. Emptystr string
  56. TimeRange string
  57. }
  58. type headnamepower struct {
  59. Time string
  60. Power string
  61. Supply string
  62. Emptystr string
  63. TimeRange string
  64. }
  65. type headnameliquidlevel struct {
  66. Time string
  67. Temperature string
  68. LiquidLevel string
  69. Emptystr string
  70. TimeRange string
  71. }
  72. // @Title 报表导出excel
  73. // @Description 报表导出excel
  74. // @Success 200 {object} business.device.DeviceChannels
  75. // @router /excel [get]
  76. func (this *DataExportsController) GetExcel() {
  77. timerange := ""
  78. svc := device.GetDeviceService(utils.DBE)
  79. svcuc := userchannels.GetUserChannelService(utils.DBE)
  80. channelid := svcuc.GetChannelids(utils.ToStr(this.User.Id))
  81. Uid := utils.ToStr(this.User.Id)
  82. start64, _ := this.GetInt64("start")
  83. end64, _ := this.GetInt64("end")
  84. begin_time := time.Unix(start64/1000, 0).Format("2006-01-02")
  85. end_time := time.Unix(end64/1000, 0).Format("2006-01-02")
  86. if begin_time == "" {
  87. timerange = utils.TimeFormat(time.Now().AddDate(0, 0, -7), "2006-01-02")
  88. } else {
  89. timerange = begin_time
  90. }
  91. if end_time == "" {
  92. timerange += " 至 " + utils.TimeFormat(time.Now(), "2006-01-02")
  93. } else {
  94. timerange += " 至 " + end_time
  95. }
  96. t1, _ := utils.TimeParse(begin_time+" 00:00:00", "2006-1-2 15:4:5")
  97. t2, _ := utils.TimeParse(end_time+" 23:59:59", "2006-1-2 15:4:5")
  98. start := t1.Unix()
  99. end := t2.Unix()
  100. u, p := this.GetuAndp()
  101. xlsx.PagePrintfooterContant = "第 &P 页"
  102. xlsx.PagePrintheadContant = ""
  103. f := xlsx.NewFile()
  104. if this.GetString("type") == "2" {
  105. where := " (a.CreateUserId=" + utils.ToStr(this.User.Id) + " or a.Id in (" + strings.Join(channelid, ",") + ")) and a.DataItem in (" + ChannelItem_Report + ") "
  106. _, channel := svc.GetPagingEntitiesWithOrderSearch(-1, 8, "c.sortcode,a.CreateOn desc", where, Uid)
  107. for i := 0; i < len(channel); i++ {
  108. name := channel[i].Title
  109. if name == "" {
  110. name = channel[i].Code
  111. }
  112. xlssheetname := utils.ToStr(i+1) + "." + name
  113. if DeviceItemContainint(ChannelItem_HaveO2, channel[i].DataItem) {
  114. this.Saveo2Xlsx(xlssheetname, channel[i].Code, timerange, u, p, f, start, end)
  115. } else if DeviceItemContainint(ChannelItem_HaveCO2, channel[i].DataItem) {
  116. this.Saveco2Xlsx(xlssheetname, channel[i].Code, timerange, u, p, f, start, end)
  117. } else if DeviceItemContainint(ChannelItem_Power, channel[i].DataItem) {
  118. this.SavepowerXlsx(xlssheetname, channel[i].Code, timerange, u, p, f, start, end)
  119. } else if DeviceItemContainint(ChannelItem_Temperature, channel[i].DataItem) {
  120. this.SaveTemperatureXlsx(xlssheetname, channel[i].Code, timerange, u, p, f, start, end)
  121. } else if DeviceItemContainint(ChannelItem_LiquidLevel, channel[i].DataItem) {
  122. this.SaveliquidlevelXlsx(xlssheetname, channel[i].Code, timerange, u, p, f, start, end)
  123. } else {
  124. this.SaveXlsx(xlssheetname, channel[i].Code, timerange, u, p, f, start, end)
  125. }
  126. }
  127. } else if this.GetString("serials") != "" {
  128. serials := strings.Split(this.GetString("serials"), ",")
  129. var codestring []string
  130. for i := 0; i < len(serials); i++ {
  131. codestring = append(codestring, "'c"+serials[i]+"'")
  132. }
  133. where := " (a.CreateUserId=" + utils.ToStr(this.User.Id) + " or a.Id in (" + strings.Join(channelid, ",") + ")) and a.DataItem in (" + ChannelItem_Report + ") and a.Code in(" + strings.Join(codestring, ",") + ")"
  134. _, channel := svc.GetPagingEntitiesWithOrderSearch(-1, 8, "c.sortcode,a.CreateOn desc", where, Uid)
  135. for i := 0; i < len(channel); i++ {
  136. name := channel[i].Title
  137. if name == "" {
  138. name = channel[i].Code
  139. }
  140. xlssheetname := utils.ToStr(i+1) + "." + name
  141. if DeviceItemContainint(ChannelItem_HaveO2, channel[i].DataItem) {
  142. this.Saveo2Xlsx(xlssheetname, channel[i].Code, timerange, u, p, f, start, end)
  143. } else if DeviceItemContainint(ChannelItem_HaveCO2, channel[i].DataItem) {
  144. this.Saveco2Xlsx(xlssheetname, channel[i].Code, timerange, u, p, f, start, end)
  145. } else if DeviceItemContainint(ChannelItem_Power, channel[i].DataItem) {
  146. this.SavepowerXlsx(xlssheetname, channel[i].Code, timerange, u, p, f, start, end)
  147. } else if DeviceItemContainint(ChannelItem_Temperature, channel[i].DataItem) {
  148. this.SaveTemperatureXlsx(xlssheetname, channel[i].Code, timerange, u, p, f, start, end)
  149. } else if DeviceItemContainint(ChannelItem_LiquidLevel, channel[i].DataItem) {
  150. this.SaveliquidlevelXlsx(xlssheetname, channel[i].Code, timerange, u, p, f, start, end)
  151. } else {
  152. this.SaveXlsx(xlssheetname, channel[i].Code, timerange, u, p, f, start, end)
  153. }
  154. }
  155. }
  156. SaveDirectory("static/file/excel/report/" + this.GetAccode())
  157. f.Save("static/file/excel/report/" + this.GetAccode() + "/devicedata.xlsx")
  158. //this.Ctx.Output.Download("static/upload/excel/devicedata.xlsx")
  159. var errinfo ErrorInfo
  160. errinfo.Message = this.Ctx.Request.Host + "/static/file/excel/report/" + this.GetAccode() + "/devicedata.xlsx"
  161. errinfo.Code = 0
  162. this.Data["json"] = &errinfo
  163. this.ServeJSON()
  164. }
  165. func (this *DataExportsController) SaveTemperatureXlsx(name, code, timerange, u, p string, f *xlsx.File, start, end int64) {
  166. sheet, _ := f.AddSheet(name)
  167. rowname := sheet.AddRow()
  168. celln := rowname.AddCell()
  169. celln.Value = "名称:" + this.User.Realname
  170. rowhead := sheet.AddRow()
  171. _, cols, data := GetChannelInfov2(code, u, p, 1, start, end)
  172. headsname := headname{
  173. "时间",
  174. "温度",
  175. "电压",
  176. "",
  177. timerange,
  178. "",
  179. }
  180. rowhead.WriteStruct(&headsname, -1)
  181. indext := utils.SliceIndexOf(cols, "temperature")
  182. indexv := utils.SliceIndexOf(cols, "voltage")
  183. indextime := utils.SliceIndexOf(cols, "time")
  184. indexstate := utils.SliceIndexOf(cols, "devicestate") //设备状态
  185. for j, l := 0, len(data); j < l; j++ {
  186. row := sheet.AddRow()
  187. valuetime, _ := data[l-j-1][indextime].(json.Number).Float64()
  188. // valuetime, _ := data[l-j-1][indextime].(float64)
  189. tstr := ""
  190. if indext > -1 {
  191. tstr = utils.ToStr(data[l-j-1][indext])
  192. bbstate := ""
  193. if indexstate > -1 && utils.ToStr(data[l-j-1][indexstate]) == "2" {
  194. bbstate = " (维修)"
  195. } else if indexstate > -1 && utils.ToStr(data[l-j-1][indexstate]) == "3" {
  196. bbstate = " (停用)"
  197. } else if indexstate > -1 && utils.ToStr(data[l-j-1][indexstate]) == "4" {
  198. bbstate = " (除霜)"
  199. }
  200. tstr += bbstate
  201. }
  202. vstr := ""
  203. if indexv > -1 {
  204. vstr = utils.ToStr(data[l-j-1][indexv])
  205. }
  206. testStruct := datas{
  207. time.Unix(int64(valuetime), 0).Format("2006-01-02 15:04:05"),
  208. tstr,
  209. vstr,
  210. "",
  211. }
  212. row.WriteStruct(&testStruct, -1)
  213. }
  214. sheet.Cols[0].Width = 20 //设置时间单元格的宽度
  215. }
  216. func (this *DataExportsController) SaveXlsx(name, code, timerange, u, p string, f *xlsx.File, start, end int64) {
  217. sheet, _ := f.AddSheet(name)
  218. rowname := sheet.AddRow()
  219. celln := rowname.AddCell()
  220. celln.Value = "名称:" + this.User.Realname
  221. rowhead := sheet.AddRow()
  222. _, cols, data := GetChannelInfov2(code, u, p, 1, start, end)
  223. headsname := headname{
  224. "时间",
  225. "温度",
  226. "湿度",
  227. "电压",
  228. "",
  229. timerange,
  230. }
  231. rowhead.WriteStruct(&headsname, -1)
  232. indext := utils.SliceIndexOf(cols, "temperature")
  233. indexh := utils.SliceIndexOf(cols, "humidity")
  234. indexv := utils.SliceIndexOf(cols, "voltage")
  235. indextime := utils.SliceIndexOf(cols, "time")
  236. indexstate := utils.SliceIndexOf(cols, "devicestate") //设备状态
  237. for j, l := 0, len(data); j < l; j++ {
  238. row := sheet.AddRow()
  239. valuetime, _ := data[l-j-1][indextime].(json.Number).Float64()
  240. // valuetime, _ := data[l-j-1][indextime].(float64)
  241. tstr := ""
  242. if indext > -1 {
  243. tstr = utils.ToStr(data[l-j-1][indext])
  244. bbstate := ""
  245. if indexstate > -1 && utils.ToStr(data[l-j-1][indexstate]) == "2" {
  246. bbstate = " (维修)"
  247. } else if indexstate > -1 && utils.ToStr(data[l-j-1][indexstate]) == "3" {
  248. bbstate = " (停用)"
  249. } else if indexstate > -1 && utils.ToStr(data[l-j-1][indexstate]) == "4" {
  250. bbstate = " (除霜)"
  251. }
  252. tstr += bbstate
  253. }
  254. hstr := ""
  255. if indexh > -1 {
  256. hstr = utils.ToStr(data[l-j-1][indexh])
  257. }
  258. vstr := ""
  259. if indexv > -1 {
  260. vstr = utils.ToStr(data[l-j-1][indexv])
  261. }
  262. testStruct := datas{
  263. time.Unix(int64(valuetime), 0).Format("2006-01-02 15:04:05"),
  264. tstr,
  265. hstr,
  266. vstr,
  267. }
  268. row.WriteStruct(&testStruct, -1)
  269. }
  270. sheet.Cols[0].Width = 20 //设置时间单元格的宽度
  271. }
  272. func (this *DataExportsController) Saveo2Xlsx(name, code, timerange, u, p string, f *xlsx.File, start, end int64) {
  273. sheet, _ := f.AddSheet(name)
  274. rowname := sheet.AddRow()
  275. celln := rowname.AddCell()
  276. celln.Value = "名称:" + this.User.Realname
  277. rowhead := sheet.AddRow()
  278. _, cols, data := GetChannelInfov2(code, u, p, 1, start, end)
  279. headsname := headnameo2{
  280. "时间",
  281. "温度",
  282. "湿度",
  283. "电压",
  284. "氧气",
  285. "",
  286. timerange,
  287. }
  288. rowhead.WriteStruct(&headsname, -1)
  289. indext := utils.SliceIndexOf(cols, "temperature")
  290. indexh := utils.SliceIndexOf(cols, "humidity")
  291. indexv := utils.SliceIndexOf(cols, "voltage")
  292. indextime := utils.SliceIndexOf(cols, "time")
  293. indexo2 := utils.SliceIndexOf(cols, "o2")
  294. for j, l := 0, len(data); j < l; j++ {
  295. row := sheet.AddRow()
  296. valuetime, _ := data[l-j-1][indextime].(json.Number).Float64()
  297. //valuetime, _ := data[l-j-1][indextime].(float64)
  298. tstr := ""
  299. if indext > -1 {
  300. tstr = utils.ToStr(data[l-j-1][indext])
  301. }
  302. hstr := ""
  303. if indexh > -1 {
  304. hstr = utils.ToStr(data[l-j-1][indexh])
  305. }
  306. vstr := ""
  307. if indexv > -1 {
  308. vstr = utils.ToStr(data[l-j-1][indexv])
  309. }
  310. o2str := ""
  311. if indexo2 > -1 {
  312. o2str = utils.ToStr(data[l-j-1][indexo2])
  313. }
  314. testStruct := dataso2{
  315. time.Unix(int64(valuetime), 0).Format("2006-01-02 15:04:05"),
  316. tstr,
  317. hstr,
  318. vstr,
  319. o2str,
  320. }
  321. row.WriteStruct(&testStruct, -1)
  322. }
  323. sheet.Cols[0].Width = 20 //设置时间单元格的宽度
  324. }
  325. func (this *DataExportsController) Saveco2Xlsx(name, code, timerange, u, p string, f *xlsx.File, start, end int64) {
  326. sheet, _ := f.AddSheet(name)
  327. rowname := sheet.AddRow()
  328. celln := rowname.AddCell()
  329. celln.Value = "名称:" + this.User.Realname
  330. rowhead := sheet.AddRow()
  331. _, cols, data := GetChannelInfov2(code, u, p, 1, start, end)
  332. headsname := headnameo2{
  333. "时间",
  334. "温度",
  335. "湿度",
  336. "电压",
  337. "二氧化碳",
  338. "",
  339. timerange,
  340. }
  341. rowhead.WriteStruct(&headsname, -1)
  342. indext := utils.SliceIndexOf(cols, "temperature")
  343. indexh := utils.SliceIndexOf(cols, "humidity")
  344. indexv := utils.SliceIndexOf(cols, "voltage")
  345. indextime := utils.SliceIndexOf(cols, "time")
  346. indexco2 := utils.SliceIndexOf(cols, "co2")
  347. for j, l := 0, len(data); j < l; j++ {
  348. row := sheet.AddRow()
  349. valuetime, _ := data[l-j-1][indextime].(json.Number).Float64()
  350. // valuetime, _ := data[l-j-1][indextime].(float64)
  351. tstr := ""
  352. if indext > -1 {
  353. tstr = utils.ToStr(data[l-j-1][indext])
  354. }
  355. hstr := ""
  356. if indexh > -1 {
  357. hstr = utils.ToStr(data[l-j-1][indexh])
  358. }
  359. vstr := ""
  360. if indexv > -1 {
  361. vstr = utils.ToStr(data[l-j-1][indexv])
  362. }
  363. co2str := ""
  364. if indexco2 > -1 {
  365. co2str = utils.ToStr(data[l-j-1][indexco2])
  366. }
  367. testStruct := datasco2{
  368. time.Unix(int64(valuetime), 0).Format("2006-01-02 15:04:05"),
  369. tstr,
  370. hstr,
  371. vstr,
  372. co2str,
  373. }
  374. row.WriteStruct(&testStruct, -1)
  375. }
  376. sheet.Cols[0].Width = 20 //设置时间单元格的宽度
  377. }
  378. func (this *DataExportsController) SavepowerXlsx(name, code, timerange, u, p string, f *xlsx.File, start, end int64) {
  379. sheet, _ := f.AddSheet(name)
  380. rowname := sheet.AddRow()
  381. celln := rowname.AddCell()
  382. celln.Value = "名称:" + this.User.Realname
  383. rowhead := sheet.AddRow()
  384. _, cols, data := GetChannelInfov2(code, u, p, 1, start, end)
  385. headsname := headnamepower{
  386. "时间",
  387. "功率",
  388. "市电",
  389. "",
  390. timerange,
  391. }
  392. rowhead.WriteStruct(&headsname, -1)
  393. indexp := utils.SliceIndexOf(cols, "electricalpower")
  394. indexs := utils.SliceIndexOf(cols, "electricalsupply")
  395. indextime := utils.SliceIndexOf(cols, "time")
  396. for j, l := 0, len(data); j < l; j++ {
  397. row := sheet.AddRow()
  398. valuetime, _ := data[l-j-1][indextime].(json.Number).Float64()
  399. // valuetime, _ := data[l-j-1][indextime].(float64)
  400. pstr := ""
  401. if indexp > -1 {
  402. pstr = utils.ToStr(data[l-j-1][indexp])
  403. }
  404. sstr := ""
  405. if indexs > -1 {
  406. sstr = utils.ToStr(data[l-j-1][indexs])
  407. }
  408. testStruct := dataspower{
  409. time.Unix(int64(valuetime), 0).Format("2006-01-02 15:04:05"),
  410. pstr,
  411. sstr,
  412. }
  413. row.WriteStruct(&testStruct, -1)
  414. }
  415. sheet.Cols[0].Width = 20 //设置时间单元格的宽度
  416. }
  417. func (this *DataExportsController) SaveliquidlevelXlsx(name, code, timerange, u, p string, f *xlsx.File, start, end int64) {
  418. sheet, _ := f.AddSheet(name)
  419. rowname := sheet.AddRow()
  420. celln := rowname.AddCell()
  421. celln.Value = "名称:" + this.User.Realname
  422. rowhead := sheet.AddRow()
  423. _, cols, data := GetChannelInfov2(code, u, p, 1, start, end)
  424. headsname := headnameliquidlevel{
  425. "时间",
  426. "温度",
  427. "液位",
  428. "",
  429. timerange,
  430. }
  431. rowhead.WriteStruct(&headsname, -1)
  432. indext := utils.SliceIndexOf(cols, "temperature")
  433. indexl := utils.SliceIndexOf(cols, "liquidlevel")
  434. indextime := utils.SliceIndexOf(cols, "time")
  435. for j, l := 0, len(data); j < l; j++ {
  436. row := sheet.AddRow()
  437. valuetime, _ := data[l-j-1][indextime].(json.Number).Float64()
  438. // valuetime, _ := data[l-j-1][indextime].(float64)
  439. pstr := ""
  440. if indext > -1 {
  441. pstr = utils.ToStr(data[l-j-1][indext])
  442. }
  443. sstr := ""
  444. if indexl > -1 {
  445. sstr = utils.ToStr(data[l-j-1][indexl])
  446. }
  447. testStruct := dataspower{
  448. time.Unix(int64(valuetime), 0).Format("2006-01-02 15:04:05"),
  449. pstr,
  450. sstr,
  451. }
  452. row.WriteStruct(&testStruct, -1)
  453. }
  454. sheet.Cols[0].Width = 20 //设置时间单元格的宽度
  455. }
  456. // @Title 报表导出pdf
  457. // @Description 报表导出pdf
  458. // @Success 200 {object} business.device.DeviceChannels
  459. // @router /pdf [get]
  460. func (this *DataExportsController) GetPDF() {
  461. timerange := ""
  462. svc := device.GetDeviceService(utils.DBE)
  463. svcuc := userchannels.GetUserChannelService(utils.DBE)
  464. channelid := svcuc.GetChannelids(utils.ToStr(this.User.Id))
  465. Uid := utils.ToStr(this.User.Id)
  466. channel := make([]device.DeviceChannels, 0)
  467. if this.GetString("type") == "1" {
  468. serials := strings.Split(this.GetString("serials"), ",")
  469. var codestring []string
  470. for i := 0; i < len(serials); i++ {
  471. codestring = append(codestring, "'c"+serials[i]+"'")
  472. }
  473. where := " (a.CreateUserId=" + utils.ToStr(this.User.Id) + " or a.Id in (" + strings.Join(channelid, ",") + ")) and a.DataItem in (" + ChannelItem_Report + ") and a.Code in(" + strings.Join(codestring, ",") + ")"
  474. _, channel = svc.GetPagingEntitiesWithOrderSearch(-1, 8, "c.sortcode,a.CreateOn desc", where, Uid)
  475. } else {
  476. where := " (a.CreateUserId=" + utils.ToStr(this.User.Id) + " or a.Id in (" + strings.Join(channelid, ",") + ")) and a.DataItem in (" + ChannelItem_Report + ") "
  477. _, channel = svc.GetPagingEntitiesWithOrderSearch(-1, 8, "c.sortcode,a.CreateOn desc", where, Uid)
  478. }
  479. start64, _ := this.GetInt64("start")
  480. end64, _ := this.GetInt64("end")
  481. begin_time := time.Unix(start64/1000, 0).Format("2006-01-02")
  482. end_time := time.Unix(end64/1000, 0).Format("2006-01-02")
  483. if begin_time == "" {
  484. this.Data["begin"] = utils.TimeFormat(time.Now().AddDate(0, 0, -7), "2006-01-02")
  485. timerange = utils.TimeFormat(time.Now().AddDate(0, 0, -7), "2006-01-02")
  486. } else {
  487. this.Data["begin"] = begin_time
  488. timerange = begin_time
  489. }
  490. if end_time == "" {
  491. this.Data["end"] = utils.TimeFormat(time.Now(), "2006-01-02")
  492. timerange += " 至 " + utils.TimeFormat(time.Now(), "2006-01-02")
  493. } else {
  494. this.Data["end"] = end_time
  495. timerange += " 至 " + end_time
  496. }
  497. t1, _ := utils.TimeParse(begin_time+" 00:00:00", "2006-1-2 15:4:5")
  498. t2, _ := utils.TimeParse(end_time+" 23:59:59", "2006-1-2 15:4:5")
  499. start := t1.Unix()
  500. end := t2.Unix()
  501. u, p := this.GetuAndp()
  502. pdf := gopdf.GoPdf{}
  503. pdf.Start(gopdf.Config{Unit: "pt", PageSize: gopdf.Rect{W: (200*4 + 600), H: (50*25 + 250)}})
  504. pdf.AddTTFFont("HDZB_5", "static/fonts/msyh.ttf")
  505. y := 0
  506. for k, v := range channel {
  507. if v.Code != "" {
  508. _, cols, data := GetChannelInfov2(v.Code, u, p, 1, start, end)
  509. indext := utils.SliceIndexOf(cols, "temperature") //温度
  510. indexh := utils.SliceIndexOf(cols, "humidity") //湿度
  511. indexv := utils.SliceIndexOf(cols, "voltage") //电压
  512. indextime := utils.SliceIndexOf(cols, "time") //时间
  513. indexco2 := utils.SliceIndexOf(cols, "co2") //二氧化碳
  514. indexo2 := utils.SliceIndexOf(cols, "o2") //氧气
  515. indexpower := utils.SliceIndexOf(cols, "electricalpower") //功率
  516. indexsupply := utils.SliceIndexOf(cols, "electricalsupply") //市电
  517. indexstate := utils.SliceIndexOf(cols, "devicestate") //设备状态
  518. indexliquidlevel := utils.SliceIndexOf(cols, "liquidlevel") //液位
  519. num := 0
  520. if len(cols) == 0 {
  521. if DeviceItemContainint(ChannelItem_TAndH, v.DataItem) {
  522. num = num + 3
  523. } else if DeviceItemContainint(ChannelItem_Temperature, v.DataItem) {
  524. num = num + 2
  525. } else if DeviceItemContainint(ChannelItem_O2, v.DataItem) {
  526. num = num + 4
  527. } else if DeviceItemContainint(ChannelItem_Co2, v.DataItem) {
  528. num = num + 4
  529. } else if DeviceItemContainint(ChannelItem_Power, v.DataItem) {
  530. num = num + 2
  531. } else if DeviceItemContainint(ChannelItem_HaveLiquidLevel, v.DataItem) {
  532. num = num + 2
  533. }
  534. } else {
  535. if indexv > -1 && DeviceItemContainint(ChannelItem_HaveV, v.DataItem) {
  536. num = num + 1
  537. }
  538. if indext > -1 && DeviceItemContainint(ChannelItem_HaveT, v.DataItem) {
  539. num = num + 1
  540. }
  541. if indexh > -1 && DeviceItemContainint(ChannelItem_HaveH, v.DataItem) {
  542. num = num + 1
  543. }
  544. if indexco2 > -1 && DeviceItemContainint(ChannelItem_HaveCO2, v.DataItem) {
  545. num = num + 1
  546. }
  547. if indexo2 > -1 && DeviceItemContainint(ChannelItem_HaveO2, v.DataItem) {
  548. num = num + 1
  549. }
  550. if indexpower > -1 && DeviceItemContainint(ChannelItem_Power, v.DataItem) {
  551. num = num + 1
  552. }
  553. if indexsupply > -1 && DeviceItemContainint(ChannelItem_Power, v.DataItem) {
  554. num = num + 1
  555. }
  556. if indexliquidlevel > -1 && DeviceItemContainint(ChannelItem_LiquidLevel, v.DataItem) {
  557. num = num + 1
  558. }
  559. }
  560. var times float64
  561. var report float64
  562. var i float64
  563. report = float64(25)
  564. times = float64(num)
  565. //pdf.Start(gopdf.Config{Unit: "pt", PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4
  566. pdf.AddPage()
  567. pdf.SetFont("HDZB_5", "B", 24) //字体
  568. pdf.Line(100, 100, 100, 50*report+150) //竖线
  569. pdf.Line(100, 100, 200*times+500, 100) //横线
  570. //页码
  571. y = y + 1
  572. yy := utils.ToStr(y)
  573. pdf.Curr.X = (200*times + 500) / 2
  574. pdf.Curr.Y = 1450
  575. pdf.Cell(nil, "第 "+yy+" 页")
  576. for i = 0; i <= times; i++ {
  577. pdf.Line(200*i+500, 100, 200*i+500, 50*report+150) //竖线
  578. }
  579. for i = 0; i <= report; i++ {
  580. pdf.Line(100, 50*i+150, 200*times+500, 50*i+150) //横线
  581. }
  582. pdf.Curr.X = 130
  583. pdf.Curr.Y = 30
  584. pdf.Cell(nil, "名称:"+this.User.Realname)
  585. pdf.Curr.X = 130
  586. pdf.Curr.Y = 70
  587. pdf.Cell(nil, "设备:"+channel[k].Title)
  588. pdf.Curr.X = 200*times + 200
  589. pdf.Curr.Y = 70
  590. pdf.Cell(nil, begin_time+"至"+end_time)
  591. pdf.Curr.X = 150
  592. pdf.Curr.Y = 120
  593. pdf.Cell(nil, "时间")
  594. if len(cols) == 0 {
  595. if DeviceItemContainint(ChannelItem_TAndH, v.DataItem) {
  596. pdf.Curr.X = 550
  597. pdf.Curr.Y = 120
  598. pdf.Cell(nil, "电压")
  599. pdf.Curr.X = 750
  600. pdf.Curr.Y = 120
  601. pdf.Cell(nil, "温度")
  602. pdf.Curr.X = 950
  603. pdf.Curr.Y = 120
  604. pdf.Cell(nil, "湿度")
  605. } else if DeviceItemContainint(ChannelItem_Temperature, v.DataItem) {
  606. pdf.Curr.X = 550
  607. pdf.Curr.Y = 120
  608. pdf.Cell(nil, "电压")
  609. pdf.Curr.X = 750
  610. pdf.Curr.Y = 120
  611. pdf.Cell(nil, "温度")
  612. } else if DeviceItemContainint(ChannelItem_O2, v.DataItem) {
  613. pdf.Curr.X = 550
  614. pdf.Curr.Y = 120
  615. pdf.Cell(nil, "电压")
  616. pdf.Curr.X = 750
  617. pdf.Curr.Y = 120
  618. pdf.Cell(nil, "温度")
  619. pdf.Curr.X = 950
  620. pdf.Curr.Y = 120
  621. pdf.Cell(nil, "湿度")
  622. pdf.Curr.X = 1150
  623. pdf.Curr.Y = 120
  624. pdf.Cell(nil, "氧气")
  625. } else if DeviceItemContainint(ChannelItem_Co2, v.DataItem) {
  626. pdf.Curr.X = 550
  627. pdf.Curr.Y = 120
  628. pdf.Cell(nil, "电压")
  629. pdf.Curr.X = 750
  630. pdf.Curr.Y = 120
  631. pdf.Cell(nil, "温度")
  632. pdf.Curr.X = 950
  633. pdf.Curr.Y = 120
  634. pdf.Cell(nil, "湿度")
  635. pdf.Curr.X = 1150
  636. pdf.Curr.Y = 120
  637. pdf.Cell(nil, "二氧化碳")
  638. } else if DeviceItemContainint(ChannelItem_Power, v.DataItem) {
  639. pdf.Curr.X = 550
  640. pdf.Curr.Y = 120
  641. pdf.Cell(nil, "功率")
  642. pdf.Curr.X = 750
  643. pdf.Curr.Y = 120
  644. pdf.Cell(nil, "市电")
  645. } else if DeviceItemContainint(ChannelItem_HaveLiquidLevel, v.DataItem) {
  646. pdf.Curr.X = 550
  647. pdf.Curr.Y = 120
  648. pdf.Cell(nil, "液位")
  649. }
  650. } else {
  651. if indexv > -1 && DeviceItemContainint(ChannelItem_HaveV, v.DataItem) {
  652. pdf.Curr.X = 550
  653. pdf.Curr.Y = 120
  654. pdf.Cell(nil, "电压")
  655. }
  656. if indext > -1 && DeviceItemContainint(ChannelItem_HaveT, v.DataItem) {
  657. pdf.Curr.X = 750
  658. pdf.Curr.Y = 120
  659. pdf.Cell(nil, "温度")
  660. }
  661. if indexh > -1 && DeviceItemContainint(ChannelItem_HaveH, v.DataItem) {
  662. pdf.Curr.X = 950
  663. pdf.Curr.Y = 120
  664. pdf.Cell(nil, "湿度")
  665. }
  666. if indexco2 > -1 && DeviceItemContainint(ChannelItem_HaveCO2, v.DataItem) {
  667. pdf.Curr.X = 1150
  668. pdf.Curr.Y = 120
  669. pdf.Cell(nil, "二氧化碳")
  670. }
  671. if indexo2 > -1 && DeviceItemContainint(ChannelItem_HaveO2, v.DataItem) {
  672. pdf.Curr.X = 1150
  673. pdf.Curr.Y = 120
  674. pdf.Cell(nil, "氧气")
  675. }
  676. if indexpower > -1 && DeviceItemContainint(ChannelItem_Power, v.DataItem) {
  677. pdf.Curr.X = 550
  678. pdf.Curr.Y = 120
  679. pdf.Cell(nil, "功率")
  680. }
  681. if indexsupply > -1 && DeviceItemContainint(ChannelItem_Power, v.DataItem) {
  682. pdf.Curr.X = 750
  683. pdf.Curr.Y = 120
  684. pdf.Cell(nil, "市电")
  685. }
  686. if DeviceItemContainint(ChannelItem_LiquidLevel, v.DataItem) {
  687. pdf.Curr.X = 550
  688. pdf.Curr.Y = 120
  689. pdf.Cell(nil, "液位")
  690. }
  691. }
  692. for j, l := 0, len(data); j < l; j++ {
  693. yu := j % 25
  694. if j != 0 && yu == 0 {
  695. pdf.AddPage()
  696. pdf.SetFont("HDZB_5", "B", 24) //字体
  697. pdf.Line(100, 100, 100, 50*report+150) //竖线
  698. pdf.Line(100, 100, 200*times+500, 100) //横线
  699. for i = 0; i <= times; i++ {
  700. pdf.Line(200*i+500, 100, 200*i+500, 50*report+150) //竖线
  701. }
  702. for i = 0; i <= report; i++ {
  703. pdf.Line(100, 50*i+150, 200*times+500, 50*i+150) //横线
  704. }
  705. //页码
  706. y = y + 1
  707. yy := utils.ToStr(y)
  708. pdf.Curr.X = (200*times + 500) / 2
  709. pdf.Curr.Y = 1450
  710. pdf.Cell(nil, "第 "+yy+" 页")
  711. pdf.Curr.X = 130
  712. pdf.Curr.Y = 30
  713. pdf.Cell(nil, "名称:"+this.User.Realname)
  714. pdf.Curr.X = 130
  715. pdf.Curr.Y = 70
  716. pdf.Cell(nil, "设备"+":"+channel[k].Title)
  717. pdf.Curr.X = 200*times + 200
  718. pdf.Curr.Y = 70
  719. pdf.Cell(nil, begin_time+"至"+end_time)
  720. pdf.Curr.X = 150
  721. pdf.Curr.Y = 120
  722. pdf.Cell(nil, "时间")
  723. if len(cols) == 0 {
  724. if DeviceItemContainint(ChannelItem_TAndH, v.DataItem) {
  725. pdf.Curr.X = 550
  726. pdf.Curr.Y = 120
  727. pdf.Cell(nil, "电压")
  728. pdf.Curr.X = 750
  729. pdf.Curr.Y = 120
  730. pdf.Cell(nil, "温度")
  731. pdf.Curr.X = 950
  732. pdf.Curr.Y = 120
  733. pdf.Cell(nil, "湿度")
  734. } else if DeviceItemContainint(ChannelItem_Temperature, v.DataItem) {
  735. pdf.Curr.X = 550
  736. pdf.Curr.Y = 120
  737. pdf.Cell(nil, "电压")
  738. pdf.Curr.X = 750
  739. pdf.Curr.Y = 120
  740. pdf.Cell(nil, "温度")
  741. } else if DeviceItemContainint(ChannelItem_O2, v.DataItem) {
  742. pdf.Curr.X = 550
  743. pdf.Curr.Y = 120
  744. pdf.Cell(nil, "电压")
  745. pdf.Curr.X = 750
  746. pdf.Curr.Y = 120
  747. pdf.Cell(nil, "温度")
  748. pdf.Curr.X = 950
  749. pdf.Curr.Y = 120
  750. pdf.Cell(nil, "湿度")
  751. pdf.Curr.X = 1150
  752. pdf.Curr.Y = 120
  753. pdf.Cell(nil, "氧气")
  754. } else if DeviceItemContainint(ChannelItem_Co2, v.DataItem) {
  755. pdf.Curr.X = 550
  756. pdf.Curr.Y = 120
  757. pdf.Cell(nil, "电压")
  758. pdf.Curr.X = 750
  759. pdf.Curr.Y = 120
  760. pdf.Cell(nil, "温度")
  761. pdf.Curr.X = 950
  762. pdf.Curr.Y = 120
  763. pdf.Cell(nil, "湿度")
  764. pdf.Curr.X = 1150
  765. pdf.Curr.Y = 120
  766. pdf.Cell(nil, "二氧化碳")
  767. } else if DeviceItemContainint(ChannelItem_Power, v.DataItem) {
  768. pdf.Curr.X = 550
  769. pdf.Curr.Y = 120
  770. pdf.Cell(nil, "功率")
  771. pdf.Curr.X = 750
  772. pdf.Curr.Y = 120
  773. pdf.Cell(nil, "市电")
  774. } else if DeviceItemContainint(ChannelItem_LiquidLevel, v.DataItem) {
  775. pdf.Curr.X = 550
  776. pdf.Curr.Y = 120
  777. pdf.Cell(nil, "液位")
  778. }
  779. } else {
  780. if indexv > -1 && DeviceItemContainint(ChannelItem_HaveV, v.DataItem) {
  781. pdf.Curr.X = 550
  782. pdf.Curr.Y = 120
  783. pdf.Cell(nil, "电压")
  784. }
  785. if indext > -1 && DeviceItemContainint(ChannelItem_HaveT, v.DataItem) {
  786. pdf.Curr.X = 750
  787. pdf.Curr.Y = 120
  788. pdf.Cell(nil, "温度")
  789. }
  790. if indexh > -1 && DeviceItemContainint(ChannelItem_HaveH, v.DataItem) {
  791. pdf.Curr.X = 950
  792. pdf.Curr.Y = 120
  793. pdf.Cell(nil, "湿度")
  794. }
  795. if indexco2 > -1 && DeviceItemContainint(ChannelItem_HaveCO2, v.DataItem) {
  796. pdf.Curr.X = 1150
  797. pdf.Curr.Y = 120
  798. pdf.Cell(nil, "二氧化碳")
  799. }
  800. if indexo2 > -1 && DeviceItemContainint(ChannelItem_HaveO2, v.DataItem) {
  801. pdf.Curr.X = 1150
  802. pdf.Curr.Y = 120
  803. pdf.Cell(nil, "氧气")
  804. }
  805. if indexpower > -1 && DeviceItemContainint(ChannelItem_Power, v.DataItem) {
  806. pdf.Curr.X = 550
  807. pdf.Curr.Y = 120
  808. pdf.Cell(nil, "功率")
  809. }
  810. if indexsupply > -1 && DeviceItemContainint(ChannelItem_Power, v.DataItem) {
  811. pdf.Curr.X = 750
  812. pdf.Curr.Y = 120
  813. pdf.Cell(nil, "市电")
  814. }
  815. if DeviceItemContainint(ChannelItem_LiquidLevel, v.DataItem) {
  816. pdf.Curr.X = 550
  817. pdf.Curr.Y = 120
  818. pdf.Cell(nil, "液位")
  819. }
  820. }
  821. }
  822. valuetime, _ := data[l-j-1][indextime].(json.Number).Float64()
  823. times := time.Unix(int64(valuetime), 0).Format("2006-01-02 15:04:05") //时间
  824. //时间
  825. pdf.Curr.X = 150
  826. pdf.Curr.Y = 50*float64(yu+1) + 120
  827. pdf.Cell(nil, times)
  828. voltage := ""
  829. if indexv > -1 && DeviceItemContainint(ChannelItem_HaveV, v.DataItem) {
  830. voltage = utils.ToStr(data[l-j-1][indexv])
  831. //电压
  832. pdf.Curr.X = 550
  833. pdf.Curr.Y = 50*float64(yu+1) + 120
  834. pdf.Cell(nil, voltage)
  835. }
  836. temperature := ""
  837. if indext > -1 && DeviceItemContainint(ChannelItem_HaveT, v.DataItem) {
  838. temperature = utils.ToStr(data[l-j-1][indext])
  839. //温度
  840. pdf.Curr.X = 750
  841. pdf.Curr.Y = 50*float64(yu+1) + 120
  842. bbstate := ""
  843. if indexstate > -1 && utils.ToStr(data[l-j-1][indexstate]) == "2" {
  844. bbstate = " (维修)"
  845. } else if indexstate > -1 && utils.ToStr(data[l-j-1][indexstate]) == "3" {
  846. bbstate = " (停用)"
  847. } else if indexstate > -1 && utils.ToStr(data[l-j-1][indexstate]) == "4" {
  848. bbstate = " (除霜)"
  849. }
  850. pdf.Cell(nil, temperature+bbstate)
  851. }
  852. humidity := ""
  853. if indexh > -1 && DeviceItemContainint(ChannelItem_HaveH, v.DataItem) {
  854. humidity = utils.ToStr(data[l-j-1][indexh])
  855. //湿度
  856. pdf.Curr.X = 950
  857. pdf.Curr.Y = 50*float64(yu+1) + 120
  858. pdf.Cell(nil, humidity)
  859. }
  860. co2 := ""
  861. if indexco2 > -1 && DeviceItemContainint(ChannelItem_HaveCO2, v.DataItem) {
  862. co2 = utils.ToStr(data[l-j-1][indexco2])
  863. //二氧化碳
  864. pdf.Curr.X = 1150
  865. pdf.Curr.Y = 50*float64(yu+1) + 120
  866. pdf.Cell(nil, co2)
  867. }
  868. o2 := ""
  869. if indexo2 > -1 && DeviceItemContainint(ChannelItem_HaveO2, v.DataItem) {
  870. o2 = utils.ToStr(data[l-j-1][indexo2])
  871. //氧气
  872. pdf.Curr.X = 1150
  873. pdf.Curr.Y = 50*float64(yu+1) + 120
  874. pdf.Cell(nil, o2)
  875. }
  876. if DeviceItemContainint(ChannelItem_Power, v.DataItem) {
  877. powerv, supplyv := "", ""
  878. powerv = utils.ToStr(data[l-j-1][indexpower])
  879. //功率
  880. pdf.Curr.X = 550
  881. pdf.Curr.Y = 50*float64(yu+1) + 120
  882. pdf.Cell(nil, powerv)
  883. supplyv = utils.ToStr(data[l-j-1][indexsupply])
  884. //市电
  885. pdf.Curr.X = 750
  886. pdf.Curr.Y = 50*float64(yu+1) + 120
  887. pdf.Cell(nil, supplyv)
  888. }
  889. if DeviceItemContainint(ChannelItem_LiquidLevel, v.DataItem) {
  890. liquidlevelv := ""
  891. pdf.Curr.X = 550
  892. pdf.Curr.Y = 50*float64(yu+1) + 120
  893. liquidlevelv = utils.ToStr(data[l-j-1][indexliquidlevel])
  894. //液位
  895. pdf.Cell(nil, liquidlevelv)
  896. }
  897. }
  898. }
  899. }
  900. SaveDirectory("static/file/pdf/report/" + this.GetAccode())
  901. pdf.WritePdf("static/file/pdf/report/" + this.GetAccode() + "/devicedata.pdf")
  902. var errinfo ErrorInfo
  903. errinfo.Message = this.Ctx.Request.Host + "/static/file/pdf/report/" + this.GetAccode() + "/devicedata.pdf"
  904. errinfo.Code = 0
  905. this.Data["json"] = &errinfo
  906. this.ServeJSON()
  907. }