using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Collections.Generic;
using DSWeb.Models;
using WebSqlHelper;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

namespace DSWeb.EntityDA
{
    public class CRMClientQuotationDetailDA
    {
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(CRMClientQuotationDetailEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into crm_quotation_detail(");
            strSql.Append("GID,LINKGID,FEETYPE,FEENAME,UNIT,UNITPRICE,CURRENCY,CARRIER,REMARK,CREATEUSER,CREATETIME,MODIFIEDUSER,MODIFIEDTIME");
            strSql.Append(") values (");
            strSql.Append("@GID,@LINKGID,@FEETYPE,@FEENAME,@UNIT,@UNITPRICE,@CURRENCY,@CARRIER,@REMARK,@CREATEUSER,getdate(),@MODIFIEDUSER,getdate()");
            strSql.Append(") ");

            SqlParameter[] parameters = {
			            new SqlParameter("@GID", SqlDbType.VarChar,36) ,            
                        new SqlParameter("@LINKGID", SqlDbType.VarChar,100) ,            
                        new SqlParameter("@FEETYPE", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@FEENAME", SqlDbType.VarChar,100) ,            
                        new SqlParameter("@UNIT", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@UNITPRICE", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@CURRENCY", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@CARRIER", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@REMARK", SqlDbType.VarChar,200) ,
                        new SqlParameter("@MODIFIEDUSER", SqlDbType.VarChar,36)
              
            };

            parameters[0].Value = model.GID;
            parameters[1].Value = model.LINKGID;
            parameters[2].Value = model.FEETYPE;
            parameters[3].Value = model.FEENAME;
            parameters[4].Value = model.UNIT;
            parameters[5].Value = model.UNITPRICE;
            parameters[6].Value = model.CURRENCY;
            parameters[7].Value = model.CARRIER;
            parameters[8].Value = model.REMARK;
            parameters[9].Value = model.CREATEUSER;
            parameters[10].Value = model.MODIFIEDUSER;

            //
            int iResult = 0;
            using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction))
            {
                int existVal = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, strSql.ToString(), parameters);
                if (existVal > 0)
                {
                    iResult = 1;
                    //
                    //string str0 = "insert into sys_log(NAME,LOGTYPE,LOGCONTENT,CREATEUSER) values('增加信息','增加操作','GID=" + model.GID.ToString() + "','" + model.MODIFIEDUSER.ToString() + "')";
                    //bool bl0 = SqlHelper.ExecuteSqlCommand(SqlHelper.ConnectionStringLocalTransaction, str0);
                }
                else
                {
                    iResult = -1;//执行异常
                }
            }
            return iResult;
        }


        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update(CRMClientQuotationDetailEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update crm_quotation_detail set ");
            strSql.Append(" LINKGID = @LINKGID , ");
            strSql.Append(" FEETYPE = @FEETYPE , ");
            strSql.Append(" FEENAME = @FEENAME , ");
            strSql.Append(" UNIT = @UNIT , ");
            strSql.Append(" UNITPRICE = @UNITPRICE , ");
            strSql.Append(" CURRENCY = @CURRENCY , ");
            strSql.Append(" CARRIER = @CARRIER , ");
            strSql.Append(" REMARK = @REMARK , ");
            strSql.Append(" MODIFIEDUSER = @MODIFIEDUSER , ");
            strSql.Append(" MODIFIEDTIME = getdate() ");
            strSql.Append(" where GID=@GID");

            SqlParameter[] parameters = {
			            new SqlParameter("@GID", SqlDbType.VarChar,36) ,            
                        new SqlParameter("@LINKGID", SqlDbType.VarChar,100) ,            
                        new SqlParameter("@FEETYPE", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@FEENAME", SqlDbType.VarChar,100) ,            
                        new SqlParameter("@UNIT", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@UNITPRICE", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@CURRENCY", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@CARRIER", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@REMARK", SqlDbType.VarChar,200) ,            
                        new SqlParameter("@MODIFIEDUSER", SqlDbType.VarChar,36)
              
            };

            parameters[0].Value = model.GID;
            parameters[1].Value = model.LINKGID;
            parameters[2].Value = model.FEETYPE;
            parameters[3].Value = model.FEENAME;
            parameters[4].Value = model.UNIT;
            parameters[5].Value = model.UNITPRICE;
            parameters[6].Value = model.CURRENCY;
            parameters[7].Value = model.CARRIER;
            parameters[8].Value = model.REMARK;
            parameters[9].Value = model.MODIFIEDUSER;
            //
            int iResult = 0;
            using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocalTransaction))
            {
                int existVal = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, strSql.ToString(), parameters);
                if (existVal > 0)
                {
                    iResult = 1;
                    //
                    //string str0 = "insert into sys_log(NAME,LOGTYPE,LOGCONTENT,CREATEUSER) values('更新信息表','更新操作','GID=" + model.GID.ToString() + "','" + model.MODIFIEDUSER.ToString()  + "')";
                    //bool bl0 = SqlHelper.ExecuteSqlCommand(SqlHelper.ConnectionStringLocalTransaction, str0);
                }
                else
                {
                    iResult = -1;//执行异常
                }
            }
            return iResult;
        }


        /// <summary>
        /// 删除一条数据
        /// </summary>
        public int Delete(string GID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from crm_quotation_detail ");
            strSql.Append(" where GID=@GID");
            SqlParameter[] parameters = {
					new SqlParameter("@GID", SqlDbType.VarChar,36)};
            parameters[0].Value = GID;
            
            //
            int iResult = 0;
            using (SqlTransaction sqlTran = SqlHelper.BeginTransaction(SqlHelper.ConnectionStringLocalTransaction))
            {
                try
                {
                    SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, strSql.ToString(), parameters);
                    iResult = 1;//状态为1表示删除成功
                    sqlTran.Commit();
                }
                catch (Exception execError)
                {
                    iResult = -1;//有异常,删除失败
                    sqlTran.Rollback();
                    iResult = -2;//插入异常,事务已回滚成功
                    throw execError;
                }
                finally
                {
                    SqlHelper.CloseConnection();
                }
            }
            return iResult;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CRMClientQuotationDetailEntity GetModel(string GID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select GID, LINKGID, FEETYPE, FEENAME, UNIT, UNITPRICE, CURRENCY, CARRIER, REMARK,CREATEUSER,CREATETIME,MODIFIEDUSER,MODIFIEDTIME ");
            strSql.Append("  from crm_quotation_detail ");
            strSql.Append(" where GID=@GID");
            SqlParameter[] parameters = {
					new SqlParameter("@GID", SqlDbType.VarChar,36)};
            parameters[0].Value = GID;

            CRMClientQuotationDetailEntity model = new CRMClientQuotationDetailEntity();
            DataSet ds = SqlHelper.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                model.GID = ds.Tables[0].Rows[0]["GID"].ToString();
                model.LINKGID = ds.Tables[0].Rows[0]["LINKGID"].ToString();
                model.FEETYPE = ds.Tables[0].Rows[0]["FEETYPE"].ToString();
                model.FEENAME = ds.Tables[0].Rows[0]["FEENAME"].ToString();
                model.UNIT = ds.Tables[0].Rows[0]["UNIT"].ToString();
                if (ds.Tables[0].Rows[0]["UNITPRICE"].ToString() != "")
                {
                    model.UNITPRICE = decimal.Parse(ds.Tables[0].Rows[0]["UNITPRICE"].ToString());
                }
                model.CURRENCY = ds.Tables[0].Rows[0]["CURRENCY"].ToString();
                model.CARRIER = ds.Tables[0].Rows[0]["CARRIER"].ToString();
                model.REMARK = ds.Tables[0].Rows[0]["REMARK"].ToString();
                model.CREATEUSER = ds.Tables[0].Rows[0]["CREATEUSER"].ToString();
                if (ds.Tables[0].Rows[0]["CREATETIME"].ToString() != "")
                {
                    model.CREATETIME = DateTime.Parse(ds.Tables[0].Rows[0]["CREATETIME"].ToString());
                }
                model.MODIFIEDUSER = ds.Tables[0].Rows[0]["MODIFIEDUSER"].ToString();
                if (ds.Tables[0].Rows[0]["MODIFIEDTIME"].ToString() != "")
                {
                    model.MODIFIEDTIME = DateTime.Parse(ds.Tables[0].Rows[0]["MODIFIEDTIME"].ToString());
                }

                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CRMClientQuotationDetailEntity GetModelLINKGID(string LINKGID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select GID, LINKGID, FEETYPE, FEENAME, UNIT, UNITPRICE, CURRENCY, CARRIER, REMARK,CREATEUSER,CREATETIME,MODIFIEDUSER,MODIFIEDTIME ");
            strSql.Append("  from crm_quotation_detail ");
            strSql.Append(" where LINKGID=@LINKGID");
            SqlParameter[] parameters = {
					new SqlParameter("@LINKGID", SqlDbType.VarChar,100)};
            parameters[0].Value = LINKGID;

            CRMClientQuotationDetailEntity model = new CRMClientQuotationDetailEntity();
            DataSet ds = SqlHelper.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                model.GID = ds.Tables[0].Rows[0]["GID"].ToString();
                model.LINKGID = ds.Tables[0].Rows[0]["LINKGID"].ToString();
                model.FEETYPE = ds.Tables[0].Rows[0]["FEETYPE"].ToString();
                model.FEENAME = ds.Tables[0].Rows[0]["FEENAME"].ToString();
                model.UNIT = ds.Tables[0].Rows[0]["UNIT"].ToString();
                if (ds.Tables[0].Rows[0]["UNITPRICE"].ToString() != "")
                {
                    model.UNITPRICE = decimal.Parse(ds.Tables[0].Rows[0]["UNITPRICE"].ToString());
                }
                model.CURRENCY = ds.Tables[0].Rows[0]["CURRENCY"].ToString();
                model.CARRIER = ds.Tables[0].Rows[0]["CARRIER"].ToString();
                model.REMARK = ds.Tables[0].Rows[0]["REMARK"].ToString();
                model.CREATEUSER = ds.Tables[0].Rows[0]["CREATEUSER"].ToString();
                if (ds.Tables[0].Rows[0]["CREATETIME"].ToString() != "")
                {
                    model.CREATETIME = DateTime.Parse(ds.Tables[0].Rows[0]["CREATETIME"].ToString());
                }
                model.MODIFIEDUSER = ds.Tables[0].Rows[0]["MODIFIEDUSER"].ToString();
                if (ds.Tables[0].Rows[0]["MODIFIEDTIME"].ToString() != "")
                {
                    model.MODIFIEDTIME = DateTime.Parse(ds.Tables[0].Rows[0]["MODIFIEDTIME"].ToString());
                }

                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM crm_quotation_detail ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            //
            DataSet DS = SqlHelper.ExecuteDataset(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, strSql.ToString());
            try
            {
                if (DS.Tables[0].Rows.Count <= 0)
                {
                    DS = null;
                }
            }
            catch
            {
                DS = null;
            }
            return DS;
        }

        #region 获取SQL语句查询数据集
        /// <summary>
        /// 获取SQL语句查询数据集
        /// </summary>
        /// <param name="strSql"></param>
        /// <returns></returns>
        public DataSet GetExcuteSql(string strSql)
        {
            DataSet tempSet = new DataSet();
            tempSet = SqlHelper.ExecuteDataset(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, strSql);
            return tempSet;
        }
        #endregion
    }
}