using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace BLL.Models
{
    public class MOrderModels
    {
        public string[] str_files = { "ID", "OrderNum", "UserId", "OrderTime", "State", "Price", "Point", "Paytype", "RecName", "RecAddress", "RecZip", "RecTel", "RecMobile", "RecArieid", "Intro", "EmsType", "EmsState", "EmsNo", "EmsIntro" };
        public string[] str_filesm = { "Id", "OrderId", "Productid", "OrderTime", "SessionId", "Price", "Count", "Intro", "Point", "Content", "ProductMoreInfo" };

        /// <summary>
        /// 查询定单
        /// </summary>
        /// <param name="start"></param>
        /// <param name="limit"></param>
        /// <param name="fiter"></param>
        /// <returns></returns>
        public string AllOrder(string start, string limit, string fiter) {


            string ret = string.Empty;
            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            string str_sql = "  select top " + limit + " * from [tb_mc_order] where id not in ( select top " + start + " id from [tb_mc_order] order by id desc ) order by id desc  ";
            str_sql = start == "0" ? "  select top " + limit + " * from [tb_mc_order] order by id desc" : str_sql;
            DataTable dt = new DataTable();
            string tcount = "0";
            try
            {

                dt = db.ExecuteDataset(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sql).Tables[0];
                tcount = db.ExecuteScalar(COMM.PucConfig.ConnectionStringA, CommandType.Text, "select count(*) from [tb_mc_order]").ToString();
            }
            catch (Exception ex) { }
            BLL.JSONHelper jsons = new BLL.JSONHelper();



            foreach (DataRow dr in dt.Rows)
            {

                foreach (string s in str_files)
                {

                    jsons.AddItem(s, dr[s].ToString());
                }
                jsons.ItemOk();

            }


            jsons.totlalCount = Convert.ToInt32(tcount);
            ret = jsons.ToString();
            return ret;
        

        }
        /// <summary>
        /// 获取一条定单信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public MODEL.MOrder GetorderById(string id) {

            MODEL.MOrder ret = new MODEL.MOrder();
            return ret;
        }
        /// <summary>
        /// 取得一条信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetOrderById(string id) {

            string ret = string.Empty;
            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            string str_sql = "  select  * from [tb_mc_order] where id =" + id;
            DataTable dt = new DataTable();
            try
            {

                dt = db.ExecuteDataset(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sql).Tables[0];
            }
            catch (Exception ex) { }
                      BLL.FormJSONHelper jsons = new BLL.FormJSONHelper();
            jsons.success = true;







                foreach (string s in str_files)
                {

                    jsons.AddItem(s,dt.Rows[0][s].ToString());
                }
                jsons.ItemOk();

            ret = jsons.ToString();
            return ret;
        }
        /// <summary>
        /// 删除定单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteOrder(string id) {

           
            bool ret = false;
            string str_sql = "delete from [tb_mc_order] where id in(" + id + ")";
            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            try
            {
                db.ExecuteNonQuery(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sql);
                ret = true;
            }
            catch (Exception ex) { }
            
        
            return ret;
        }
        /// <summary>
        /// 修改定单
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool UpdateOrder(MODEL.MOrder model) {

            bool ret = false;
            string str_sql = " update  [tb_mc_order] set OrderNum='"+model.OrderNum+"', UserId="+model.Userid.ToString()+", OrderTime='"+model.OrderTime.ToString()+"', State="+model.State.ToString()+", Price="+model.Price.ToString()+", Point="+model.Point.ToString()+", Paytype="+model.Paytype.ToString()+", RecName='"+model.Paytype+"', RecAddress='"+model.RecAddress+"', RecZip='"+model.RecZip+"', RecTel='"+model.RecTel+"', RecMobile='"+model.RecMobile+"', RecArieid="+model.RecArieid.ToString()+", Intro='"+model.Intro+"', EmsType="+model.EmsType.ToString()+", EmsState="+model.EmsState.ToString()+", EmsNo='"+model.EmsNo+"', EmsIntro='"+model.EmsIntro+"'";
            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            try
            {

                db.ExecuteNonQuery(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sql);
                ret = true;
            }
            catch (Exception ex) { }

            return ret;
        }
        /// <summary>
        /// 按指定条件查询定单
        /// </summary>
        /// <param name="fiter"></param>
        /// <returns></returns>
        public DataTable AllOrderByFiter(string fiter) {

            DataTable ret = new DataTable();
            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            string str_sql = "  select  * from [tb_mc_order] where  "+fiter;
            DataTable dt = new DataTable();
            try
            {

                dt = db.ExecuteDataset(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sql).Tables[0];
            }
            catch (Exception ex) { }
            ret = dt;
            return ret;
        }
        /// <summary>
        /// 创建定单
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool CreateOrder(MODEL.MOrder model) {

            bool ret = false;
            string str_sql = " insert into  [tb_mc_order] (OrderNum, UserId, OrderTime, State, Price, Point, Paytype, RecName, RecAddress, RecZip, RecTel, RecMobile, RecArieid, Intro, EmsType, EmsState, EmsNo, EmsIntro) values ('" + model.OrderNum + "', " + model.Userid.ToString() + ", '" + model.OrderTime.ToString() + "', " + model.State.ToString() + ", " + model.Price.ToString() + "," + model.Point.ToString() + "," + model.Paytype.ToString() + ",'" + model.Paytype + "', '" + model.RecAddress + "', '" + model.RecZip + "', '" + model.RecTel + "', '" + model.RecMobile + "', " + model.RecArieid.ToString() + ", '" + model.Intro + "', " + model.EmsType.ToString() + ", " + model.EmsState.ToString() + ", '" + model.EmsNo + "', '" + model.EmsIntro + "')";

            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            try
            {

                db.ExecuteNonQuery(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sql);
                ret = true;
            }
            catch (Exception ex) { }

            return ret;
        }
        /// <summary>
        /// 插入定单
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool CreateOrderMore(MODEL.MorderMore model) {

            bool ret = false;
            string str_sql = "insert into [tb_mc_ordermore] ([OrderId],[Productid],[OrderTime],[SessionId], [Price],[Count], [Intro], [Point], [Content], [ProductMoreInfo]) values ( "+model.OrderId.ToString()+", "+model.Productid.ToString()+", '"+model.OrderTiem.ToString()+"', '"+model.SessionId.ToString()+"', "+model.Price.ToString()+", "+model.Count.ToString()+", '"+model.Intro.ToString()+"', "+model.Point.ToString()+", '"+model.Content+"', '"+model.ProductMoreInfo.ToString()+"')";
            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            try
            {

                db.ExecuteNonQuery(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sql);
                ret = true;
            }
            catch (Exception ex) { }

            return ret;
        
        }
        /// <summary>
        /// 通过条件获取定单信息
        /// </summary>
        /// <param name="fiter"></param>
        /// <returns></returns>
        public DataTable GetOrderByFiter(string fiter) {

            DataTable dt = new DataTable();

            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            string str_sql = "  select  * from [tb_mc_order] where " + fiter;
            try
            {

                dt = db.ExecuteDataset(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sql).Tables[0];
            }
            catch (Exception ex) { }

            return dt;
        }
        /// <summary>
        /// 获得更多定单
        /// </summary>
        /// <param name="fiter"></param>
        /// <returns></returns>
        public DataTable GetOrderMoreByFiter(string fiter)
        {

            DataTable dt = new DataTable();

            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            string str_sql = "  select  * from [tb_mc_order_more] [tb_mc_product] where tb_mc_order_more.productid=tb_mc_product.id and " + fiter;
            try
            {

                dt = db.ExecuteDataset(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sql).Tables[0];
            }
            catch (Exception ex) { }

            return dt;
        }
        /// <summary>
        /// 获取定单详细信息
        /// </summary>
        /// <param name="fiter"></param>
        /// <returns></returns>
        public DataTable GetOrderMoreById(string id)
        {

            DataTable dt = new DataTable();

            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            string str_sql = "  select  * from [tb_mc_ordermore]  where orderid=" + id;
            try
            {

                dt = db.ExecuteDataset(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sql).Tables[0];
            }
            catch (Exception ex) { }

            return dt;
        }
        /// <summary>
        /// 取得定单号码
        /// </summary>
        /// <returns></returns>
        public string GetOrderNumbernew(){

            string tempstr = DateTime.Now.ToString("yyyyMMddhhmmss");
            Random rnd = new Random();
            tempstr = tempstr + rnd.Next(100000).ToString()+rnd.Next(100000,999999).ToString();
            return tempstr;
        }
    }
}
