using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using ZXDT.DBUtility;
using ZXDT.CallCenter.Model;
using System.Collections.Generic;//Please add references
namespace ZXDT.CallCenter.DAL
{
	/// <summary>
	/// 数据访问类:T_Wo_WorkOrderType
	/// </summary>
	public partial class T_Wo_WorkOrderType
	{
		public T_Wo_WorkOrderType()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("F_WorkOrderTypeId", "T_Wo_WorkOrderType"); 
		}
        /// <summary>
        /// 查询编号最大值
        /// </summary>
        public object maxNumber(int layer)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select max(f_number) from T_Wo_WorkOrderType");
            strSql.Append(" where f_layer=@f_layer ");
            SqlParameter[] parameters = {
                    new SqlParameter("@f_layer", SqlDbType.Int,4)};
            parameters[0].Value = layer;

            return DbHelperSQL.GetSingle(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 查询编号最大值
        /// </summary>
        public object maxNumber(int layer, int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select max(f_number) from T_Wo_WorkOrderType");
            strSql.Append(" where f_layer=@f_layer and F_WorkOrderTypeId = @F_WorkOrderTypeId ");
            SqlParameter[] parameters = {
                    new SqlParameter("@f_layer", SqlDbType.Int,4),
                    new SqlParameter("@F_WorkOrderTypeId", SqlDbType.Int,4)};
            parameters[0].Value = layer;
            parameters[1].Value = id;

            return DbHelperSQL.GetSingle(strSql.ToString(), parameters);
        }
      
		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int F_WorkOrderTypeId)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from T_Wo_WorkOrderType");
			strSql.Append(" where F_WorkOrderTypeId=@F_WorkOrderTypeId");
			SqlParameter[] parameters = {
					new SqlParameter("@F_WorkOrderTypeId", SqlDbType.Int,4)
};
			parameters[0].Value = F_WorkOrderTypeId;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(ZXDT.CallCenter.Model.T_Wo_WorkOrderType model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into T_Wo_WorkOrderType(");
			strSql.Append("F_Name,F_Ename,F_Number,F_ParentId,F_ParentName,F_Sort,F_Layer,F_DeleteFlag,F_Createby,F_CreateDate,F_Modifyby,F_ModifyDate,TypeFrom,F_Icon,F_Remark)");
			strSql.Append(" values (");
			strSql.Append("@F_Name,@F_Ename,@F_Number,@F_ParentId,@F_ParentName,@F_Sort,@F_Layer,@F_DeleteFlag,@F_Createby,@F_CreateDate,@F_Modifyby,@F_ModifyDate,@TypeFrom,@F_Icon,@F_Remark)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@F_Name", SqlDbType.NVarChar,50),
					new SqlParameter("@F_Ename", SqlDbType.NVarChar,50),
					new SqlParameter("@F_Number", SqlDbType.NVarChar,50),
					new SqlParameter("@F_ParentId", SqlDbType.Int,4),
					new SqlParameter("@F_ParentName", SqlDbType.NVarChar,50),
					new SqlParameter("@F_Sort", SqlDbType.Int,4),
					new SqlParameter("@F_Layer", SqlDbType.Int,4),
					new SqlParameter("@F_DeleteFlag", SqlDbType.Int,4),
					new SqlParameter("@F_Createby", SqlDbType.Int,4),
					new SqlParameter("@F_CreateDate", SqlDbType.DateTime),
					new SqlParameter("@F_Modifyby", SqlDbType.Int,4),
					new SqlParameter("@F_ModifyDate", SqlDbType.DateTime),
					new SqlParameter("@TypeFrom", SqlDbType.Int,4),
					new SqlParameter("@F_Icon", SqlDbType.NVarChar,50),
					new SqlParameter("@F_Remark", SqlDbType.NVarChar,100)};
			parameters[0].Value = model.F_Name;
			parameters[1].Value = model.F_Ename;
			parameters[2].Value = model.F_Number;
			parameters[3].Value = model.F_ParentId;
			parameters[4].Value = model.F_ParentName;
			parameters[5].Value = model.F_Sort;
			parameters[6].Value = model.F_Layer;
			parameters[7].Value = model.F_DeleteFlag;
			parameters[8].Value = model.F_Createby;
			parameters[9].Value = model.F_CreateDate;
			parameters[10].Value = model.F_Modifyby;
			parameters[11].Value = model.F_ModifyDate;
			parameters[12].Value = model.TypeFrom;
			parameters[13].Value = model.F_Icon;
			parameters[14].Value = model.F_Remark;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public int Update(ZXDT.CallCenter.Model.T_Wo_WorkOrderType model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update T_Wo_WorkOrderType set ");
			strSql.Append("F_Name=@F_Name,");
			strSql.Append("F_Ename=@F_Ename,");
			strSql.Append("F_Number=@F_Number,");
			strSql.Append("F_ParentId=@F_ParentId,");
			strSql.Append("F_ParentName=@F_ParentName,");
			strSql.Append("F_Sort=@F_Sort,");
			strSql.Append("F_Layer=@F_Layer,");
			strSql.Append("F_DeleteFlag=@F_DeleteFlag,");
			strSql.Append("F_Createby=@F_Createby,");
			strSql.Append("F_CreateDate=@F_CreateDate,");
			strSql.Append("F_Modifyby=@F_Modifyby,");
			strSql.Append("F_ModifyDate=@F_ModifyDate,");
			strSql.Append("TypeFrom=@TypeFrom,");
			strSql.Append("F_Icon=@F_Icon,");
			strSql.Append("F_Remark=@F_Remark");
			strSql.Append(" where F_WorkOrderTypeId=@F_WorkOrderTypeId");
			SqlParameter[] parameters = {
					new SqlParameter("@F_Name", SqlDbType.NVarChar,50),
					new SqlParameter("@F_Ename", SqlDbType.NVarChar,50),
					new SqlParameter("@F_Number", SqlDbType.NVarChar,50),
					new SqlParameter("@F_ParentId", SqlDbType.Int,4),
					new SqlParameter("@F_ParentName", SqlDbType.NVarChar,50),
					new SqlParameter("@F_Sort", SqlDbType.Int,4),
					new SqlParameter("@F_Layer", SqlDbType.Int,4),
					new SqlParameter("@F_DeleteFlag", SqlDbType.Int,4),
					new SqlParameter("@F_Createby", SqlDbType.Int,4),
					new SqlParameter("@F_CreateDate", SqlDbType.DateTime),
					new SqlParameter("@F_Modifyby", SqlDbType.Int,4),
					new SqlParameter("@F_ModifyDate", SqlDbType.DateTime),
					new SqlParameter("@TypeFrom", SqlDbType.Int,4),
					new SqlParameter("@F_Icon", SqlDbType.NVarChar,50),
					new SqlParameter("@F_Remark", SqlDbType.NVarChar,100),
					new SqlParameter("@F_WorkOrderTypeId", SqlDbType.Int,4)};
			parameters[0].Value = model.F_Name;
			parameters[1].Value = model.F_Ename;
			parameters[2].Value = model.F_Number;
			parameters[3].Value = model.F_ParentId;
			parameters[4].Value = model.F_ParentName;
			parameters[5].Value = model.F_Sort;
			parameters[6].Value = model.F_Layer;
			parameters[7].Value = model.F_DeleteFlag;
			parameters[8].Value = model.F_Createby;
			parameters[9].Value = model.F_CreateDate;
			parameters[10].Value = model.F_Modifyby;
			parameters[11].Value = model.F_ModifyDate;
			parameters[12].Value = model.TypeFrom;
			parameters[13].Value = model.F_Icon;
			parameters[14].Value = model.F_Remark;
			parameters[15].Value = model.F_WorkOrderTypeId;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return rows;
			}
			else
			{
				return 0;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int F_WorkOrderTypeId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from T_Wo_WorkOrderType ");
			strSql.Append(" where F_WorkOrderTypeId=@F_WorkOrderTypeId");
			SqlParameter[] parameters = {
					new SqlParameter("@F_WorkOrderTypeId", SqlDbType.Int,4)
};
			parameters[0].Value = F_WorkOrderTypeId;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool DeleteList(string F_WorkOrderTypeIdlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from T_Wo_WorkOrderType ");
			strSql.Append(" where F_WorkOrderTypeId in ("+F_WorkOrderTypeIdlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public ZXDT.CallCenter.Model.T_Wo_WorkOrderType GetModel(int F_WorkOrderTypeId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 F_WorkOrderTypeId,F_Name,F_Ename,F_Number,F_ParentId,F_ParentName,F_Sort,F_Layer,F_DeleteFlag,F_Createby,F_CreateDate,F_Modifyby,F_ModifyDate,TypeFrom,F_Icon,F_Remark from T_Wo_WorkOrderType ");
			strSql.Append(" where F_WorkOrderTypeId=@F_WorkOrderTypeId");
			SqlParameter[] parameters = {
					new SqlParameter("@F_WorkOrderTypeId", SqlDbType.Int,4)
};
			parameters[0].Value = F_WorkOrderTypeId;

			ZXDT.CallCenter.Model.T_Wo_WorkOrderType model=new ZXDT.CallCenter.Model.T_Wo_WorkOrderType();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["F_WorkOrderTypeId"]!=null && ds.Tables[0].Rows[0]["F_WorkOrderTypeId"].ToString()!="")
				{
					model.F_WorkOrderTypeId=int.Parse(ds.Tables[0].Rows[0]["F_WorkOrderTypeId"].ToString());
				}
				if(ds.Tables[0].Rows[0]["F_Name"]!=null && ds.Tables[0].Rows[0]["F_Name"].ToString()!="")
				{
					model.F_Name=ds.Tables[0].Rows[0]["F_Name"].ToString();
				}
				if(ds.Tables[0].Rows[0]["F_Ename"]!=null && ds.Tables[0].Rows[0]["F_Ename"].ToString()!="")
				{
					model.F_Ename=ds.Tables[0].Rows[0]["F_Ename"].ToString();
				}
				if(ds.Tables[0].Rows[0]["F_Number"]!=null && ds.Tables[0].Rows[0]["F_Number"].ToString()!="")
				{
					model.F_Number=ds.Tables[0].Rows[0]["F_Number"].ToString();
				}
				if(ds.Tables[0].Rows[0]["F_ParentId"]!=null && ds.Tables[0].Rows[0]["F_ParentId"].ToString()!="")
				{
					model.F_ParentId=int.Parse(ds.Tables[0].Rows[0]["F_ParentId"].ToString());
				}
				if(ds.Tables[0].Rows[0]["F_ParentName"]!=null && ds.Tables[0].Rows[0]["F_ParentName"].ToString()!="")
				{
					model.F_ParentName=ds.Tables[0].Rows[0]["F_ParentName"].ToString();
				}
				if(ds.Tables[0].Rows[0]["F_Sort"]!=null && ds.Tables[0].Rows[0]["F_Sort"].ToString()!="")
				{
					model.F_Sort=int.Parse(ds.Tables[0].Rows[0]["F_Sort"].ToString());
				}
				if(ds.Tables[0].Rows[0]["F_Layer"]!=null && ds.Tables[0].Rows[0]["F_Layer"].ToString()!="")
				{
					model.F_Layer=int.Parse(ds.Tables[0].Rows[0]["F_Layer"].ToString());
				}
				if(ds.Tables[0].Rows[0]["F_DeleteFlag"]!=null && ds.Tables[0].Rows[0]["F_DeleteFlag"].ToString()!="")
				{
					model.F_DeleteFlag=int.Parse(ds.Tables[0].Rows[0]["F_DeleteFlag"].ToString());
				}
				if(ds.Tables[0].Rows[0]["F_Createby"]!=null && ds.Tables[0].Rows[0]["F_Createby"].ToString()!="")
				{
					model.F_Createby=int.Parse(ds.Tables[0].Rows[0]["F_Createby"].ToString());
				}
				if(ds.Tables[0].Rows[0]["F_CreateDate"]!=null && ds.Tables[0].Rows[0]["F_CreateDate"].ToString()!="")
				{
					model.F_CreateDate=DateTime.Parse(ds.Tables[0].Rows[0]["F_CreateDate"].ToString());
				}
				if(ds.Tables[0].Rows[0]["F_Modifyby"]!=null && ds.Tables[0].Rows[0]["F_Modifyby"].ToString()!="")
				{
					model.F_Modifyby=int.Parse(ds.Tables[0].Rows[0]["F_Modifyby"].ToString());
				}
				if(ds.Tables[0].Rows[0]["F_ModifyDate"]!=null && ds.Tables[0].Rows[0]["F_ModifyDate"].ToString()!="")
				{
					model.F_ModifyDate=DateTime.Parse(ds.Tables[0].Rows[0]["F_ModifyDate"].ToString());
				}
				if(ds.Tables[0].Rows[0]["TypeFrom"]!=null && ds.Tables[0].Rows[0]["TypeFrom"].ToString()!="")
				{
					model.TypeFrom=int.Parse(ds.Tables[0].Rows[0]["TypeFrom"].ToString());
				}
				if(ds.Tables[0].Rows[0]["F_Icon"]!=null && ds.Tables[0].Rows[0]["F_Icon"].ToString()!="")
				{
					model.F_Icon=ds.Tables[0].Rows[0]["F_Icon"].ToString();
				}
				if(ds.Tables[0].Rows[0]["F_Remark"]!=null && ds.Tables[0].Rows[0]["F_Remark"].ToString()!="")
				{
					model.F_Remark=ds.Tables[0].Rows[0]["F_Remark"].ToString();
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select F_WorkOrderTypeId,F_Name,F_Ename,F_Number,F_ParentId,F_ParentName,F_Sort,F_Layer,F_DeleteFlag,F_Createby,F_CreateDate,F_Modifyby,F_ModifyDate,TypeFrom,F_Icon,F_Remark ");
			strSql.Append(" FROM T_Wo_WorkOrderType ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
            strSql.Append("  order by F_Sort "); 
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" F_WorkOrderTypeId,F_Name,F_Ename,F_Number,F_ParentId,F_ParentName,F_Sort,F_Layer,F_DeleteFlag,F_Createby,F_CreateDate,F_Modifyby,F_ModifyDate,TypeFrom,F_Icon,F_Remark ");
			strSql.Append(" FROM T_Wo_WorkOrderType ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "T_Wo_WorkOrderType";
			parameters[1].Value = "F_WorkOrderTypeId";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method

        #region 获取树结构
        /// <summary>
        ///  获得当前节点下的所有子集以tree的格式
        /// </summary>
        public TreeNodeModel GetDicTittleJsonModel(int parentId)
        {
            TreeNodeModel returnModel = new TreeNodeModel();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM T_Wo_WorkOrderType  order by F_Sort asc");
         
            DataSet ds = DbHelperSQL.Query(strSql.ToString());

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                DataRow[] rows = ds.Tables[0].Select("F_ParentId=" + parentId);
                if (rows != null && rows.Length > 0)
                {
                    TreeNodeModel model = new TreeNodeModel();
                    List<TreeNodeModel> modelList = new List<TreeNodeModel>();
                    model.id = int.Parse(rows[0]["F_WorkOrderTypeId"].ToString());
                    model.name = rows[0]["F_Name"].ToString();
                    model.code = int.Parse(rows[0]["F_WorkOrderTypeId"].ToString());
                    model.iconClose = "../../Content/layui/zTree/v3/css/zTreeStyle/img/diy/1_close.png";
                    model.iconOpen = "../../Content/layui/zTree/v3/css/zTreeStyle/img/diy/1_open.png";
                    model.open = "true";
                    model.children = modelList;
                    returnModel = GetTreeModel(ds.Tables[0], model);
                }
            }
            return returnModel;
        }


        /// <summary>
        /// 获取chlid
        /// </summary>
        /// <param name="table">数据结合</param>
        /// <param name="model">当前model</param>
        /// <returns></returns>
        public TreeNodeModel GetTreeModel(DataTable table, TreeNodeModel model)
        {
            DataRow[] rows = table.Select("F_ParentId=" + model.code);
            if (rows != null && rows.Length > 0)
            {
                foreach (DataRow row in rows)
                {
                    List<TreeNodeModel> modelList = new List<TreeNodeModel>();
                    TreeNodeModel childModel = new TreeNodeModel();
                    childModel.id = int.Parse(row["F_WorkOrderTypeId"].ToString());
                    childModel.name = row["F_Name"].ToString();
                    childModel.code = int.Parse(row["F_WorkOrderTypeId"].ToString());
                    childModel.iconClose = "../../Content/layui/zTree/v3/css/zTreeStyle/img/diy/1_close.png";
                    childModel.iconOpen = "../../Content/layui/zTree/v3/css/zTreeStyle/img/diy/1_open.png";
                    childModel.open = "true";
                    childModel.children = modelList;
                    model.children.Add(childModel);
                    GetTreeModel(table, childModel);
                }
            }
            return model;

        }
      
        #endregion

    }
}

