123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253 |
- 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")
- 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_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
- T_ist := 1
- 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)
- 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)
- 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)
- 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)
- 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)
- 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)
- 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)
- }
- 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_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
- }
|