1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293 |
- package controllers
- import (
- "Cold_Api/Nats/NatsServer"
- "Cold_Api/conf"
- "Cold_Api/controllers/lib"
- "Cold_Api/models/Account"
- "Cold_Api/models/Device"
- "encoding/base64"
- "errors"
- "fmt"
- "github.com/beego/beego/v2/core/logs"
- beego "github.com/beego/beego/v2/server/web"
- "github.com/signintech/gopdf"
- "github.com/xuri/excelize/v2"
- "math"
- "os"
- "strconv"
- "strings"
- "time"
- )
- type DataController struct {
- beego.Controller
- Admin_r Account.Admin // 登陆的用户
- T_pid int // 公司id
- }
- func (c *DataController) Prepare() {
- GetCookie := c.Ctx.GetCookie("User_tokey")
- GetString := c.GetString("User_tokey")
- User_tokey := GetCookie
- if len(User_tokey) == 0 {
- User_tokey = GetString
- }
- if Account.Admin_r == nil {
- return
- }
- c.Admin_r = *Account.Admin_r
- T_pid := c.Admin_r.T_pid
- EntryPid, _ := Account.Redis_Tokey_T_pid_Get(User_tokey)
- if EntryPid > 0 {
- T_pid = EntryPid
- }
- c.T_pid = T_pid
- }
- // 获取传感器
- func (c *DataController) Device_Sensor_Get() {
- T_sn := c.GetString("T_sn")
- T_id, _ := c.GetInt("T_id")
- //c.Data["Class_List"] = Device.Read_Class_All_1()
- DeviceSensor_r, err := Device.Read_DeviceSensor_ByTsn_Tid(T_sn, T_id)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "获取失败!"}
- c.ServeJSON()
- return
- }
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Device.DeviceSensorToDeviceSensor_R(DeviceSensor_r)}
- c.ServeJSON()
- return
- }
- // 传感器列表
- func (c *DataController) Device_Sensor_List() {
- type R_JSONS struct {
- //必须的大写开头
- DeviceSensor_lite []Device.DeviceSensor_R
- Num int
- Page int
- Page_size int
- Pages []lib.Page_T
- }
- var r_jsons R_JSONS
- page, _ := c.GetInt("page")
- if page < 1 {
- page = 1
- }
- page_z, _ := c.GetInt("page_z")
- if page_z == 0 {
- page_z = conf.Page_size
- }
- T_Class_id, _ := c.GetInt("T_calssid")
- T_type, T_type_err := c.GetInt("T_type")
- if T_type_err != nil {
- T_type = -1
- }
- T_name := c.GetString("T_name")
- // 数据展示页面
- T_RealTime, T_RealTime_err := c.GetInt("T_RealTime")
- if T_RealTime_err != nil {
- T_RealTime = 0
- }
- // 轨迹展示页面
- T_MapShow, T_MapShow_err := c.GetInt("T_MapShow")
- if T_MapShow_err != nil {
- T_MapShow = 0
- }
- T_abandon, _ := c.GetInt("T_abandon")
- var cnt int64
- //Device.Read_DeviceSensorParameter_All_Map("", 0)
- bindSN, err := Account.Read_UserDevice_List(c.Admin_r)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
- c.ServeJSON()
- return
- }
- r_jsons.DeviceSensor_lite, cnt = Device.Read_DeviceSensor_List_For_Data(bindSN, c.T_pid, T_name, T_Class_id, T_type, T_RealTime, T_MapShow, T_abandon, page, page_z)
- page_size := math.Ceil(float64(cnt) / float64(page_z))
- r_jsons.Page = page
- r_jsons.Page_size = int(page_size)
- r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
- r_jsons.Num = int(cnt)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- func (c *DataController) Company_Device_Sensor_List() {
- var r_jsons lib.R_JSONS
- page, _ := c.GetInt("page")
- if page < 1 {
- page = 1
- }
- page_z, _ := c.GetInt("page_z")
- if page_z == 0 {
- page_z = conf.Page_size
- }
- T_Class_id, _ := c.GetInt("T_calssid")
- T_type, T_type_err := c.GetInt("T_type")
- if T_type_err != nil {
- T_type = -1
- }
- T_name := c.GetString("T_name")
- // 数据展示页面
- T_RealTime, T_RealTime_err := c.GetInt("T_RealTime")
- if T_RealTime_err != nil {
- T_RealTime = 0
- }
- // 轨迹展示页面
- T_MapShow, T_MapShow_err := c.GetInt("T_MapShow")
- if T_MapShow_err != nil {
- T_MapShow = 0
- }
- // 查询公司
- Company_r, err := Account.Read_Company_ById(c.T_pid)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
- c.ServeJSON()
- return
- }
- // 查询公司下面所有子公司id
- T_pids := Account.ReadCompanyIds_T_path(Company_r.T_path)
- var cnt int64
- Account.Read_Company_All_Maps()
- r_jsons.Data, cnt = Device.Read_CompanyDeviceSensor_List_For_Data(T_pids, T_name, T_Class_id, T_type, T_RealTime, T_MapShow, page, page_z)
- page_size := math.Ceil(float64(cnt) / float64(page_z))
- r_jsons.Page = page
- r_jsons.Page_size = int(page_size)
- r_jsons.Num = cnt
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- func (c *DataController) Company_Device_Sensor_List_ByKey() {
- var r_jsons lib.R_JSONS
- key := c.GetString("key")
- T_sn := c.GetString("T_sn")
- // 查询公司
- Company_r, err := Account.Read_Company_ByKey(key)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id Err!"}
- c.ServeJSON()
- return
- }
- // 查询公司下面所有子公司id
- T_pids := Account.ReadCompanyIds_T_path(Company_r.T_path)
- Account.Read_Company_All_Maps()
- r_jsons.Data, r_jsons.Num = Device.Read_CompanyDeviceSensor_List_For_Data_ByKey(T_pids, T_sn)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- // 传感器数据列表
- func (c *DataController) Device_Sensor_Data() {
- page, _ := c.GetInt("page")
- page_z, _ := c.GetInt("page_z")
- T_id, _ := c.GetInt("T_id")
- SN := c.GetString("T_sn")
- Time_start := c.GetString("Time_start")
- Time_end := c.GetString("Time_end")
- type R_JSONS struct {
- //必须的大写开头
- DeviceSensor_data []Device.DeviceData_R
- Num int
- Page int
- Page_size int
- Pages []lib.Page_T
- }
- var r_jsons R_JSONS
- var cnt int
- Device.Read_DeviceSensorParameter_All_Map(SN, T_id)
- r_jsons.DeviceSensor_data, cnt = Device.Read_DeviceData_ById_List(SN, T_id, Time_start, Time_end, page, page_z)
- page_size := math.Ceil(float64(cnt) / float64(page_z))
- r_jsons.Page = page
- r_jsons.Page_size = int(page_size)
- r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
- r_jsons.Num = cnt
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- // 实时轨迹
- func (c *DataController) Device_Sensor_Data_Real() {
- page, _ := c.GetInt("page")
- page_z, _ := c.GetInt("page_z")
- T_id, _ := c.GetInt("T_id")
- T_sn := c.GetString("T_sn")
- TaskList, num := Device.Read_DeviceTask_All(T_sn)
- if num == 0 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "暂时没有轨迹信息"}
- c.ServeJSON()
- return
- }
- Time_start := TaskList[0].T_Ut_start
- Time_end := TaskList[0].T_Ut_end
- type R_JSONS struct {
- //必须的大写开头
- DeviceSensor_data []Device.DeviceData_R
- Num int
- Page int
- Page_size int
- Pages []lib.Page_T
- }
- var r_jsons R_JSONS
- var cnt int
- Device.Read_DeviceSensorParameter_All_Map(T_sn, T_id)
- r_jsons.DeviceSensor_data, cnt = Device.Read_DeviceData_ById_List(T_sn, T_id, Time_start, Time_end, page, page_z)
- page_size := math.Ceil(float64(cnt) / float64(page_z))
- r_jsons.Page = page
- r_jsons.Page_size = int(page_size)
- r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
- r_jsons.Num = cnt
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- // 设备数据
- func (c *DataController) Device_Sensor_Data_More() {
- page, _ := c.GetInt("page")
- if page < 1 {
- page = 1
- }
- page_z, _ := c.GetInt("page_z")
- if page_z < 1 {
- page_z = conf.Page_size
- }
- T_snid := c.GetString("T_snid")
- Time_start := c.GetString("Time_start")
- Time_end := c.GetString("Time_end")
- if len(T_snid) < 10 {
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- type R_JSONS struct {
- //必须的大写开头
- Data []Device.DeviceData_R
- Num int64
- Page int
- Page_size int
- }
- var r_jsons R_JSONS
- r_jsons.Data, r_jsons.Num = Device.Read_DeviceData_By_T_snid_List(T_snid, Time_start, Time_end, page, page_z)
- r_jsons.Page = page
- r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- // -------------------------------------
- // 导出传感器数据列表
- func (c *DataController) Device_Sensor_Data_Excel() {
- T_snid := c.GetString("T_snid")
- Time_start := c.GetString("Time_start")
- Time_end := c.GetString("Time_end")
- if len(T_snid) < 10 {
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- var DeviceSensor_data []Device.DeviceData_R
- DeviceSensor_data, _ = Device.Read_DeviceData_By_T_snid_List(T_snid, Time_start, Time_end, 0, 9999)
- // 按照传感器T_sort排序
- //var snids string
- //dsList := Device.Read_DeviceSensorList_By_T_snid(T_snid)
- //// 创建名称到权重的映射
- //orderMap := make(map[int]int)
- //for i, v := range dsList {
- // snids += fmt.Sprintf("%s,%d|", v.T_sn, v.T_id)
- // orderMap[v.T_id] = i
- //}
- //sort.Slice(DeviceSensor_data, func(i, j int) bool {
- // if DeviceSensor_data[i].T_time == DeviceSensor_data[j].T_time {
- // // 如果时间相同,则按预设顺序排序
- // return orderMap[DeviceSensor_data[i].T_id] < orderMap[DeviceSensor_data[j].T_id]
- // }
- // return DeviceSensor_data[i].T_time > DeviceSensor_data[j].T_time
- //})
- f := excelize.NewFile() // 设置单元格的值
- // 这里设置表头
- f.SetCellValue("Sheet1", "A1", "编号")
- f.SetCellValue("Sheet1", "B1", "传感器名称")
- f.SetCellValue("Sheet1", "C1", "温度℃")
- f.SetCellValue("Sheet1", "D1", "湿度%")
- f.SetCellValue("Sheet1", "E1", "温度范围 ℃")
- f.SetCellValue("Sheet1", "F1", "湿度范围 %")
- f.SetCellValue("Sheet1", "G1", "记录时间")
- f.SetCellValue("Sheet1", "H1", "备注")
- // 设置列宽
- f.SetColWidth("Sheet1", "A", "A", 10)
- f.SetColWidth("Sheet1", "B", "B", 15)
- f.SetColWidth("Sheet1", "C", "D", 10)
- f.SetColWidth("Sheet1", "E", "F", 15)
- f.SetColWidth("Sheet1", "G", "G", 22)
- f.SetColWidth("Sheet1", "H", "H", 22)
- line := 1
- headStyleLower, _ := f.NewStyle(
- &excelize.Style{
- Font: &excelize.Font{Size: 11, Color: "#ff8585", Family: "arial"},
- })
- headStyleUpper, _ := f.NewStyle(
- &excelize.Style{
- Font: &excelize.Font{Size: 11, Color: "#ff8585", Family: "arial"},
- })
- // 循环写入数据
- for _, v := range DeviceSensor_data {
- line++
- f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), line-1)
- f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.T_name+"["+strconv.Itoa(v.T_id)+"]")
- if v.T_ist == 1 {
- f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.T_t)
- f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), strconv.FormatFloat(float64(v.T_tl), 'f', 2, 64)+"~"+strconv.FormatFloat(float64(v.T_tu), 'f', 2, 64))
- if (v.T_t < v.T_tl || v.T_t > v.T_tu) && v.T_free == 0 {
- f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("G%d", line), headStyleLower)
- }
- }
- if v.T_ish == 1 {
- f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_rh)
- f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), strconv.FormatFloat(float64(v.T_rhl), 'f', 2, 64)+"~"+strconv.FormatFloat(float64(v.T_rhu), 'f', 2, 64))
- if v.T_rh < v.T_rhl || v.T_rh > v.T_rhu && v.T_free == 0 {
- f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("G%d", line), headStyleUpper)
- }
- }
- f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.T_time)
- f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), v.T_remark)
- //if v.T_ist == 1 && (v.T_t < v.T_tl || v.T_t > v.T_tu) {
- // f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("G%d", line), headStyleLower)
- //}
- //if v.T_ish == 1 && (v.T_rh < v.T_rhl || v.T_rh > v.T_rhu) {
- // f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("G%d", line), headStyleUpper)
- //}
- }
- timeStr := time.Now().Format("20060102150405")
- // 保存文件
- if err := f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil {
- logs.Error(err)
- }
- // 上传 OSS
- url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx")
- if !is {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
- c.ServeJSON()
- return
- }
- //删除目录
- err := os.Remove("ofile/" + timeStr + ".xlsx")
- if err != nil {
- logs.Error(err)
- }
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
- c.ServeJSON()
- return
- }
- // 列表 - 接口
- func (c *DataController) Device_Sensor_Data_Excel_m() {
- T_snid := c.GetString("T_snid")
- Time_start := c.GetString("Time_start")
- Time_end := c.GetString("Time_end")
- t_x, _ := c.GetInt("t_x")
- if len(T_snid) < 10 {
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- var DeviceSensor_data []Device.DeviceData_R
- DeviceSensor_data, _ = Device.Read_DeviceData_By_T_snid_List(T_snid, Time_start, Time_end, 0, 9999)
- f := excelize.NewFile() // 设置单元格的值
- // 这里设置表头
- f.SetCellValue("Sheet1", "A1", "编号")
- f.SetCellValue("Sheet1", "B1", "传感器名称")
- f.SetCellValue("Sheet1", "C1", "温度℃")
- f.SetCellValue("Sheet1", "D1", "湿度%")
- f.SetCellValue("Sheet1", "E1", "温度范围 ℃")
- f.SetCellValue("Sheet1", "F1", "湿度范围 %")
- f.SetCellValue("Sheet1", "G1", "记录时间")
- // 设置列宽
- f.SetColWidth("Sheet1", "A", "A", 10)
- f.SetColWidth("Sheet1", "B", "B", 15)
- f.SetColWidth("Sheet1", "C", "D", 10)
- f.SetColWidth("Sheet1", "E", "F", 15)
- f.SetColWidth("Sheet1", "G", "G", 22)
- line := 1
- headStyleLower, _ := f.NewStyle(
- &excelize.Style{
- Font: &excelize.Font{Size: 11, Color: "#a8f7ff", Family: "arial"},
- })
- headStyleUpper, _ := f.NewStyle(
- &excelize.Style{
- Font: &excelize.Font{Size: 11, Color: "#ff8585", Family: "arial"},
- })
- atime, _ := time.ParseDuration("-1m") // Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
- // 循环写入数据
- for v_i, v := range DeviceSensor_data {
- line++
- f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), line-1)
- f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.T_name+"["+strconv.Itoa(v.T_id)+"]")
- f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.T_t)
- f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_rh)
- f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), strconv.FormatFloat(float64(v.T_tl), 'f', 2, 64)+"~"+strconv.FormatFloat(float64(v.T_tu), 'f', 2, 64))
- f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), strconv.FormatFloat(float64(v.T_rhl), 'f', 2, 64)+"~"+strconv.FormatFloat(float64(v.T_rhu), 'f', 2, 64))
- f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.T_time)
- if v.T_t < v.T_tl || v.T_t > v.T_tu {
- f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("G%d", line), headStyleLower)
- }
- if v.T_rh < v.T_rhl || v.T_rh > v.T_rhu {
- f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("G%d", line), headStyleUpper)
- }
- if len(DeviceSensor_data)-1 == v_i {
- break
- }
- cha_v := ((DeviceSensor_data[v_i+1].T_t - v.T_t) / 3)
- println("t_x:", t_x)
- for i := 0; i < t_x; i++ {
- println("// -------", i)
- v.T_t = float32(lib.Decimal(float64(v.T_t + cha_v)))
- fTime, _ := time.Parse("2006-01-02 15:04:05", v.T_time)
- fTime = fTime.Add(atime)
- v.T_time = fTime.Format("2006-01-02 15:04:05")
- line++
- f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), line-1)
- f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.T_name+"["+strconv.Itoa(v.T_id)+"]")
- f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.T_t)
- f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_rh)
- f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), strconv.FormatFloat(float64(v.T_tl), 'f', 2, 64)+"~"+strconv.FormatFloat(float64(v.T_tu), 'f', 2, 64))
- f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), strconv.FormatFloat(float64(v.T_rhl), 'f', 2, 64)+"~"+strconv.FormatFloat(float64(v.T_rhu), 'f', 2, 64))
- f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), v.T_time)
- if v.T_t < v.T_tl || v.T_t > v.T_tu {
- f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("G%d", line), headStyleLower)
- }
- if v.T_rh < v.T_rhl || v.T_rh > v.T_rhu {
- f.SetCellStyle("Sheet1", fmt.Sprintf("A%d", line), fmt.Sprintf("G%d", line), headStyleUpper)
- }
- }
- }
- timeStr := time.Now().Format("20060102150405")
- // 保存文件
- if err := f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil {
- logs.Error(err)
- }
- // 上传 OSS
- url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx")
- if !is {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
- c.ServeJSON()
- return
- }
- //删除目录
- //err := os.Remove("ofile/" + timeStr + ".xlsx")
- //if err != nil {
- // logs.Error(err)
- //}
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
- c.ServeJSON()
- return
- }
- // 小程序 - 打印
- func (c *DataController) DeviceSensor_Data_Print() {
- SN := c.GetString("T_sn")
- Time_start := c.GetString("Time_start")
- Time_end := c.GetString("Time_end")
- type R_JSONS struct {
- //必须的大写开头
- T_time string
- T1 interface{}
- T2 interface{}
- T3 interface{}
- T4 interface{}
- }
- r_jsons := make(map[string]R_JSONS)
- type JSONS struct {
- //必须的大写开头
- Code int16
- Msg string
- DeviceSensorNum int
- DeviceSensor Device.DeviceSensor_Applet
- AdminNane string
- Data interface{} // 泛型
- }
- GROUP_BY_t_time := Device.Read_DeviceData_List_GROUP_BY_t_time(SN, Time_start, Time_end)
- // 时间 MAP
- for _, vt := range GROUP_BY_t_time {
- stamp, _ := time.Parse("2006-1-02 15:04:05", lib.To_string(vt[0]))
- T_time := stamp.Format("2006-01-02 15:04:05")
- r_jsons[T_time] = R_JSONS{T_time: T_time, T1: nil, T2: nil, T3: nil, T4: nil}
- }
- dsList := Device.Read_DeviceSensor_List_T_sn_T_datashow(SN)
- if len(dsList) == 0 {
- c.Data["json"] = JSONS{Code: 200, Msg: "ok!", DeviceSensorNum: len(dsList), Data: r_jsons}
- c.ServeJSON()
- return
- }
- company, _ := Account.Read_Company_ById(c.T_pid)
- for i, v := range dsList {
- if i >= 4 {
- break
- }
- // 记录传感器
- maps, num := Device.Read_DeviceData_ById_List(SN, v.T_id, Time_start, Time_end, 1, 9999)
- if num > 0 {
- for _, v_map := range maps {
- ws, ok := r_jsons[v_map.T_time]
- if ok {
- switch i {
- case 0:
- ws.T1 = v_map
- case 1:
- ws.T2 = v_map
- case 2:
- ws.T3 = v_map
- case 3:
- ws.T4 = v_map
- }
- r_jsons[v_map.T_time] = ws
- }
- }
- }
- }
- c.Data["json"] = JSONS{Code: 200, Msg: "ok!", DeviceSensorNum: len(dsList), DeviceSensor: Device.DeviceSensorToDeviceSensor_Applet(dsList[0]), AdminNane: company.T_name, Data: r_jsons}
- c.ServeJSON()
- return
- }
- // 列表 - 接口
- func (c *DataController) Device_Sensor_Data_PDF() {
- T_snid := c.GetString("T_snid")
- Time_start := c.GetString("Time_start")
- Time_end := c.GetString("Time_end")
- T_ish, _ := c.GetInt("T_ish") // 展示湿度
- if len(T_snid) < 10 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_snid Err!"}
- c.ServeJSON()
- return
- }
- //T_ist // 温度 1开启 2关闭
- //T_ish // 湿度 1开启 2关闭
- T_ist := 1
- //T_ist_list := make([]int, 0)
- //T_ish_list := make([]int, 0)
- //T_snid_list := strings.Split(strings.Trim(T_snid, "|"), "|")
- //for _, v := range T_snid_list {
- // sn_id := strings.Split(v, ",")
- // if len(sn_id) == 2 {
- // device, _ := Device.Read_Device_ByT_sn(sn_id[0])
- // T_ist_list = append(T_ist_list, device.T_ist)
- // T_ish_list = append(T_ish_list, device.T_ish)
- // }
- //}
- //istHasOne := false
- //istHasTwo := false
- //
- //for _, v := range T_ist_list {
- // if v == 1 {
- // istHasOne = true
- // }
- // if v == 2 {
- // istHasTwo = true
- // }
- //}
- //// 全部都为2 则不显示温度
- //if !istHasOne && istHasTwo {
- // T_ist = 2
- //}
- //
- //ishHasOne := false
- //ishHasTwo := false
- //T_ish := 1
- //for _, v := range T_ish_list {
- // if v == 1 {
- // ishHasOne = true
- // }
- // if v == 2 {
- // ishHasTwo = true
- // }
- //}
- //// 全部都为2 则不显示湿度
- //if !ishHasOne && ishHasTwo {
- // T_ish = 2
- //}
- var DeviceSensor_data []Device.DeviceData_R
- DeviceSensor_data, _ = Device.Read_DeviceData_By_T_snid_List(T_snid, Time_start, Time_end, 0, 9999)
- // 按照传感器T_sort排序
- //var snids string
- //dsList := Device.Read_DeviceSensorList_By_T_snid(T_snid)
- //// 创建名称到权重的映射
- //orderMap := make(map[int]int)
- //for i, v := range dsList {
- // snids += fmt.Sprintf("%s,%d|", v.T_sn, v.T_id)
- // orderMap[v.T_id] = i
- //}
- //sort.Slice(DeviceSensor_data, func(i, j int) bool {
- // if DeviceSensor_data[i].T_time == DeviceSensor_data[j].T_time {
- // // 如果时间相同,则按预设顺序排序
- // return orderMap[DeviceSensor_data[i].T_id] < orderMap[DeviceSensor_data[j].T_id]
- // }
- // return DeviceSensor_data[i].T_time > DeviceSensor_data[j].T_time
- //})
- // -------------------获取最高温湿度、最低温湿度、平均温湿度
- // 最高温度、最低温度
- var maxTemp, minTemp, maxRH, minRH float32
- // 最高温度时间、最低温度时间
- var maxTempTime, minTempTime, maxRHTime, minRHTime string
- // 总温度
- var totalTemp, totalRH float32
- // 平均温度
- var avgTemp, avgRH float32
- var TempCount, RHCount int
- if len(DeviceSensor_data) > 0 {
- // 最高温度及时刻
- firstTempFlag := false
- //var maxTemp,minTemp float32
- //var maxTempTime,minTempTime string
- //maxTemp = DeviceSensor_data[0].T_t
- //maxTempTime = DeviceSensor_data[0].T_time
- //// 最低温度及时刻
- //minTemp = DeviceSensor_data[0].T_t
- //minTempTime = DeviceSensor_data[0].T_time
- // 最高湿度度及时刻
- firstRHFlag := false
- //maxRH = DeviceSensor_data[0].T_rh
- //maxRHTime = DeviceSensor_data[0].T_time
- //// 最低湿度及时刻
- //minRH = DeviceSensor_data[0].T_rh
- //minRHTime = DeviceSensor_data[0].T_time
- for _, data := range DeviceSensor_data {
- if data.T_ist == 1 {
- if firstTempFlag {
- if data.T_t > maxTemp {
- maxTemp = data.T_t
- maxTempTime = data.T_time
- }
- if data.T_t < minTemp {
- minTemp = data.T_t
- minTempTime = data.T_time
- }
- totalTemp += data.T_t
- TempCount++
- } else {
- maxTemp = data.T_t
- maxTempTime = data.T_time
- // 最低温度及时刻
- minTemp = data.T_t
- minTempTime = data.T_time
- firstTempFlag = true
- totalTemp += data.T_t
- TempCount = 1
- }
- }
- if data.T_ish == 1 {
- if firstRHFlag {
- if data.T_rh > maxRH {
- maxRH = data.T_rh
- maxRHTime = data.T_time
- }
- if data.T_rh < minRH {
- minRH = data.T_rh
- minRHTime = data.T_time
- }
- totalRH += data.T_rh
- RHCount++
- } else {
- maxRH = data.T_rh
- maxRHTime = data.T_time
- // 最低温度及时刻
- minRH = data.T_rh
- minRHTime = data.T_time
- firstRHFlag = true
- totalRH += data.T_rh
- RHCount = 1
- }
- }
- }
- if TempCount > 0 {
- // 平均温度
- avgTemp = totalTemp / float32(TempCount)
- }
- // 平均湿度
- if RHCount > 0 {
- avgRH = totalRH / float32(RHCount)
- }
- }
- // -------------------获取最高温湿度、最低温湿度、平均温湿度结束
- maxTempTime = maxTempTime
- minTempTime = minTempTime
- maxRHTime = maxRHTime
- minRHTime = minRHTime
- var err error
- pdf := &gopdf.GoPdf{}
- pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4
- //err = GetFont(pdf, "LiberationSerif-Regular.ttf")
- //if err != nil {
- // log.Fatalln(err)
- //}
- //err = pdf.SetFont("Ubuntu-L", "", 14)
- //if err != nil {
- // log.Fatalln(err)
- //}
- err = pdf.AddTTFFont("simsun", "static/fonts/MiSans-Medium.ttf")
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
- c.ServeJSON()
- return
- }
- err = pdf.SetFont("simsun", "", 24)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
- c.ServeJSON()
- return
- }
- pdf.SetGrayFill(0.5)
- pdf.SetMargins(0, 20, 0, 20)
- pdf.AddPage()
- company, _ := Account.Read_Company_ById(c.T_pid)
- textw, _ := pdf.MeasureTextWidth(company.T_name)
- pdf.SetX((595 / 2) - (textw / 2))
- pdf.SetY(40)
- pdf.Text(company.T_name)
- // 线
- pdf.SetLineWidth(2)
- pdf.SetLineType("dashed")
- pdf.Line(10, 60, 585, 60)
- err = pdf.AddTTFFont("wts", "static/fonts/MiSans-Medium.ttf")
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
- c.ServeJSON()
- return
- }
- err = pdf.SetFont("wts", "", 12)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
- c.ServeJSON()
- return
- }
- var s_time, e_time string
- if len(DeviceSensor_data) > 0 {
- s_time = fmt.Sprintf("%s", DeviceSensor_data[0].T_time)
- e_time = fmt.Sprintf("%s", DeviceSensor_data[len(DeviceSensor_data)-1].T_time)
- }
- lib.RectFillColor(pdf, "历史数据["+e_time+" / "+s_time+"]", 14, 22, 80, 550, 40, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- var y float64 = 120
- if T_ist == 1 {
- lib.RectFillColor(pdf, fmt.Sprintf("最高温度: %.1f ", maxTemp), 12, 22, y, 183, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- lib.RectFillColor(pdf, fmt.Sprintf("最低温度: %.1f ", minTemp), 12, 205, y, 183, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- lib.RectFillColor(pdf, fmt.Sprintf("平均温度: %.1f ", avgTemp), 12, 388, y, 184, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- y += 20
- }
- if T_ish == 1 {
- lib.RectFillColor(pdf, fmt.Sprintf("最高湿度: %.1f ", maxRH), 12, 22, y, 183, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- lib.RectFillColor(pdf, fmt.Sprintf("最低湿度: %.1f ", minRH), 12, 205, y, 183, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- lib.RectFillColor(pdf, fmt.Sprintf("平均湿度: %.1f ", avgRH), 12, 388, y, 184, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- y += 20
- }
- y += 5
- lib.RectFillColor(pdf, "序号", 12, 22, y, 30, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- if T_ish == 1 {
- lib.RectFillColor(pdf, "传感器名称", 12, 52, y, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- lib.RectFillColor(pdf, "温度℃", 12, 152, y, 50, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- lib.RectFillColor(pdf, "湿度%", 12, 202, y, 50, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- }
- if T_ish == 0 {
- lib.RectFillColor(pdf, "传感器名称", 12, 52, y, 140, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- lib.RectFillColor(pdf, "温度℃", 12, 192, y, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- }
- if T_ish == 1 {
- lib.RectFillColor(pdf, "温度范围", 12, 252, y, 80, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- lib.RectFillColor(pdf, "湿度范围", 12, 332, y, 80, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- }
- if T_ish == 0 {
- lib.RectFillColor(pdf, "温度范围", 12, 292, y, 120, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- }
- lib.RectFillColor(pdf, "记录时间", 12, 412, y, 120, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- lib.RectFillColor(pdf, "备注", 12, 532, y, 40, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- y += 20
- err = pdf.SetFont("wts", "", 10)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
- c.ServeJSON()
- return
- }
- for i, v := range DeviceSensor_data {
- text := fmt.Sprintf(" %d ", i+1)
- var textH float64 = 25 // if text height is 25px.
- pdf.SetNewY(y, textH)
- y = pdf.GetY()
- // newY = 20
- if y < 30 {
- lib.RectFillColor(pdf, "序号", 12, 22, y, 30, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- if T_ish == 1 {
- lib.RectFillColor(pdf, "传感器名称", 12, 52, y, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- lib.RectFillColor(pdf, "温度℃", 12, 152, y, 50, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- lib.RectFillColor(pdf, "湿度%", 12, 202, y, 50, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- }
- if T_ish == 0 {
- lib.RectFillColor(pdf, "传感器名称", 12, 52, y, 140, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- lib.RectFillColor(pdf, "温度℃", 12, 192, y, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- }
- if T_ish == 1 {
- lib.RectFillColor(pdf, "温度范围", 12, 252, y, 80, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- lib.RectFillColor(pdf, "湿度范围", 12, 332, y, 80, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- }
- if T_ish == 0 {
- lib.RectFillColor(pdf, "温度范围", 12, 292, y, 120, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- }
- lib.RectFillColor(pdf, "记录时间", 12, 412, y, 120, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- lib.RectFillColor(pdf, "备注", 12, 532, y, 40, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- y += 20
- }
- //pdf.SetX(x) // must after pdf.SetNewY() called.
- //err = pdf.Text(text)
- //if err != nil {
- // log.Fatalln(err)
- //}
- T_t := fmt.Sprintf(" %.1f ", v.T_t)
- T_rh := fmt.Sprintf(" %.1f ", v.T_rh)
- T_Tlu := fmt.Sprintf(" %.1f ~ %.1f ", v.T_tl, v.T_tu)
- T_Rlu := fmt.Sprintf(" %.1f ~ %.1f ", v.T_rhl, v.T_rhu)
- T_time := fmt.Sprintf("%s", v.T_time)
- //if v.T_ist == 1 {
- // lib.RectFillColor(pdf, T_t, 10, 152, y, 50, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- // lib.RectFillColor(pdf, T_Tlu, 10, 252, y, 80, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- //} else {
- // lib.RectFillColor(pdf, "", 10, 152, y, 50, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- // lib.RectFillColor(pdf, "", 10, 252, y, 80, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- //}
- //if v.T_ish == 1 {
- // lib.RectFillColor(pdf, T_rh, 10, 202, y, 50, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- // lib.RectFillColor(pdf, T_Rlu, 10, 332, y, 80, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- //} else {
- // lib.RectFillColor(pdf, "", 10, 202, y, 50, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- // lib.RectFillColor(pdf, "", 10, 332, y, 80, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- //}
- if T_ish == 1 {
- lib.RectFillColor(pdf, T_t, 10, 152, y, 50, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- lib.RectFillColor(pdf, T_Tlu, 10, 252, y, 80, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- lib.RectFillColor(pdf, T_rh, 10, 202, y, 50, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- lib.RectFillColor(pdf, T_Rlu, 10, 332, y, 80, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- lib.RectFillColor(pdf, v.T_name, 10, 52, y, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- }
- if T_ish == 0 {
- lib.RectFillColor(pdf, v.T_name, 10, 52, y, 140, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- lib.RectFillColor(pdf, T_t, 10, 192, y, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- lib.RectFillColor(pdf, T_Tlu, 10, 292, y, 120, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- }
- lib.RectFillColor(pdf, text, 10, 22, y, 30, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- lib.RectFillColor(pdf, T_time, 10, 412, y, 120, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- lib.RectFillColor(pdf, v.T_remark, 10, 532, y, 40, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- y += 20
- }
- timeStr := "ofile/" + time.Now().Format("20060102150405") + ".pdf"
- err = pdf.WritePdf(timeStr)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
- c.ServeJSON()
- return
- }
- // 上传 OSS
- url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/"+timeStr, timeStr)
- if !is {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
- c.ServeJSON()
- return
- }
- //删除目录
- err = os.Remove(timeStr)
- if err != nil {
- logs.Error(err)
- }
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
- c.ServeJSON()
- return
- }
- func (c *DataController) Device_Sensor_Data_ChartShow_PDF() {
- T_snid := c.GetString("T_snid")
- Time_start := c.GetString("Time_start")
- Time_end := c.GetString("Time_end")
- T_forwarding_unit := c.GetString("T_forwarding_unit")
- T_consignee_unit := c.GetString("T_consignee_unit")
- T_remark := c.GetString("T_remark")
- T_temp_show, _ := c.GetInt("T_temp_show")
- T_Humidity_show, _ := c.GetInt("T_Humidity_show")
- T_template := c.GetString("T_template")
- if T_temp_show == 0 && T_Humidity_show == 0 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "温湿度不能同时不显示"}
- c.ServeJSON()
- return
- }
- if len(T_snid) < 10 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_snid Err!"}
- c.ServeJSON()
- return
- }
- // 获取图片信息
- base64String := c.GetString("file")
- imageBytes, err := base64.StdEncoding.DecodeString(base64String)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "解析base64失败"}
- c.ServeJSON()
- return
- }
- company, _ := Account.Read_Company_ById(c.T_pid)
- if T_template == "1" {
- url, err := Device_Sensor_Data_ChartShow_PDF1(company.T_name, T_snid, Time_start, Time_end, T_forwarding_unit, T_consignee_unit, T_remark, imageBytes, T_temp_show, T_Humidity_show)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
- c.ServeJSON()
- return
- }
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
- c.ServeJSON()
- return
- }
- url, err := Device_Sensor_Data_ChartShow_PDF0(company.T_name, T_snid, Time_start, Time_end, T_forwarding_unit, T_consignee_unit, T_remark, imageBytes, T_temp_show, T_Humidity_show)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
- c.ServeJSON()
- return
- }
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
- c.ServeJSON()
- return
- }
- // 图表展示 下载PDF 箱/车
- func Device_Sensor_Data_ChartShow_PDF1(companyName, T_snid, Time_start, Time_end, T_forwarding_unit, T_consignee_unit, T_remark string, imageBytes []byte,
- T_temp_show, T_Humidity_show int) (url string, err error) {
- snidNum := strings.Split(strings.Trim(T_snid, "|"), "|")
- if len(snidNum) > 2 {
- err = errors.New("车/箱最多只能选择2个探头!")
- return
- }
- var sn1, sn2 string
- var id1, id2 int
- var r_maps_sn1, r_maps_sn2 []Device.DeviceData_R
- var DeviceSensor_data []Device.DeviceData_R
- var Pdf_data []Device.DeviceData_Pdf
- if len(snidNum) == 1 {
- sn_id := strings.Split(snidNum[0], ",")
- sn1, id1 = sn_id[0], lib.To_int(sn_id[1])
- r_maps_sn1, _ = Device.Read_DeviceData_ById_List_OrderByTimeAes(sn1, id1, Time_start, Time_end, 0, 9999)
- DeviceSensor_data = r_maps_sn1
- Pdf_data = Device.Read_DeviceData_ByIds_ForPDF(sn1, []int{id1}, Time_start, Time_end)
- }
- if len(snidNum) == 2 {
- sn_id1 := strings.Split(snidNum[0], ",")
- sn_id2 := strings.Split(snidNum[1], ",")
- sn1, id1 = sn_id1[0], lib.To_int(sn_id1[1])
- sn2, id2 = sn_id2[0], lib.To_int(sn_id2[1])
- if sn1 != sn2 {
- err = errors.New("请选择相同设备编号的探头导出!")
- return
- }
- r_maps_sn1, _ = Device.Read_DeviceData_ById_List_OrderByTimeAes(sn1, id1, Time_start, Time_end, 0, 9999)
- r_maps_sn2, _ = Device.Read_DeviceData_ById_List_OrderByTimeAes(sn2, id2, Time_start, Time_end, 0, 9999)
- DeviceSensor_data = append(DeviceSensor_data, r_maps_sn1...)
- DeviceSensor_data = append(DeviceSensor_data, r_maps_sn2...)
- Pdf_data = Device.Read_DeviceData_ByIds_ForPDF(sn1, []int{id1, id2}, Time_start, Time_end)
- }
- device, err := Device.Read_Device_ByT_sn(sn1)
- if err != nil {
- err = errors.New("查询主机信息失败!")
- return
- }
- // -------------------获取最高温湿度、最低温湿度、平均温湿度
- // 最高温度、最低温度
- var maxTemp, minTemp float32
- // 最高温度时间、最低温度时间
- var maxTempTime, minTempTime string
- // 总温度
- var totalTemp float32
- // 平均温度
- var avgTemp float32
- if len(DeviceSensor_data) > 0 {
- // 最高温度及时刻
- maxTemp = DeviceSensor_data[0].T_t
- maxTempTime = DeviceSensor_data[0].T_time
- // 最低温度及时刻
- minTemp = DeviceSensor_data[0].T_t
- minTempTime = DeviceSensor_data[0].T_time
- for _, data := range DeviceSensor_data {
- if data.T_t > maxTemp {
- maxTemp = data.T_t
- maxTempTime = data.T_time
- }
- if data.T_t < minTemp {
- minTemp = data.T_t
- minTempTime = data.T_time
- }
- totalTemp += data.T_t
- }
- // 平均温度
- avgTemp = totalTemp / float32(len(DeviceSensor_data))
- // 平均湿度
- //avgHumidity = totalHumidity / float32(len(DeviceSensor_data))
- }
- // -------------------获取最高温湿度、最低温湿度、平均温湿度结束
- pdf := &gopdf.GoPdf{}
- pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4
- err = pdf.AddTTFFont("wts", "static/fonts/MiSans-Medium.ttf")
- if err != nil {
- return
- }
- err = pdf.SetFont("wts", "", 20)
- if err != nil {
- return
- }
- pdf.SetGrayFill(0.5)
- pdf.SetMargins(0, 20, 0, 20)
- pdf.AddPage()
- title := companyName + "温湿度记录"
- var y float64 = 40
- textw, _ := pdf.MeasureTextWidth(title)
- pdf.SetX((595 / 2) - (textw / 2))
- pdf.SetY(y)
- pdf.Text(title)
- y += 30
- pdf.SetFont("wts", "", 16)
- pdf.SetXY(10, y)
- pdf.Text("实施设备信息")
- // 线
- y += 10
- pdf.SetLineWidth(0.5)
- pdf.SetStrokeColor(169, 169, 169)
- pdf.Line(10, y, 585, y)
- pdf.SetFont("wts", "", 10)
- y += 20
- pdf.SetXY(10, y)
- pdf.Text(fmt.Sprintf("主机名称:%s", device.T_devName))
- pdf.SetXY(300, y)
- pdf.Text(fmt.Sprintf("主机编号:%s", device.T_sn))
- // 0 仓库 1 车厢
- y += 20
- ds1, _ := Device.Read_DeviceSensor_ByT_sn(sn1, id1)
- pdf.SetXY(10, y)
- pdf.Text(fmt.Sprintf("测点信息T1:%s", ds1.T_name))
- if len(snidNum) > 1 {
- ds2, _ := Device.Read_DeviceSensor_ByT_sn(sn2, id2)
- pdf.SetXY(300, y)
- pdf.Text(fmt.Sprintf("测点信息T2:%s", ds2.T_name))
- }
- y += 35
- pdf.SetFont("wts", "", 16)
- pdf.SetXY(10, y)
- pdf.Text("记录概要信息")
- // 线
- y += 10
- pdf.SetLineWidth(0.5)
- pdf.SetStrokeColor(169, 169, 169)
- pdf.Line(10, y, 585, y)
- y += 20
- var s_time, e_time string
- if len(r_maps_sn1) > 0 {
- s_time = fmt.Sprintf("%s", Pdf_data[0].T_time)
- e_time = fmt.Sprintf("%s", Pdf_data[len(Pdf_data)-1].T_time)
- }
- pdf.SetFont("wts", "", 10)
- pdf.SetXY(10, y)
- pdf.Text(fmt.Sprintf("记录开始时间:%s", s_time))
- pdf.SetXY(240, y)
- pdf.Text(fmt.Sprintf("记录结束时间:%s", e_time))
- sTime, _ := lib.TimeStrToTime(s_time)
- eTime, _ := lib.TimeStrToTime(e_time)
- pdf.SetXY(470, y)
- minutes := int(eTime.Sub(sTime).Minutes())
- hours := minutes / 60
- remainingMinutes := minutes % 60
- pdf.Text(fmt.Sprintf("记录总时间:%dh%dmin", hours, remainingMinutes))
- if T_temp_show == 1 {
- y += 15
- pdf.SetXY(10, y)
- pdf.Text(fmt.Sprintf("最高温度:%s【%.1f℃】", maxTempTime, lib.RoundToDecimal(float64(maxTemp), 1)))
- pdf.SetXY(240, y)
- pdf.Text(fmt.Sprintf("最低温度:%s【%.1f℃】", minTempTime, lib.RoundToDecimal(float64(minTemp), 1)))
- pdf.SetXY(470, y)
- pdf.Text(fmt.Sprintf("平均温度:%.2f℃", lib.RoundToDecimal(float64(avgTemp), 1)))
- }
- y += 15
- pdf.SetXY(10, y)
- var start_temp, end_temp string
- if len(r_maps_sn1) > 0 {
- start_temp = fmt.Sprintf("T1【%.1f℃】", r_maps_sn1[0].T_t)
- end_temp = fmt.Sprintf("T1【%.1f℃】", r_maps_sn1[len(r_maps_sn1)-1].T_t)
- }
- if len(r_maps_sn2) > 0 {
- start_temp += fmt.Sprintf(",T2【%.1f℃】", r_maps_sn2[0].T_t)
- end_temp += fmt.Sprintf(",T2【%.1f℃】", r_maps_sn2[len(r_maps_sn2)-1].T_t)
- }
- pdf.Text(fmt.Sprintf("启运温度:%s", start_temp))
- pdf.SetXY(240, y)
- pdf.Text(fmt.Sprintf("到达温度:%s", end_temp))
- var temp_range string
- if len(DeviceSensor_data) > 0 {
- temp_range = fmt.Sprintf("%.1f ~ %.1f℃", DeviceSensor_data[0].T_tl, DeviceSensor_data[0].T_tu)
- }
- pdf.SetXY(470, y)
- pdf.Text(fmt.Sprintf("温度范围:%s", temp_range))
- y += 15
- pdf.SetXY(10, y)
- T_forwarding_unit_temp := []rune(T_forwarding_unit)
- if len(T_forwarding_unit_temp) > 34 {
- err = errors.New("发货单位长度超过限制(34个字符)")
- return
- }
- if len(T_forwarding_unit_temp) > 17 {
- pdf.Text(fmt.Sprintf("发货单位:%s", string(T_forwarding_unit_temp[0:17])))
- pdf.SetXY(60, y+15)
- pdf.Text(fmt.Sprintf("%s", string(T_forwarding_unit_temp[17:])))
- } else {
- pdf.Text(fmt.Sprintf("发货单位:%s", string(T_forwarding_unit_temp)))
- }
- pdf.SetXY(240, y)
- T_consignee_unit_temp := []rune(T_consignee_unit)
- if len(T_consignee_unit_temp) > 34 {
- err = errors.New("收货单位长度超过限制(34个字符)")
- return
- }
- if len(T_consignee_unit_temp) > 17 {
- pdf.Text(fmt.Sprintf("收货单位:%s", string(T_consignee_unit_temp[0:17])))
- pdf.SetXY(290, y+15)
- pdf.Text(fmt.Sprintf("%s", string(T_consignee_unit_temp[17:])))
- } else {
- pdf.Text(fmt.Sprintf("收货单位:%s", string(T_consignee_unit_temp)))
- }
- pdf.SetXY(470, y)
- T_remark_temp := []rune(T_remark)
- if len(T_remark_temp) > 34 {
- err = errors.New("备注长度超过限制(16个字符)")
- return
- }
- if len(T_remark) > 8 {
- pdf.Text(fmt.Sprintf("备注:%s", string(T_remark_temp[0:8])))
- pdf.SetXY(500, y+15)
- pdf.Text(fmt.Sprintf("%s", string(T_remark_temp[8:])))
- } else {
- pdf.Text(fmt.Sprintf("备注: %s", string(T_remark_temp)))
- }
- y += 35
- pdf.SetFont("wts", "", 16)
- pdf.SetXY(10, y)
- pdf.Text("记录曲线信息")
- // 线
- y += 10
- pdf.SetLineWidth(0.5)
- pdf.SetStrokeColor(169, 169, 169)
- pdf.Line(10, y, 585, y)
- y += 1
- // ----------------绘制图片
- //pdf.ImageFrom(img, 10, y, &gopdf.Rect{W: 575, H: 630})
- imgH, err := gopdf.ImageHolderByBytes(imageBytes)
- if T_temp_show == 1 && T_Humidity_show == 1 {
- //pdf.ImageByHolder(imgH, 10, y, &gopdf.Rect{W: 575, H: 630})
- pdf.ImageByHolder(imgH, 32, y, &gopdf.Rect{W: 522, H: 572})
- y += 562
- } else {
- pdf.ImageByHolder(imgH, 10, y, &gopdf.Rect{W: 575, H: 315})
- y += 315
- }
- y += 35
- if y > 841.89 {
- // 图片结束直接分页
- pdf.AddPage()
- y = 40
- }
- pdf.SetFont("wts", "", 16)
- pdf.SetXY(10, y)
- pdf.Text("记录数据信息")
- // 线
- y += 10
- pdf.SetLineWidth(0.5)
- pdf.SetStrokeColor(169, 169, 169)
- pdf.Line(10, y, 585, y)
- y += 10
- pdf.SetFont("wts", "", 10)
- if len(snidNum) == 1 {
- var x float64 = 19
- var w float64 = 112
- lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x = x + w
- w = 74
- lib.RectFillColor(pdf, "T1", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x = x + w
- w = 110
- lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x = x + w
- w = 74
- lib.RectFillColor(pdf, "T1", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x = x + w
- w = 110
- lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x = x + w
- w = 74
- lib.RectFillColor(pdf, "T1", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- for i, v := range Pdf_data {
- if i%3 == 0 {
- y += 20
- x, w = 19, 112
- var textH float64 = 25 // if text height is 25px.
- pdf.SetNewY(y, textH)
- y = pdf.GetY()
- lib.RectFillColor(pdf, v.T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x = x + w
- w = 74
- lib.RectFillColor(pdf, lib.Float32_to_string(v.T_id1), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- }
- if i%3 == 1 {
- x = x + w
- w = 110
- lib.RectFillColor(pdf, v.T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x = x + w
- w = 74
- lib.RectFillColor(pdf, lib.Float32_to_string(v.T_id1), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- }
- if i%3 == 2 {
- x = x + w
- w = 110
- lib.RectFillColor(pdf, v.T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x = x + w
- w = 74
- lib.RectFillColor(pdf, lib.Float32_to_string(v.T_id1), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- }
- }
- }
- if len(snidNum) == 2 {
- var x float64 = 19
- var w float64 = 112
- lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x = x + w
- w = 37
- lib.RectFillColor(pdf, "T1", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x = x + w
- lib.RectFillColor(pdf, "T2", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x = x + w
- w = 112
- lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x = x + w
- w = 37
- lib.RectFillColor(pdf, "T1", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x = x + w
- lib.RectFillColor(pdf, "T2", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x = x + w
- w = 112
- lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x = x + w
- w = 37
- lib.RectFillColor(pdf, "T1", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x = x + w
- lib.RectFillColor(pdf, "T2", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- for i, v := range Pdf_data {
- if i%3 == 0 {
- y += 20
- var textH float64 = 25 // if text height is 25px.
- pdf.SetNewY(y, textH)
- y = pdf.GetY()
- x, w = 19, 112
- lib.RectFillColor(pdf, v.T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x = x + w
- w = 37
- lib.RectFillColor(pdf, lib.Float32_to_string(v.T_id1), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x = x + w
- lib.RectFillColor(pdf, lib.Float32_to_string(v.T_id2), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- }
- if i%3 == 1 {
- x = x + w
- w = 112
- lib.RectFillColor(pdf, v.T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x = x + w
- w = 37
- lib.RectFillColor(pdf, lib.Float32_to_string(v.T_id1), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x = x + w
- lib.RectFillColor(pdf, lib.Float32_to_string(v.T_id2), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- }
- if i%3 == 2 {
- x = x + w
- w = 112
- lib.RectFillColor(pdf, v.T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x = x + w
- w = 37
- lib.RectFillColor(pdf, lib.Float32_to_string(v.T_id1), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x = x + w
- lib.RectFillColor(pdf, lib.Float32_to_string(v.T_id2), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- }
- }
- }
- timeStr := "ofile/" + time.Now().Format("20060102150405") + ".pdf"
- err = pdf.WritePdf(timeStr)
- if err != nil {
- return
- }
- // 上传 OSS
- url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/"+timeStr, timeStr)
- if !is {
- err = errors.New("oss!")
- return
- }
- //删除目录
- err = os.Remove(timeStr)
- if err != nil {
- logs.Error(err)
- }
- return url, nil
- }
- // 图表展示 下载PDF 库房
- func Device_Sensor_Data_ChartShow_PDF0(companyName, T_snid, Time_start, Time_end, T_forwarding_unit, T_consignee_unit, T_remark string, imageBytes []byte,
- T_temp_show, T_Humidity_show int) (url string, err error) {
- sn, ids, isSameSn := lib.IsSNAllSame(T_snid)
- if !isSameSn {
- err = errors.New("请选择相同设备编号的探头导出!")
- return
- }
- device, err := Device.Read_Device_ByT_sn(sn)
- if err != nil {
- err = errors.New("查询主机信息失败!")
- return
- }
- var DeviceSensor_data []Device.DeviceData_R
- Device.Read_DeviceSensorParameter_All_Map(sn, 0)
- // -------------------获取最高温湿度、温蒂温湿度、平均温湿度
- //DeviceSensor_data, _ = Device.Read_DeviceData_By_T_snid_List(T_snid, Time_start, Time_end, 0, 9999)
- DeviceSensor_data, _ = Device.Read_DeviceData_ByIds(sn, ids, Time_start, Time_end, 0, 9999)
- // 根据传感器T_sort排序
- //dsList := Device.Read_DeviceSensorList_By_T_snid(T_snid)
- //// 创建名称到权重的映射
- //orderMap := make(map[int]int)
- //for i, v := range dsList {
- // orderMap[v.T_id] = i
- //}
- //sort.Slice(DeviceSensor_data, func(i, j int) bool {
- // if DeviceSensor_data[i].T_time == DeviceSensor_data[j].T_time {
- // // 如果时间相同,则按预设顺序排序
- // return orderMap[DeviceSensor_data[i].T_id] < orderMap[DeviceSensor_data[j].T_id]
- // }
- // return DeviceSensor_data[i].T_time > DeviceSensor_data[j].T_time
- //})
- // 最高温度、最低温度、最高湿度、最低湿度
- var maxTemp, minTemp, maxHumidity, minHumidity float32
- // 最高温度时间、最低温度时间、最高湿度时间、最低湿度时间
- var maxTempTime, minTempTime, maxHumidityTime, minHumidityTime string
- // 总温度 总湿度
- var totalTemp, totalHumidity float32
- // 平均温度 平均湿度
- var avgTemp, avgHumidity float32
- // 温度阈值,湿度阈值
- var tempThreshold, humidityThreshold string
- // 记录开始时间,记录结束时间
- var s_time, e_time string
- var lastTime string
- var isFirst, isSecond = true, false
- var first_column, second_column []Device.DeviceData_R
- if len(DeviceSensor_data) > 0 {
- tempThreshold = fmt.Sprintf("%.1f-%.1f", DeviceSensor_data[0].T_tl, DeviceSensor_data[0].T_tu)
- humidityThreshold = fmt.Sprintf("%.1f-%.1f", DeviceSensor_data[0].T_rhl, DeviceSensor_data[0].T_rhu)
- e_time = DeviceSensor_data[0].T_time
- s_time = DeviceSensor_data[len(DeviceSensor_data)-1].T_time
- // 最高温度及时刻
- maxTemp = DeviceSensor_data[0].T_t
- maxTempTime = DeviceSensor_data[0].T_time
- // 最低温度及时刻
- minTemp = DeviceSensor_data[0].T_t
- minTempTime = DeviceSensor_data[0].T_time
- // 最高湿度及时刻
- maxHumidity = DeviceSensor_data[0].T_rh
- maxHumidityTime = DeviceSensor_data[0].T_time
- // 获取最低湿度及时刻
- minHumidity = DeviceSensor_data[0].T_rh
- minHumidityTime = DeviceSensor_data[0].T_time
- for i := 0; i < len(DeviceSensor_data); i++ {
- data := DeviceSensor_data[i]
- if data.T_t > maxTemp {
- maxTemp = data.T_t
- maxTempTime = data.T_time
- }
- if data.T_t < minTemp {
- minTemp = data.T_t
- minTempTime = data.T_time
- }
- totalTemp += data.T_t
- if data.T_rh > maxHumidity {
- maxHumidity = data.T_rh
- maxHumidityTime = data.T_time
- }
- if data.T_rh < minHumidity {
- minHumidity = data.T_rh
- minHumidityTime = data.T_time
- }
- totalHumidity += data.T_rh
- if len(lastTime) > 0 {
- if lastTime != data.T_time && isFirst == true {
- isFirst = false
- isSecond = true
- } else if lastTime != data.T_time && isSecond == true {
- isFirst = true
- isSecond = false
- }
- }
- if isFirst {
- first_column = append(first_column, data)
- lastTime = data.T_time
- }
- if isSecond {
- second_column = append(second_column, data)
- lastTime = data.T_time
- }
- }
- // 平均温度
- avgTemp = totalTemp / float32(len(DeviceSensor_data))
- // 平均湿度
- avgHumidity = totalHumidity / float32(len(DeviceSensor_data))
- }
- // -------------------获取最高温湿度、温蒂温湿度、平均温湿度结束
- pdf := &gopdf.GoPdf{}
- pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4
- err = pdf.AddTTFFont("wts", "static/fonts/MiSans-Medium.ttf")
- if err != nil {
- return
- }
- err = pdf.SetFont("wts", "", 20)
- if err != nil {
- return
- }
- pdf.SetGrayFill(0.5)
- pdf.SetMargins(0, 20, 0, 20)
- pdf.AddPage()
- title := companyName + "温湿度记录"
- var y float64 = 40
- textw, _ := pdf.MeasureTextWidth(title)
- pdf.SetX((595 / 2) - (textw / 2))
- pdf.SetY(y)
- pdf.Text(title)
- y += 30
- pdf.SetFont("wts", "", 16)
- pdf.SetXY(10, y)
- pdf.Text("实施设备信息")
- // 线
- y += 10
- pdf.SetLineWidth(0.5)
- pdf.SetStrokeColor(169, 169, 169)
- pdf.Line(10, y, 585, y)
- pdf.SetFont("wts", "", 10)
- y += 20
- pdf.SetXY(10, y)
- pdf.Text(fmt.Sprintf("主机名称:%s", device.T_devName))
- pdf.SetXY(300, y)
- pdf.Text(fmt.Sprintf("主机编号:%s", device.T_sn))
- y += 35
- pdf.SetFont("wts", "", 16)
- pdf.SetXY(10, y)
- pdf.Text("记录概要信息")
- // 线
- y += 10
- pdf.SetLineWidth(0.5)
- pdf.SetStrokeColor(169, 169, 169)
- pdf.Line(10, y, 585, y)
- y += 20
- pdf.SetFont("wts", "", 10)
- pdf.SetXY(10, y)
- pdf.Text(fmt.Sprintf("记录开始时间:%s", s_time))
- pdf.SetXY(240, y)
- pdf.Text(fmt.Sprintf("记录结束时间:%s", e_time))
- sTime, _ := lib.TimeStrToTime(s_time)
- eTime, _ := lib.TimeStrToTime(e_time)
- pdf.SetXY(470, y)
- minutes := int(eTime.Sub(sTime).Minutes())
- hours := minutes / 60
- remainingMinutes := minutes % 60
- pdf.Text(fmt.Sprintf("记录总时间:%dh%dmin", hours, remainingMinutes))
- // -------------最高温/湿度 最低温/湿度 平均温/湿度
- if T_temp_show == 1 {
- y += 15
- pdf.SetXY(10, y)
- pdf.Text(fmt.Sprintf("最高温度:%.1f℃,%s", lib.RoundToDecimal(float64(maxTemp), 1), maxTempTime))
- pdf.SetXY(240, y)
- pdf.Text(fmt.Sprintf("最低温度:%.1f℃,%s", lib.RoundToDecimal(float64(minTemp), 1), minTempTime))
- pdf.SetXY(470, y)
- pdf.Text(fmt.Sprintf("平均温度:%.1f℃", lib.RoundToDecimal(float64(avgTemp), 1)))
- }
- if T_Humidity_show == 1 {
- y += 15
- pdf.SetXY(10, y)
- pdf.Text(fmt.Sprintf("最高湿度:%.1f%%RH,%s", lib.RoundToDecimal(float64(maxHumidity), 1), maxHumidityTime))
- pdf.SetXY(240, y)
- pdf.Text(fmt.Sprintf("最低湿度:%.1f%%RH,%s", lib.RoundToDecimal(float64(minHumidity), 1), minHumidityTime))
- pdf.SetXY(470, y)
- pdf.Text(fmt.Sprintf("平均湿度:%.1f%%RH", lib.RoundToDecimal(float64(avgHumidity), 1)))
- }
- // -------------温/湿度阈值
- if T_temp_show == 1 {
- y += 15
- pdf.SetXY(10, y)
- pdf.Text(fmt.Sprintf("温度阈值:%s℃", tempThreshold))
- }
- if T_Humidity_show == 1 {
- if T_temp_show != 1 {
- y += 15
- }
- pdf.SetXY(240, y)
- pdf.Text(fmt.Sprintf("湿度阈值:%s%%", humidityThreshold))
- }
- //-------------发货单位,收货单位,备注
- y += 15
- pdf.SetXY(10, y)
- T_forwarding_unit_temp := []rune(T_forwarding_unit)
- if len(T_forwarding_unit_temp) > 34 {
- err = errors.New("发货单位长度超过限制(34个字符)")
- return
- }
- if len(T_forwarding_unit_temp) > 17 {
- pdf.Text(fmt.Sprintf("发货单位:%s", string(T_forwarding_unit_temp[0:17])))
- pdf.SetXY(60, y+15)
- pdf.Text(fmt.Sprintf("%s", string(T_forwarding_unit_temp[17:])))
- } else {
- pdf.Text(fmt.Sprintf("发货单位:%s", string(T_forwarding_unit_temp)))
- }
- pdf.SetXY(240, y)
- T_consignee_unit_temp := []rune(T_consignee_unit)
- if len(T_consignee_unit_temp) > 34 {
- err = errors.New("收货单位长度超过限制(34个字符)")
- return
- }
- if len(T_consignee_unit_temp) > 17 {
- pdf.Text(fmt.Sprintf("收货单位:%s", string(T_consignee_unit_temp[0:17])))
- pdf.SetXY(290, y+15)
- pdf.Text(fmt.Sprintf("%s", string(T_consignee_unit_temp[17:])))
- } else {
- pdf.Text(fmt.Sprintf("收货单位:%s", string(T_consignee_unit_temp)))
- }
- pdf.SetXY(470, y)
- T_remark_temp := []rune(T_remark)
- if len(T_remark_temp) > 34 {
- err = errors.New("备注长度超过限制(16个字符)")
- return
- }
- if len(T_remark) > 8 {
- pdf.Text(fmt.Sprintf("备注:%s", string(T_remark_temp[0:8])))
- pdf.SetXY(500, y+15)
- pdf.Text(fmt.Sprintf("%s", string(T_remark_temp[8:])))
- } else {
- pdf.Text(fmt.Sprintf("备注: %s", string(T_remark_temp)))
- }
- y += 35
- pdf.SetFont("wts", "", 16)
- pdf.SetXY(10, y)
- pdf.Text("记录曲线信息")
- // 线
- y += 10
- pdf.SetLineWidth(0.5)
- pdf.SetStrokeColor(169, 169, 169)
- pdf.Line(10, y, 585, y)
- y += 1
- // ----------------绘制图片
- //pdf.ImageFrom(img, 10, y, &gopdf.Rect{W: 575, H: 630})
- imgH, err := gopdf.ImageHolderByBytes(imageBytes)
- if T_temp_show == 1 && T_Humidity_show == 1 {
- //pdf.ImageByHolder(imgH, 10, y, &gopdf.Rect{W: 575, H: 630})
- pdf.ImageByHolder(imgH, 32, y, &gopdf.Rect{W: 522, H: 572})
- y += 562
- } else {
- pdf.ImageByHolder(imgH, 10, y, &gopdf.Rect{W: 575, H: 315})
- y += 315
- }
- y += 35
- if y > 841.89 {
- // 图片结束直接分页
- pdf.AddPage()
- y = 40
- }
- pdf.SetFont("wts", "", 16)
- pdf.SetXY(10, y)
- pdf.Text("记录数据信息")
- // 线
- y += 10
- pdf.SetLineWidth(0.5)
- pdf.SetStrokeColor(169, 169, 169)
- pdf.Line(10, y, 585, y)
- y += 10
- pdf.SetFont("wts", "", 10)
- var x float64 = 10
- var w float64 = 112
- lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x = x + w
- w = 101
- lib.RectFillColor(pdf, "名称", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- if T_temp_show == 1 && T_Humidity_show == 1 {
- x = x + w
- w = 37
- lib.RectFillColor(pdf, "温度℃", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x = x + w
- w = 37
- lib.RectFillColor(pdf, "湿度%", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- }
- if T_temp_show == 1 && T_Humidity_show == 0 {
- x = x + w
- w = 74
- lib.RectFillColor(pdf, "温度℃", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- }
- if T_temp_show == 0 && T_Humidity_show == 1 {
- x = x + w
- w = 74
- lib.RectFillColor(pdf, "湿度%", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- }
- x = x + w
- w = 112
- lib.RectFillColor(pdf, "时间", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x = x + w
- w = 101
- lib.RectFillColor(pdf, "名称", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- if T_temp_show == 1 && T_Humidity_show == 1 {
- x = x + w
- w = 37
- lib.RectFillColor(pdf, "温度℃", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x = x + w
- w = 37
- lib.RectFillColor(pdf, "湿度%", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- }
- if T_temp_show == 1 && T_Humidity_show == 0 {
- x = x + w
- w = 74
- lib.RectFillColor(pdf, "温度℃", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- }
- if T_temp_show == 0 && T_Humidity_show == 1 {
- x = x + w
- w = 74
- lib.RectFillColor(pdf, "湿度%", 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- }
- y += 20
- var textH float64 = 25 // if text height is 25px.
- for i, v := range first_column {
- pdf.SetNewY(y, textH)
- y = pdf.GetY()
- x, w = 10, 112
- lib.RectFillColor(pdf, v.T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x = x + w
- w = 101
- lib.RectFillColor(pdf, v.T_name, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- // 显示温湿度
- if T_temp_show == 1 && T_Humidity_show == 1 {
- x = x + w
- w = 37
- lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", v.T_t), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x = x + w
- w = 37
- lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", v.T_rh), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- }
- // 只显示温度
- if T_temp_show == 1 && T_Humidity_show == 0 {
- x = x + w
- w = 74
- lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", v.T_t), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- }
- // 只显示湿度
- if T_temp_show == 0 && T_Humidity_show == 1 {
- x = x + w
- w = 74
- lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", v.T_rh), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- }
- if i < len(second_column) {
- x = x + w
- w = 112
- lib.RectFillColor(pdf, second_column[i].T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x = x + w
- w = 101
- lib.RectFillColor(pdf, second_column[i].T_name, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- if T_temp_show == 1 && T_Humidity_show == 1 {
- x = x + w
- w = 37
- lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", second_column[i].T_t), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x = x + w
- w = 37
- lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", second_column[i].T_rh), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- }
- if T_temp_show == 1 && T_Humidity_show == 0 {
- x = x + w
- w = 74
- lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", second_column[i].T_t), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- }
- if T_temp_show == 0 && T_Humidity_show == 1 {
- x = x + w
- w = 74
- lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", second_column[i].T_rh), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- }
- }
- y += 20
- }
- if len(second_column) > len(first_column) {
- for i := len(first_column); i < len(second_column); i++ {
- pdf.SetNewY(y, textH)
- y = pdf.GetY()
- x, w = 297, 112
- lib.RectFillColor(pdf, second_column[i].T_time, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x = x + w
- w = 101
- lib.RectFillColor(pdf, second_column[i].T_name, 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- if T_temp_show == 1 && T_Humidity_show == 1 {
- x = x + w
- w = 37
- lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", second_column[i].T_t), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- x = x + w
- w = 37
- lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", second_column[i].T_rh), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- }
- if T_temp_show == 1 && T_Humidity_show == 0 {
- x = x + w
- w = 74
- lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", second_column[i].T_t), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- }
- if T_temp_show == 0 && T_Humidity_show == 1 {
- x = x + w
- w = 74
- lib.RectFillColor(pdf, fmt.Sprintf(" %.1f ", second_column[i].T_rh), 12, x, y, w, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- }
- y += 20
- }
- }
- timeStr := "ofile/" + time.Now().Format("20060102150405") + ".pdf"
- err = pdf.WritePdf(timeStr)
- if err != nil {
- return
- }
- // 上传 OSS
- url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/"+timeStr, timeStr)
- if !is {
- err = errors.New("oss!")
- return
- }
- //删除目录
- err = os.Remove(timeStr)
- if err != nil {
- logs.Error(err)
- }
- return url, nil
- }
- // 数据备份
- func (c *DataController) Device_Sensor_Data_BackUp() {
- page, _ := c.GetInt("page")
- if page < 1 {
- page = 1
- }
- page_z, _ := c.GetInt("page_z")
- if page_z < 1 {
- page_z = conf.Page_size
- }
- T_snid := c.GetString("T_snid")
- Time_start := c.GetString("Time_start")
- Time_end := c.GetString("Time_end")
- if len(T_snid) < 10 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_snid Err!"}
- c.ServeJSON()
- return
- }
- if _, is := lib.DateStrToDate(Time_start); !is {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
- c.ServeJSON()
- return
- }
- if _, is := lib.DateStrToDate(Time_end); !is {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
- c.ServeJSON()
- return
- }
- Time_start += " 00:00:00"
- Time_end += " 23:59:59"
- var r_jsons lib.R_JSONS
- r_jsons.Data, r_jsons.Num = Device.Read_DeviceData_BackUp_List(T_snid, Time_start, Time_end, page, page_z)
- r_jsons.Page = page
- r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- // 数据备份pdf
- func (c *DataController) Device_Sensor_Data_BackUp_PDF() {
- T_sn := c.GetString("T_sn")
- T_id, _ := c.GetInt("T_id")
- T_date := c.GetString("T_date")
- T_snid := fmt.Sprintf("%s,%d", T_sn, T_id)
- date, is := lib.DateStrToDate(T_date)
- if !is {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
- c.ServeJSON()
- return
- }
- Time_start := T_date + " 00:00:00"
- Time_end := T_date + " 23:59:59"
- var DeviceSensor_data []Device.DeviceData_R
- DeviceSensor_data, _ = Device.Read_DeviceData_By_T_snid_List(T_snid, Time_start, Time_end, 0, 9999)
- var err error
- pdf := &gopdf.GoPdf{}
- pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) //595.28, 841.89 = A4
- //err = GetFont(pdf, "LiberationSerif-Regular.ttf")
- //if err != nil {
- // log.Fatalln(err)
- //}
- //err = pdf.SetFont("Ubuntu-L", "", 14)
- //if err != nil {
- // log.Fatalln(err)
- //}
- err = pdf.AddTTFFont("simsun", "static/fonts/三极行楷简体-粗.ttf")
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
- c.ServeJSON()
- return
- }
- err = pdf.SetFont("simsun", "", 24)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
- c.ServeJSON()
- return
- }
- pdf.SetGrayFill(0.5)
- pdf.SetMargins(0, 20, 0, 20)
- pdf.AddPage()
- //use path
- //pdf.Image("logo.png", 100, 50, &gopdf.Rect{W: 50, H: 50})
- company, _ := Account.Read_Company_ById(c.T_pid)
- textw, _ := pdf.MeasureTextWidth(company.T_name)
- pdf.SetX((595 / 2) - (textw / 2))
- pdf.SetY(40)
- pdf.Text(company.T_name)
- // 线
- pdf.SetLineWidth(2)
- pdf.SetLineType("dashed")
- pdf.Line(10, 60, 585, 60)
- err = pdf.AddTTFFont("wts", "static/fonts/MiSans-Medium.ttf")
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "ok!", Data: err}
- c.ServeJSON()
- return
- }
- err = pdf.SetFont("wts", "", 12)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
- c.ServeJSON()
- return
- }
- var s_time, e_time string
- if len(DeviceSensor_data) > 0 {
- s_time = fmt.Sprintf("%s", DeviceSensor_data[0].T_time)
- e_time = fmt.Sprintf("%s", DeviceSensor_data[len(DeviceSensor_data)-1].T_time)
- }
- //fmt.Sprintf(" %.1f ", v.T_t)
- lib.RectFillColor(pdf, "历史数据["+e_time+" / "+s_time+"]", 14, 22, 80, 550, 40, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- lib.RectFillColor(pdf, "序号", 12, 22, 120, 30, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- lib.RectFillColor(pdf, "传感器名称", 12, 52, 120, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- lib.RectFillColor(pdf, "温度℃", 12, 152, 120, 60, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- lib.RectFillColor(pdf, "湿度%", 12, 212, 120, 60, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- lib.RectFillColor(pdf, "温度范围", 12, 272, 120, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- lib.RectFillColor(pdf, "湿度范围", 12, 362, 120, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- lib.RectFillColor(pdf, "记录时间", 12, 452, 120, 120, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- var y float64 = 140
- err = pdf.SetFont("wts", "", 10)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
- c.ServeJSON()
- return
- }
- for i, v := range DeviceSensor_data {
- text := fmt.Sprintf(" %d ", i+1)
- var textH float64 = 25 // if text height is 25px.
- pdf.SetNewY(y, textH)
- y = pdf.GetY()
- //pdf.SetX(x) // must after pdf.SetNewY() called.
- //err = pdf.Text(text)
- //if err != nil {
- // log.Fatalln(err)
- //}
- T_t := fmt.Sprintf(" %.1f ", v.T_t)
- T_rh := fmt.Sprintf(" %.1f ", v.T_rh)
- T_Tlu := fmt.Sprintf(" %.1f ~ %.1f ", v.T_tl, v.T_tu)
- T_Rlu := fmt.Sprintf(" %.1f ~ %.1f ", v.T_rhl, v.T_rhu)
- T_time := fmt.Sprintf("%s", v.T_time)
- //if user_r.Admin_rh == 0 && strings.Contains(v.T_sn, "YD") {
- // T_rh = "-"
- // T_Rlu = "-"
- //}
- lib.RectFillColor(pdf, text, 10, 22, y, 30, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- lib.RectFillColor(pdf, v.T_name, 10, 52, y, 100, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- lib.RectFillColor(pdf, T_t, 10, 152, y, 60, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- lib.RectFillColor(pdf, T_rh, 10, 212, y, 60, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- lib.RectFillColor(pdf, T_Tlu, 10, 272, y, 90, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- lib.RectFillColor(pdf, T_Rlu, 10, 362, y, 90, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- lib.RectFillColor(pdf, T_time, 10, 452, y, 120, 20, 255, 255, 255, lib.AlignCenter, lib.ValignMiddle)
- y += 20
- }
- deviceSensor, _ := Device.Read_DeviceSensor_ByT_sn(T_sn, T_id)
- rand_x := int64(lib.Random(0, 10000))
- filenameStr := fmt.Sprintf("backup/sn/%s_%s(%s).pdf", lib.GetRandstring(8, "0123456789", rand_x), deviceSensor.T_name, date.Format("2006-01-02"))
- err = pdf.WritePdf(filenameStr)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
- c.ServeJSON()
- return
- }
- // 上传 OSS
- url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/"+filenameStr, filenameStr)
- if !is {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
- c.ServeJSON()
- return
- }
- //删除目录
- err = os.Remove(filenameStr)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
- c.ServeJSON()
- return
- }
- // 对接电信实时数据
- func (c *DataController) Docking_Real_Data() {
- key := c.GetString("T_key")
- // 查询公司
- Company_r, err := Account.Read_Company_ByKey(key)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_key Err!"}
- c.ServeJSON()
- return
- }
- // 查询公司下面所有子公司id
- T_pids := Account.ReadCompanyIds_T_path(Company_r.T_path)
- type R_JSONS struct {
- //必须的大写开头
- Data []Device.DeviceData_Docking
- }
- var r_jsons R_JSONS
- r_jsons.Data = Device.Read_Docking_Real_Data(T_pids)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- // 对接电信历史数据
- func (c *DataController) Docking_Note_Data() {
- key := c.GetString("T_key")
- // 查询公司,验证T_key
- _, err := Account.Read_Company_ByKey(key)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_key Err!"}
- c.ServeJSON()
- return
- }
- page, _ := c.GetInt("page")
- if page < 1 {
- page = 1
- }
- page_z, _ := c.GetInt("page_z")
- if page_z < 1 {
- page_z = conf.Page_size
- }
- if page_z > 50 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "page_z 超出限制"}
- c.ServeJSON()
- return
- }
- T_sn := c.GetString("T_sn")
- T_id, T_id_Err := c.GetInt("T_id")
- Time_start := c.GetString("Time_start")
- Time_end := c.GetString("Time_end")
- if len(T_sn) == 0 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn 格式错误"}
- c.ServeJSON()
- return
- }
- if T_id_Err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_id 格式错误"}
- c.ServeJSON()
- return
- }
- st, is := lib.TimeStrToTime(Time_start)
- if !is {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "Time_start 格式不正确"}
- c.ServeJSON()
- return
- }
- if st.Add(40 * 24 * time.Hour).Before(time.Now()) {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询时间范围超出限制(40天)"}
- c.ServeJSON()
- return
- }
- et, is := lib.TimeStrToTime(Time_end)
- if !is {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "Time_start 格式不正确"}
- c.ServeJSON()
- return
- }
- if et.Add(40 * 24 * time.Hour).Before(time.Now()) {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询时间范围超出限制(40天)"}
- c.ServeJSON()
- return
- }
- type R_JSONS struct {
- //必须的大写开头
- DeviceSensor_data []Device.DeviceData_R
- Num int
- Page int
- Page_size int
- Pages []lib.Page_T
- }
- var r_jsons R_JSONS
- var cnt int
- Device.Read_DeviceSensorParameter_All_Map(T_sn, T_id)
- r_jsons.DeviceSensor_data, cnt = Device.Read_DeviceData_ById_List(T_sn, T_id, Time_start, Time_end, page, page_z)
- page_size := math.Ceil(float64(cnt) / float64(page_z))
- r_jsons.Page = page
- r_jsons.Page_size = int(page_size)
- r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
- r_jsons.Num = cnt
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
|