Ver Fonte

2023-11-29 小程序查询公司下面所有子公司数据

zoie há 1 ano atrás
pai
commit
97f4589cd8

+ 1 - 1
conf/app.conf

@@ -63,7 +63,7 @@ Qiniu_Url = "https://coldoss.coldbaozhida.com/"
 # Panel
 Panel_url = "http://127.0.0.1:6204/Cold_Panel"
 
-FilterExcludeURL = /Login_verification,/Data/List,/WxPay/Notify,/Data/Company_key_Device_Sensor_List
+FilterExcludeURL = /Login_verification,/Data/List,/WxPay/Notify,/Data/Company_key_Device_Sensor_List,/docking/Real_Data,/docking/Note_Data
 # 小程序 冷链验证 /Data/List
 # 小程序接口 /DeviceWarning/DeviceSensor_List,/Device/Applet_Stat_View2,/DeviceSensor/Applet_List_View1,/DeviceSensor/Applet_List_View2,/DeviceSensorType/List,/Device/DeviceTask_Post
 FilterOnlyLoginCheckURL = /Menu/List,/User/Info,/User/Home,/User/Post,/UpFileToken,/User/WxQRCode,/Company/Get,/DataSource,/Company/Bill_Excel,/WarningSend/List,/WxPay/Get_QRCode,/WxPay/GetOrderState,/WarningHandle/List

+ 112 - 3
controllers/Data.go

@@ -849,7 +849,7 @@ func Device_Sensor_Data_ChartShow_PDF1(companyName, T_snid, Time_start, Time_end
 	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(sn1, id1, Time_start, Time_end, 0, 9999)
+		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)
 	}
@@ -862,8 +862,8 @@ func Device_Sensor_Data_ChartShow_PDF1(companyName, T_snid, Time_start, Time_end
 			err = errors.New("请选择相同设备编号的探头导出!")
 			return
 		}
-		r_maps_sn1, _ = Device.Read_DeviceData_ById_List(sn1, id1, Time_start, Time_end, 0, 9999)
-		r_maps_sn2, _ = Device.Read_DeviceData_ById_List(sn2, id2, Time_start, Time_end, 0, 9999)
+		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...)
 
@@ -1926,3 +1926,112 @@ func (c *DataController) Device_Sensor_Data_BackUp_PDF() {
 	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
+	}
+
+	type R_JSONS struct {
+		//必须的大写开头
+		Data []Device.DeviceData_R
+	}
+	var r_jsons R_JSONS
+
+	r_jsons.Data = Device.Read_Docking_Real_Data(Company_r.Id)
+
+	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
+}

+ 61 - 9
controllers/Device.go

@@ -1875,7 +1875,21 @@ func (c *DeviceController) DeviceSensorType_List() {
 		c.ServeJSON()
 		return
 	}
-	list, err := Device.Read_DeviceSensor_T_type(c.T_pid, bindSN)
+
+	var T_pids []int
+	if c.Admin_r.T_pid > 0 {
+		// 查询公司
+		Company_r, err := Account.Read_Company_ById(c.Admin_r.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)
+	}
+
+	list, err := Device.Read_DeviceSensor_T_type(c.Admin_r, c.T_pid, bindSN, T_pids)
 	if err != nil {
 		c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
 		c.ServeJSON()
@@ -1929,7 +1943,20 @@ func (c *DeviceController) Device_Applet_Stat() {
 		return
 	}
 
-	snList, err := Device.Read_DeviceSensor_T_sn_ByT_type(c.T_pid, bindSN, T_type)
+	var T_pids []int
+	if c.Admin_r.T_pid > 0 {
+		// 查询公司
+		Company_r, err := Account.Read_Company_ById(c.Admin_r.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)
+	}
+
+	snList, err := Device.Read_DeviceSensor_T_sn_ByT_type(c.Admin_r, c.T_pid, bindSN, T_pids, T_type)
 	if err != nil {
 		c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
 		c.ServeJSON()
@@ -1946,10 +1973,10 @@ func (c *DeviceController) Device_Applet_Stat() {
 	}
 
 	// 0 未启用  1 在线  2 离线
-	r_jsons.Data.Count = Device.Read_Device_Count_ByT_online(c.T_pid, snList, "")
-	r_jsons.Data.Online = Device.Read_Device_Count_ByT_online(c.T_pid, snList, "1")
-	r_jsons.Data.NoNetwork = Device.Read_Device_Count_ByT_online(c.T_pid, snList, "2")
-	r_jsons.Data.NotEnabled = Device.Read_Device_Count_ByT_online(c.T_pid, snList, "0")
+	r_jsons.Data.Count = Device.Read_Device_Count_ByT_online(snList, "")
+	r_jsons.Data.Online = Device.Read_Device_Count_ByT_online(snList, "1")
+	r_jsons.Data.NoNetwork = Device.Read_Device_Count_ByT_online(snList, "2")
+	r_jsons.Data.NotEnabled = Device.Read_Device_Count_ByT_online(snList, "0")
 
 	c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
 	c.ServeJSON()
@@ -1983,8 +2010,21 @@ func (c *DeviceController) DeviceSensor_Applet_List_View1() {
 		c.ServeJSON()
 		return
 	}
+	var T_pids []int
+	if c.Admin_r.T_pid > 0 {
+		// 查询公司
+		Company_r, err := Account.Read_Company_ById(c.Admin_r.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)
+	}
 
-	r_jsons.Data, r_jsons.Num = Device.Read_DeviceSensor_List_For_Applet(c.Admin_r, bindSN, c.T_pid, T_name, "3", 1, T_type, page, page_z)
+	Account.Read_Company_All_Maps()
+	r_jsons.Data, r_jsons.Num = Device.Read_DeviceSensor_List_For_Applet(c.Admin_r, bindSN, c.T_pid, T_pids, T_name, "3", 1, T_type, page, page_z)
 	r_jsons.Page = page
 	r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
 
@@ -2020,8 +2060,20 @@ func (c *DeviceController) DeviceSensor_Applet_List_View2() {
 		c.ServeJSON()
 		return
 	}
-
-	dsList, _ := Device.Read_DeviceSensor_List_For_Applet(c.Admin_r, bindSN, c.T_pid, T_name, T_online, 1, T_type, page, 9999)
+	var T_pids []int
+	if c.Admin_r.T_pid > 0 {
+		// 查询公司
+		Company_r, err := Account.Read_Company_ById(c.Admin_r.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)
+	}
+	Account.Read_Company_All_Maps()
+	dsList, _ := Device.Read_DeviceSensor_List_For_Applet(c.Admin_r, bindSN, c.T_pid, T_pids, T_name, T_online, 1, T_type, page, 9999)
 
 	var deviceMap = make(map[string][]Device.DeviceSensor_Applet)
 	var num int64

+ 14 - 9
controllers/User.go

@@ -745,6 +745,19 @@ func (c *UserController) User_Home() {
 	var info Info
 	info.T_name = admin_r.T_name
 	info.T_pid = admin_r.T_pid
+
+	var Company_r Account.Company
+	if c.T_pid > 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
+		}
+		info.CompanyKey = Company_r.T_key
+		info.CompanyName = Company_r.T_name
+	}
+
 	if admin_r.T_pid > 0 {
 		info.WarningSign = T_warning_sign
 		bindSN, err := Account.Read_UserDevice_List(c.Admin_r)
@@ -763,12 +776,6 @@ func (c *UserController) User_Home() {
 		}
 		info.Admin = Admin_
 
-		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
-		}
 		T_pids := []int{}
 		// 拥有公司管理-设备报警权限 统计公司及其所有子公司的今日报警数量、未处理报警数量、我的消息数量
 		if T_warning_sign == Warning.CompanyCallthepolice {
@@ -787,10 +794,8 @@ func (c *UserController) User_Home() {
 		info.Balance = company.T_money
 		info.NoticeRecord.Sms = Warning.Get_WarningSandNum_CurrentMonth(admin_r.T_pid, 1)
 		info.NoticeRecord.Phone = Warning.Get_WarningSandNum_CurrentMonth(admin_r.T_pid, 2)
-
-		info.CompanyKey = Company_r.T_key
-		info.CompanyName = Company_r.T_name
 	}
+
 	if admin_r.T_pid == 0 {
 		if len(admin_r.T_pids) > 0 && admin_r.T_pids != "*" {
 			info.T_pids = admin_r.T_pids

+ 15 - 1
controllers/Warning.go

@@ -580,7 +580,21 @@ func (c *DeviceController) Read_Warning_List_By_DS_T_type() {
 		return
 	}
 
-	r_jsons.Data, r_jsons.Num = Warning.Read_Warning_List_By_DS_T_type(c.T_pid, bindSN, T_type, T_name, page, page_z)
+	var T_pids []int
+	if c.Admin_r.T_pid > 0 {
+		// 查询公司
+		Company_r, err := Account.Read_Company_ById(c.Admin_r.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)
+	}
+	countRedisKey := fmt.Sprintf("Company_Warning_Count_By_DS_T_type_%d_%s_%d_%d", c.T_pid, T_name, T_type, c.Admin_r.T_pid)
+
+	r_jsons.Data, r_jsons.Num = Warning.Read_Warning_List_By_DS_T_type(c.Admin_r, c.T_pid, bindSN, T_pids, T_type, T_name, page, page_z, countRedisKey)
 	r_jsons.Page = page
 	r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
 

+ 1 - 3
models/Account/Menu.go

@@ -278,9 +278,7 @@ func Read_Menu_Bind_List(Power_Id int, Bind_Menu string, T_pid int) (r Menu_Perm
 
 func Get_Permission(menu []Menu) (permission []string) {
 	for _, v := range menu {
-		if v.T_type == ButtonType {
-			permission = append(permission, v.T_permission)
-		}
+		permission = append(permission, v.T_permission)
 	}
 	return
 }

+ 2 - 2
models/Device/Device.go

@@ -364,7 +364,7 @@ func Read_Device_List_ByT_model(T_model string) (r []Device) {
 
 // 小程序 T_type = 2 分类管理统计
 // 通过T_monitor判断主机监控状态
-func Read_Device_Count_ByT_online(T_pid int, snList []string, T_online string) (cnt int64) {
+func Read_Device_Count_ByT_online(snList []string, T_online string) (cnt int64) {
 
 	o := orm2.NewOrm()
 	// 也可以直接使用 Model 结构体作为表名
@@ -372,7 +372,7 @@ func Read_Device_Count_ByT_online(T_pid int, snList []string, T_online string) (
 	qs := o.QueryTable(new(Device))
 
 	cond := orm2.NewCondition()
-	cond1 := cond.And("T_pid", T_pid).And("T_State", 1)
+	cond1 := cond.And("T_State", 1)
 
 	if len(snList) > 0 {
 		cond1 = cond1.And("T_sn__in", snList)

+ 62 - 0
models/Device/DeviceData.go

@@ -306,6 +306,67 @@ func Read_DeviceData_ById_List(SN string, T_id int, Time_start_ string, Time_end
 	}
 	//fmt.Println("maps_z;",maps_z[0][0])
 	//t_tl,t_tu,t_rhl,t_rhu,
+	sql = "SELECT t_id,t_sp,t_t,t_rh,t_site,DATE_FORMAT(t_time,'%Y-%m-%d %H:%i:%s') AS t_time,t_time AS t_time1 FROM z_device_data_" + SN + " WHERE " + sql_time + " t_id = " + strconv.Itoa(T_id) + " ORDER BY t_time1 DESC,t_id DESC "
+	if page_z != 9999 {
+		sql = sql + " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(page_z)
+	}
+
+	fmt.Println(sql)
+	_, err = o.Raw(sql).QueryRows(&maps)
+	if err != nil {
+		logs.Error(lib.FuncName(), err)
+		return r, 0
+	}
+
+	//value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", cnt), 64)
+	key, _ := strconv.Atoi(maps_z[0][0].(string))
+	device, err := Read_Device_ByT_sn(SN)
+	if err != nil {
+		logs.Error(lib.FuncName(), err)
+	}
+	for _, v := range maps {
+		r = append(r, DeviceData_ToDeviceData_R(device, v))
+	}
+
+	return r, key
+}
+func Read_DeviceData_ById_List_OrderByTimeAes(SN string, T_id int, Time_start_ string, Time_end_ string, page int, page_z int) ([]DeviceData_R, int) {
+	o := orm.NewOrm()
+	var maps []DeviceData_
+	var maps_z []orm2.ParamsList
+	var r []DeviceData_R
+	var offset int
+	if page_z == 0 {
+		page_z = conf.Page_size
+	}
+
+	if page <= 1 {
+		offset = 0
+	} else {
+		offset = (page - 1) * page_z
+	}
+	sql_time := ""
+
+	if len(Time_start_) > 1 {
+		sql_time += " t_time >= '" + Time_start_ + "' AND "
+	}
+
+	if len(Time_end_) > 1 {
+		sql_time += " t_time <= '" + Time_end_ + "' AND "
+	}
+
+	sql := "SELECT COUNT(t_id) FROM z_device_data_" + SN + " WHERE " + sql_time + " t_id = " + strconv.Itoa(T_id)
+	fmt.Println(sql)
+	_, err := o.Raw(sql).ValuesList(&maps_z)
+	if err != nil {
+		logs.Error(lib.FuncName(), err)
+		return r, 0
+	}
+	if len(maps_z) == 0 {
+		return r, 0
+	}
+	//fmt.Println("maps_z;",maps_z[0][0])
+	//t_tl,t_tu,t_rhl,t_rhu,
 	sql = "SELECT t_id,t_sp,t_t,t_rh,t_site,DATE_FORMAT(t_time,'%Y-%m-%d %H:%i:%s') AS t_time,t_time AS t_time1 FROM z_device_data_" + SN + " WHERE " + sql_time + " t_id = " + strconv.Itoa(T_id) + " ORDER BY t_time1,t_id DESC "
 	if page_z != 9999 {
 		sql = sql + " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(page_z)
@@ -468,6 +529,7 @@ func Read_DeviceData_By_T_snid_List(T_snid string, Time_start_ string, Time_end_
 		return maps, maps_num
 	}
 
+	// 倒序
 	sort.Slice(maps, func(i, j int) bool {
 		if maps[i].T_time > maps[j].T_time {
 			return true

+ 86 - 13
models/Device/DeviceSensor.go

@@ -80,13 +80,15 @@ type DeviceSensor_R struct {
 	T_DeviceSensorData      DeviceData_R            // 传感器最新数据
 	T_DeviceSensorParameter DeviceSensorParameter_R //  设备参数
 	T_DeviceSensorType      DeviceSensorType_R      //  传感器类型
-
 }
 type DeviceSensor_Applet struct {
 	T_sn   string // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机
 	T_id   int    // 传感器编号
 	T_name string // 标题
 
+	T_pid      int    // 公司id
+	T_pid_name string // 公司名称
+
 	T_3dview   string // 3D 视图ID
 	T_sort     int    // 排序
 	T_Dattery  int    // 电量
@@ -334,6 +336,9 @@ func DeviceSensorToDeviceSensor_Applet(DeviceSensor_ DeviceSensor) (DeviceSensor
 	DeviceSensor_r.T_sn = DeviceSensor_.T_sn
 	DeviceSensor_r.T_id = DeviceSensor_.T_id
 	DeviceSensor_r.T_name = DeviceSensor_.T_name
+	DeviceSensor_r.T_pid = DeviceSensor_.T_pid
+	DeviceSensor_r.T_pid_name = Account.Read_Company_Get(DeviceSensor_.T_pid)
+
 	DeviceSensor_r.T_Site = DeviceSensor_.T_Site
 	DeviceSensor_r.T_Dattery = DeviceSensor_.T_Dattery
 	DeviceSensor_r.T_monitor = DeviceSensor_.T_monitor
@@ -998,9 +1003,9 @@ func Read_DeviceSensorManageList(admin_r *Account.Admin, bindSN []string, T_pid
 		sql_WHERE += " AND t_datashow =" + strconv.Itoa(T_datashow)
 	}
 
-	sql_ORDER := " ORDER BY t_sort ASC"
+	sql_ORDER := " ORDER BY t_sort ASC,ds.t_id"
 	if T_sort == 1 {
-		sql_ORDER = " ORDER BY t_sort DESC"
+		sql_ORDER = " ORDER BY t_sort DESC,ds.t_id"
 	}
 
 	// -------------
@@ -1472,16 +1477,26 @@ func Read_CompanyDeviceSensor_List_For_Data_ByKey(T_pids []int) (DeviceSensor_r
 	return DeviceSensor_r, cnt
 }
 
-// 获取列表
-func Read_DeviceSensor_T_type(T_pid int, bindSN []string) (lists orm2.ParamsList, err error) {
+// 小程序-获取传感器类型ID
+func Read_DeviceSensor_T_type(admin *Account.Admin, T_pid int, bindSN []string, T_pids []int) (lists orm2.ParamsList, err error) {
 	o := orm.NewOrm()
 	var pl_lists orm2.ParamsList
-	sql := "SELECT DISTINCT t_type FROM device_sensor WHERE t_pid=? AND t_datashow=1 AND t__state=1"
+	sql := "SELECT DISTINCT t_type FROM device_sensor WHERE t_datashow=1 AND t__state=1"
+	if admin.T_pid > 0 {
+		// 客户访问自己及其子公司数据
+		if len(T_pids) > 0 {
+			sql += fmt.Sprintf(" AND t_pid in (%s) ", lib.IntListToDotStr(T_pids))
+		}
+	} else {
+		// 管理员访问当前公司数据
+		sql += " AND t_pid = " + strconv.Itoa(T_pid)
+	}
+
 	if len(bindSN) > 0 {
 		sql += fmt.Sprintf(" AND t_sn in (%s)", lib.StringListToQuotesDotStr(bindSN))
 	}
 	sql += " LIMIT 0,1000"
-	_, err = o.Raw(sql, T_pid).ValuesFlat(&pl_lists)
+	_, err = o.Raw(sql).ValuesFlat(&pl_lists)
 	if err != nil {
 		logs.Error(lib.FuncName(), err)
 		return
@@ -1491,7 +1506,8 @@ func Read_DeviceSensor_T_type(T_pid int, bindSN []string) (lists orm2.ParamsList
 }
 
 // 数据展示菜单下 传感器参数列表
-func Read_DeviceSensor_List_For_Applet(admin *Account.Admin, bindSN []string, T_pid int, T_name, T_online string, T_RealTime, T_type, page, page_z int) (DeviceSensor_r []DeviceSensor_Applet, cnt int64) {
+func Read_DeviceSensor_List_For_Applet(admin *Account.Admin, bindSN []string, T_pid int, T_pids []int, T_name, T_online string,
+	T_RealTime, T_type, page, page_z int) (DeviceSensor_r []DeviceSensor_Applet, cnt int64) {
 
 	o := orm.NewOrm()
 	// 也可以直接使用 Model 结构体作为表名
@@ -1507,7 +1523,12 @@ func Read_DeviceSensor_List_For_Applet(admin *Account.Admin, bindSN []string, T_
 
 	var r []DeviceSensor
 	cond := orm.NewCondition()
-	cond1 := cond.And("T_pid", T_pid)
+	cond1 := orm.NewCondition()
+	if admin.T_pid > 0 {
+		cond1 = cond.And("T_pid__in", T_pids)
+	} else {
+		cond1 = cond.And("T_pid", T_pid)
+	}
 
 	if T_type > 0 {
 		cond1 = cond1.And("T_type", T_type)
@@ -1569,18 +1590,28 @@ func Read_DeviceSensor_List_For_Applet(admin *Account.Admin, bindSN []string, T_
 	return DeviceSensor_r, cnt
 }
 
-// 小程序-传感器类型
-func Read_DeviceSensor_T_sn_ByT_type(T_pid int, bindSN []string, T_type int) (lists []string, err error) {
+// 小程序-通过传感器类型获取sn
+func Read_DeviceSensor_T_sn_ByT_type(admin *Account.Admin, T_pid int, bindSN []string, T_pids []int, T_type int) (lists []string, err error) {
 
 	o := orm.NewOrm()
 
-	sql := "t_pid=? AND t__state=1 AND t_datashow=1 AND t_type = " + strconv.Itoa(T_type)
+	sql := "t__state=1 AND t_datashow=1 AND t_type = " + strconv.Itoa(T_type)
+
+	if admin.T_pid > 0 {
+		// 客户访问自己及其子公司数据
+		if len(T_pids) > 0 {
+			sql += fmt.Sprintf(" AND t_pid in (%s) ", lib.IntListToDotStr(T_pids))
+		}
+	} else {
+		// 管理员访问当前公司数据
+		sql += " AND t_pid = " + strconv.Itoa(T_pid)
+	}
 
 	if len(bindSN) > 0 {
 		sql += fmt.Sprintf(" AND t_sn in (%s)", lib.StringListToQuotesDotStr(bindSN))
 	}
 
-	_, err = o.Raw("SELECT DISTINCT T_sn FROM device_sensor WHERE "+sql+" LIMIT 0,1000", T_pid).QueryRows(&lists)
+	_, err = o.Raw("SELECT DISTINCT T_sn FROM device_sensor WHERE " + sql + " LIMIT 0,1000").QueryRows(&lists)
 	if err != nil {
 		logs.Error(lib.FuncName(), err)
 		return
@@ -1624,3 +1655,45 @@ func Read_DeviceSensor_All() (DeviceSensor_r []DeviceSensor) {
 
 	return DeviceSensor_r
 }
+
+// 获取对接电信的实时数据
+func Read_Docking_Real_Data(T_pid int) (DeviceData_r []DeviceData_R) {
+	o := orm.NewOrm()
+	// 也可以直接使用 Model 结构体作为表名
+
+	qs := o.QueryTable(new(DeviceSensor))
+
+	var r []DeviceSensor
+	cond := orm.NewCondition()
+	cond1 := cond.And("T_pid", T_pid)
+
+	_, err := qs.SetCond((*orm2.Condition)(cond1)).All(&r)
+	if err != nil {
+		logs.Error(lib.FuncName(), err)
+		return
+	}
+
+	for _, v := range r {
+		sp, _ := Read_DeviceSensorParameter(v.T_sn, v.T_id)
+		deviceDate := Read_DeviceData(v.T_sn, v.T_id)
+
+		data := DeviceData_R{
+			T_sn:   v.T_sn,
+			T_id:   v.T_id,
+			T_t:    deviceDate.T_t,
+			T_rh:   deviceDate.T_rh,
+			T_site: deviceDate.T_site,
+			T_time: deviceDate.T_time.Format("2006-01-02 15:04:05"),
+			T_sp:   deviceDate.T_sp,
+			T_name: sp.T_name,
+			T_tl:   sp.T_Tlower,
+			T_tu:   sp.T_Tupper,
+			T_rhl:  sp.T_RHlower,
+			T_rhu:  sp.T_RHupper,
+		}
+
+		DeviceData_r = append(DeviceData_r, data)
+
+	}
+	return DeviceData_r
+}

+ 26 - 10
models/Warning/Warning.go

@@ -853,7 +853,8 @@ func Read_DeviceLogs_List(T_sn string, page, page_z int) (r []DeviceLogs, cnt in
 }
 
 // 通过传感器类型获取报警列表
-func Read_Warning_List_By_DS_T_type(T_pid int, bindSN []string, DS_T_type int, T_name string, page int, page_z int) (r []Warning_Applet, cnt int64) {
+func Read_Warning_List_By_DS_T_type(admin *Account.Admin, T_pid int, bindSN []string, T_pids []int, DS_T_type int, T_name string,
+	page int, page_z int, countRedisKey string) (r []Warning_Applet, cnt int64) {
 	o := orm.NewOrm()
 	// 也可以直接使用 Model 结构体作为表名
 	var maps []Warning
@@ -866,7 +867,14 @@ func Read_Warning_List_By_DS_T_type(T_pid int, bindSN []string, DS_T_type int, T
 	}
 
 	// T_State 0 删除   1 不处理   2 已处理   3 未处理
-	sql_WHERE := " t__state > 1 AND t_pid = " + strconv.Itoa(T_pid)
+	sql_WHERE := "1 < t__state"
+	if admin.T_pid > 0 {
+		if len(T_pids) > 0 {
+			sql_WHERE += fmt.Sprintf(" AND t_pid in (%s) ", lib.IntListToDotStr(T_pids))
+		}
+	} else {
+		sql_WHERE += " AND t_pid = " + strconv.Itoa(T_pid)
+	}
 
 	if len(bindSN) > 0 {
 		sql_WHERE += fmt.Sprintf(" AND t_sn in (%s)", lib.StringListToQuotesDotStr(bindSN))
@@ -884,17 +892,27 @@ func Read_Warning_List_By_DS_T_type(T_pid int, bindSN []string, DS_T_type int, T
 		"LEFT JOIN ( SELECT t_sn AS tsn,t_id AS tid,t_type FROM device_sensor) AS ds " +
 		"ON w.t_sn = ds.tsn AND w.t_id = ds.tid " +
 		"WHERE " + sql_WHERE
-	fmt.Println(sql)
-	_, err := o.Raw(sql).ValuesList(&maps_z)
+	cnt, err := Redis_WarningCount_GetOne(T_pid, countRedisKey)
 	if err != nil {
-		logs.Error(lib.FuncName(), err)
-		return r, 0
+		fmt.Println(sql)
+		_, err = o.Raw(sql).ValuesList(&maps_z)
+		if err != nil {
+			logs.Error(lib.FuncName(), err)
+			return r, 0
+		}
+		if len(maps_z) == 0 {
+			return r, 0
+		}
+		cnt, _ = strconv.ParseInt(maps_z[0][0].(string), 10, 64)
+
+		Redis_WarningCount_Set(T_pid, countRedisKey, cnt)
 	}
-	if len(maps_z) == 0 {
+
+	if cnt == 0 {
 		return r, 0
 	}
 
-	sql = "SELECT * FROM warning w " +
+	sql = "SELECT w.*, ds.tsn, ds.tid, ds.t_type FROM warning w " +
 		"LEFT JOIN ( SELECT t_sn AS tsn,t_id AS tid,t_type FROM device_sensor) AS ds " +
 		"ON w.t_sn = ds.tsn AND w.t_id = ds.tid " +
 		"WHERE " + sql_WHERE + " ORDER BY t__ut DESC"
@@ -914,8 +932,6 @@ func Read_Warning_List_By_DS_T_type(T_pid int, bindSN []string, DS_T_type int, T
 		r = append(r, WarningToWarning_Applet(v))
 	}
 
-	cnt, _ = strconv.ParseInt(maps_z[0][0].(string), 10, 64)
-
 	return r, cnt
 
 }

+ 2 - 0
routers/Data.go

@@ -32,6 +32,8 @@ func init() {
 			beego.NSRouter("/Raw", &controllers.RawSqlController{}, "*:Raw"), // 执行 SQL
 		),
 	)
+	beego.Router("/docking/Real_Data", &controllers.DataController{}, "*:Docking_Real_Data")
+	beego.Router("/docking/Note_Data", &controllers.DataController{}, "*:Docking_Note_Data")
 
 	beego.AddNamespace(ns)
 

+ 4 - 0
routers/filter.go

@@ -25,6 +25,10 @@ func init() {
 		excludeUrlSlice := strings.Split(excludeUrl, ",")
 		if len(excludeUrlSlice) > 0 {
 			for _, v := range excludeUrlSlice {
+				if v == "/docking/Real_Data" || v == "/docking/Note_Data" {
+					filterExcludeURLMap[v] = 1
+					continue
+				}
 				filterExcludeURLMap[version+v] = 1
 			}
 		}