package controllers import ( "encoding/json" "fmt" "strings" "time" "dashoo.cn/base_common/labsop" "dashoo.cn/base_common/utils" "dashoo.cn/mcs_api/business/company" "dashoo.cn/mcs_api/business/device" "dashoo.cn/mcs_common/business/equipment" "github.com/signintech/gopdf" "github.com/tealeg/xlsx" ) type MonthReportsController struct { BaseController } type MonthModel struct { ChannelName string `json:"cname"` Data []Reportinfo `json:"data"` } // @Title 周报月报列表 // @Description 周报月报列表 // @Success 200 {object} business.device.DeviceChannels // @router /list [get] func (this *MonthReportsController) List() { var showcols []string var timearea string var code string svc := device.GetDeviceService(utils.DBE) client := labsop.GetLabSopClient(this.GetupdbAndHost()) start, _ := this.GetInt64("start") end, _ := this.GetInt64("end") cols := this.GetString("cols") code = "c" + this.GetString("serial") startstr := time.Unix(start/1000, 0).Format("2006-01-02") endstr := time.Unix(end/1000, 0).Format("2006-01-02") t1, _ := utils.TimeParse(startstr+" 00:00:00", "2006-1-2 15:4:5") t2, _ := utils.TimeParse(endstr+" 23:59:59", "2006-1-2 15:4:5") day := (t2.Unix()-t1.Unix())/(60*60*24) + 1 var reports = make([]Reportinfo, 0) if cols == "" { timearea = ReportDefaultTimearea showcols = strings.Split(ReportDefaultTimearr, ",") } else { timearea = "1h" showcolsstr := cols showcols = strings.Split(showcolsstr, ",") } channelmodel := svc.GetEntitieByCode(code) for i := 0; int64(i) < day; i++ { var begin int64 = t1.AddDate(0, 0, i).Unix() var end int64 = t1.AddDate(0, 0, i+1).Unix() - 1 data := new(labsop.DatapointLabSop) queryCommand := fmt.Sprintf("select MEDIAN(temperature) as temperature,MEDIAN(humidity) as humidity,MEDIAN(voltage) as voltage,MEDIAN(rssi) as rssi,MEDIAN(o2) as o2,MEDIAN(co2) as co2 ,MEDIAN(ots) as ots,MEDIAN(displacement) as displacement,MEDIAN(h2o) as h2o,MEDIAN(so2) as so2,MEDIAN(windspeed) as windspeed ,MEDIAN(pressure) as pressure,MEDIAN(clo2) as clo2,MEDIAN(c2h4) as c2h4,MEDIAN(c2h2) as c2h2,MEDIAN(cl2) as cl2,MEDIAN(o3) as o3,MEDIAN(tvoc) as tvoc from %v where time > %vs and time < %vs group by time(%v) fill(none)", code, begin, end, timearea) fmt.Println("--------queryCommand-1111--------", queryCommand) if DeviceItemContainint(ChannelItem_Power, channelmodel.DataItem) { queryCommand = fmt.Sprintf("select MEDIAN(temperature) as temperature,MEDIAN(humidity) as humidity,MEDIAN(voltage) as voltage,MEDIAN(rssi) as rssi,MEDIAN(o2) as o2,MEDIAN(co2) as co2,MEDIAN(ots) as ots,MEDIAN(displacement) as displacement,MEDIAN(h2o) as h2o,MEDIAN(so2) as so2,MEDIAN(windspeed) as windspeed ,MEDIAN(pressure) as pressure,MEDIAN(clo2) as clo2,MEDIAN(c2h4) as c2h4,MEDIAN(c2h2) as c2h2,MEDIAN(cl2) as cl2,MEDIAN(o3) as o3 ,MEDIAN(tvoc) as tvoc ,MEDIAN(electricalsupply) as electricalsupply,MEDIAN(electricalpower) as electricalpower from %v where time > %vs and time < %vs group by time(%v) fill(none)", code, begin, end, timearea) fmt.Println("--------queryCommand---------", queryCommand) } else if DeviceItemContainint(ChannelItem_HaveLiquidLevel, channelmodel.DataItem) { queryCommand = fmt.Sprintf("select MEDIAN(temperature) as temperature,MEDIAN(humidity) as humidity,MEDIAN(voltage) as voltage,MEDIAN(rssi) as rssi,MEDIAN(o2) as o2,MEDIAN(co2) as co2 ,MEDIAN(ots) as ots,MEDIAN(displacement) as displacement,MEDIAN(h2o) as h2o,MEDIAN(so2) as so2,MEDIAN(windspeed) as windspeed ,MEDIAN(pressure) as pressure,MEDIAN(clo2) as clo2,MEDIAN(c2h4) as c2h4,MEDIAN(c2h2) as c2h2,MEDIAN(cl2) as cl2,MEDIAN(o3) as o3,MEDIAN(tvoc) as tvoc,MEDIAN(liquidlevel) as liquidlevel from %v where time > %vs and time < %vs group by time(%v) fill(none)", code, begin, end, timearea) } result := client.QueryLabSop(queryCommand, data) reportdata := GetReportData(utils.TimeFormat(t1.AddDate(0, 0, i), "2006-1-2"), channelmodel.DataItem, showcols, result, []TriggerHourData{}) reports = append(reports, reportdata) } this.Data["reports"] = reports var datainfo MonthModel datainfo.Data = reports datainfo.ChannelName = channelmodel.Title this.Data["json"] = &datainfo this.ServeJSON() } // @Title 周报月报导出EXCEL // @Description 周报月报导出EXCEL // @Success 200 {object} business.device.DeviceChannels // @router /excel [get] func (this *MonthReportsController) GetExcel() { var showcols []string var showxlscols []string var timearea string svc := device.GetDeviceService(utils.DBE) client := labsop.GetLabSopClient(this.GetupdbAndHost()) start, _ := this.GetInt64("start") end, _ := this.GetInt64("end") cols := this.GetString("cols") serials := strings.Split(this.GetString("serials"), ",") startstr := time.Unix(start/1000, 0).Format("2006-01-02") endstr := time.Unix(end/1000, 0).Format("2006-01-02") t1, _ := utils.TimeParse(startstr+" 00:00:00", "2006-1-2 15:4:5") t2, _ := utils.TimeParse(endstr+" 23:59:59", "2006-1-2 15:4:5") day := (t2.Unix()-t1.Unix())/(60*60*24) + 1 if cols == "" { timearea = ReportDefaultTimearea showcols = strings.Split(ReportDefaultTimearr, ",") showxlscols = strings.Split(strings.Replace(ReportDefaultTimearr, ",", ":00,", -1)+":00", ",") } else { timearea = "1h" showcolsstr := cols showcols = strings.Split(showcolsstr, ",") showxlscols = strings.Split(strings.Replace(showcolsstr, ",", ":00,", -1)+":00", ",") } svccompany := company.GetCompanyService(utils.DBE) companyname := svccompany.GetFullnameByAccode(this.GetAccode()) xlsx.PagePrintfooterContant = "第 &P 页 " xlsx.PagePrintheadContant = companyname f := xlsx.NewFile() if this.GetString("type") == "1" { var codestring []string for i := 0; i < len(serials); i++ { codestring = append(codestring, "'c"+serials[i]+"'") } svceq := equipment.GetEquipmentService(utils.DBE) eids := svceq.GetEquipmentidsByUid(this.User.Id) where := " (a.CreateUserId=" + utils.ToStr(this.User.Id) + " or a.EquipMentId in (" + eids + ")) and a.DataItem in (" + ChannelItem_Report + ") and a.Code in(" + strings.Join(codestring, ",") + ")" _, channel1 := svc.GetChannelsList(-1, 8, where, this.User.Id) for _, v := range channel1 { if v.Code != "" { var reportsall = make([]Reportinfo, 0) for i := 0; int64(i) < day; i++ { var begin int64 = t1.AddDate(0, 0, i).Unix() var end int64 = t1.AddDate(0, 0, i+1).Unix() - 1 data := new(labsop.DatapointLabSop) queryCommand := fmt.Sprintf("select MEDIAN(temperature) as temperature,MEDIAN(humidity) as humidity,MEDIAN(voltage) as voltage,MEDIAN(rssi) as rssi,MEDIAN(o2) as o2,MEDIAN(co2) as co2 ,MEDIAN(ots) as ots,MEDIAN(displacement) as displacement,MEDIAN(h2o) as h2o ,MEDIAN(so2) as so2,MEDIAN(windspeed) as windspeed ,MEDIAN(pressure) as pressure,MEDIAN(clo2) as clo2,MEDIAN(c2h4) as c2h4,MEDIAN(c2h2) as c2h2,MEDIAN(cl2) as cl2,MEDIAN(o3) as o3,MEDIAN(tvoc) as tvoc from %v where time > %vs and time < %vs group by time(%v) fill(none)", v.Code, begin, end, timearea) if DeviceItemContainint(ChannelItem_Power, v.DataItem) { queryCommand = fmt.Sprintf("select MEDIAN(temperature) as temperature,MEDIAN(humidity) as humidity,MEDIAN(voltage) as voltage,MEDIAN(rssi) as rssi,MEDIAN(o2) as o2,MEDIAN(co2) as co2,MEDIAN(ots) as ots,MEDIAN(displacement) as displacement,MEDIAN(h2o) as h2o ,MEDIAN(so2) as so2,MEDIAN(windspeed) as windspeed ,MEDIAN(pressure) as pressure,MEDIAN(clo2) as clo2,MEDIAN(c2h4) as c2h4,MEDIAN(c2h2) as c2h2,MEDIAN(cl2) as cl2,MEDIAN(o3) as o3 ,MEDIAN(tvoc) as tvoc ,MEDIAN(electricalsupply) as electricalsupply,MEDIAN(electricalpower) as electricalpower from %v where time > %vs and time < %vs group by time(%v) fill(none)", v.Code, begin, end, timearea) } else if DeviceItemContainint(ChannelItem_HaveLiquidLevel, v.DataItem) { queryCommand = fmt.Sprintf("select MEDIAN(temperature) as temperature,MEDIAN(humidity) as humidity,MEDIAN(voltage) as voltage,MEDIAN(rssi) as rssi,MEDIAN(o2) as o2,MEDIAN(co2) as co2 ,MEDIAN(ots) as ots,MEDIAN(displacement) as displacement,MEDIAN(h2o) as h2o ,MEDIAN(so2) as so2,MEDIAN(windspeed) as windspeed ,MEDIAN(pressure) as pressure,MEDIAN(clo2) as clo2,MEDIAN(c2h4) as c2h4,MEDIAN(c2h2) as c2h2,MEDIAN(cl2) as cl2,MEDIAN(o3) as o3,MEDIAN(tvoc) as tvoc,MEDIAN(liquidlevel) as liquidlevel from %v where time > %vs and time < %vs group by time(%v) fill(none)", v.Code, begin, end, timearea) } result := client.QueryLabSop(queryCommand, data) reportdata := GetReportData(utils.TimeFormat(t1.AddDate(0, 0, i), "2006-1-2"), v.DataItem, showcols, result, []TriggerHourData{}) reportsall = append(reportsall, reportdata) } DaySaveXlsx("日期", v.Title, this.User.Realname, v.Title, showxlscols, reportsall, f) } } SaveDirectory("static/file/excel/report/" + this.GetAccode()) f.Save("static/file/excel/report/" + this.GetAccode() + "/monthdevicedata.xlsx") var errinfo ErrorInfo errinfo.Message = this.Ctx.Request.Host + "/static/file/excel/report/" + this.GetAccode() + "/monthdevicedata.xlsx" errinfo.Code = 0 this.Data["json"] = &errinfo this.ServeJSON() } else if this.GetString("type") == "2" { svceq := equipment.GetEquipmentService(utils.DBE) eids := svceq.GetEquipmentidsByUid(this.User.Id) Uid := this.User.Id where := " (a.CreateUserId=" + utils.ToStr(this.User.Id) + " or a.EquipMentId in (" + eids + ")) and a.DataItem in (" + ChannelItem_Report + ") " _, channel := svc.GetChannelsList(-1, 8, where, Uid) for _, v := range channel { if v.Code != "" { var reportsall = make([]Reportinfo, 0) var i int for i = 0; int64(i) < day; i++ { var begin int64 = t1.AddDate(0, 0, i).Unix() var end int64 = t1.AddDate(0, 0, i+1).Unix() - 1 data := new(labsop.DatapointLabSop) queryCommand := fmt.Sprintf("select MEDIAN(temperature) as temperature,MEDIAN(humidity) as humidity,MEDIAN(voltage) as voltage,MEDIAN(rssi) as rssi,MEDIAN(o2) as o2,MEDIAN(co2) as co2,MEDIAN(ots) as ots,MEDIAN(displacement) as displacement ,MEDIAN(h2o) as h2o ,MEDIAN(so2) as so2,MEDIAN(windspeed) as windspeed ,MEDIAN(pressure) as pressure,MEDIAN(clo2) as clo2,MEDIAN(c2h4) as c2h4,MEDIAN(c2h2) as c2h2,MEDIAN(cl2) as cl2,MEDIAN(o3) as o3,MEDIAN(tvoc) as tvoc from %v where time > %vs and time < %vs group by time(%v) fill(none)", v.Code, begin, end, timearea) if DeviceItemContainint(ChannelItem_Power, v.DataItem) { queryCommand = fmt.Sprintf("select MEDIAN(temperature) as temperature,MEDIAN(humidity) as humidity,MEDIAN(voltage) as voltage,MEDIAN(rssi) as rssi,MEDIAN(o2) as o2,MEDIAN(co2) as co2,MEDIAN(ots) as ots,MEDIAN(displacement) as displacement,MEDIAN(h2o) as h2o ,MEDIAN(so2) as so2,MEDIAN(windspeed) as windspeed ,MEDIAN(pressure) as pressure,MEDIAN(clo2) as clo2,MEDIAN(c2h4) as c2h4,MEDIAN(c2h2) as c2h2,MEDIAN(cl2) as cl2,MEDIAN(o3) as o3 ,MEDIAN(tvoc) as tvoc ,MEDIAN(electricalsupply) as electricalsupply,MEDIAN(electricalpower) as electricalpower from %v where time > %vs and time < %vs group by time(%v) fill(none)", v.Code, begin, end, timearea) } else if DeviceItemContainint(ChannelItem_HaveLiquidLevel, v.DataItem) { queryCommand = fmt.Sprintf("select MEDIAN(temperature) as temperature,MEDIAN(humidity) as humidity,MEDIAN(voltage) as voltage,MEDIAN(rssi) as rssi,MEDIAN(o2) as o2,MEDIAN(co2) as co2,MEDIAN(ots) as ots,MEDIAN(displacement) as displacement,MEDIAN(h2o) as h2o ,MEDIAN(so2) as so2,MEDIAN(windspeed) as windspeed ,MEDIAN(pressure) as pressure,MEDIAN(clo2) as clo2,MEDIAN(c2h4) as c2h4,MEDIAN(c2h2) as c2h2,MEDIAN(cl2) as cl2,MEDIAN(o3) as o3 ,MEDIAN(tvoc) as tvoc ,MEDIAN(liquidlevel) as liquidlevel from %v where time > %vs and time < %vs group by time(%v) fill(none)", v.Code, begin, end, timearea) } result := client.QueryLabSop(queryCommand, data) reportdata := GetReportData(utils.TimeFormat(t1.AddDate(0, 0, i), "2006-1-2"), v.DataItem, showcols, result, []TriggerHourData{}) reportsall = append(reportsall, reportdata) } DaySaveXlsx("日期", v.Title, this.User.Realname, v.Title, showxlscols, reportsall, f) } } SaveDirectory("static/file/excel/report/" + this.GetAccode()) f.Save("static/file/excel/report/" + this.GetAccode() + "/monthdevicedata.xlsx") var errinfo ErrorInfo errinfo.Message = this.Ctx.Request.Host + "/static/file/excel/report/" + this.GetAccode() + "/monthdevicedata.xlsx" errinfo.Code = 0 this.Data["json"] = &errinfo this.ServeJSON() } } // @Title 周报月报导出PDF // @Description 周报月报导出PDF // @Success 200 {object} business.device.DeviceChannels // @router /pdf [get] func (this *MonthReportsController) GetPDF() { var showcols []string var timearea string svc := device.GetDeviceService(utils.DBE) client := labsop.GetLabSopClient(this.GetupdbAndHost()) start, _ := this.GetInt64("start") end, _ := this.GetInt64("end") cols := this.GetString("cols") startstr := time.Unix(start/1000, 0).Format("2006-01-02") endstr := time.Unix(end/1000, 0).Format("2006-01-02") t1, _ := utils.TimeParse(startstr+" 00:00:00", "2006-1-2 15:4:5") t2, _ := utils.TimeParse(endstr+" 23:59:59", "2006-1-2 15:4:5") day := (t2.Unix()-t1.Unix())/(60*60*24) + 1 svceq := equipment.GetEquipmentService(utils.DBE) eids := svceq.GetEquipmentidsByUid(this.User.Id) Uid := this.User.Id channel := make([]device.DeviceChannels, 0) if this.GetString("type") == "1" { serials := strings.Split(this.GetString("serials"), ",") var codestring []string for i := 0; i < len(serials); i++ { codestring = append(codestring, "'c"+serials[i]+"'") } where := " (a.CreateUserId=" + utils.ToStr(this.User.Id) + " or a.EquipMentId in (" + eids + ")) and a.DataItem in (" + ChannelItem_Report + ") and a.Code in(" + strings.Join(codestring, ",") + ")" _, channel = svc.GetChannelsList(-1, 8, where, Uid) } else { where := " (a.CreateUserId=" + utils.ToStr(this.User.Id) + " or a.EquipMentId in (" + eids + ")) and a.DataItem in (" + ChannelItem_Report + ") " _, channel = svc.GetChannelsList(-1, 8, where, Uid) } if cols == "" { timearea = ReportDefaultTimearea showcols = strings.Split(ReportDefaultTimearr, ",") } else { timearea = "1h" showcolsstr := cols showcols = strings.Split(showcolsstr, ",") } var times float64 // var report float64 // report = float64(len(reports)) times = float64(len(showcols)) y := 0 pdf := gopdf.GoPdf{} pdf.Start(gopdf.Config{Unit: "pt", PageSize: gopdf.Rect{W: (200*times + 600), H: 2550}}) //595.28, 841.89 = A4 pdf.AddTTFFont("HDZB_5", "static/fonts/msyh.ttf") for k, v := range channel { if v.Code != "" { var reports = make([]Reportinfo, 0) var j int for j = 0; int64(j) < day; j++ { var begin int64 = t1.AddDate(0, 0, j).Unix() var end int64 = t1.AddDate(0, 0, j+1).Unix() - 1 data := new(labsop.DatapointLabSop) queryCommand := fmt.Sprintf("select MEDIAN(temperature) as temperature,MEDIAN(humidity) as humidity,MEDIAN(voltage) as voltage,MEDIAN(rssi) as rssi,MEDIAN(o2) as o2,MEDIAN(co2) as co2 ,MEDIAN(ots) as ots,MEDIAN(displacement) as displacement,MEDIAN(h2o) as h2o ,MEDIAN(so2) as so2,MEDIAN(windspeed) as windspeed ,MEDIAN(pressure) as pressure,MEDIAN(clo2) as clo2,MEDIAN(c2h4) as c2h4,MEDIAN(c2h2) as c2h2,MEDIAN(cl2) as cl2,MEDIAN(o3) as o3,MEDIAN(tvoc) as tvoc from %v where time > %vs and time < %vs group by time(%v) fill(none)", v.Code, begin, end, timearea) if DeviceItemContainint(ChannelItem_Power, v.DataItem) { queryCommand = fmt.Sprintf("select MEDIAN(temperature) as temperature,MEDIAN(humidity) as humidity,MEDIAN(voltage) as voltage,MEDIAN(rssi) as rssi,MEDIAN(o2) as o2,MEDIAN(co2) as co2,MEDIAN(ots) as ots,MEDIAN(displacement) as displacement,MEDIAN(h2o) as h2o ,MEDIAN(so2) as so2,MEDIAN(windspeed) as windspeed ,MEDIAN(pressure) as pressure,MEDIAN(clo2) as clo2,MEDIAN(c2h4) as c2h4,MEDIAN(c2h2) as c2h2,MEDIAN(cl2) as cl2,MEDIAN(o3) as o3 ,MEDIAN(tvoc) as tvoc ,MEDIAN(electricalsupply) as electricalsupply,MEDIAN(electricalpower) as electricalpower from %v where time > %vs and time < %vs group by time(%v) fill(none)", v.Code, begin, end, timearea) } else if DeviceItemContainint(ChannelItem_HaveLiquidLevel, v.DataItem) { queryCommand = fmt.Sprintf("select MEDIAN(temperature) as temperature,MEDIAN(humidity) as humidity,MEDIAN(voltage) as voltage,MEDIAN(rssi) as rssi,MEDIAN(o2) as o2,MEDIAN(co2) as co2 ,MEDIAN(ots) as ots,MEDIAN(displacement) as displacement,MEDIAN(h2o) as h2o ,MEDIAN(so2) as so2,MEDIAN(windspeed) as windspeed ,MEDIAN(pressure) as pressure,MEDIAN(clo2) as clo2,MEDIAN(c2h4) as c2h4,MEDIAN(c2h2) as c2h2,MEDIAN(cl2) as cl2,MEDIAN(o3) as o3,MEDIAN(tvoc) as tvoc,MEDIAN(liquidlevel) as liquidlevel from %v where time > %vs and time < %vs group by time(%v) fill(none)", v.Code, begin, end, timearea) } result := client.QueryLabSop(queryCommand, data) reportdata := GetReportData(utils.TimeFormat(t1.AddDate(0, 0, j), "2006-1-2"), v.DataItem, showcols, result, []TriggerHourData{}) reports = append(reports, reportdata) this.Data["reports"] = reports } this.Data["showcols"] = showcols var times float64 var report float64 var i float64 report = float64(len(reports)) times = float64(len(showcols)) //pdf.Start(gopdf.Config{Unit: "pt", PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4 pdf.AddPage() pdf.Line(100, 100, 200*times+500, 100) //横线 pdf.Line(100, 150, 200*times+500, 150) //横线 pdf.Line(100, 100, 100, 150) //竖线 for i = 0; i <= times; i++ { pdf.Line(200*i+500, 100, 200*i+500, 150) //竖线 } pdf.SetFont("HDZB_5", "B", 24) //字体 pdf.Curr.X = 105 pdf.Curr.Y = 120 pdf.Cell(nil, channel[k].Title) pdf.Curr.X = (200*times+600)/2 - 100 pdf.Curr.Y = 70 pdf.Cell(nil, "名称:"+this.User.Realname) var yyy float64 var r float64 var t float64 yyy = 120 for i = 0; i < report; i++ { pdf.Curr.Y = yyy + 20 pdf.Br(26) pdf.Curr.X = 105 pdf.Cell(nil, reports[int(i)].Name) if yyy == 120 { for r = 0; r < times; r++ { pdf.Curr.X = 200*r + 510 pdf.Curr.Y = 120 pdf.Cell(nil, (showcols[int(r)] + ":00")) } } if len(reports[int(i)].DataT) != 0 { for k, temp := range reports[int(i)].DataT { pdf.Curr.X = 310 + float64(k*200) pdf.Curr.Y = 50 + yyy pdf.Cell(nil, temp) } yyy = yyy + 50 pdf.Line(300, yyy+30, 200*times+500, yyy+30) //横线 } if len(reports[int(i)].DataH) != 0 { for k, temp := range reports[int(i)].DataH { pdf.Curr.X = 310 + float64(k*200) pdf.Curr.Y = 50 + yyy pdf.Cell(nil, temp) } yyy = yyy + 50 pdf.Line(300, yyy+30, 200*times+500, yyy+30) //横线 } if len(reports[int(i)].DataO2) != 0 { for k, temp := range reports[int(i)].DataO2 { pdf.Curr.X = 310 + float64(k*200) pdf.Curr.Y = 50 + yyy pdf.Cell(nil, temp) } yyy = yyy + 50 pdf.Line(300, yyy+30, 200*times+500, yyy+30) //横线 } if len(reports[int(i)].DataCO2) != 0 { for k, temp := range reports[int(i)].DataCO2 { pdf.Curr.X = 310 + float64(k*200) pdf.Curr.Y = 50 + yyy pdf.Cell(nil, temp) } yyy = yyy + 50 pdf.Line(300, yyy+30, 200*times+500, yyy+30) //横线 } if len(reports[int(i)].DataDoorlock) != 0 { for k, temp := range reports[int(i)].DataDoorlock { pdf.Curr.X = 310 + float64(k*200) pdf.Curr.Y = 50 + yyy pdf.Cell(nil, temp) } yyy = yyy + 50 pdf.Line(300, yyy+30, 200*times+500, yyy+30) //横线 } if len(reports[int(i)].DataDisplacement) != 0 { for k, temp := range reports[int(i)].DataDisplacement { pdf.Curr.X = 310 + float64(k*200) pdf.Curr.Y = 50 + yyy pdf.Cell(nil, temp) } yyy = yyy + 50 pdf.Line(300, yyy+30, 200*times+500, yyy+30) //横线 } if len(reports[int(i)].DataH2O) != 0 { for k, temp := range reports[int(i)].DataH2O { pdf.Curr.X = 310 + float64(k*200) pdf.Curr.Y = 50 + yyy pdf.Cell(nil, temp) } yyy = yyy + 50 pdf.Line(300, yyy+30, 200*times+500, yyy+30) //横线 } if len(reports[int(i)].DataSO2) != 0 { for k, temp := range reports[int(i)].DataSO2 { pdf.Curr.X = 310 + float64(k*200) pdf.Curr.Y = 50 + yyy pdf.Cell(nil, temp) } yyy = yyy + 50 pdf.Line(300, yyy+30, 200*times+500, yyy+30) //横线 } if len(reports[int(i)].Datapower) != 0 { for k, temp := range reports[int(i)].Datapower { pdf.Curr.X = 310 + float64(k*200) pdf.Curr.Y = 50 + yyy pdf.Cell(nil, temp) } yyy = yyy + 50 pdf.Line(300, yyy+30, 200*times+500, yyy+30) //横线 } if len(reports[int(i)].Datasupply) != 0 { for k, temp := range reports[int(i)].Datasupply { pdf.Curr.X = 310 + float64(k*200) pdf.Curr.Y = 50 + yyy pdf.Cell(nil, temp) } yyy = yyy + 50 pdf.Line(300, yyy+30, 200*times+500, yyy+30) //横线 } if len(reports[int(i)].DataLiquidLevel) != 0 { for k, temp := range reports[int(i)].DataLiquidLevel { pdf.Curr.X = 310 + float64(k*200) pdf.Curr.Y = 50 + yyy pdf.Cell(nil, temp) } yyy = yyy + 50 pdf.Line(300, yyy+30, 200*times+500, yyy+30) //横线 } if yyy > 2350 || i == report-1 { for r = 0; r <= (times + 2); r++ { pdf.Line(200*r+100, 150, 200*r+100, yyy+30) //竖线 } y = y + 1 yy := utils.ToStr(y) pdf.Curr.X = (200*times+600)/2 - 100 pdf.Curr.Y = 2500 pdf.Cell(nil, "第 "+yy+" 页") } pdf.Line(100, yyy+30, 300, yyy+30) //横线 if yyy > 2350 && i != report-1 { pdf.AddPage() yyy = 120 pdf.Line(100, 100, 200*times+500, 100) //横线 pdf.Line(100, 150, 200*times+500, 150) //横线 pdf.Line(100, 100, 100, 150) //竖线 for t = 0; t <= times; t++ { pdf.Line(200*t+500, 100, 200*t+500, 150) //竖线 } pdf.SetFont("HDZB_5", "B", 24) //字体 pdf.Curr.X = 105 pdf.Curr.Y = 120 pdf.Cell(nil, channel[k].Title) pdf.Curr.X = (200*times+600)/2 - 100 pdf.Curr.Y = 70 pdf.Cell(nil, "名称:"+this.User.Realname) for t = 0; t < times; t++ { pdf.Curr.X = 200*t + 510 pdf.Curr.Y = 120 pdf.Cell(nil, (showcols[int(t)] + ":00")) } } } } } SaveDirectory("static/file/pdf/report/" + this.GetAccode()) pdf.WritePdf("static/file/pdf/report/" + this.GetAccode() + "/devicedata.pdf") var errinfo ErrorInfo errinfo.Message = this.Ctx.Request.Host + "/static/file/pdf/report/" + this.GetAccode() + "/devicedata.pdf" errinfo.Code = 0 this.Data["json"] = &errinfo this.ServeJSON() } // @Title 周报月报导出最值报表 // @Description 周报月报导出最值报表 // @Success 200 {object} business.device.DeviceChannels // @router /maxreport [get] func (this *MonthReportsController) MaxReport() { this.Data["PageTitle"] = "title.data_query" svc := device.GetDeviceService(utils.DBE) client := labsop.GetLabSopClient(this.GetupdbAndHost()) rtype := this.GetString("type") start, _ := this.GetInt64("start") end, _ := this.GetInt64("end") startstr := time.Unix(start/1000, 0).Format("2006-01-02") endstr := time.Unix(end/1000, 0).Format("2006-01-02") t1, _ := utils.TimeParse(startstr+" 00:00:00", "2006-1-2 15:4:5") t2, _ := utils.TimeParse(endstr+" 23:59:59", "2006-1-2 15:4:5") svccompany := company.GetCompanyService(utils.DBE) companyname := svccompany.GetFullnameByAccode(this.GetAccode()) xlsx.PagePrintfooterContant = "第 &P 页 " xlsx.PagePrintheadContant = companyname f := xlsx.NewFile() if rtype == "3" { svceq := equipment.GetEquipmentService(utils.DBE) eids := svceq.GetEquipmentidsByUid(this.User.Id) uidstr := this.User.Id var codestring []string serials := strings.Split(this.GetString("serials"), ",") for i := 0; i < len(serials); i++ { codestring = append(codestring, "'c"+serials[i]+"'") } where := " (a.CreateUserId=" + utils.ToStr(this.User.Id) + " or a.EquipMentId in (" + eids + ")) and a.DataItem in (" + ChannelItem_Report + ") and a.Code in(" + strings.Join(codestring, ",") + ")" _, channel1 := svc.GetChannelsList(-1, 8, where, uidstr) for k, v := range channel1 { if v.Code != "" { queryGroupCommand := fmt.Sprintf("select max(temperature),min(temperature),max(humidity),min(humidity),max(o2),min(o2),max(co2),min(co2),max(ots),min(ots),max(displacement),min(displacement),max(h2o),min(h2o),max(so2),min(so2),max(electricalpower),min(electricalpower),max(electricalsupply),min(electricalsupply),max(liquidlevel),min(liquidlevel) from %v where time > %vs and time < %vs group by time(1d,16h) fill(none) ", v.Code, t1.Unix()-1, t2.Unix()+1) datagroup, _ := client.QueryOneResultByCommand(queryGroupCommand) var datas [][]interface{} if datagroup != nil && len(datagroup) > 0 { datas = datagroup[0].Values } this.GroupSaveXlsx(utils.ToStr(k+1)+"."+v.Title, v.Serial, startstr+" ~ "+endstr, v.DataItem, datas, f) } } SaveDirectory("static/file/excel/report/" + this.GetAccode()) f.Save("static/file/excel/report/" + this.GetAccode() + "/reportdata.xlsx") var errinfo ErrorInfo errinfo.Message = this.Ctx.Request.Host + "/static/file/excel/report/" + this.GetAccode() + "/reportdata.xlsx" errinfo.Code = 0 this.Data["json"] = &errinfo this.ServeJSON() } else if rtype == "4" { svceq := equipment.GetEquipmentService(utils.DBE) eids := svceq.GetEquipmentidsByUid(this.User.Id) uidstr := this.User.Id where := " (a.CreateUserId=" + uidstr + " or a.EquipMentId in (" + eids + ")) and a.DataItem in (" + ChannelItem_Report + ") " _, channel := svc.GetChannelsList(-1, 8, where, uidstr) for k, v := range channel { if v.Code != "" { queryGroupCommand := fmt.Sprintf("select max(temperature),min(temperature),max(humidity),min(humidity),max(o2),min(o2),max(co2),min(co2),max(ots),min(ots),max(displacement),min(displacement),max(h2o),min(h2o),max(so2),min(so2),max(electricalpower),min(electricalpower),max(electricalsupply),min(electricalsupply),max(liquidlevel),min(liquidlevel) from %v where time > %vs and time < %vs group by time(1d,16h) fill(none) ", v.Code, t1.Unix()-1, t2.Unix()+1) datagroup, _ := client.QueryOneResultByCommand(queryGroupCommand) var datas [][]interface{} if datagroup != nil && len(datagroup) > 0 { datas = datagroup[0].Values } this.GroupSaveXlsx(utils.ToStr(k+1)+"."+v.Title, v.Serial, startstr+" ~ "+endstr, v.DataItem, datas, f) } } SaveDirectory("static/file/excel/report/" + this.GetAccode()) f.Save("static/file/excel/report/" + this.GetAccode() + "/reportdata.xlsx") var errinfo ErrorInfo errinfo.Message = this.Ctx.Request.Host + "/static/file/excel/report/" + this.GetAccode() + "/reportdata.xlsx" errinfo.Code = 0 this.Data["json"] = &errinfo this.ServeJSON() } } func (this *MonthReportsController) GroupSaveXlsx(name, code, timestr string, dataitem int, report [][]interface{}, f *xlsx.File) { sheet, _ := f.AddSheet(name + "(" + code + ")") rowname := sheet.AddRow() celln := rowname.AddCell() celln.Value = "设备名称:" + name rowname = sheet.AddRow() celln = rowname.AddCell() celln.Value = "设备编号:" + code rowname = sheet.AddRow() celln = rowname.AddCell() celln.Value = "项目:" rowname = sheet.AddRow() celln = rowname.AddCell() celln.Value = "日期:" + timestr rowhead := sheet.AddRow() cell := rowhead.AddCell() cell.Value = "" cell = rowhead.AddCell() cell.Value = "" if DeviceItemContainint(ChannelItem_HaveT, dataitem) { cell = rowhead.AddCell() cell.Value = "温度°C" } if DeviceItemContainint(ChannelItem_HaveH, dataitem) { cell = rowhead.AddCell() cell.Value = "湿度%" } if DeviceItemContainint(ChannelItem_HaveO2, dataitem) { cell = rowhead.AddCell() cell.Value = "氧气%" } if DeviceItemContainint(ChannelItem_HaveCO2, dataitem) { //Ots Displacement cell = rowhead.AddCell() cell.Value = "二氧化碳%" } if DeviceItemContainint(ChannelItem_Doorlock, dataitem) { cell = rowhead.AddCell() cell.Value = "二氧化碳%" } if DeviceItemContainint(ChannelItem_Displacement, dataitem) { cell = rowhead.AddCell() cell.Value = "二氧化碳%" } if DeviceItemContainint(ChannelItem_H2O, dataitem) { cell = rowhead.AddCell() cell.Value = "电导率us/cm" } if DeviceItemContainint(ChannelItem_SO2, dataitem) { cell = rowhead.AddCell() cell.Value = "二氧化硫ppm" } if DeviceItemContainint(ChannelItem_WindSpeed, dataitem) { cell = rowhead.AddCell() cell.Value = "风速m/s" } if DeviceItemContainint(ChannelItem_Pressure, dataitem) { cell = rowhead.AddCell() cell.Value = "气压hpa" } if DeviceItemContainint(ChannelItem_ClO2, dataitem) { cell = rowhead.AddCell() cell.Value = "二氧化氯PPM" } if DeviceItemContainint(ChannelItem_C2H4, dataitem) { cell = rowhead.AddCell() cell.Value = "乙烯PPM" } if DeviceItemContainint(ChannelItem_C2H2, dataitem) { cell = rowhead.AddCell() cell.Value = "乙炔PPM" } if DeviceItemContainint(ChannelItem_Cl2, dataitem) { cell = rowhead.AddCell() cell.Value = "氯气PPM" } if DeviceItemContainint(ChannelItem_O3, dataitem) { cell = rowhead.AddCell() cell.Value = "臭氧PPM" } if DeviceItemContainint(ChannelItem_TVOC, dataitem) { cell = rowhead.AddCell() cell.Value = "TVOC" } if DeviceItemContainint(ChannelItem_Power, dataitem) { cell = rowhead.AddCell() cell.Value = "功率W" cell = rowhead.AddCell() cell.Value = "电量V" } if DeviceItemContainint(ChannelItem_HaveLiquidLevel, dataitem) { cell = rowhead.AddCell() cell.Value = "液位mm" } if report != nil && len(report) > 0 && report[0] != nil && len(report[0]) > 0 { for _, v := range report { var valuef0, valuef1, valuef2, valuef3, valuef4, valuef5, valuef6, valuef7, valuef8, valuef9, valuef10 float64 var valuei0, valuei1, valuei2, valuei3 int64 valuef0, _ = v[0].(json.Number).Float64() vint := int64(valuef0) if DeviceItemContainint(ChannelItem_HaveT, dataitem) { valuef1, _ = v[1].(json.Number).Float64() valuef2, _ = v[2].(json.Number).Float64() } if DeviceItemContainint(ChannelItem_HaveH, dataitem) { valuef3, _ = v[3].(json.Number).Float64() valuef4, _ = v[4].(json.Number).Float64() } if DeviceItemContainint(ChannelItem_HaveO2, dataitem) { valuef5, _ = v[5].(json.Number).Float64() valuef6, _ = v[6].(json.Number).Float64() } if DeviceItemContainint(ChannelItem_HaveCO2, dataitem) { //Ots Displacement valuef7, _ = v[7].(json.Number).Float64() valuef8, _ = v[8].(json.Number).Float64() } if DeviceItemContainint(ChannelItem_Doorlock, dataitem) { //Ots Displacement valuef7, _ = v[7].(json.Number).Float64() valuef8, _ = v[8].(json.Number).Float64() } if DeviceItemContainint(ChannelItem_Displacement, dataitem) { //Ots Displacement valuef7, _ = v[7].(json.Number).Float64() valuef8, _ = v[8].(json.Number).Float64() } if DeviceItemContainint(ChannelItem_H2O, dataitem) { valuef7, _ = v[7].(json.Number).Float64() valuef8, _ = v[8].(json.Number).Float64() } if DeviceItemContainint(ChannelItem_SO2, dataitem) { valuef7, _ = v[7].(json.Number).Float64() valuef8, _ = v[8].(json.Number).Float64() } if DeviceItemContainint(ChannelItem_Power, dataitem) { valuei0, _ = v[9].(json.Number).Int64() valuei1, _ = v[10].(json.Number).Int64() valuei2, _ = v[11].(json.Number).Int64() valuei3, _ = v[12].(json.Number).Int64() } if DeviceItemContainint(ChannelItem_HaveLiquidLevel, dataitem) { valuef9, _ = v[20].(json.Number).Float64() valuef10, _ = v[21].(json.Number).Float64() } row := sheet.AddRow() cell = row.AddCell() cell.Value = time.Unix(vint, 0).Format("2006-01-02") cell = row.AddCell() cell.Value = "最高值" if DeviceItemContainint(ChannelItem_HaveT, dataitem) { cell = row.AddCell() cell.Value = utils.ToStr(valuef1) } if DeviceItemContainint(ChannelItem_HaveH, dataitem) { cell = row.AddCell() cell.Value = utils.ToStr(valuef3) } if DeviceItemContainint(ChannelItem_HaveO2, dataitem) { cell = row.AddCell() cell.Value = utils.ToStr(valuef5) } if DeviceItemContainint(ChannelItem_HaveCO2, dataitem) { //Ots Displacement cell = row.AddCell() cell.Value = utils.ToStr(valuef7) } if DeviceItemContainint(ChannelItem_Doorlock, dataitem) { //Ots Displacement cell = row.AddCell() cell.Value = utils.ToStr(valuef7) } if DeviceItemContainint(ChannelItem_Displacement, dataitem) { //Ots Displacement cell = row.AddCell() cell.Value = utils.ToStr(valuef7) } if DeviceItemContainint(ChannelItem_H2O, dataitem) { cell = row.AddCell() cell.Value = utils.ToStr(valuef7) } if DeviceItemContainint(ChannelItem_SO2, dataitem) { cell = row.AddCell() cell.Value = utils.ToStr(valuef7) } if DeviceItemContainint(ChannelItem_Power, dataitem) { cell = row.AddCell() cell.Value = utils.ToStr(valuei0) cell = row.AddCell() cell.Value = utils.ToStr(valuei2) } if DeviceItemContainint(ChannelItem_HaveLiquidLevel, dataitem) { cell = row.AddCell() cell.Value = utils.ToStr(valuef9) } row = sheet.AddRow() cell = row.AddCell() cell.Value = "" cell = row.AddCell() cell.Value = "最低值" if DeviceItemContainint(ChannelItem_HaveT, dataitem) { cell = row.AddCell() cell.Value = utils.ToStr(valuef2) } if DeviceItemContainint(ChannelItem_HaveH, dataitem) { cell = row.AddCell() cell.Value = utils.ToStr(valuef4) } if DeviceItemContainint(ChannelItem_HaveO2, dataitem) { cell = row.AddCell() cell.Value = utils.ToStr(valuef6) } if DeviceItemContainint(ChannelItem_HaveCO2, dataitem) { //Ots Displacement cell = row.AddCell() cell.Value = utils.ToStr(valuef8) } if DeviceItemContainint(ChannelItem_Doorlock, dataitem) { //Ots Displacement cell = row.AddCell() cell.Value = utils.ToStr(valuef8) } if DeviceItemContainint(ChannelItem_Displacement, dataitem) { //Ots Displacement cell = row.AddCell() cell.Value = utils.ToStr(valuef8) } if DeviceItemContainint(ChannelItem_H2O, dataitem) { cell = row.AddCell() cell.Value = utils.ToStr(valuef8) } if DeviceItemContainint(ChannelItem_SO2, dataitem) { cell = row.AddCell() cell.Value = utils.ToStr(valuef8) } if DeviceItemContainint(ChannelItem_Power, dataitem) { cell = row.AddCell() cell.Value = utils.ToStr(valuei1) cell = row.AddCell() cell.Value = utils.ToStr(valuei3) } if DeviceItemContainint(ChannelItem_HaveLiquidLevel, dataitem) { cell = row.AddCell() cell.Value = utils.ToStr(valuef10) } } } // for i, j := 0, len(showcols); i < j; i++ { // sheet.Cols[i+2].Width = 10 // } sheet.Cols[0].Width = 17 //设置时间单元格的宽度 sheet.Cols[1].Width = 11 //设置时间单元格的宽度 }