package controllers import ( "encoding/json" "fmt" "math" "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 ReportsController struct { BaseController } const ( ReportDefaultTimearea = "1h" ReportDefaultTimearr = "0,4,8,12,16,20" ) type Reportinfo struct { Name string DataItem int DataT []string DataH []string DataO2 []string DataCO2 []string DataDoorlock []string DataDisplacement []string DataWindspeed []string DataPressure []string DataClO2 []string DataC2H4 []string DataC2H2 []string DataCl2 []string DataO3 []string DataTVOC []string DataH2O []string DataSO2 []string Datapower []string //功率 Datasupply []string //电量 DataLiquidLevel []string //液位 } type TriggerHourData struct { ChannelCode string `json:"ccode"` EventFiled string `json:"eventfiled"` Value float32 `json:"value"` ReportData string `json:"reportdata"` ReportHour int `json:"reporthour"` } type TriggerHourDataRequest struct { CurrentItemCount int64 `json:"currentItemCount,omitempty"` //结果集中的条目数目 ItemsPerPage int64 `json:"itemsPerPage,omitempty"` //每页记录数目 PageIndex int64 `json:"pageIndex,omitempty"` //条目的当前页索引 Items []TriggerHourData `json:"items"` //数据列表 } type ShowColsModel struct { ColsName string `json:"colsname"` } // @Title 日报报表列表 // @Description 日报报表列表 // @Success 200 {object} DataInfo // @router /list [get] func (this *ReportsController) List() { page := this.GetPageInfoForm() var showcols []string var timearea string //分页 svc := device.GetDeviceService(utils.DBE) // svccompany := company.GetCompanyService(utils.DBE) // companyname := svccompany.GetFullnameByAccode(this.GetAccode()) u, p := this.GetuAndp() client := labsop.GetLabSopClient(this.GetupdbAndHost()) fmt.Println(this.GetupdbAndHost()) timerq, _ := this.GetInt64("timerq") cols := this.GetString("cols") timestr := time.Unix(timerq/1000, 0).Format("2006-01-02") t1, _ := utils.TimeParse(timestr+" 23:59:59", "2006-1-2 15:4:5") t2, _ := utils.TimeParse(timestr+" 00:00:00", "2006-1-2 15:4:5") end := t1.Unix() begin := t2.Unix() var reports = make([]Reportinfo, 0) 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 + ") " total, channel := svc.GetChannelsList(page.CurrentPage, page.Size, where, Uid) fmt.Println("--------channel---------", channel) 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", ",") } //报警数据 apiurl := utils.Cfg.MustValue("server", "apiurl") + "/triggerhistorys/hourdata/?u=" + u + "&p=" + p + "&source=coldchain&account=" + this.GetAccode() + "&start=" + utils.ToStr(begin) + "&end=" + utils.ToStr(end) //取报警数据 var datalist TriggerHourDataRequest strUrl := apiurl + "&ccode=" + svc.GetCIds(where) json.Unmarshal(Apiget(strUrl), &datalist) trihourmap := make(map[string][]TriggerHourData) for _, v := range datalist.Items { trihourmap[v.ChannelCode] = append(trihourmap[v.ChannelCode], v) } for _, v := range channel { 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(h2o) as h2o,MEDIAN(so2) as so2,MEDIAN(windspeed) as windspeed ,MEDIAN(pressure) as pressure,MEDIAN(clo2) as clo2,MEDIAN(tvoc) as tvoc,MEDIAN(c2h4) as c2h4,MEDIAN(c2h2) as c2h2,MEDIAN(doorlock) as doorlock,MEDIAN(cl2) as cl2,MEDIAN(o3) as o3 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(h2o) as h2o,MEDIAN(so2) as so2,MEDIAN(windspeed) as windspeed ,MEDIAN(pressure) as pressure,MEDIAN(clo2) as clo2,MEDIAN(c2h2) as c2h2,MEDIAN(tvoc) as tvoc,MEDIAN(c2h4) as c2h4,MEDIAN(cl2) as cl2,MEDIAN(doorlock) as doorlock,MEDIAN(o3) as o3 ,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) } result := client.QueryLabSop(queryCommand, data) fmt.Println("&&&result:", result) titlename := v.Title if v.DeviceState == 2 || v.DeviceState == 3 || v.DeviceState == 4 { statename := "" if v.DeviceState == 2 { statename = "维修" } else if v.DeviceState == 3 { statename = "停用" } else { statename = "除霜" } titlename = titlename + " (" + statename + ")" } reportdata := GetReportData(titlename, v.DataItem, showcols, result, trihourmap[v.Code]) reports = append(reports, reportdata) } var datainfo DataInfo datainfo.Items = reports datainfo.CurrentItemCount = total this.Data["json"] = &datainfo this.ServeJSON() } func GetReportData(name string, dataitem int, showcols []string, data []labsop.DatapointLabSop, hourdata []TriggerHourData) Reportinfo { datat := make([]string, 0) datah := make([]string, 0) datao2 := make([]string, 0) dataco2 := make([]string, 0) dataots := make([]string, 0) datadisplacement := make([]string, 0) datah2o := make([]string, 0) dataso2 := make([]string, 0) datawindspeed := make([]string, 0) datapressure := make([]string, 0) dataclo2 := make([]string, 0) datac2h4 := make([]string, 0) datac2h2 := make([]string, 0) datacl2 := make([]string, 0) datatvoc := make([]string, 0) datao3 := make([]string, 0) datapower := make([]string, 0) datasupply := make([]string, 0) dataliquidlevel := make([]string, 0) if DeviceItemContainint(ChannelItem_HaveT, dataitem) { datat = append(datat, "温度(℃)") } if DeviceItemContainint(ChannelItem_HaveH, dataitem) { datah = append(datah, "湿度(RH%)") } if DeviceItemContainint(ChannelItem_HaveO2, dataitem) { datao2 = append(datao2, "氧气(%)") } if DeviceItemContainint(ChannelItem_Co2, dataitem) { dataco2 = append(dataco2, "二氧化碳(%)") } if DeviceItemContainint(ChannelItem_H2O, dataitem) { datah2o = append(datah2o, "电导率(us/cm)") } if DeviceItemContainint(ChannelItem_SO2, dataitem) { dataso2 = append(dataso2, "二氧化硫(ppm)") } if DeviceItemContainint(ChannelItem_WindSpeed, dataitem) { datawindspeed = append(datawindspeed, "风速(m/s)") } if DeviceItemContainint(ChannelItem_Pressure, dataitem) { datapressure = append(datapressure, "气压(hpa)") } if DeviceItemContainint(ChannelItem_ClO2, dataitem) { dataclo2 = append(dataclo2, "二氧化氯(PPM)") } if DeviceItemContainint(ChannelItem_C2H4, dataitem) { datac2h4 = append(datac2h4, "乙烯(PPM)") } if DeviceItemContainint(ChannelItem_C2H2, dataitem) { datac2h2 = append(datac2h2, "乙炔(PPM)") } if DeviceItemContainint(ChannelItem_Cl2, dataitem) { datacl2 = append(datacl2, "氯气(PPM)") } if DeviceItemContainint(ChannelItem_O3, dataitem) { datao3 = append(datao3, "臭氧(PPM)") } if DeviceItemContainint(ChannelItem_Power, dataitem) { datapower = append(datapower, "功率(W)") datasupply = append(datasupply, "电量(V)") } if DeviceItemContainint(ChannelItem_HaveLiquidLevel, dataitem) { dataliquidlevel = append(dataliquidlevel, "液位(mm)") } if DeviceItemContainint(ChannelItem_TVOC, dataitem) { datatvoc = append(datatvoc, "TVOC(ppb)") } if DeviceItemContainint(ChannelItem_MicroPressure, dataitem) { datapressure = append(dataots, "微压差(Pa)") } for i, n := 0, len(data); i < 24; i++ { if utils.SliceContains(showcols, utils.ToStr(i)) { t, h, o2, co2, doorlock, displacement, h2o, so2, windspeed, pressure, clo2, c2h4, c2h2, cl2, tvoc, o3, power, supply, liquidlevel := "----", "----", "----", "----", "----", "----", "----", "----", "----", "----", "----", "----", "----", "----", "----", "----", "----", "----", "----" for m := 0; m < n; m++ { if data[m].Time.Hour() == i { t = Get2point(data[m].Temperature) if data[m].DeviceState == 2 { t = t + " (维修)" } else if data[m].DeviceState == 3 { t = t + " (停用)" } else if data[m].DeviceState == 4 { t = t + " (除霜)" } h = Get2point(data[m].Humidity) o2 = Get2point(data[m].O2) co2 = Get2point(data[m].Co2) h2o = Get2point(data[m].H2O) so2 = Get2point(data[m].SO2) windspeed = Get2point(data[m].WindSpeed) pressure = Get2point(data[m].Pressure) clo2 = Get2point(data[m].ClO2) c2h4 = Get2point(data[m].C2H4) c2h2 = Get2point(data[m].C2H2) cl2 = Get2point(data[m].Cl2) o3 = Get2point(data[m].O3) doorlock = Get2point(data[m].Doorlock) displacement = Get2point(data[m].Displacement) tvoc = Get2point(data[m].TVOC) power = utils.ToStr(data[m].ElectricalPower) supply = utils.ToStr(data[m].ElectricalSupply) liquidlevel = utils.ToStr(data[m].LiquidLevel) //在报警数据后加符号 * for x, y := 0, len(hourdata); x < y; x++ { if hourdata[x].ReportHour == i { switch hourdata[x].EventFiled { case "temperature": t = utils.ToStr(hourdata[x].Value) + " *" case "humidity": h = utils.ToStr(hourdata[x].Value) + " *" case "o2": o2 = utils.ToStr(hourdata[x].Value) + " *" case "co2": co2 = utils.ToStr(hourdata[x].Value) + " *" case "h2o": h2o = utils.ToStr(hourdata[x].Value) + " *" case "so2": so2 = utils.ToStr(hourdata[x].Value) + " *" case "windspeed": windspeed = utils.ToStr(hourdata[x].Value) + " *" case "pressure": pressure = utils.ToStr(hourdata[x].Value) + " *" case "clo2": clo2 = utils.ToStr(hourdata[x].Value) + " *" case "c2h4": c2h4 = utils.ToStr(hourdata[x].Value) + " *" case "c2h2": c2h2 = utils.ToStr(hourdata[x].Value) + " *" case "cl2": cl2 = utils.ToStr(hourdata[x].Value) + " *" case "o3": o3 = utils.ToStr(hourdata[x].Value) + " *" case "tvoc": tvoc = utils.ToStr(hourdata[x].Value) + " *" case "doorlock": doorlock = utils.ToStr(hourdata[x].Value) + " *" case "displacement": displacement = utils.ToStr(hourdata[x].Value) + " *" case "electricalsupply": supply = utils.ToStr(hourdata[x].Value) + " *" case "electricalpower": power = utils.ToStr(hourdata[x].Value) + " *" case "liquidlevel": liquidlevel = utils.ToStr(hourdata[x].Value) + " *" } break } } break } } if DeviceItemContainint(ChannelItem_HaveT, dataitem) { datat = append(datat, t) } if DeviceItemContainint(ChannelItem_HaveH, dataitem) { datah = append(datah, h) } if DeviceItemContainint(ChannelItem_HaveO2, dataitem) { datao2 = append(datao2, o2) } if DeviceItemContainint(ChannelItem_HaveCO2, dataitem) { dataco2 = append(dataco2, co2) } if DeviceItemContainint(ChannelItem_Doorlock, dataitem) { dataots = append(dataots, doorlock) } if DeviceItemContainint(ChannelItem_Displacement, dataitem) { datadisplacement = append(datadisplacement, displacement) } if DeviceItemContainint(ChannelItem_H2O, dataitem) { datah2o = append(datah2o, h2o) } if DeviceItemContainint(ChannelItem_SO2, dataitem) { dataso2 = append(dataso2, so2) } if DeviceItemContainint(ChannelItem_WindSpeed, dataitem) { datawindspeed = append(datawindspeed, windspeed) } if DeviceItemContainint(ChannelItem_Pressure, dataitem) { datapressure = append(datapressure, pressure) } if DeviceItemContainint(ChannelItem_MicroPressure, dataitem) { datapressure = append(datapressure, pressure) } if DeviceItemContainint(ChannelItem_ClO2, dataitem) { dataclo2 = append(dataclo2, clo2) } if DeviceItemContainint(ChannelItem_C2H4, dataitem) { datac2h4 = append(datac2h4, c2h4) } if DeviceItemContainint(ChannelItem_C2H2, dataitem) { datac2h2 = append(datac2h2, c2h2) } if DeviceItemContainint(ChannelItem_Cl2, dataitem) { datacl2 = append(datacl2, cl2) } if DeviceItemContainint(ChannelItem_O3, dataitem) { datao3 = append(datao3, o3) } if DeviceItemContainint(ChannelItem_TVOC, dataitem) { datatvoc = append(datatvoc, tvoc) } if DeviceItemContainint(ChannelItem_MicroPressure, dataitem) { datapressure = append(datapressure, pressure) } if DeviceItemContainint(ChannelItem_Power, dataitem) { datapower = append(datapower, power) datasupply = append(datasupply, supply) } if DeviceItemContainint(ChannelItem_HaveLiquidLevel, dataitem) { dataliquidlevel = append(dataliquidlevel, liquidlevel) } } } return Reportinfo{name, dataitem, datat, datah, datao2, dataco2, dataots, datadisplacement, datawindspeed, datapressure, dataclo2, datac2h4, datac2h2, datacl2, datao3, datatvoc, datah2o, dataso2, datapower, datasupply, dataliquidlevel} } // @Title 报表导出excel // @Description 报表导出excel // @Success 200 {object} business.device.DeviceChannels // @router /excel [get] func (this *ReportsController) GetExcel() { var showcols []string var showxlscols []string var timearea string svc := device.GetDeviceService(utils.DBE) u, p := this.GetuAndp() client := labsop.GetLabSopClient(this.GetupdbAndHost()) timerq, _ := this.GetInt64("timerq") cols := this.GetString("cols") timestr := time.Unix(timerq/1000, 0).Format("2006-01-02") t1, _ := utils.TimeParse(timestr+" 23:59:59", "2006-1-2 15:4:5") t2, _ := utils.TimeParse(timestr+" 00:00:00", "2006-1-2 15:4:5") end := t1.Unix() begin := t2.Unix() var reports = make([]Reportinfo, 0) 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) 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", ",") } //报警数据 apiurl := utils.Cfg.MustValue("server", "apiurl") + "/triggerhistorys/hourdata/?u=" + u + "&p=" + p + "&source=coldchain&account=" + this.GetAccode() + "&start=" + utils.ToStr(begin) + "&end=" + utils.ToStr(end) //取报警数据 var datalist TriggerHourDataRequest strUrl := apiurl + "&ccode=" + svc.GetCIds(where) json.Unmarshal(Apiget(strUrl), &datalist) trihourmap := make(map[string][]TriggerHourData) for _, v := range datalist.Items { trihourmap[v.ChannelCode] = append(trihourmap[v.ChannelCode], v) } for _, v := range channel { 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(doorlock) as doorlock,MEDIAN(doorlock) as h2o,MEDIAN(so2) as so2,MEDIAN(windspeed) as windspeed ,MEDIAN(pressure) as pressure,MEDIAN(clo2) as clo2,MEDIAN(tvoc) as tvoc,MEDIAN(c2h4) as c2h4,MEDIAN(c2h2) as c2h2,MEDIAN(cl2) as cl2,MEDIAN(o3) as o3 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(doorlock) as doorlock, MEDIAN(h2o) as h2o,MEDIAN(so2) as so2,MEDIAN(windspeed) as windspeed ,MEDIAN(pressure) as pressure,MEDIAN(clo2) as clo2,MEDIAN(tvoc) as tvoc,MEDIAN(c2h4) as c2h4,MEDIAN(c2h2) as c2h2,MEDIAN(cl2) as cl2,MEDIAN(o3) as o3 ,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) } result := client.QueryLabSop(queryCommand, data) titlename := v.Title if v.DeviceState == 2 || v.DeviceState == 3 || v.DeviceState == 4 { statename := "" if v.DeviceState == 2 { statename = "维修" } else if v.DeviceState == 3 { statename = "停用" } else { statename = "除霜" } titlename = titlename + " (" + statename + ")" } reportdata := GetReportData(titlename, v.DataItem, showcols, result, trihourmap[v.Code]) reports = append(reports, reportdata) } xlsx.PagePrintfooterContant = "第 &P 页 值班人员签字:" svccompany := company.GetCompanyService(utils.DBE) companyname := svccompany.GetFullnameByAccode(this.GetAccode()) xlsx.PagePrintheadContant = companyname + " (" + timestr + ")" f := xlsx.NewFile() DaySaveXlsx("传感器", "数据日报", this.User.Realname, timestr, showxlscols, reports, f) SaveDirectory("static/file/excel/report/" + this.GetAccode()) f.Save("static/file/excel/report/" + this.GetAccode() + "/daydevicedata.xlsx") var errinfo ErrorInfo errinfo.Message = this.Ctx.Request.Host + "/static/file/excel/report/" + this.GetAccode() + "/daydevicedata.xlsx" errinfo.Code = 0 this.Data["json"] = &errinfo this.ServeJSON() } func DaySaveXlsx(cell1name, name, username, time string, showcols []string, report []Reportinfo, f *xlsx.File) { sheet, _ := f.AddSheet(name) rowname := sheet.AddRow() celln := rowname.AddCell() celln.Value = "名称:" + username rowhead := sheet.AddRow() cell := rowhead.AddCell() cell.Value = cell1name cell = rowhead.AddCell() cell.Value = time rowhead.WriteSlice(&showcols, -1) for _, v := range report { if len(v.DataT) > 0 { row := sheet.AddRow() cell = row.AddCell() cell.Value = v.Name row.WriteSlice(&v.DataT, -1) } if len(v.DataH) > 0 { row := sheet.AddRow() cell = row.AddCell() cell.Value = v.Name row.WriteSlice(&v.DataH, -1) } if len(v.DataO2) > 0 { row := sheet.AddRow() cell = row.AddCell() cell.Value = v.Name row.WriteSlice(&v.DataO2, -1) } if len(v.DataCO2) > 0 { row := sheet.AddRow() cell = row.AddCell() cell.Value = v.Name row.WriteSlice(&v.DataCO2, -1) } if len(v.DataDisplacement) > 0 { //DataDisplacement row := sheet.AddRow() cell = row.AddCell() cell.Value = v.Name row.WriteSlice(&v.DataDisplacement, -1) } if len(v.DataDoorlock) > 0 { row := sheet.AddRow() cell = row.AddCell() cell.Value = v.Name row.WriteSlice(&v.DataDoorlock, -1) } if len(v.DataH2O) > 0 { row := sheet.AddRow() cell = row.AddCell() cell.Value = v.Name row.WriteSlice(&v.DataH2O, -1) } if len(v.DataSO2) > 0 { row := sheet.AddRow() cell = row.AddCell() cell.Value = v.Name row.WriteSlice(&v.DataSO2, -1) } if len(v.DataWindspeed) > 0 { row := sheet.AddRow() cell = row.AddCell() cell.Value = v.Name row.WriteSlice(&v.DataWindspeed, -1) } if len(v.DataPressure) > 0 { row := sheet.AddRow() cell = row.AddCell() cell.Value = v.Name row.WriteSlice(&v.DataPressure, -1) } if len(v.DataClO2) > 0 { row := sheet.AddRow() cell = row.AddCell() cell.Value = v.Name row.WriteSlice(&v.DataClO2, -1) } if len(v.DataC2H4) > 0 { row := sheet.AddRow() cell = row.AddCell() cell.Value = v.Name row.WriteSlice(&v.DataC2H4, -1) } if len(v.DataC2H2) > 0 { row := sheet.AddRow() cell = row.AddCell() cell.Value = v.Name row.WriteSlice(&v.DataC2H2, -1) } if len(v.DataCl2) > 0 { row := sheet.AddRow() cell = row.AddCell() cell.Value = v.Name row.WriteSlice(&v.DataCl2, -1) } if len(v.DataO3) > 0 { row := sheet.AddRow() cell = row.AddCell() cell.Value = v.Name row.WriteSlice(&v.DataO3, -1) } if len(v.DataTVOC) > 0 { row := sheet.AddRow() cell = row.AddCell() cell.Value = v.Name row.WriteSlice(&v.DataTVOC, -1) } if len(v.Datapower) > 0 { row := sheet.AddRow() cell = row.AddCell() cell.Value = v.Name row.WriteSlice(&v.Datapower, -1) } if len(v.Datasupply) > 0 { row := sheet.AddRow() cell = row.AddCell() cell.Value = v.Name row.WriteSlice(&v.Datasupply, -1) } if len(v.DataLiquidLevel) > 0 { row := sheet.AddRow() cell = row.AddCell() cell.Value = v.Name row.WriteSlice(&v.DataLiquidLevel, -1) } } 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 //设置时间单元格的宽度 } // @Title 报表导出pdf // @Description 报表导出pdf // @Success 200 {object} business.device.DeviceChannels // @router /pdf [get] func (this *ReportsController) GetPDF() { var showcols []string var timearea string svc := device.GetDeviceService(utils.DBE) client := labsop.GetLabSopClient(this.GetupdbAndHost()) timerq, _ := this.GetInt64("timerq") cols := this.GetString("cols") timestr := time.Unix(timerq/1000, 0).Format("2006-01-02") t1, _ := utils.TimeParse(timestr+" 23:59:59", "2006-1-2 15:4:5") t2, _ := utils.TimeParse(timestr+" 00:00:00", "2006-1-2 15:4:5") end := t1.Unix() begin := t2.Unix() var reports = make([]Reportinfo, 0) 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) if cols == "" { timearea = ReportDefaultTimearea showcols = strings.Split(ReportDefaultTimearr, ",") } else { timearea = "1h" showcolsstr := cols showcols = strings.Split(showcolsstr, ",") } for _, v := range channel { 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(h2o) as h2o,MEDIAN(so2) as so2 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(h2o) as h2o,,MEDIAN(so2) as so2 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) } result := client.QueryLabSop(queryCommand, data) reportdata := GetReportData(v.Title, v.DataItem, showcols, result, []TriggerHourData{}) reports = append(reports, reportdata) } this.Data["reports"] = reports this.Data["timestr"] = timestr this.Data["showcols"] = showcols var times float64 var report float64 var i float64 report = float64(len(reports)) times = float64(len(showcols)) pdf := gopdf.GoPdf{} //pdf.Start(gopdf.Config{Unit: "pt", PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4 // pdf.Start(gopdf.Config{Unit: "pt", PageSize: gopdf.Rect{W: (200*times + 600), H: (100*report + 250)}}) //595.28, 841.89 = A4 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") 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", "", 24) //字体 pdf.Curr.X = 280 pdf.Curr.Y = 120 pdf.Cell(nil, "Sensor") pdf.Curr.X = (200*times+600)/2 - 200 pdf.Curr.Y = 70 pdf.Cell(nil, "名称:"+this.User.Realname+" ( "+timestr+" )") var yyy float64 yyy = 120 var r float64 var t float64 y := 0 for i = 0; i < report; i++ { var hang int contants := GetpdfContaintext(reports[int(i)].Name) pdf.Curr.Y = yyy + 20 for _, v := range contants { pdf.Br(26) pdf.Curr.X = 105 pdf.Cell(nil, v) } 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 { hang++ 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 { hang++ 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 { hang++ 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 { hang++ 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)].DataDisplacement) != 0 { hang++ 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)].DataDoorlock) != 0 { hang++ 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)].DataH2O) != 0 { hang++ 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 { hang++ 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)].DataWindspeed) != 0 { hang++ for k, temp := range reports[int(i)].DataWindspeed { 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)].DataPressure) != 0 { hang++ for k, temp := range reports[int(i)].DataPressure { 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)].DataClO2) != 0 { hang++ for k, temp := range reports[int(i)].DataClO2 { 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)].DataC2H4) != 0 { hang++ for k, temp := range reports[int(i)].DataC2H4 { 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)].DataC2H2) != 0 { hang++ for k, temp := range reports[int(i)].DataC2H2 { 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)].DataCl2) != 0 { hang++ for k, temp := range reports[int(i)].DataCl2 { 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)].DataO3) != 0 { hang++ for k, temp := range reports[int(i)].DataO3 { 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)].DataTVOC) != 0 { hang++ for k, temp := range reports[int(i)].DataTVOC { 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 { hang++ 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 { hang++ 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 { hang++ 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 - 300 pdf.Curr.Y = 2500 pdf.Cell(nil, "第 "+yy+" 页") pdf.Curr.X = (200*times+600)/2 + 100 pdf.Curr.Y = 2500 pdf.Cell(nil, "值班人员签字:") } 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[0].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")) } } if len(contants)*26 > hang*50 { for i := 0; i < int(math.Ceil((float64(len(contants)*26-(hang*50))+20)/50)); i++ { yyy = yyy + 50 pdf.Line(300, yyy+30, 200*times+500, yyy+30) //横线 } } pdf.Line(100, yyy+30, 200*times+500, yyy+30) //横线 } 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() } func show_strlen(s string) int { sl := 0 rs := []rune(s) for _, r := range rs { rint := int(r) if rint < 128 { sl++ } else { sl += 2 } } return sl } func GetpdfContaintext(str string) (texts []string) { strs := strings.Split(str, "") fnum, start := 1, 0 for k, _ := range strs { fnum++ if fnum > 7 { strfont := strings.Join(strs[start:k+1], "") pwidth := show_strlen(strfont) if pwidth >= 12 { texts = append(texts, strfont) start = k + 1 } } } if start < len(strs) { texts = append(texts, strings.Join(strs[start:len(strs)], "")) } return texts }