Beispiel #1
0
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/json";

            int    start = 0;
            int    limit = 10;
            string sort  = string.Empty;
            string dir   = string.Empty;
            string query = string.Empty;

            if (!string.IsNullOrEmpty(context.Request["start"]))
            {
                start = int.Parse(context.Request["start"]);
            }

            if (!string.IsNullOrEmpty(context.Request["limit"]))
            {
                limit = int.Parse(context.Request["limit"]);
            }

            if (!string.IsNullOrEmpty(context.Request["sort"]))
            {
                sort = context.Request["sort"];
            }

            if (!string.IsNullOrEmpty(context.Request["dir"]))
            {
                dir = context.Request["dir"];
            }

            if (!string.IsNullOrEmpty(context.Request["query"]))
            {
                query = context.Request["query"].ToUpper();
            }

            Paging <Drug> Drugs = Drug.drugsPaging(start, limit, sort, dir, query);

            context.Response.Write(string.Format("{{total:{1},'drugs':{0}}}", JSON.Serialize(Drugs.Data), Drugs.TotalRecords));
        }
Beispiel #2
0
        public static Paging <Drug> drugsPaging(int start, int limit, string sort, string dir, string filter)
        {
            List <Drug> drugs = Drug.Getdrugname;

            if (filter.Length > 0)
            {
                drugs.Clear();
                DBMysql     db   = new DBMysql();
                List <Drug> data = new List <Drug>();

                string sql = "SELECT drg_name FROM drug_list "; //簡碼輸入
                sql += "WHERE short_code LIKE '%" + filter + "%' AND drg_status='Y'";
                DataTable dt1 = db.Query(sql);
                if (dt1.Rows.Count > 0)
                {
                    for (int j = 0; j < dt1.Rows.Count; j++)
                    {
                        Drug drug = new Drug();
                        drug.py       = filter;
                        drug.drugname = dt1.Rows[j]["drg_name"].ToString(); //drg_name
                        drugs.Add(drug);
                    }
                }
                else //直接輸入中文字
                {
                    sql  = "SELECT drg_name FROM drug_list ";
                    sql += "WHERE drg_name LIKE '%" + filter + "%' AND drg_status='Y' ";
                    dt1  = db.Query(sql);
                    if (dt1.Rows.Count > 0)
                    {
                        for (int j = 0; j < dt1.Rows.Count; j++)
                        {
                            Drug drug = new Drug();
                            drug.py       = filter;
                            drug.drugname = dt1.Rows[j]["drg_name"].ToString(); //drg_name
                            drugs.Add(drug);
                        }
                    }
                    else
                    {
                        sql = "SELECT PY, HZ, ZM FROM pinyin ";
                        if (filter != "*")
                        {
                            sql += "WHERE PY LIKE '" + filter + "%' AND ZM='" + filter.Substring(0, 1) + "' ";
                        }
                        DataTable dt = db.Query(sql);
                        if (dt.Rows.Count > 0) //使用拼音輸入
                        {
                            sql = "SELECT drg_name FROM drug_list WHERE (1=0 ";
                            for (int i = 0; i < dt.Rows.Count; i++)
                            {
                                sql += "OR drg_name LIKE '" + dt.Rows[i]["HZ"].ToString() + "%' ";
                            }
                            sql += ") AND drg_status='Y'";
                            dt1  = db.Query(sql);
                            if (dt1.Rows.Count > 0)
                            {
                                for (int j = 0; j < dt1.Rows.Count; j++)
                                {
                                    Drug drug = new Drug();
                                    drug.py       = filter.Substring(0, 1);
                                    drug.drugname = dt1.Rows[j]["drg_name"].ToString(); //drg_name
                                    drugs.Add(drug);
                                }
                            }
                        }
                        dt.Dispose();
                    }
                    dt1.Dispose();
                    db.Close();
                }
            }
            //if (!string.IsNullOrEmpty(filter) && filter != "*")
            //{
            //    drugs.RemoveAll(drug => !drug.py.StartsWith(filter));
            //}

            if (!string.IsNullOrEmpty(sort))
            {
                drugs.Sort(delegate(Drug x, Drug y)
                {
                    object a;
                    object b;

                    int direction = dir == "DESC" ? -1 : 1;

                    a = x.GetType().GetProperty(sort).GetValue(x, null);
                    b = y.GetType().GetProperty(sort).GetValue(y, null);

                    return(CaseInsensitiveComparer.Default.Compare(a, b) * direction);
                });
            }

            if ((start + limit) > drugs.Count)
            {
                limit = drugs.Count - start;
            }
            List <Drug> rangeDrugs = (start < 0 || limit < 0) ? drugs : drugs.GetRange(start, limit);

            return(new Paging <Drug>(rangeDrugs, drugs.Count));
        }