using System;
using System.Collections;
using System.Configuration;
using System.Data;
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 DSWeb.Models;
using DSWeb.EntityDA;
using System.Text;
using System.Text.RegularExpressions;
using DSWeb.ConfigManager;
using System.Collections.Generic;

namespace DSWeb.cheque
{
    public partial class ChequeBooksGridSource : System.Web.UI.Page
    {
        private string strHandle;
        private int iCurrentPage;//当前页数
        private int iShowPage;//显示最大页数
        private string strSearch;//查询搜索条件
        private string strCHEQUENUMSTART;//支票号码起始值
        private string strCHEQUENUMEND;//支票号码结束值
        private int CHEQUETOTAL;//支票总数
        private string strPostInfo;
        private string strBookID;//支票册编号

        UnicodeEncoding unicode = new UnicodeEncoding();
        ChequeBooksDA ChequeBooksDA = new ChequeBooksDA();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["handle"] != null)
            {
                strHandle = Request.QueryString["handle"].ToString();
            }

            if (Request.QueryString["cur_page"] != null)
            {
                iCurrentPage = int.Parse(Request.QueryString["cur_page"].ToString().Trim());
            }
            else
            {
                iCurrentPage = 0;
            }
            if (Request.QueryString["show_page"] != null)
            {
                iShowPage = int.Parse(Request.QueryString["show_page"].ToString().Trim());
            }
            else
            {
                iShowPage = 0;
            }

            if (Request.QueryString["search"] != null)
            {
                strSearch = unicode.GetString(unicode.GetBytes(Regex.Unescape(Request.QueryString["search"].ToString())));
            }
            if (Request.QueryString["startnum"] != null)
            {
                strCHEQUENUMSTART = Request.QueryString["startnum"].ToString();
            }
            if (Request.QueryString["endnum"] != null)
            {
                strCHEQUENUMEND = Request.QueryString["endnum"].ToString();
            }

            if (Request.QueryString["totalnum"] != null)
            {
                CHEQUETOTAL = int.Parse(Request.QueryString["totalnum"].ToString());
            }
            else
            {
                CHEQUETOTAL = 0;
            }

            if (Request.QueryString["post"] != null)
            {
                strPostInfo = unicode.GetString(unicode.GetBytes(Regex.Unescape(Request.QueryString["post"].ToString())));
            }

            if (Request.QueryString["bookid"] != null)
            {
                strBookID = Request.QueryString["bookid"].ToString();
            }


            if (strHandle != null)
            {
                if (strHandle.Equals("booklist"))
                {
                    Response.Write(GetChequeBookList());
                }

                if (strHandle.Equals("booklistpage"))
                {
                    Response.Write(GetBookListPageCount().ToString());
                }

                if (strHandle.Equals("itemlist") && strBookID != null)
                {
                    Response.Write(GetChequeBookItemList());
                }

                if (strHandle.Equals("itemlistpage"))
                {
                    Response.Write(GetBookItemListPageCount().ToString());
                }

                if (strHandle.Equals("validCheque") && strCHEQUENUMSTART != null && strCHEQUENUMEND != null)
                {
                    Response.Write(ValidateChequeNum(strCHEQUENUMSTART, strCHEQUENUMEND).ToString());
                }

                if (strHandle.Equals("createCheque") && strPostInfo != null)
                {
                    Response.Write(CreateCheque());
                }

                if (strHandle.Equals("deletebook") && strBookID != null)
                {

                }
            }
        }

        /// <summary>
        /// 生成支票
        /// </summary>
        /// <returns></returns>
        private string CreateCheque()
        {
            int iResult = 0;
            ChequeBooksDA ChequeBooksDA = new ChequeBooksDA();
            ChequeBooksEntity ChequeBooksEntity = new ChequeBooksEntity();
            StringBuilder resultBuilder = new StringBuilder();
            ChequeBooksEntity.GID = Guid.NewGuid().ToString();

            if (strPostInfo != null)
            {
                if (!strPostInfo.Trim().Equals(""))
                {
                    string tempSearch = strPostInfo;
                    tempSearch = tempSearch.Replace("{", "");
                    tempSearch = tempSearch.Replace("}", "");
                    tempSearch = tempSearch.Replace("[", "");
                    tempSearch = tempSearch.Replace("]", "");

                    string[] searchArg = tempSearch.Split(new char[] { ',' });

                    for (int i = 0; i < searchArg.Length; i++)
                    {
                        string[] strArg = searchArg[i].Split(new char[] { ':' });

                        if (!strArg[1].Replace("\"", "").Trim().Equals(""))
                        {
                            switch (strArg[0].Replace("\"", ""))
                            {
                                case "name"://Customer
                                    if (!strArg[1].Replace("\"", "").Trim().Equals(""))
                                    {
                                        ChequeBooksEntity.Name = strArg[1].Replace("\"", "").Trim();
                                    }
                                    break;
                                case "desp"://BillNO
                                    if (!strArg[1].Replace("\"", "").Trim().Equals(""))
                                    {
                                        ChequeBooksEntity.Description = strArg[1].Replace("\"", "").Trim();
                                    }
                                    break;
                                case "type"://ETD BeginDate
                                    if (!strArg[1].Replace("\"", "").Trim().Equals(""))
                                    {
                                        ChequeBooksEntity.Type = int.Parse(strArg[1].Replace("\"", "").Trim());
                                    }
                                    break;
                                case "code"://ETD EndDate
                                    if (!strArg[1].Replace("\"", "").Trim().Equals(""))
                                    {
                                        ChequeBooksEntity.CHEQUECODE = strArg[1].Replace("\"", "").Trim();
                                    }
                                    break;
                                case "start"://ETD EndDate
                                    if (!strArg[1].Replace("\"", "").Trim().Equals(""))
                                    {
                                        ChequeBooksEntity.CHEQUENUMSTART = strArg[1].Replace("\"", "").Trim();
                                    }
                                    break;
                                case "end"://ETD EndDate
                                    if (!strArg[1].Replace("\"", "").Trim().Equals(""))
                                    {
                                        ChequeBooksEntity.CHEQUENUMEND = strArg[1].Replace("\"", "").Trim();
                                    }
                                    break;
                                case "comp"://ETD EndDate
                                    if (!strArg[1].Replace("\"", "").Trim().Equals(""))
                                    {
                                        ChequeBooksEntity.CompanyID = strArg[1].Replace("\"", "").Trim();
                                    }
                                    break;
                                case "total"://ETD EndDate
                                    if (!strArg[1].Replace("\"", "").Trim().Equals(""))
                                    {
                                        ChequeBooksEntity.CHEQUETOTAL = int.Parse(strArg[1].Replace("\"", "").Trim());
                                    }
                                    break;
                                case "creator"://ETD EndDate
                                    if (!strArg[1].Replace("\"", "").Trim().Equals(""))
                                    {
                                        ChequeBooksEntity.CreateUser = strArg[1].Replace("\"", "").Trim();
                                    }
                                    break;
                                case "CURRENCY"://ETD EndDate
                                    if (!strArg[1].Replace("\"", "").Trim().Equals(""))
                                    {
                                        ChequeBooksEntity.CURRENCY = strArg[1].Replace("\"", "").Trim();
                                    }
                                    break;
                                default:
                                    break;
                            }
                        }
                    }
                }
            }

            string strHead = "";
            int iStartCount = 0;
            int iEndCount = 0;
            int iStartNum = 0;
            int iEndNum = 0;
            int iTotalCount = 0;

            for (int i = 0; i < ChequeBooksEntity.CHEQUENUMSTART.Length; i++)
            {
                //将支票起始值进行过滤,将开头为0的字符过滤掉
                if (ChequeBooksEntity.CHEQUENUMSTART[i].ToString().Equals("0"))
                {
                    iStartCount++;
                }
                else
                {
                    break;
                }
            }

            iStartNum = int.Parse(ChequeBooksEntity.CHEQUENUMSTART.Substring(iStartCount, ChequeBooksEntity.CHEQUENUMSTART.Length - 1));

            if (!ChequeBooksEntity.CHEQUENUMEND.Equals(""))
            {
                for (int j = 0; j < ChequeBooksEntity.CHEQUENUMEND.Length; j++)
                {
                    //将支票起始值进行过滤,将开头为0的字符过滤掉
                    if (ChequeBooksEntity.CHEQUENUMEND[j].ToString().Equals("0"))
                    {
                        iEndCount++;
                    }
                    else
                    {
                        break;
                    }
                }

                iEndNum = int.Parse(ChequeBooksEntity.CHEQUENUMEND.Substring(iEndCount, ChequeBooksEntity.CHEQUENUMEND.Length - 1));

                iTotalCount = iEndNum - iStartNum + 1;
            }
            else
            {
                iTotalCount = ChequeBooksEntity.CHEQUETOTAL;
            }
            IList<ChequeItemsEntity> ChequeItemsEntities = new List<ChequeItemsEntity>();

            if (iStartCount > 0)
            {
                strHead = ChequeBooksEntity.CHEQUENUMSTART.Substring(0, iStartCount);
            }
            for (int i = 0; i < iTotalCount; i++)
            {
                ChequeItemsEntity ChequeItemsEntity = new ChequeItemsEntity();

                ChequeItemsEntity.GID = Guid.NewGuid().ToString();
                ChequeItemsEntity.BookID = ChequeBooksEntity.GID;
                ChequeItemsEntity.ChequeNum = strHead + (iStartNum + i).ToString();
                ChequeItemsEntity.IsLock = false;
                ChequeItemsEntity.IsDelete = false;
                ChequeItemsEntity.IsMakeOut = false;
                ChequeItemsEntity.CreateUser = ChequeBooksEntity.CreateUser;

                ChequeItemsEntities.Add(ChequeItemsEntity);
            }

            if (ChequeItemsEntities.Count > 0)
            {
                if (ChequeBooksEntity.CHEQUENUMEND == "")
                {
                    ChequeBooksEntity.CHEQUENUMEND = ChequeItemsEntities[ChequeItemsEntities.Count - 1].ChequeNum;
                }
                ChequeBooksEntity.CHEQUETOTAL = iTotalCount;
                ChequeBooksEntity.IsDelete = false;
                ChequeBooksEntity.IsLock = false;
                ChequeBooksEntity.ChequeItemsEntities = ChequeItemsEntities;
                //开始生成支票
                iResult = ChequeBooksDA.InsertChequeBook(ChequeBooksEntity);
            }

            if (iResult > 0)
            {
                resultBuilder.Append("{");
                resultBuilder.Append("\"result\":");
                resultBuilder.Append("[");
                resultBuilder.Append("{");

                resultBuilder.Append("\"result\":" + iResult.ToString());

                resultBuilder.Append("}");
                resultBuilder.Append("]");
                resultBuilder.Append("}");
            }
            return resultBuilder.ToString();
        }

        #region 验证生成支票输入值
        /// <summary>
        /// 验证生成支票输入值
        /// </summary>
        /// <param name="tempStartValue">支票起始号码</param>
        /// <param name="tempEndValue">支票结束号码</param>
        /// <returns>值1表示验证通过 值不等于1表示验证失败</returns>
        private int ValidateChequeNum(string tempStartValue, string tempEndValue)
        {
            int iResult = 0;
            int iStartCount = 0;
            int iEndCount = 0;
            int iStartNum = 0;
            int iEndNum = 0;
            for (int i = 0; i < tempStartValue.Length; i++)
            {
                //将支票起始值进行过滤,将开头为0的字符过滤掉
                if (tempStartValue[i].ToString().Equals("0"))
                {
                    iStartCount++;
                }
                else
                {
                    break;
                }
            }

            iStartNum = int.Parse(tempStartValue.Substring(iStartCount, tempStartValue.Length - 1));

            for (int j = 0; j < tempEndValue.Length; j++)
            {
                //将支票起始值进行过滤,将开头为0的字符过滤掉
                if (tempEndValue[j].ToString().Equals("0"))
                {
                    iEndCount++;
                }
                else
                {
                    break;
                }
            }

            iEndNum = int.Parse(tempEndValue.Substring(iEndCount, tempEndValue.Length - 1));

            if (iStartCount != iEndCount)
            {
                iResult = -1;//验证错误,支票号码长度错误
            }
            else if (iStartNum >= iEndNum)
            {
                iResult = -2;//支票起始值不能大于等于支票结束值,输入错误
            }
            else if (CHEQUETOTAL > 0)
            {
                if ((iEndNum - iStartNum) > CHEQUETOTAL)
                {
                    iResult = -3;//支票总数与起始结束值不相符
                }
                else
                {
                    iResult = 1;
                }
            }
            else
            {
                //??????
                int iInvoiceBookTotal = 0;
                //查看是否超过发票最大行数
                object configObj = ConfigManager.ConfigManager.Instance().GetConfigInfo(ConfigType.INVOICECONFIG);
                IList<InvoiceConfigEntity> invoiceConfigEntities = new List<InvoiceConfigEntity>();
                invoiceConfigEntities = (IList<InvoiceConfigEntity>)configObj;

                if (invoiceConfigEntities.Count > 0)
                {
                    foreach (InvoiceConfigEntity invoicConfigEntity in invoiceConfigEntities)
                    {
                        if (invoicConfigEntity.ItemName.ToLower().Equals("booktotal"))
                        {
                            iInvoiceBookTotal = int.Parse(invoicConfigEntity.ItemValue);
                            break;
                        }
                    }
                }

                if ((iEndNum - iStartNum) > iInvoiceBookTotal)
                {
                    iResult = -4;//录入的发票起始和结束间值超过发票册最大发票数值
                }
                else
                {
                    iResult = 1;
                }
                //??????
            }
            return iResult;
        }
        #endregion

        #region 获取支票册总页数
        /// <summary>
        /// 获取支票册总页数
        /// </summary>
        /// <returns>返回总页数</returns>
        private int GetBookListPageCount()
        {
            ChequeBooksDA ChequeBooksDA = new ChequeBooksDA();
            string strSql = "SELECT COUNT(*) FROM ch_fee_chequebooks WHERE 1 > 0 ";

            string strCondition = "";
            if (strSearch != null)
            {
                if (!strSearch.Trim().Equals(""))
                {
                    string tempSearch = strSearch;
                    tempSearch = tempSearch.Replace("{", "");
                    tempSearch = tempSearch.Replace("}", "");
                    tempSearch = tempSearch.Replace("[", "");
                    tempSearch = tempSearch.Replace("]", "");

                    string[] searchArg = tempSearch.Split(new char[] { ',' });

                    for (int i = 0; i < searchArg.Length; i++)
                    {
                        string[] strArg = searchArg[i].Split(new char[] { ':' });

                        if (!strArg[1].Replace("\"", "").Trim().Equals(""))
                        {
                            switch (strArg[0].Replace("\"", ""))
                            {
                                case "btime"://Customer
                                    strCondition += String.Format(" AND convert(char(10),APPLYTIME,120) >= '{0}' ", strArg[1].Replace("\"", ""));
                                    break;
                                case "etime"://BillNO
                                    strCondition += String.Format(" AND convert(char(10),APPLYTIME,120) <= '{0}' ", strArg[1].Replace("\"", ""));
                                    break;
                                case "cus"://ETD BeginDate
                                    strCondition += String.Format(" AND CUSTOMERNAME LIKE  '%{0}%' ", strArg[1].Replace("\"", ""));
                                    break;
                                case "no"://ETD EndDate
                                    strCondition += String.Format(" AND BILLNO LIKE '%{0}%'", strArg[1].Replace("\"", ""));
                                    break;
                                default:
                                    break;
                            }
                        }
                    }

                    strSql += strCondition;
                }
            }

            int iTotal = int.Parse(ChequeBooksDA.GetExcuteSql(strSql).Tables[0].Rows[0][0].ToString());

            return iTotal;
        }
        #endregion

        #region 获取支票总页数
        /// <summary>
        /// 获取支票总页数
        /// </summary>
        /// <returns>返回总页数</returns>
        private int GetBookItemListPageCount()
        {
            ChequeBooksDA ChequeBooksDA = new ChequeBooksDA();
            string strSql = "SELECT COUNT(*) FROM ch_fee_Chequeitems WHERE BOOKID = '" + strBookID + "'";

            string strCondition = "";
            if (strSearch != null)
            {
                if (!strSearch.Trim().Equals(""))
                {
                    string tempSearch = strSearch;
                    tempSearch = tempSearch.Replace("{", "");
                    tempSearch = tempSearch.Replace("}", "");
                    tempSearch = tempSearch.Replace("[", "");
                    tempSearch = tempSearch.Replace("]", "");

                    string[] searchArg = tempSearch.Split(new char[] { ',' });

                    for (int i = 0; i < searchArg.Length; i++)
                    {
                        string[] strArg = searchArg[i].Split(new char[] { ':' });

                        if (!strArg[1].Replace("\"", "").Trim().Equals(""))
                        {
                            switch (strArg[0].Replace("\"", ""))
                            {
                                case "btime"://Customer
                                    strCondition += String.Format(" AND convert(char(10),APPLYTIME,120) >= '{0}' ", strArg[1].Replace("\"", ""));
                                    break;
                                case "etime"://BillNO
                                    strCondition += String.Format(" AND convert(char(10),APPLYTIME,120) <= '{0}' ", strArg[1].Replace("\"", ""));
                                    break;
                                case "cus"://ETD BeginDate
                                    strCondition += String.Format(" AND CUSTOMERNAME LIKE  '%{0}%' ", strArg[1].Replace("\"", ""));
                                    break;
                                case "no"://ETD EndDate
                                    strCondition += String.Format(" AND BILLNO LIKE '%{0}%'", strArg[1].Replace("\"", ""));
                                    break;
                                default:
                                    break;
                            }
                        }
                    }

                    strSql += strCondition;
                }
            }

            int iTotal = int.Parse(ChequeBooksDA.GetExcuteSql(strSql).Tables[0].Rows[0][0].ToString());

            return iTotal;
        }
        #endregion

        #region 获取支票册列表信息
        /// <summary>
        /// 获取支票册列表信息
        /// </summary>
        /// <returns>返回JSON数据</returns>
        private string GetChequeBookList()
        {
            ChequeBooksDA ChequeBooksDA = new ChequeBooksDA();
            DataTable sourceTable;

            string strCondition = "";
            string strTopInclude = "";
            string strTopNotInclude = "";
            if (strSearch != null)
            {
                if (!strSearch.Trim().Equals(""))
                {
                    string tempSearch = strSearch;
                    tempSearch = tempSearch.Replace("{", "");
                    tempSearch = tempSearch.Replace("}", "");
                    tempSearch = tempSearch.Replace("[", "");
                    tempSearch = tempSearch.Replace("]", "");

                    string[] searchArg = tempSearch.Split(new char[] { ',' });

                    for (int i = 0; i < searchArg.Length; i++)
                    {
                        string[] strArg = searchArg[i].Split(new char[] { ':' });

                        if (!strArg[1].Replace("\"", "").Trim().Equals(""))
                        {
                            switch (strArg[0].Replace("\"", ""))
                            {
                                case "btime"://Customer
                                    strCondition += String.Format(" AND convert(char(10),A.APPLYTIME,120) >= '{0}' ", strArg[1].Replace("\"", ""));
                                    break;
                                case "etime"://BillNO
                                    strCondition += String.Format(" AND convert(char(10),A.APPLYTIME,120) <= '{0}' ", strArg[1].Replace("\"", ""));
                                    break;
                                case "cus"://ETD BeginDate
                                    strCondition += String.Format(" AND A.CUSTOMERNAME LIKE  '%{0}%' ", strArg[1].Replace("\"", ""));
                                    break;
                                case "no"://ETD EndDate
                                    strCondition += String.Format(" AND A.BILLNO LIKE '%{0}%'", strArg[1].Replace("\"", ""));
                                    break;
                                default:
                                    break;
                            }
                        }
                    }
                }
            }
            string strSql = "";
            if (iCurrentPage > 0 && iShowPage > 0)
            {
                if (iCurrentPage == 1)
                {
                    strSql = String.Format(" SELECT {0} A.GID,A.NAME,A.DESCRIPTION,A.CHEQUECODE,A.CHEQUENUMSTART,A.CHEQUENUMEND,A.CHEQUETOTAL,CASE WHEN A.ISLOCK = 1 THEN 1 ELSE 0 END as ISLOCK, CASE WHEN A.ISDELETE = 1 THEN 1 ELSE 0 END as ISDELETE,B.SHOWNAME,A.CREATETIME,A.CURRENCY "
                              + " FROM ch_fee_chequebooks as A INNER JOIN [user] as B ON A.CREATEUSER = B.GID  WHERE 1>0 " + strCondition + " ORDER BY A.CREATETIME DESC ", "top " + iShowPage.ToString());
                }
                else
                {
                    strTopNotInclude = "top " + (iShowPage * (iCurrentPage - 1)).ToString();//RowCount*PageNum
                    strTopInclude = "top " + iShowPage.ToString();


                    strSql = String.Format(" SELECT {0} A.GID,A.NAME,A.DESCRIPTION,A.CHEQUECODE,A.CHEQUENUMSTART,A.CHEQUENUMEND,A.CHEQUETOTAL,CASE WHEN A.ISLOCK = 1 THEN 1 ELSE 0 END as ISLOCK, CASE WHEN A.ISDELETE = 1 THEN 1 ELSE 0 END as ISDELETE,B.SHOWNAME,A.CREATETIME,A.CURRENCY "
                       + " FROM ch_fee_chequebooks as A INNER JOIN [user] as B ON A.CREATEUSER = B.GID WHERE A.GID NOT IN "
                       + " (SELECT {1} GID FROM ch_fee_chequebooks  WHERE 1>0 " + strCondition + " ORDER BY CREATETIME DESC ) " + strCondition
                       + " ORDER BY A.CREATETIME DESC ", strTopInclude, strTopNotInclude);
                }
            }
            else
            {
                strSql = " SELECT A.GID,A.NAME,A.DESCRIPTION,A.CHEQUECODE,A.CHEQUENUMSTART,A.CHEQUENUMEND,A.CHEQUETOTAL,CASE WHEN A.ISLOCK = 1 THEN 1 ELSE 0 END as ISLOCK, CASE WHEN A.ISDELETE = 1 THEN 1 ELSE 0 END as ISDELETE,B.SHOWNAME,A.CREATETIME,A.CURRENCY "
                       + " FROM ch_fee_chequebooks as A INNER JOIN [user] as B ON A.CREATEUSER = B.GID WHERE 1>0 " + strCondition + " ORDER BY A.CREATETIME DESC ";
            }
            sourceTable = ChequeBooksDA.GetExcuteSql(strSql).Tables[0];

            StringBuilder sourceBuilder = new StringBuilder();
            sourceBuilder.Append("{");
            sourceBuilder.Append("rows:[");
            for (int i = 0; i < sourceTable.Rows.Count; i++)
            {
                sourceBuilder.Append("{id:\"" + sourceTable.Rows[i][0].ToString() + "\",");
                sourceBuilder.Append("data:[");
                sourceBuilder.Append("\"0\",");
                for (int j = 1; j < sourceTable.Columns.Count; j++)
                {
                    if (j == sourceTable.Columns.Count - 1)
                    {
                        sourceBuilder.Append("\"" + sourceTable.Rows[i][j].ToString() + "\"");
                    }
                    else
                    {
                        sourceBuilder.Append("\"" + sourceTable.Rows[i][j].ToString() + "\",");
                    }
                }
                if (i == sourceTable.Rows.Count - 1)
                {
                    sourceBuilder.Append("]}");
                }
                else
                {
                    sourceBuilder.Append("]},");
                }

            }
            sourceBuilder.Append("]");
            sourceBuilder.Append("}");

            return sourceBuilder.ToString();
        }
        #endregion

        #region 获取支票列表信息
        /// <summary>
        /// 获取支票列表信息
        /// </summary>
        /// <returns>返回JSON数据</returns>
        private string GetChequeBookItemList()
        {
            DataTable sourceTable;
            string strCondition = "";
            string strTopInclude = "";
            string strTopNotInclude = "";
            if (strSearch != null)
            {
                if (!strSearch.Trim().Equals(""))
                {
                    string tempSearch = strSearch;
                    tempSearch = tempSearch.Replace("{", "");
                    tempSearch = tempSearch.Replace("}", "");
                    tempSearch = tempSearch.Replace("[", "");
                    tempSearch = tempSearch.Replace("]", "");

                    string[] searchArg = tempSearch.Split(new char[] { ',' });

                    for (int i = 0; i < searchArg.Length; i++)
                    {
                        string[] strArg = searchArg[i].Split(new char[] { ':' });

                        if (!strArg[1].Replace("\"", "").Trim().Equals(""))
                        {
                            switch (strArg[0].Replace("\"", ""))
                            {
                                case "btime"://Customer
                                    strCondition += String.Format(" AND convert(char(10),A.APPLYTIME,120) >= '{0}' ", strArg[1].Replace("\"", ""));
                                    break;
                                case "etime"://BillNO
                                    strCondition += String.Format(" AND convert(char(10),A.APPLYTIME,120) <= '{0}' ", strArg[1].Replace("\"", ""));
                                    break;
                                case "cus"://ETD BeginDate
                                    strCondition += String.Format(" AND A.CUSTOMERNAME LIKE  '%{0}%' ", strArg[1].Replace("\"", ""));
                                    break;
                                case "no"://ETD EndDate
                                    strCondition += String.Format(" AND A.BILLNO LIKE '%{0}%'", strArg[1].Replace("\"", ""));
                                    break;
                                default:
                                    break;
                            }
                        }
                    }
                }
            }
            string strSql = "";
            if (iCurrentPage > 0 && iShowPage > 0)
            {
                if (iCurrentPage == 1)
                {
                    strSql = String.Format(" SELECT {0} A.GID, A.ChequeNUM,CASE WHEN A.ISLOCK = 1 THEN 1 ELSE 0 END as ISLOCK, CASE WHEN A.ISDELETE = 1 THEN 1 ELSE 0 END as ISDELETE,CASE WHEN A.ISMAKEOUT = 1 THEN 1 ELSE 0 END as ISMAKEOUT,B.SHOWNAME,A.CREATETIME,A.MAKEOUTTIME,A.MAKEOUTUSER "
                              + " FROM ch_fee_Chequeitems as A INNER JOIN [user] as B ON A.CREATEUSER = B.GID  WHERE BOOKID = '" + strBookID + "' " + strCondition + " ORDER BY A.ChequeNUM ASC ", "top " + iShowPage.ToString());
                }
                else
                {
                    strTopNotInclude = "top " + (iShowPage * (iCurrentPage - 1)).ToString();//RowCount*PageNum
                    strTopInclude = "top " + iShowPage.ToString();


                    strSql = String.Format(" SELECT {0} A.GID, A.ChequeNUM,CASE WHEN A.ISLOCK = 1 THEN 1 ELSE 0 END as ISLOCK, CASE WHEN A.ISDELETE = 1 THEN 1 ELSE 0 END as ISDELETE,CASE WHEN A.ISMAKEOUT = 1 THEN 1 ELSE 0 END as ISMAKEOUT,B.SHOWNAME,A.CREATETIME,A.MAKEOUTTIME,A.MAKEOUTUSER "
                       + " FROM ch_fee_Chequeitems as A INNER JOIN [user] as B ON A.CREATEUSER = B.GID WHERE A.GID NOT IN "
                       + " (SELECT {1} GID FROM ch_fee_Chequeitems  WHERE BOOKID = '" + strBookID + "' " + strCondition + " ORDER BY A.ChequeNUM ASC ) AND BOOKID = '" + strBookID + "' " + strCondition
                       + " ORDER BY A.ChequeNUM ASC ", strTopInclude, strTopNotInclude);
                }
            }
            else
            {
                strSql = " SELECT A.GID, A.ChequeNUM,CASE WHEN A.ISLOCK = 1 THEN 1 ELSE 0 END as ISLOCK, CASE WHEN A.ISDELETE = 1 THEN 1 ELSE 0 END as ISDELETE,CASE WHEN A.ISMAKEOUT = 1 THEN 1 ELSE 0 END as ISMAKEOUT,B.SHOWNAME,A.CREATETIME,A.MAKEOUTTIME,A.MAKEOUTUSER "
                       + " FROM ch_fee_Chequeitems as A INNER JOIN [user] as B ON A.CREATEUSER = B.GID WHERE BOOKID = '" + strBookID + "' " + strCondition + " ORDER BY A.ChequeNUM ASC ";
            }
            sourceTable = ChequeBooksDA.GetExcuteSql(strSql).Tables[0];

            StringBuilder sourceBuilder = new StringBuilder();
            sourceBuilder.Append("{");
            sourceBuilder.Append("rows:[");
            for (int i = 0; i < sourceTable.Rows.Count; i++)
            {
                sourceBuilder.Append("{id:\"" + sourceTable.Rows[i][0].ToString() + "\",");
                sourceBuilder.Append("data:[");
                sourceBuilder.Append("\"0\",");
                for (int j = 1; j < sourceTable.Columns.Count; j++)
                {
                    if (j == sourceTable.Columns.Count - 1)
                    {
                        sourceBuilder.Append("\"" + sourceTable.Rows[i][j].ToString() + "\"");
                    }
                    else
                    {
                        sourceBuilder.Append("\"" + sourceTable.Rows[i][j].ToString() + "\",");
                    }
                }
                if (i == sourceTable.Rows.Count - 1)
                {
                    sourceBuilder.Append("]}");
                }
                else
                {
                    sourceBuilder.Append("]},");
                }

            }
            sourceBuilder.Append("]");
            sourceBuilder.Append("}");

            return sourceBuilder.ToString();
        }
        #endregion
    }
}