public HttpResponseBase GroupQuery()
 {
     string result = "{success:false}";
     try
     {
         uint pid = uint.Parse(Request.Params["ProductId"] ?? "0");
         uint channel_id = uint.Parse(Request.Params["cId"] ?? "0");
         uint pmId = uint.Parse(Request.Params["pmId"] ?? "0");
         ProductItemMapMgr _mapMgr = new ProductItemMapMgr(connectionString);
         _ProductItemMapExcelImplMgr = new ProductItemMapExcelMgr(connectionString);
         List<BLL.gigade.Model.Custom.ProductMapCustom> resultList = _mapMgr.CombinationQuery(new ProductItemMapCustom { product_id = pid });
         int pileCount = resultList.LastOrDefault().pile_id;
         if (resultList.Count() > 0)
         {
             List<List<ProductComboMap>> itemAll = new List<List<ProductComboMap>>();
             List<ProductComboMap> itemList = new List<ProductComboMap>();
             for (int i = 0, j = resultList.Count(); i < j; i++)
             {
                 itemList.Add(_mapMgr.ProductComboItemQuery(int.Parse(resultList[i].child_id.ToString()), int.Parse(pid.ToString())).FirstOrDefault());
             }
             for (int Pi = 1; Pi <= pileCount; Pi++)
             {
                 itemAll.Add(itemList.Where(e => e.pile_id == Pi).ToList());
             }
             string s = "";
             getCombo(0, itemAll, "", ref s);
             s = s.Substring(0, s.Length - 1);
             string strb = "[";
             if (!string.IsNullOrEmpty(s))
             {
                 for (int Si = 0, Sj = s.Split('&').Length; Si < Sj; Si++)
                 {
                     ProductItemMap _productitemmap = new ProductItemMap();
                     _productitemmap.product_id = pid;
                     _productitemmap.channel_id = channel_id;
                     _productitemmap.price_master_id = pmId;//edit by xiangwang0413w 2014/07/07
                     _productitemmap.group_item_id = CommonFunction.Rank_ItemId(s.Split('&')[Si].ToString());
                     _productitemmap = _ProductItemMapExcelImplMgr.QueryProductItemMap(_productitemmap).FirstOrDefault();
                     strb += "{";
                     for (int Li = 0, Lj = s.Split('&')[Si].Split(',').Length; Li < Lj; Li++)
                     {
                         uint ItemStr = uint.Parse(s.Split('&')[Si].Split(',')[Li].ToString());
                         string product_name = itemAll[Li].Where(e => e.item_id == ItemStr).FirstOrDefault().product_name;
                         string spec_name_1 = itemAll[Li].Where(e => e.item_id == ItemStr).FirstOrDefault().spec_name_1;
                         string spec_name_2 = itemAll[Li].Where(e => e.item_id == ItemStr).FirstOrDefault().spec_name_2;
                         string set_num = itemAll[Li].Where(e => e.item_id == ItemStr).FirstOrDefault().set_num.ToString();
                         strb += "item_id_" + (Li + 1).ToString() + ":" + s.Split('&')[Si].Split(',')[Li].ToString() + ",product_name_" + (Li + 1).ToString() + ":'" + product_name + (spec_name_1 != "" ? "(" + spec_name_1 + " " + spec_name_2 + ")" : "") + "',product_num_" + (Li + 1).ToString() + ":" + set_num;
                         if (Li != Lj - 1)
                         {
                             strb += ",";
                         }
                     }
                     strb += ",group_item_id:'" + BLL.gigade.Common.CommonFunction.Rank_ItemId(s.Split('&')[Si].ToString()) + "'";
                     if (_productitemmap != null)
                     {
                         strb += ",rid:" + _productitemmap.rid + ",product_name:'" + _productitemmap.product_name + "',channel_detail_id:'" + _productitemmap.channel_detail_id + "',product_cost:" + _productitemmap.product_cost + ",product_price:" + _productitemmap.product_price;
                     }
                     else
                     {
                         strb += ",product_name:'',channel_detail_id:'',product_cost:'',product_price:''";
                     }
                     strb += "}";
                     if (Si != Sj - 1)
                     {
                         strb += ",";
                     }
                 }
             }
             strb += "]";
             result = "{success:true,data:" + strb + "}";
         }
     }
     catch (Exception ex)
     {
         Log4NetCustom.LogMessage logMessage = new Log4NetCustom.LogMessage();
         logMessage.Content = string.Format("TargetSite:{0},Source:{1},Message:{2}", ex.TargetSite.Name, ex.Source, ex.Message);
         logMessage.MethodName = System.Reflection.MethodBase.GetCurrentMethod().Name;
         log.Error(logMessage);
     }
     this.Response.Clear();
     this.Response.Write(result);
     this.Response.End();
     return this.Response;
 }
        public HttpResponseBase OptionalQuery()
        {
            string result = "{success:false}";
            try
            {
                uint pid = uint.Parse(Request.Params["ProductId"] ?? "0");
                uint channel_id = uint.Parse(Request.Params["cId"] ?? "0");
                uint pmId = uint.Parse(Request.Form["pmId"] ?? "0");
                string type = Request.Params["type"] ?? "";
                if (pid != 0)
                {
                    ProductMapSetMgr _mapsetMgr = new ProductMapSetMgr(connectionString);
                    ProductItemMapMgr _mapMgr = new ProductItemMapMgr(connectionString);
                    _ProductItemMapExcelImplMgr = new ProductItemMapExcelMgr(connectionString);
                    List<BLL.gigade.Model.Custom.ProductMapCustom> resultList = _mapMgr.CombinationQuery(new ProductItemMapCustom { product_id = pid });
                    //查詢是否已經建立過對照
                    List<BLL.gigade.Model.ProductMapSet> maplist = _mapsetMgr.Query(pid);
                    List<ProductComboMap> itemAll = new List<ProductComboMap>();
                    for (int i = 0, j = resultList.Count(); i < j; i++)
                    {
                        itemAll.Add(_mapMgr.ProductComboItemQuery(int.Parse(resultList[i].child_id.ToString()), int.Parse(pid.ToString())).FirstOrDefault());
                    }
                    //獲取任選中是否有必選單位 hufeng0813w
                    string str_must_itemId = "";
                    for (int i = 0, j = itemAll.Count; i < j; i++)
                    {
                        if (itemAll[i].set_num > 0)
                        {
                            if (str_must_itemId != "")
                            {
                                str_must_itemId += ",";
                            }
                            str_must_itemId += itemAll[i].item_id;
                        }
                    }
                    str_must_itemId = CommonFunction.Rank_ItemId(str_must_itemId);
                    string optional = "";
                    string StrOptional = "";

                    #region 僅限一單位勾選
                    if (resultList[0].buy_limit == 1)
                    {
                        //遞歸獲取所有的組合
                        GetOptional(0, itemAll, "", ref optional, 0, itemAll[0].g_must_buy);
                        optional = optional.Substring(0, optional.Length - 2).Replace(",&", "&");
                        //進行必選的篩選 hufeng0813w
                        if (str_must_itemId != "")
                        {
                            optional = CheckHas(optional, str_must_itemId);
                        }
                        StrOptional = "[";
                        if (!string.IsNullOrEmpty(optional))
                        {
                            for (int i = 0, Li = optional.Split('&').Length; i < Li; i++)
                            {
                                if (StrOptional != "" && i != 0)
                                {
                                    StrOptional += ",";
                                }
                                ProductItemMap _productitemmap = new ProductItemMap();
                                _productitemmap.product_id = pid;
                                _productitemmap.channel_id = channel_id;
                                _productitemmap.price_master_id = pmId;//edit by xiangwang0413w 2014/07/02
                                _productitemmap.group_item_id = CommonFunction.Rank_ItemId(optional.Split('&')[i].ToString());
                                _productitemmap = _ProductItemMapExcelImplMgr.QueryProductItemMap(_productitemmap).FirstOrDefault();
                                StrOptional += "{";
                                for (int j = 0, Lj = optional.Split('&')[i].Split(',').Length; j < Lj; j++)
                                {
                                    string product_name = itemAll.Where(e => e.item_id == uint.Parse(optional.Split('&')[i].Split(',')[j])).FirstOrDefault().product_name;
                                    string spec_name_1 = itemAll.Where(e => e.item_id == uint.Parse(optional.Split('&')[i].Split(',')[j])).FirstOrDefault().spec_name_1;
                                    string spec_name_2 = itemAll.Where(e => e.item_id == uint.Parse(optional.Split('&')[i].Split(',')[j])).FirstOrDefault().spec_name_2;
                                    //先判斷是否已經建立對照 
                                    string set_num = maplist.Count == 0 ? itemAll.Where(e => e.item_id == uint.Parse(optional.Split('&')[i].Split(',')[j])).FirstOrDefault().set_num.ToString()
                                        : (maplist.Where(e => e.item_id == uint.Parse(optional.Split('&')[i].Split(',')[j]) && e.map_rid == (_productitemmap == null ? 0 : _productitemmap.rid)).FirstOrDefault() == null ? itemAll.Where(e => e.item_id == uint.Parse(optional.Split('&')[i].Split(',')[j])).FirstOrDefault().set_num.ToString()
                                        : maplist.Where(e => e.item_id == uint.Parse(optional.Split('&')[i].Split(',')[j]) && e.map_rid == (_productitemmap == null ? 0 : _productitemmap.rid)).FirstOrDefault().set_num.ToString());
                                    string s_must_buy = itemAll.Where(e => e.item_id == uint.Parse(optional.Split('&')[i].Split(',')[j])).FirstOrDefault().set_num.ToString();//必選數量
                                    StrOptional += "item_id_" + (j + 1).ToString() + ":" + optional.Split('&')[i].Split(',')[j].ToString() + ",product_name_" + (j + 1).ToString() + ":'" + product_name + (spec_name_1 != "" ? "(" + spec_name_1 + " " + spec_name_2 + ")" : "") + "',product_num_" + (j + 1).ToString() + ":" + set_num + ",s_must_buy_" + (j + 1) + ":" + s_must_buy;
                                    if (j != Lj - 1)
                                    {
                                        StrOptional += ",";
                                    }
                                }

                                StrOptional += ",group_item_id:'" + BLL.gigade.Common.CommonFunction.Rank_ItemId(optional.Split('&')[i].ToString()) + "'";
                                if (_productitemmap != null)
                                {
                                    StrOptional += ",rid:" + _productitemmap.rid + ",product_name:'" + _productitemmap.product_name + "',channel_detail_id:'" + _productitemmap.channel_detail_id + "',product_cost:" + _productitemmap.product_cost + ",product_price:" + _productitemmap.product_price;
                                }
                                else
                                {
                                    StrOptional += ",product_name:'',channel_detail_id:'',product_cost:'',product_price:''";
                                }
                                StrOptional += "}";
                            }
                        }
                        StrOptional += "]";
                    }
                    #endregion

                    #region 僅限一單位不勾選
                    else
                    {
                        //不是必須所有item_id 都需要拼接上 add by hufeng0813w 2014/03/31
                        for (int i = 0, j = itemAll.Count; i < j; i++)
                        {
                            if (i != 0)
                            {
                                optional += ",";
                            }
                            optional += itemAll[i].item_id;
                        }
                        //end add by hufeng0813w 2014/03/31
                        ProductItemMap _pitemMap = new ProductItemMap();
                        _pitemMap.product_id = pid;
                        _pitemMap.channel_id = channel_id;
                        _pitemMap.price_master_id = pmId;//edit by xiangwang0413w 2014/07/02
                        List<ProductItemMap> _listPitemMap = _mapMgr.QueryOldItemMap(_pitemMap);

                        #region 查詢最初的商品信息
                        if (type == "searchInfo")
                        {
                            StrOptional = "[";
                            if (!string.IsNullOrEmpty(optional))
                            {
                                for (int i = 0, Li = optional.Split('&').Length; i < Li; i++)
                                {
                                    if (StrOptional != "" && i != 0)
                                    {
                                        StrOptional += ",";
                                    }
                                    ProductItemMap _productitemmap = new ProductItemMap();
                                    _productitemmap.product_id = pid;
                                    _productitemmap.channel_id = channel_id;
                                    _productitemmap.price_master_id = pmId;//edit by xiangwang0413w 2014/07/02
                                    _productitemmap.group_item_id = CommonFunction.Rank_ItemId(optional.Split('&')[i].ToString());
                                    _productitemmap = _ProductItemMapExcelImplMgr.QueryProductItemMap(_productitemmap).FirstOrDefault();
                                    StrOptional += "{";
                                    for (int j = 0, Lj = optional.Split('&')[i].Split(',').Length; j < Lj; j++)
                                    {
                                        string product_name = itemAll.Where(e => e.item_id == uint.Parse(optional.Split('&')[i].Split(',')[j])).FirstOrDefault().product_name;
                                        string spec_name_1 = itemAll.Where(e => e.item_id == uint.Parse(optional.Split('&')[i].Split(',')[j])).FirstOrDefault().spec_name_1;
                                        string spec_name_2 = itemAll.Where(e => e.item_id == uint.Parse(optional.Split('&')[i].Split(',')[j])).FirstOrDefault().spec_name_2;
                                        //先判斷是否已經建立對照 
                                        string set_num = maplist.Count == 0 ? itemAll.Where(e => e.item_id == uint.Parse(optional.Split('&')[i].Split(',')[j])).FirstOrDefault().set_num.ToString()
                                            : (maplist.Where(e => e.item_id == uint.Parse(optional.Split('&')[i].Split(',')[j]) && e.map_rid == (_productitemmap == null ? 0 : _productitemmap.rid)).FirstOrDefault() == null ? itemAll.Where(e => e.item_id == uint.Parse(optional.Split('&')[i].Split(',')[j])).FirstOrDefault().set_num.ToString()
                                            : maplist.Where(e => e.item_id == uint.Parse(optional.Split('&')[i].Split(',')[j]) && e.map_rid == (_productitemmap == null ? 0 : _productitemmap.rid)).FirstOrDefault().set_num.ToString());
                                        string s_must_buy = itemAll.Where(e => e.item_id == uint.Parse(optional.Split('&')[i].Split(',')[j])).FirstOrDefault().set_num.ToString();//必選數量
                                        StrOptional += "item_id_" + (j + 1).ToString() + ":" + optional.Split('&')[i].Split(',')[j].ToString() + ",product_name_" + (j + 1).ToString() + ":'" + product_name + (spec_name_1 != "" ? "(" + spec_name_1 + " " + spec_name_2 + ")" : "") + "',product_num_" + (j + 1).ToString() + ":" + set_num + ",s_must_buy_" + (j + 1) + ":" + s_must_buy;
                                        if (j != Lj - 1)
                                        {
                                            StrOptional += ",";
                                        }
                                    }

                                    StrOptional += ",group_item_id:'" + BLL.gigade.Common.CommonFunction.Rank_ItemId(optional.Split('&')[i].ToString()) + "'";
                                    if (_productitemmap != null)
                                    {
                                        StrOptional += ",rid:" + _productitemmap.rid + ",product_name:'" + _productitemmap.product_name + "',channel_detail_id:'" + _productitemmap.channel_detail_id + "',product_cost:" + _productitemmap.product_cost + ",product_price:" + _productitemmap.product_price;
                                    }
                                    else
                                    {
                                        StrOptional += ",product_name:'',channel_detail_id:'',product_cost:'',product_price:''";
                                    }
                                    StrOptional += "}";
                                }
                            }
                            StrOptional += "]";
                        }
                        #endregion

                        #region --建立過對照
                        else if (_listPitemMap.Count > 0)
                        {
                            StrOptional = "[";
                            for (int i = 0, j = _listPitemMap.Count; i < j; i++)
                            {
                                //對照下面的item_id
                                string list_groupItem = optional;
                                if (i != 0)
                                {
                                    StrOptional += ",";
                                }
                                StrOptional += "{";
                                for (int m = 0, Lj = list_groupItem.Split(',').Length; m < Lj; m++)
                                {
                                    string product_name = itemAll.Where(e => e.item_id == uint.Parse(list_groupItem.Split(',')[m])).FirstOrDefault().product_name;
                                    string spec_name_1 = itemAll.Where(e => e.item_id == uint.Parse(list_groupItem.Split(',')[m])).FirstOrDefault().spec_name_1;
                                    string spec_name_2 = itemAll.Where(e => e.item_id == uint.Parse(list_groupItem.Split(',')[m])).FirstOrDefault().spec_name_2;
                                    //先判斷是否已經建立對照 
                                    string set_num = _mapsetMgr.Query(_listPitemMap[i].rid, uint.Parse(list_groupItem.Split(',')[m])).FirstOrDefault() == null ? "0" : _mapsetMgr.Query(_listPitemMap[i].rid, uint.Parse(list_groupItem.Split(',')[m])).FirstOrDefault().set_num.ToString();
                                    string s_must_buy = itemAll.Where(e => e.item_id == uint.Parse(list_groupItem.Split(',')[m])).FirstOrDefault().set_num.ToString();//必選數量
                                    StrOptional += "item_id_" + (m + 1).ToString() + ":" + list_groupItem.Split(',')[m].ToString() + ",product_name_" + (m + 1).ToString() + ":'" + product_name + (spec_name_1 != "" ? "(" + spec_name_1 + " " + spec_name_2 + ")" : "") + "',product_num_" + (m + 1).ToString() + ":" + set_num + ",s_must_buy_" + (m + 1) + ":" + s_must_buy;
                                    if (m != Lj - 1)
                                    {
                                        StrOptional += ",";
                                    }
                                }

                                StrOptional += ",group_item_id:'" + list_groupItem + "'";
                                if (_listPitemMap != null)
                                {
                                    StrOptional += ",rid:" + _listPitemMap[i].rid + ",product_name:'" + _listPitemMap[i].product_name + "',channel_detail_id:'" + _listPitemMap[i].channel_detail_id + "',product_cost:" + _listPitemMap[i].product_cost + ",product_price:" + _listPitemMap[i].product_price;
                                }
                                else
                                {
                                    StrOptional += ",product_name:'',channel_detail_id:'',product_cost:'',product_price:''";
                                }
                                StrOptional += "}";
                            }
                            StrOptional += "]";
                        }
                        #endregion

                        #region --沒有建立過對照
                        else
                        {
                            StrOptional = "[";
                            if (!string.IsNullOrEmpty(optional))
                            {
                                for (int i = 0, Li = optional.Split('&').Length; i < Li; i++)
                                {
                                    if (StrOptional != "" && i != 0)
                                    {
                                        StrOptional += ",";
                                    }
                                    ProductItemMap _productitemmap = new ProductItemMap();
                                    _productitemmap.product_id = pid;
                                    _productitemmap.channel_id = channel_id;
                                    _productitemmap.price_master_id = pmId;//edit by xiangwang0413w 2014/07/02
                                    _productitemmap.group_item_id = CommonFunction.Rank_ItemId(optional.Split('&')[i].ToString());
                                    _productitemmap = _ProductItemMapExcelImplMgr.QueryProductItemMap(_productitemmap).FirstOrDefault();
                                    StrOptional += "{";
                                    for (int j = 0, Lj = optional.Split('&')[i].Split(',').Length; j < Lj; j++)
                                    {
                                        string product_name = itemAll.Where(e => e.item_id == uint.Parse(optional.Split('&')[i].Split(',')[j])).FirstOrDefault().product_name;
                                        string spec_name_1 = itemAll.Where(e => e.item_id == uint.Parse(optional.Split('&')[i].Split(',')[j])).FirstOrDefault().spec_name_1;
                                        string spec_name_2 = itemAll.Where(e => e.item_id == uint.Parse(optional.Split('&')[i].Split(',')[j])).FirstOrDefault().spec_name_2;
                                        //先判斷是否已經建立對照 
                                        string set_num = maplist.Count == 0 ? itemAll.Where(e => e.item_id == uint.Parse(optional.Split('&')[i].Split(',')[j])).FirstOrDefault().set_num.ToString()
                                            : (maplist.Where(e => e.item_id == uint.Parse(optional.Split('&')[i].Split(',')[j]) && e.map_rid == (_productitemmap == null ? 0 : _productitemmap.rid)).FirstOrDefault() == null ? itemAll.Where(e => e.item_id == uint.Parse(optional.Split('&')[i].Split(',')[j])).FirstOrDefault().set_num.ToString()
                                            : maplist.Where(e => e.item_id == uint.Parse(optional.Split('&')[i].Split(',')[j]) && e.map_rid == (_productitemmap == null ? 0 : _productitemmap.rid)).FirstOrDefault().set_num.ToString());
                                        string s_must_buy = itemAll.Where(e => e.item_id == uint.Parse(optional.Split('&')[i].Split(',')[j])).FirstOrDefault().set_num.ToString();//必選數量
                                        StrOptional += "item_id_" + (j + 1).ToString() + ":" + optional.Split('&')[i].Split(',')[j].ToString() + ",product_name_" + (j + 1).ToString() + ":'" + product_name + (spec_name_1 != "" ? "(" + spec_name_1 + " " + spec_name_2 + ")" : "") + "',product_num_" + (j + 1).ToString() + ":" + set_num + ",s_must_buy_" + (j + 1) + ":" + s_must_buy;
                                        if (j != Lj - 1)
                                        {
                                            StrOptional += ",";
                                        }
                                    }

                                    StrOptional += ",group_item_id:'" + BLL.gigade.Common.CommonFunction.Rank_ItemId(optional.Split('&')[i].ToString()) + "'";
                                    if (_productitemmap != null)
                                    {
                                        StrOptional += ",rid:" + _productitemmap.rid + ",product_name:'" + _productitemmap.product_name + "',channel_detail_id:'" + _productitemmap.channel_detail_id + "',product_cost:" + _productitemmap.product_cost + ",product_price:" + _productitemmap.product_price;
                                    }
                                    else
                                    {
                                        StrOptional += ",product_name:'',channel_detail_id:'',product_cost:'',product_price:''";
                                    }
                                    StrOptional += "}";
                                }
                            }
                            StrOptional += "]";
                        }
                        #endregion
                    }
                    #endregion
                    result = "{success:true,data:" + StrOptional + "}";
                }
            }
            catch (Exception ex)
            {
                Log4NetCustom.LogMessage logMessage = new Log4NetCustom.LogMessage();
                logMessage.Content = string.Format("TargetSite:{0},Source:{1},Message:{2}", ex.TargetSite.Name, ex.Source, ex.Message);
                logMessage.MethodName = System.Reflection.MethodBase.GetCurrentMethod().Name;
                log.Error(logMessage);
            }
            this.Response.Clear();
            this.Response.Write(result);
            this.Response.End();
            return this.Response;
        }