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 { fmt.Println(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 { fmt.Println(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 { fmt.Println(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 { // fmt.Println(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 { fmt.Println(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 { fmt.Println(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 { fmt.Println(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 }