
using CallCenterApi.DB;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;

namespace CallCenterApi.DAL
{
    /// <summary>
    /// 数据访问类:submit_case_info
    /// </summary>
    public partial class submit_case_info
    {
        public submit_case_info()
        { }
        #region  BasicMethod

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("F_ID", "submit_case_info");
        }

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

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


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(CallCenterApi.Model.submit_case_info model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into submit_case_info(");
            strSql.Append("CASE_SERIAL,AREA_CODE,CASE_ID,APPLICANT_NAME,APPLICANT_SEX,APPLICANT_AGE,APPLICANT_NUMBER,APPLICANT_TIME,HWY_NAME,HWY_LOGINID,CASE_TIME,CASE_TITLE,CASE_CONTENT,CASE_SOURCE,CASE_TYPE,CASE_ACCORD,CASE_ISPUBLIC,CASE_ISVISIT,CASE_ISURGENT,CASE_LNGLAT,CASE_EMOTION,CASE_ADDRESS,Create_Time,ErrorObjs,Code,USER_TYPE)");
            strSql.Append(" values (");
            strSql.Append("@CASE_SERIAL,@AREA_CODE,@CASE_ID,@APPLICANT_NAME,@APPLICANT_SEX,@APPLICANT_AGE,@APPLICANT_NUMBER,@APPLICANT_TIME,@HWY_NAME,@HWY_LOGINID,@CASE_TIME,@CASE_TITLE,@CASE_CONTENT,@CASE_SOURCE,@CASE_TYPE,@CASE_ACCORD,@CASE_ISPUBLIC,@CASE_ISVISIT,@CASE_ISURGENT,@CASE_LNGLAT,@CASE_EMOTION,@CASE_ADDRESS,@Create_Time,@ErrorObjs,@Code,@USER_TYPE)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@CASE_SERIAL", SqlDbType.NVarChar,50),
                    new SqlParameter("@AREA_CODE", SqlDbType.NVarChar,50),
                    new SqlParameter("@CASE_ID", SqlDbType.NVarChar,50),
                    new SqlParameter("@APPLICANT_NAME", SqlDbType.NVarChar,50),
                    new SqlParameter("@APPLICANT_SEX", SqlDbType.NVarChar,50),
                    new SqlParameter("@APPLICANT_AGE", SqlDbType.NVarChar,50),
                    new SqlParameter("@APPLICANT_NUMBER", SqlDbType.NVarChar,50),
                    new SqlParameter("@APPLICANT_TIME", SqlDbType.NVarChar,50),
                    new SqlParameter("@HWY_NAME", SqlDbType.NVarChar,50),
                    new SqlParameter("@HWY_LOGINID", SqlDbType.NVarChar,50),
                    new SqlParameter("@CASE_TIME", SqlDbType.NVarChar,50),
                    new SqlParameter("@CASE_TITLE", SqlDbType.NVarChar,1000),
                    new SqlParameter("@CASE_CONTENT", SqlDbType.NVarChar,-1),
                    new SqlParameter("@CASE_SOURCE", SqlDbType.NVarChar,50),
                    new SqlParameter("@CASE_TYPE", SqlDbType.NVarChar,50),
                    new SqlParameter("@CASE_ACCORD", SqlDbType.NVarChar,50),
                    new SqlParameter("@CASE_ISPUBLIC", SqlDbType.NVarChar,50),
                    new SqlParameter("@CASE_ISVISIT", SqlDbType.NVarChar,50),
                    new SqlParameter("@CASE_ISURGENT", SqlDbType.NVarChar,50),
                    new SqlParameter("@CASE_LNGLAT", SqlDbType.NVarChar,50),
                    new SqlParameter("@CASE_EMOTION", SqlDbType.NVarChar,50),
                    new SqlParameter("@CASE_ADDRESS", SqlDbType.NVarChar,500),
                    new SqlParameter("@Create_Time", SqlDbType.DateTime),
                    new SqlParameter("@ErrorObjs", SqlDbType.NVarChar,-1),
                    new SqlParameter("@Code", SqlDbType.Int,4),
                    new SqlParameter("@USER_TYPE", SqlDbType.NVarChar,50)
            }; 
             parameters[0].Value = model.CASE_SERIAL;
            parameters[1].Value = model.AREA_CODE;
            parameters[2].Value = model.CASE_ID;
            parameters[3].Value = model.APPLICANT_NAME;
            parameters[4].Value = model.APPLICANT_SEX;
            parameters[5].Value = model.APPLICANT_AGE;
            parameters[6].Value = model.APPLICANT_NUMBER;
            parameters[7].Value = model.APPLICANT_TIME;
            parameters[8].Value = model.HWY_NAME;
            parameters[9].Value = model.HWY_LOGINID;
            parameters[10].Value = model.CASE_TIME;
            parameters[11].Value = model.CASE_TITLE;
            parameters[12].Value = model.CASE_CONTENT;
            parameters[13].Value = model.CASE_SOURCE;
            parameters[14].Value = model.CASE_TYPE;
            parameters[15].Value = model.CASE_ACCORD;
            parameters[16].Value = model.CASE_ISPUBLIC;
            parameters[17].Value = model.CASE_ISVISIT;
            parameters[18].Value = model.CASE_ISURGENT;
            parameters[19].Value = model.CASE_LNGLAT;
            parameters[20].Value = model.CASE_EMOTION;
            parameters[21].Value = model.CASE_ADDRESS;
            parameters[22].Value = model.Create_Time;
            parameters[23].Value = model.ErrorObjs;
            parameters[24].Value = model.Code;
            parameters[25].Value = model.USER_TYPE;
            
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(CallCenterApi.Model.submit_case_info model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update submit_case_info set ");
            strSql.Append("CASE_SERIAL=@CASE_SERIAL,");
            strSql.Append("AREA_CODE=@AREA_CODE,");
            strSql.Append("CASE_ID=@CASE_ID,");
            strSql.Append("APPLICANT_NAME=@APPLICANT_NAME,");
            strSql.Append("APPLICANT_SEX=@APPLICANT_SEX,");
            strSql.Append("APPLICANT_AGE=@APPLICANT_AGE,");
            strSql.Append("APPLICANT_NUMBER=@APPLICANT_NUMBER,");
            strSql.Append("APPLICANT_TIME=@APPLICANT_TIME,");
            strSql.Append("HWY_NAME=@HWY_NAME,");
            strSql.Append("HWY_LOGINID=@HWY_LOGINID,");
            strSql.Append("CASE_TIME=@CASE_TIME,");
            strSql.Append("CASE_TITLE=@CASE_TITLE,");
            strSql.Append("CASE_CONTENT=@CASE_CONTENT,");
            strSql.Append("CASE_SOURCE=@CASE_SOURCE,");
            strSql.Append("CASE_TYPE=@CASE_TYPE,");
            strSql.Append("CASE_ACCORD=@CASE_ACCORD,");
            strSql.Append("CASE_ISPUBLIC=@CASE_ISPUBLIC,");
            strSql.Append("CASE_ISVISIT=@CASE_ISVISIT,");
            strSql.Append("CASE_ISURGENT=@CASE_ISURGENT,");
            strSql.Append("CASE_LNGLAT=@CASE_LNGLAT,");
            strSql.Append("CASE_EMOTION=@CASE_EMOTION,");
            strSql.Append("CASE_ADDRESS=@CASE_ADDRESS,");
            strSql.Append("Create_Time=@Create_Time,");
            strSql.Append("ErrorObjs=@ErrorObjs,");
            strSql.Append("USER_TYPE=@USER_TYPE,");
            strSql.Append("Code=@Code");
            strSql.Append(" where F_ID=@F_ID");
            SqlParameter[] parameters = {
                    new SqlParameter("@CASE_SERIAL", SqlDbType.NVarChar,50),
                    new SqlParameter("@AREA_CODE", SqlDbType.NVarChar,50),
                    new SqlParameter("@CASE_ID", SqlDbType.NVarChar,50),
                    new SqlParameter("@APPLICANT_NAME", SqlDbType.NVarChar,50),
                    new SqlParameter("@APPLICANT_SEX", SqlDbType.NVarChar,50),
                    new SqlParameter("@APPLICANT_AGE", SqlDbType.NVarChar,50),
                    new SqlParameter("@APPLICANT_NUMBER", SqlDbType.NVarChar,50),
                    new SqlParameter("@APPLICANT_TIME", SqlDbType.NVarChar,50),
                    new SqlParameter("@HWY_NAME", SqlDbType.NVarChar,50),
                    new SqlParameter("@HWY_LOGINID", SqlDbType.NVarChar,50),
                    new SqlParameter("@CASE_TIME", SqlDbType.NVarChar,50),
                    new SqlParameter("@CASE_TITLE", SqlDbType.NVarChar,1000),
                    new SqlParameter("@CASE_CONTENT", SqlDbType.NVarChar,-1),
                    new SqlParameter("@CASE_SOURCE", SqlDbType.NVarChar,50),
                    new SqlParameter("@CASE_TYPE", SqlDbType.NVarChar,50),
                    new SqlParameter("@CASE_ACCORD", SqlDbType.NVarChar,50),
                    new SqlParameter("@CASE_ISPUBLIC", SqlDbType.NVarChar,50),
                    new SqlParameter("@CASE_ISVISIT", SqlDbType.NVarChar,50),
                    new SqlParameter("@CASE_ISURGENT", SqlDbType.NVarChar,50),
                    new SqlParameter("@CASE_LNGLAT", SqlDbType.NVarChar,50),
                    new SqlParameter("@CASE_EMOTION", SqlDbType.NVarChar,50),
                    new SqlParameter("@CASE_ADDRESS", SqlDbType.NVarChar,500),
                    new SqlParameter("@Create_Time", SqlDbType.DateTime),
                    new SqlParameter("@ErrorObjs", SqlDbType.NVarChar,-1),
                    new SqlParameter("@USER_TYPE", SqlDbType.NVarChar,50),
                    new SqlParameter("@Code", SqlDbType.Int,4),
                    new SqlParameter("@F_ID", SqlDbType.Int,4)};
            parameters[0].Value = model.CASE_SERIAL;
            parameters[1].Value = model.AREA_CODE;
            parameters[2].Value = model.CASE_ID;
            parameters[3].Value = model.APPLICANT_NAME;
            parameters[4].Value = model.APPLICANT_SEX;
            parameters[5].Value = model.APPLICANT_AGE;
            parameters[6].Value = model.APPLICANT_NUMBER;
            parameters[7].Value = model.APPLICANT_TIME;
            parameters[8].Value = model.HWY_NAME;
            parameters[9].Value = model.HWY_LOGINID;
            parameters[10].Value = model.CASE_TIME;
            parameters[11].Value = model.CASE_TITLE;
            parameters[12].Value = model.CASE_CONTENT;
            parameters[13].Value = model.CASE_SOURCE;
            parameters[14].Value = model.CASE_TYPE;
            parameters[15].Value = model.CASE_ACCORD;
            parameters[16].Value = model.CASE_ISPUBLIC;
            parameters[17].Value = model.CASE_ISVISIT;
            parameters[18].Value = model.CASE_ISURGENT;
            parameters[19].Value = model.CASE_LNGLAT;
            parameters[20].Value = model.CASE_EMOTION;
            parameters[21].Value = model.CASE_ADDRESS;
            parameters[22].Value = model.Create_Time;
            parameters[23].Value = model.ErrorObjs;
            parameters[24].Value = model.USER_TYPE;
            parameters[25].Value = model.Code;
            parameters[26].Value = model.F_ID;

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

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int F_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from submit_case_info ");
            strSql.Append(" where F_ID=@F_ID");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_ID", SqlDbType.Int,4)
            };
            parameters[0].Value = F_ID;

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


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CallCenterApi.Model.submit_case_info GetModel(int F_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 CASE_SERIAL,USER_TYPE,AREA_CODE,CASE_ID,APPLICANT_NAME,APPLICANT_SEX,APPLICANT_AGE,APPLICANT_NUMBER,APPLICANT_TIME,HWY_NAME,HWY_LOGINID,CASE_TIME,CASE_TITLE,CASE_CONTENT,CASE_SOURCE,CASE_TYPE,CASE_ACCORD,CASE_ISPUBLIC,CASE_ISVISIT,CASE_ISURGENT,CASE_LNGLAT,CASE_EMOTION,CASE_ADDRESS,Create_Time,F_ID,ErrorObjs,Code from submit_case_info ");
            strSql.Append(" where F_ID=@F_ID");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_ID", SqlDbType.Int,4)
            };
            parameters[0].Value = F_ID;

            CallCenterApi.Model.submit_case_info model = new CallCenterApi.Model.submit_case_info();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CallCenterApi.Model.submit_case_info DataRowToModel(DataRow row)
        {
            CallCenterApi.Model.submit_case_info model = new CallCenterApi.Model.submit_case_info();
            if (row != null)
            {
                if (row["CASE_SERIAL"] != null)
                {
                    model.CASE_SERIAL = row["CASE_SERIAL"].ToString();
                }
                if (row["AREA_CODE"] != null)
                {
                    model.AREA_CODE = row["AREA_CODE"].ToString();
                }
                if (row["CASE_ID"] != null)
                {
                    model.CASE_ID = row["CASE_ID"].ToString();
                }
                if (row["APPLICANT_NAME"] != null)
                {
                    model.APPLICANT_NAME = row["APPLICANT_NAME"].ToString();
                }
                if (row["APPLICANT_SEX"] != null)
                {
                    model.APPLICANT_SEX = row["APPLICANT_SEX"].ToString();
                }
                if (row["APPLICANT_AGE"] != null)
                {
                    model.APPLICANT_AGE = row["APPLICANT_AGE"].ToString();
                }
                if (row["APPLICANT_NUMBER"] != null)
                {
                    model.APPLICANT_NUMBER = row["APPLICANT_NUMBER"].ToString();
                }
                if (row["APPLICANT_TIME"] != null)
                {
                    model.APPLICANT_TIME = row["APPLICANT_TIME"].ToString();
                }
                if (row["HWY_NAME"] != null)
                {
                    model.HWY_NAME = row["HWY_NAME"].ToString();
                }
                if (row["HWY_LOGINID"] != null)
                {
                    model.HWY_LOGINID = row["HWY_LOGINID"].ToString();
                }
                if (row["CASE_TIME"] != null)
                {
                    model.CASE_TIME = row["CASE_TIME"].ToString();
                }
                if (row["CASE_TITLE"] != null)
                {
                    model.CASE_TITLE = row["CASE_TITLE"].ToString();
                }
                if (row["CASE_CONTENT"] != null)
                {
                    model.CASE_CONTENT = row["CASE_CONTENT"].ToString();
                }
                if (row["CASE_SOURCE"] != null)
                {
                    model.CASE_SOURCE = row["CASE_SOURCE"].ToString();
                }
                if (row["CASE_TYPE"] != null)
                {
                    model.CASE_TYPE = row["CASE_TYPE"].ToString();
                }
                if (row["CASE_ACCORD"] != null)
                {
                    model.CASE_ACCORD = row["CASE_ACCORD"].ToString();
                }
                if (row["CASE_ISPUBLIC"] != null)
                {
                    model.CASE_ISPUBLIC = row["CASE_ISPUBLIC"].ToString();
                }
                if (row["CASE_ISVISIT"] != null)
                {
                    model.CASE_ISVISIT = row["CASE_ISVISIT"].ToString();
                }
                if (row["CASE_ISURGENT"] != null)
                {
                    model.CASE_ISURGENT = row["CASE_ISURGENT"].ToString();
                }
                if (row["CASE_LNGLAT"] != null)
                {
                    model.CASE_LNGLAT = row["CASE_LNGLAT"].ToString();
                }
                if (row["CASE_EMOTION"] != null)
                {
                    model.CASE_EMOTION = row["CASE_EMOTION"].ToString();
                }
                if (row["CASE_ADDRESS"] != null)
                {
                    model.CASE_ADDRESS = row["CASE_ADDRESS"].ToString();
                }
                if (row["Create_Time"] != null && row["Create_Time"].ToString() != "")
                {
                    model.Create_Time = DateTime.Parse(row["Create_Time"].ToString());
                }
                if (row["F_ID"] != null && row["F_ID"].ToString() != "")
                {
                    model.F_ID = int.Parse(row["F_ID"].ToString());
                }
                if (row["ErrorObjs"] != null)
                {
                    model.ErrorObjs = row["ErrorObjs"].ToString();
                }
                if (row["Code"] != null && row["Code"].ToString() != "")
                {
                    model.Code = int.Parse(row["Code"].ToString());
                }
                if (row["USER_TYPE"] != null)
                {
                    model.USER_TYPE = row["USER_TYPE"].ToString();
                }
                
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select CASE_SERIAL,USER_TYPE,AREA_CODE,CASE_ID,APPLICANT_NAME,APPLICANT_SEX,APPLICANT_AGE,APPLICANT_NUMBER,APPLICANT_TIME,HWY_NAME,HWY_LOGINID,CASE_TIME,CASE_TITLE,CASE_CONTENT,CASE_SOURCE,CASE_TYPE,CASE_ACCORD,CASE_ISPUBLIC,CASE_ISVISIT,CASE_ISURGENT,CASE_LNGLAT,CASE_EMOTION,CASE_ADDRESS,Create_Time,F_ID,ErrorObjs,Code ");
            strSql.Append(" FROM submit_case_info ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            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(" CASE_SERIAL,AREA_CODE,USER_TYPE,CASE_ID,APPLICANT_NAME,APPLICANT_SEX,APPLICANT_AGE,APPLICANT_NUMBER,APPLICANT_TIME,HWY_NAME,HWY_LOGINID,CASE_TIME,CASE_TITLE,CASE_CONTENT,CASE_SOURCE,CASE_TYPE,CASE_ACCORD,CASE_ISPUBLIC,CASE_ISVISIT,CASE_ISURGENT,CASE_LNGLAT,CASE_EMOTION,CASE_ADDRESS,Create_Time,F_ID,ErrorObjs,Code ");
            strSql.Append(" FROM submit_case_info ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM submit_case_info ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.F_ID desc");
            }
            strSql.Append(")AS Row, T.*  from submit_case_info T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            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 = "submit_case_info";
			parameters[1].Value = "F_ID";
			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  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}