using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
namespace FangYar.BLL
{
///
/// 设备(BLL)
///
public class CommomBLL
{
private static readonly FangYar.IDAL.CommomIDAL dal = FangYar.DALFactory.Factory.GeCommomDAL();
///
/// 获取记录数
///
public int Count(string table, string where)
{
return dal.Count(table, where);
}
///
/// 获取列表
///
///
public string GetJsonList(string field, string table, string strwhere)
{
return dal.GetJsonList(field, table, strwhere);
}
///
/// 获取列表
///
///
public DataTable GetTableList(string field, string table, string strwhere)
{
return dal.GetTableList(field, table, strwhere);
}
public DataTable GetTableList(string sql)
{
return dal.GetTableList(sql);
}
///
/// 获取列表
///
///
public string GetJsonMonthCount(string field, string table, string year, string where)
{
return dal.GetJsonMonthCount(field, table, year, where);
}
///
public DataTable GetMonthCount(string field, string table, string year, string where)
{
return dal.GetMonthCount(field, table, year, where);
}
public string GetJsonXQHYCount(string field, string table, string where)
{
return FangYar.Common.JsonHelper.ToJson(GetXQHYCount(field, table, where));
}
public DataTable GetXQHYCount(string field, string table, string where)
{
string sql = " select t.dic_text as hy, IFNULL(q.sl,0) as sl from (select * from TBL_SYS_DICDETAIL where MOD_CODE='HYTYPE') t left join (";
if (table == "ZHSQ_WARN_RECORD")
{//报警记录
sql += " select sum(b.sl) as sl,dev_hy from ZHSQ_DEVICE_INFO m left join (select count(*) as sl, dev_id from ZHSQ_WARN_RECORD group by dev_id ) b on m.dev_code=b.dev_id ";
}
else if (table == "ZHSQ_DEVICE_INFO")
{//安装记录
sql += "select count(*) as sl,DEV_HY from ZHSQ_DEVICE_INFO t ";
}
if (!string.IsNullOrEmpty(where))
{
sql += " WHERE " + where;
}
sql += " group by " + field + ")q on t.dic_value=q." + field + " order by IFNULL(q.sl,0) desc ";
return FangYar.Common.MySqlHelper.QueryTable(sql);
}
public string GetJsonXQDWCount(string field, string table, string where, string where2)
{
return FangYar.Common.JsonHelper.ToJson(GetXQDWCount(field, table, where, where2));
}
public DataTable GetXQDWCount(string field, string table, string where, string where2)
{
string sql = "select t.* ,IFNULL(d.sl,0) as sl from (select * from tbl_sys_org ";
sql += " where area_id in( select area_code from TBL_SYS_AREA t ";
if (!string.IsNullOrEmpty(where))
{
sql += " WHERE " + where;
}
sql += " )) t left join ";
sql += " (select count(*) as sl," + field + " from " + table + " t ";
if (!string.IsNullOrEmpty(where2))
{
sql += " WHERE " + where2;
}
sql += " group by " + field + ") d on t.id=d." + field + " order by IFNULL(d.sl,0) desc ";
return FangYar.Common.MySqlHelper.QueryTable(sql);
}
public string GetJsonWeekCount(string field, string table, string where)
{
return FangYar.Common.JsonHelper.ToJson(GetWeekCount(field, table, where));
}
public DataTable GetWeekCount(string field, string table, string where)
{
string dayStr = "";
int daynum = 7;
for (int i = daynum; i > 0; i--)
{
if (i < daynum)
{
dayStr += " union ";
}
dayStr += "select '" + DateTime.Now.AddDays(-i).ToString("yyyy-MM-dd HH:mm:ss") + "' D";
}
string sql = " with t1 as (" + dayStr + ") ";
sql += " select date_format(t1.d,'%Y-%m-%d') as rq, count(t2." + field + " ) as sl ";
sql += " from t1 left join (select record_time from " + table;
if (!string.IsNullOrEmpty(where))
{
sql += " WHERE " + where;
}
sql += " ) t2 ";
sql += "on date_format(t1.d,'%Y-%m-%d') = date_format(t2." + field + ",'%Y-%m-%d') ";
sql += " group by t1.d order by t1.d ";
return FangYar.Common.MySqlHelper.QueryTable(sql);
}
public int GetXQCount(string field, string table, string where, string where2)
{
string sql = " select count(1) as num from " + table + " t WHERE ";
if (!string.IsNullOrEmpty(where))
{
sql += where + " and ";
}
sql += field + " in(select id from tbl_sys_org where area_id ";
sql += " in(select area_code from tbl_sys_area ";
if (!string.IsNullOrEmpty(where2))
{
sql += " WHERE " + where2;
}
sql += " )) ";
return FangYar.Common.MySqlHelper.GetCount(sql);
}
public DataTable GetTodayWarnList(string field, string table, string where)
{
string sql = "select t.id,d.dev_code,t.record_time, t.record_state,t.warn_val,t.warn_type,t.dev_hy,d.dev_name,d.group_ID,d.dev_loc,d.dev_addr,d.org_code,d.Cpy_Id,d.dev_lxr,d.dev_lxrtel from ZHSQ_WARN_RECORD t left join zhsq_device_info d on t.dev_id=d.dev_code where t.record_time>=date_format(now(),'%Y-%m-%d %H:%i:%s') and t.record_time
/// 分组查询,逗号隔开
///
///
///
///
///
///
public static string GetTableIDS(string keyField, string groupField, string tableName, string condition)
{
string RecordCount = "";
string sql = "select group_concat(" + keyField + ") as " + keyField + " from " + tableName + " " + condition + " group by " + groupField;
DataTable dt = FangYar.Common.MySqlHelper.QueryTable(sql);
if (dt.Rows.Count > 0)
{
RecordCount = dt.Rows[0][0].ToString();
}
dt.Clear();
dt.Dispose();
return RecordCount;
}
#endregion
//获取报警原因
public string GetBJReasonJson(string table, string where)
{
return FangYar.Common.JsonHelper.ToJson(GetBjReasonDT(table, where));
}
//获取报警原因
public DataTable GetBjReasonDT(string table, string where)
{
string sql = "select count(*) as sl ,q.warn_type from (";
sql += "select r.dev_id, r.warn_type,i.org_code from " + table + " r inner join ZHSQ_DEVICE_INFO i on i.dev_code=r.dev_id ";
sql += "where" + where;
sql += ")q group by q.warn_type";
DataTable dt = FangYar.Common.MySqlHelper.QueryTable(sql);
return dt;
}
public DataTable GetAllWarnList(string table, string where)
{
// string sql = " select b.*,a.record_state,a.alarmvalue,a.gathertype,a.creattime from ZHSQ_DEVICE_INFO b right join zhsq_warn_record a on a.gather=b.dev_code where b.org_code in(select id from tbl_sys_org where area_id in(select area_code from tbl_sys_area WHERE area_code like '37%' ))order by a.creattime desc ";
string sql = "select b.*,a.record_state,a.alarmvalue,a.gathertype,a.creattime ,o.org_name from ZHSQ_DEVICE_INFO b ";
sql += " right join " + table + " a on a.gather=b.dev_code left join tbl_sys_org o on o.id=b.org_code ";
sql += " where b. " + where;
sql += "order by a.creattime desc ";
DataTable dt = FangYar.Common.MySqlHelper.QueryTable(sql);
return dt;
}
public DataTable getUserMenu(string where)
{
string sql = " select m2.* from tbl_sys_rolemenu m1 left join TBL_SYS_MENU m2 on m1.menu_id=m2.id ";
sql += " where " + where;
DataTable dt = FangYar.Common.MySqlHelper.QueryTable(sql);
return dt;
}
//根据入库登录名,更新登录名获取相应 姓名
public DataTable getUserName(string A_PER, string U_PER)
{
string sql = "select (select t1.users_name from tbl_sys_users t1 where t1.users_uid = '" + A_PER + "' ) A_PER_NAME,(select t2.users_name from tbl_sys_users t2 where t2.users_uid = '" + U_PER + "') U_PER_NAME from dual";
//string sql = " select t1.users_name A_PER_NAME,t2.users_name U_PER_NAME from tbl_sys_users t1 , tbl_sys_users t2 where t1.users_uid = '" + A_PER + "' and t2.users_uid = '" + U_PER + "' ";
DataTable dt = FangYar.Common.MySqlHelper.QueryTable(sql);
return dt;
}
//根据入库登录名,更新登录名获取相应 姓名
public DataTable getHomeCount(string strwhere)
{
//string sql = " select t1.users_name A_PER_NAME,t2.users_name U_PER_NAME from tbl_sys_users t1 , tbl_sys_users t2 where t1.users_uid = '" + A_PER + "' and t2.users_uid = '" + U_PER + "' ";
StringBuilder sql = new StringBuilder();
//在岗
sql.Append("SELECT 'PERSON'as BC,0 as ty, count(1) as count FROM TBL_SYS_EMP h where h.IS_WORK = '0' and IS_ADMIN='0' " + strwhere);
//请假
sql.Append(" union all ");
sql.Append("SELECT 'PERSON',3 as tyz, count(1) FROM TBL_SYS_EMP h where h.IS_WORK = '3' and IS_ADMIN='0' " + strwhere);
//执勤可用
sql.Append(" union all ");
sql.Append("SELECT 'CAR1' as bc,0 as ty, count(1) as num FROM TBL_SYS_CAR t where t.class='1' and t.isrun='0' " + strwhere);
//执勤在用
sql.Append(" union all ");
sql.Append("SELECT 'CAR1' as bc,1 as ty, count(1) as num FROM TBL_SYS_CAR t where t.class='1' and t.isrun='1' " + strwhere);
//执勤故障
sql.Append(" union all ");
sql.Append("SELECT 'CAR1' as bc,2 as ty, count(1) as num FROM TBL_SYS_CAR t where t.class='1' and t.isrun='2' " + strwhere);
//公务可用
sql.Append(" union all ");
sql.Append("SELECT 'CAR0' as bc,0 as ty, count(1) as num FROM TBL_SYS_CAR t where t.class='0' and t.isrun='0'" + strwhere);
//公务在用
sql.Append(" union all ");
sql.Append("SELECT 'CAR0' as bc,1 as ty, count(1) as num FROM TBL_SYS_CAR t where t.class='0' and t.isrun='1'" + strwhere);
//公务故障
sql.Append(" union all ");
sql.Append("SELECT 'CAR0' as bc,2 as ty, count(1) as num FROM TBL_SYS_CAR t where t.class='0' and t.isrun='2'" + strwhere);
//摄像头
sql.Append(" union all ");
sql.Append("SELECT 'CAMERA',0, count(1) FROM TBL_CAMERA y where 1=1 " + strwhere);
DataTable dt = FangYar.Common.MySqlHelper.QueryTable(sql.ToString());
return dt;
}
}
}