using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;

namespace BRServer.DAL
{
    public class BR : IDAL.IBR
    {
        public static readonly string ConnectionStringOrderDistributedTransaction =
            System.Configuration.ConfigurationManager.AppSettings["MySqlConnString"];

        // Static constants
        private const string TABLE_NAME = "BRGDF_DATA";

        private const string COLUMN_DEVCODE = "DEVCODE";
        private const string COLUMN_LOGTIME = "LOGTIME";
        private const string COLUMN_CELL = "CELL";
        private const string COLUMN_VALUE = "OPENING";
        private const string COLUMN_UPTIME = "UPTIME";

        private const string PARM_DEVCODE = "@DEVCODE";
        private const string PARM_LOGTIME = "@LOGTIME";
        private const string PARM_CELL = "@CELL";
        private const string PARM_VALUE = "@OPENING";
        private const string PARM_UPTIME = "@UPTIME";

        private const string SQL_INSERT_CJ = "INSERT INTO " + TABLE_NAME
                        + " (" + COLUMN_DEVCODE + ","
                        + " " + COLUMN_LOGTIME + ","
                        + " " + COLUMN_CELL + ","
                        + " " + COLUMN_VALUE + ","
                        + " " + COLUMN_UPTIME + ")"
                        + " VALUES "
                        + " (" + PARM_DEVCODE + ","
                        + " " + PARM_LOGTIME + ","
                        + " " + PARM_CELL + ","
                        + " " + PARM_VALUE + ","
                        + " " + PARM_UPTIME + ")";

        
        public void insert(List<Model.BR> brs)
        {
            MySqlTransaction tran = null;
            using (MySqlConnection conn = new MySqlConnection(ConnectionStringOrderDistributedTransaction))
            {
                try
                {
                    conn.Open();
                    tran = conn.BeginTransaction();

                    foreach (Model.BR br in brs)
                    {
                        if (string.IsNullOrEmpty(br.DEVCODE))
                        {
                            throw new Exception("设备ID为空！");
                        }

                        MySqlParameter[] parms = GetAdapterParameters();
                        SetAdapterParameters(parms, br);

                        MySqlCommand cmd = new MySqlCommand(SQL_INSERT_CJ, conn);
                        foreach (MySqlParameter pram in parms)
                            cmd.Parameters.Add(pram);
                        cmd.ExecuteNonQuery();
                    }

                    tran.Commit();
                }
                catch (Exception e)
                {
                    if (null != tran)
                    {
                        tran.Rollback();
                    }
                    throw (e);
                }
            }
        }

        /**
        *sequeceName:需要更新的序列名称
        *key:主键字段名称
        *tableName:表名称
        */
        public void updateSequence(String sequeceName)
        {
            MySqlTransaction tran = null;
            using (MySqlConnection conn = new MySqlConnection(ConnectionStringOrderDistributedTransaction))
            {
                try
                {
                    conn.Open();
                    tran = conn.BeginTransaction();

                    String SQL_DROP_SEQUENCE = "ALTER TABLE " + TABLE_NAME +" DROP "+ sequeceName;
                    MySqlCommand cmd1 = new MySqlCommand(SQL_DROP_SEQUENCE, conn);
                    cmd1.ExecuteNonQuery();

                    String SQL_ADD_SEQUENC = "ALTER TABLE " + TABLE_NAME + " ADD " + sequeceName +
                        " INT UNSIGNED NOT NULL AUTO_INCREMENT FIRST, ADD PRIMARY KEY (" + sequeceName + ")";

                    MySqlCommand cmd2 = new MySqlCommand(SQL_ADD_SEQUENC, conn);
                    cmd2.ExecuteNonQuery();

                    tran.Commit();
                }
                catch (Exception e)
                {
                    if (null != tran)
                    {
                        tran.Rollback();
                    }
                    throw (e);
                }
            }
        }




        /// <summary>
        /// An internal function to get the database parameters
        /// </summary>
        /// <returns>Parameter array</returns>
        private static MySqlParameter[] GetAdapterParameters()
        {
            MySqlParameter[] parms = null;//OracleHelperParameterCache.GetCachedParameterSet(SQL_INSERT_WS, "INSERT:UPDATE");

            if (parms == null)
            {
                parms = new MySqlParameter[]{					                            
                                        new MySqlParameter(PARM_DEVCODE, MySqlDbType.VarChar, 255),                                        
                                        new MySqlParameter(PARM_LOGTIME, MySqlDbType.DateTime),
                                        new MySqlParameter(PARM_CELL, MySqlDbType.VarChar,255),
                                        new MySqlParameter(PARM_VALUE, MySqlDbType.Float,20),
                                        new MySqlParameter(PARM_UPTIME, MySqlDbType.DateTime)
                };
            }

            return parms;
        }


        /// <summary>
        /// An internal function to bind values parameters for insert
        /// </summary>
        /// <param name="parms">Database parameters</param>
        /// <param name="RQ">Values to bind to parameters</param>
        private void SetAdapterParameters(MySqlParameter[] parms, Model.BR br)
        {
            parms[0].Value = br.DEVCODE;

            if (null != br.LOGTIME)
            {
                parms[1].Value = br.LOGTIME;
            }
            else
            {
                parms[1].Value = DBNull.Value;
            }

            if (null != br.CELL)
            {
                parms[2].Value = br.CELL;
            }
            else
            {
                parms[2].Value = DBNull.Value;
            }

            if (null != br.OPENING)
            {
                parms[3].Value = br.OPENING;
            }
            else
            {
                parms[3].Value = DBNull.Value;
            }

            if (null != br.UPTIME)
            {
                parms[4].Value = br.UPTIME;
            }
            else
            {
                parms[4].Value = DBNull.Value;
            }
        }
    }
}
