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

namespace BLL.Models
{
    public class MPayMModels
    {
        public string[] str_files = { "ID", "Name", "PayUserName", "PayPass", "PayKey", "PayKeyMore", "Intro", "Flag" };
        /// <summary>
        /// 添加支付方式
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool AddPay(MODEL.MPar model)
        {

            bool ret = false;
            string str_sql = "inster into [tb_mc_pay] (Name, PayUserName, PayPass, PayKey, PayKeyMore, Intro, Flag)value( '" + model.Name + "','" + model.PayUserName + "' ,'" + model.PayPass + "' ,'" + model.PayKey + "','" + model.PayKeyMore + "','" + model.Intro + "'," + model.Flag.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="model"></param>
        /// <returns></returns>
        public bool UpdatePay(MODEL.MPar model)
        {

            bool ret = false;
            string str_sql = "update [tb_mc_pay] set Name='"+model.Name+"', PayUserName='"+model.PayUserName+"', PayPass='"+model.PayPass+"', PayKey='"+model.PayKey+"', PayKeyMore='"+model.PayKeyMore+"', Intro='"+model.Intro+"', Flag="+model.Flag.ToString()+" where id="+model.Id.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="id"></param>
        /// <returns></returns>
        public bool DelPay(string id)
        {

            bool ret = false;
            string str_sql = " delete from [tb_mc_pay] 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="start"></param>
        /// <param name="limit"></param>
        /// <param name="fiter"></param>
        /// <returns></returns>
        public string AllPay(string start, string limit, string fiter)
        {

            string ret = string.Empty;
            string str_sql = "select top " + limit + " * from [tb_mc_pay] where id not in( select " + start + " id from [tb_mc_pay] order by id desc ) order by id desc";
            if (start == "0")
            {

                str_sql = " select top " + limit + " * from [tb_mc_pay] order by id desc ";
            }
            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            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_brand] ").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 string GetPayById(string id)
        {

            string ret = string.Empty;
            string str_sql = "select * from [tb_mc_pay] where id=" + id;
            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            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 (DataRow dr in dt.Rows)
            {

                foreach (string s in str_files)
                {

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

            }



            ret = jsons.ToString();
            return ret;
        }
        /// <summary>
        /// 获得支付信息通过条件
        /// </summary>
        /// <returns></returns>
        public DataTable GetPayInfoByFiter(string fiter)
        {

            DataTable dt = new DataTable();

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

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

            return dt;
        }
        /// <summary>
        /// 支付名称
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetPayNameById(string id) {

            string ret = string.Empty;
            DataTable dt = new DataTable();
            dt = GetPayInfoByFiter( " id= "+id);
            ret = dt.Rows[0]["name"].ToString();
            return ret;
        }
    }
}
