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;
        }
Beispiel #2
0
        /// <summary>
        /// 合作外站商品查詢
        /// </summary>
        /// <returns></returns>
        public HttpResponseBase OrderInfoQueryByCooperator()
        {
            string jsonStr = "{success:false}";
            string pid = Request.Params["pid"] ?? "";
            uint channelId = 0;
            UInt32.TryParse(Request.Params["channelId"] ?? "0", out channelId);
            if (!string.IsNullOrEmpty(pid))
            {
                IProductItemMapImplMgr _pMapMgr = new ProductItemMapMgr(connectionString);
                IProductItemImplMgr _proItemMgr = new ProductItemMgr(connectionString);
                try
                {
                    ProductItemMap pMap = new ProductItemMap();

                    pMap.channel_detail_id = pid;
                    pMap.channel_id = channelId;
                    List<ProductItemMap> pMapList = _pMapMgr.QueryAll(pMap);
                    IPriceMasterImplMgr priceMgr = new PriceMasterMgr(connectionString);
                    IProductItemImplMgr _itemMgr = new ProductItemMgr(connectionString);
                    PriceMaster priceMap = priceMgr.QueryPriceMaster(new PriceMaster { price_master_id = pMapList.FirstOrDefault().price_master_id });
                    if (pMapList.Count() == 0)
                    {
                        jsonStr = "{success:false}";
                        this.Response.Clear();
                        this.Response.Write(jsonStr);
                        this.Response.End();
                        return this.Response;
                    }
                    uint product_id = pMapList[0].product_id;
                    if (product_id == 0)
                    {
                        product_id = _itemMgr.Query(new ProductItem { Item_Id = uint.Parse(pMapList[0].item_id.ToString()) }).FirstOrDefault().Product_Id;
                    }

                    IProductImplMgr _proMgr = new ProductMgr(connectionString);
                    Product prod = _proMgr.Query(new Product { Product_Id = product_id }).FirstOrDefault();

                    //判断商品是否上架
                    // long ltime = BLL.gigade.Common.CommonFunction.GetPHPTime(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                    if (prod.Combination != 1 && prod.Combination != 0)
                    {
                        OrderComboAddCustom orderAdd = _proMgr.OrderQuery(new Product { Product_Id = product_id }, priceMap.user_level, priceMap.user_id, priceMap.site_id);
                        orderAdd.product_id = 0;
                        orderAdd.out_product_id = pMapList[0].channel_detail_id;
                        orderAdd.product_name = pMapList[0].product_name;
                        orderAdd.product_cost = pMapList[0].product_price;
                        jsonStr = "{success:true";
                        string price_scale = "", cost_scale = "";
                        //查詢子商品信息
                        _prodCombMgr = new ProductComboMgr(connectionString);
                        List<ProductComboCustom> prodComList = _prodCombMgr.combQuery(new ProductComboCustom { Parent_Id = int.Parse(product_id.ToString()) });

                        IPriceMasterImplMgr pMgr = new PriceMasterMgr(connectionString);
                        PriceMaster query = new PriceMaster { user_id = priceMap.user_id, user_level = priceMap.user_level, site_id = priceMap.site_id };

                        jsonStr += ",child:[";
                        double childTotalPrice = 0;
                        double childTotalCost = 0;
                        string[] itemList = pMapList[0].group_item_id.ToString().Split(',');
                        foreach (var item in prodComList)
                        {
                            #region 計算子商品總價

                            for (int i = 0; i < itemList.Length; i++)
                            {
                                //在item_price中查找該itemid對應的價格
                                IItemPriceImplMgr iPMgr = new ItemPriceMgr(connectionString);

                                if (prod.Price_type == 1)
                                {
                                    query.product_id = uint.Parse(item.Child_Id.ToString());
                                    query.child_id = 0;
                                }
                                else if (prod.Price_type == 2)
                                {
                                    query.product_id = prod.Product_Id;
                                    query.child_id = int.Parse(item.Child_Id.ToString());
                                }


                                PriceMaster pM = priceMgr.QueryPriceMaster(query);
                                List<ItemPriceCustom> ipList = new List<ItemPriceCustom>();

                                //查詢子商品詳細信息(規格,庫存)
                                ProductItem pItem = new ProductItem();
                                pItem.Item_Id = uint.Parse(itemList[i]);
                                pItem.Product_Id = uint.Parse(item.Child_Id.ToString());
                                List<ProductItem> pItemList = _proItemMgr.Query(pItem);

                                if (pM != null && pItemList.Count > 0)
                                {
                                    //查找外站商品必購數量
                                    IProductMapSetImplMgr msMgr = new ProductMapSetMgr(connectionString);
                                    List<ProductMapSet> mSetList = msMgr.Query(new ProductMapSet { map_rid = uint.Parse(pMapList[0].rid.ToString()), item_id = uint.Parse(itemList[i]) });
                                    if (pM.same_price == 1)
                                    {
                                        childTotalPrice += int.Parse((pM.price * mSetList[0].set_num).ToString());
                                        childTotalCost += int.Parse((pM.cost * mSetList[0].set_num).ToString());
                                    }
                                    else
                                    {
                                        ipList = iPMgr.Query(new ItemPrice
                                        {
                                            item_id = uint.Parse(itemList[i]),
                                            price_master_id = pM.price_master_id
                                        });
                                        if (ipList.Count != 0)
                                        {
                                            if (mSetList.Count == 1)
                                            {
                                                childTotalPrice += int.Parse((ipList[0].item_money * mSetList[0].set_num).ToString());
                                                childTotalCost += int.Parse((ipList[0].item_cost * mSetList[0].set_num).ToString());
                                            }
                                            else
                                            {
                                                //在item_price中未查出對應價格
                                                this.Response.Clear();
                                                this.Response.Write("{success:false,msg:'" + Resources.OrderAdd.COMBO_CHILD_PRICE_NULL + "'}");
                                                this.Response.End();
                                                return this.Response;
                                            }
                                        }
                                    }
                                }


                            }
                            #endregion
                        }

                        double beforePrice = 0;
                        double beforeCost = 0;
                        int minStock = 0;
                        int index = 0;
                        double comboPrice = orderAdd.product_cost;
                        int curIndex = 0;
                        foreach (var item in prodComList)
                        {
                            curIndex++;
                            OrderAddCustom oc = new OrderAddCustom();
                            List<Product> pList = _proMgr.Query(new Product { Product_Id = uint.Parse(item.Child_Id.ToString()) });
                            oc.price_type = prod.Price_type;
                            oc.Product_Id = uint.Parse(item.Child_Id.ToString());
                            oc.ignore_stock = pList[0].Ignore_Stock;
                            oc.newparent_id = pMapList[0].channel_detail_id;
                            //補貨中是否還能販賣
                            if (pList.FirstOrDefault().Shortage == 1)
                            {
                                this.Response.Clear();
                                this.Response.Write("{success:false,msg:'" + Resources.OrderAdd.SHPRT_AGE + "'}");
                                this.Response.End();
                                return this.Response;
                            }
                            //讀取product_name
                            if (prod.Price_type == 1)
                            {
                                query.product_id = oc.Product_Id;
                                query.child_id = 0;
                            }
                            else if (prod.Price_type == 2)
                            {
                                query.product_id = prod.Product_Id;
                                query.child_id = Convert.ToInt32(oc.Product_Id);
                            }
                            PriceMaster pm = pMgr.QueryPriceMaster(query);
                            if (pm != null)
                            {
                                oc.product_name = pm.product_name;
                            }


                            for (int i = 0; i < itemList.Length; i++)
                            {
                                //查詢子商品詳細信息(規格,庫存)
                                ProductItem pItem = new ProductItem();
                                pItem.Item_Id = uint.Parse(itemList[i]);
                                pItem.Product_Id = uint.Parse(item.Child_Id.ToString());
                                oc.Item_Id = pItem.Item_Id;
                                List<ProductItem> pItemList = _proItemMgr.Query(pItem);
                                if (pItemList.Count() != 0)
                                {
                                    index++;

                                    //在item_price中查找該itemid對應的價格
                                    IItemPriceImplMgr iPMgr = new ItemPriceMgr(connectionString);
                                    List<ItemPriceCustom> ipList = new List<ItemPriceCustom>();
                                    if (pm != null)
                                    {
                                        oc.price_master_id = pm.price_master_id;
                                        //查找外站商品必購數量
                                        IProductMapSetImplMgr msMgr = new ProductMapSetMgr(connectionString);
                                        List<ProductMapSet> mSetList = msMgr.Query(new ProductMapSet { map_rid = uint.Parse(pMapList[0].rid.ToString()), item_id = uint.Parse(itemList[i]) });
                                        if (mSetList.Count != 0)
                                        {
                                            oc.s_must_buy = int.Parse(mSetList[0].set_num.ToString());
                                        }
                                        if (prod.Price_type == 1)
                                        {
                                            #region 按比例拆分


                                            if (i == itemList.Length - 1 && curIndex == prodComList.Count)
                                            {
                                                oc.product_cost = uint.Parse(CommonFunction.Math4Cut5Plus((orderAdd.product_cost - beforePrice) / oc.s_must_buy).ToString());
                                                oc.Item_Cost = uint.Parse(CommonFunction.Math4Cut5Plus((orderAdd.cost - beforeCost) / oc.s_must_buy).ToString());

                                            }
                                            else
                                            {
                                                double price = 0.0;
                                                double cost = 0.0;
                                                string price_res = "0";
                                                string cost_res = "0";
                                                double price_result = 0.0;
                                                double cost_result = 0.0;

                                                //规格同价
                                                if (pm.same_price == 1)
                                                {
                                                    if (childTotalPrice != 0)
                                                    {
                                                        double singlePrice = double.Parse((pm.price * oc.s_must_buy).ToString());
                                                        double totalPrice = childTotalPrice;
                                                        double c_scale = singlePrice / totalPrice;
                                                        price = orderAdd.product_cost * c_scale / oc.s_must_buy;
                                                        price_result = CommonFunction.Math4Cut5Plus(price);
                                                        price_res = c_scale.ToString();
                                                    }
                                                    beforePrice += price_result;
                                                    oc.product_cost = uint.Parse(price_result.ToString());
                                                    price_scale += price_res + ",";

                                                    if (childTotalCost != 0)
                                                    {
                                                        double singleCost = double.Parse((pm.cost * oc.s_must_buy).ToString());
                                                        double totalCost = childTotalCost;
                                                        double c_scale = singleCost / totalCost;
                                                        cost = orderAdd.cost * c_scale / oc.s_must_buy;
                                                        cost_result = CommonFunction.Math4Cut5Plus(cost);
                                                        cost_res = c_scale.ToString();
                                                    }
                                                    beforeCost += cost_result;
                                                    oc.Item_Cost = uint.Parse(cost_result.ToString());
                                                    cost_scale += cost_res + ",";


                                                }
                                                else
                                                {
                                                    ipList = iPMgr.Query(new ItemPrice
                                                    {
                                                        item_id = uint.Parse(itemList[i]),
                                                        price_master_id = pm.price_master_id
                                                    });
                                                    if (ipList.Count != 0)
                                                    {
                                                        if (childTotalPrice != 0)
                                                        {
                                                            double singlePrice = double.Parse((ipList[0].item_money * oc.s_must_buy).ToString());
                                                            double totalPrice = childTotalPrice;
                                                            double c_scale = singlePrice / totalPrice;
                                                            price = orderAdd.product_cost * c_scale / oc.s_must_buy;
                                                            price_result = CommonFunction.Math4Cut5Plus(price);
                                                            price_res = c_scale.ToString();
                                                        }
                                                        beforePrice += price_result;
                                                        oc.product_cost = uint.Parse(price_result.ToString());
                                                        price_scale += price_res + ",";

                                                        if (childTotalCost != 0)
                                                        {
                                                            double singleCost = double.Parse((ipList[0].item_cost * oc.s_must_buy).ToString());
                                                            double totalCost = childTotalCost;
                                                            double c_scale = singleCost / totalCost;
                                                            cost = orderAdd.cost * c_scale / oc.s_must_buy;
                                                            cost_result = CommonFunction.Math4Cut5Plus(cost);
                                                            cost_res = c_scale.ToString();
                                                        }
                                                        beforeCost += cost_result;
                                                        oc.Item_Cost = uint.Parse(cost_result.ToString());
                                                        cost_scale += cost_res + ",";
                                                    }
                                                }
                                            }
                                            #endregion
                                        }
                                        else if (prod.Price_type == 2)
                                        {
                                            #region 各自定價

                                            ipList = iPMgr.Query(new ItemPrice { item_id = uint.Parse(itemList[i]), price_master_id = pm.price_master_id });
                                            if (ipList.FirstOrDefault() != null)
                                            {
                                                oc.product_cost = ipList.FirstOrDefault().item_money;
                                            }
                                            #endregion
                                        }
                                    }


                                    if (index == 1)
                                    {
                                        minStock = oc.s_must_buy > 0 ? Convert.ToInt32(pItemList[0].Item_Stock / oc.s_must_buy) : pItemList[0].Item_Stock;
                                    }
                                    oc.Item_Stock = pItemList[0].Item_Stock;
                                    int tmp_stock = oc.s_must_buy > 0 ? Convert.ToInt32(oc.Item_Stock / oc.s_must_buy) : oc.Item_Stock;
                                    if (tmp_stock < minStock)
                                    {
                                        minStock = tmp_stock;
                                    }
                                    //根據spec_id找出spec_name
                                    IProductSpecImplMgr _specMgr = new BLL.gigade.Mgr.ProductSpecMgr(connectionString);
                                    ProductSpec ps1 = _specMgr.query(int.Parse(pItemList[0].Spec_Id_1.ToString()));
                                    if (ps1 != null)
                                    {
                                        oc.Spec_Name_1 = ps1.spec_name;
                                        oc.Spec_Id_1 = ps1.spec_id;
                                    }
                                    ProductSpec ps2 = _specMgr.query(int.Parse(pItemList[0].Spec_Id_2.ToString()));
                                    if (ps2 != null)
                                    {
                                        oc.Spec_Name_2 = ps2.spec_name;
                                        oc.Spec_Id_2 = ps2.spec_id;
                                    }
                                    jsonStr += "{";
                                    jsonStr += "'product_id':'" + oc.Product_Id + "','product_name':'" + oc.product_name + "','item_id':'" + oc.Item_Id + "'";
                                    jsonStr += ",'spec1':'" + oc.Spec_Id_1 + "','spec2':'" + oc.Spec_Id_2 + "','spec1_show':'" + oc.Spec_Name_1 + "',s_must_buy:'" + oc.s_must_buy + "'";
                                    jsonStr += ",'spec2_show':'" + oc.Spec_Name_2 + "','product_cost':'" + oc.product_cost + "',Item_Cost:'" + oc.Item_Cost + "','stock':'" + oc.Item_Stock + "',price_master_id:" + oc.price_master_id;
                                    jsonStr += ",price_type:" + oc.price_type + ",ignore_stock:" + oc.ignore_stock;
                                    if (oc.parent_id != 0)
                                    {
                                        jsonStr += ",parent_id:'" + oc.parent_id + "'";
                                    }
                                    else
                                    {
                                        jsonStr += ",parent_id:'" + oc.newparent_id + "'";
                                    }
                                    jsonStr += "},";
                                }
                            }
                        }
                        if (price_scale != "")
                        {
                            price_scale = price_scale.Substring(0, price_scale.Length - 1);
                        }
                        jsonStr = jsonStr.Substring(0, jsonStr.Length - 1) + "]";
                        jsonStr += ",data:[";
                        jsonStr += "{product_id:'" + orderAdd.out_product_id + "',item_id:0,product_name:'" + orderAdd.product_name + "',product_cost:'" + orderAdd.product_cost + "'";
                        jsonStr += ",s_must_buy:'" + pMapList[0].group_item_id.ToString().Split(',').Length + "',Item_Cost:'" + orderAdd.cost + "',child_cost_scale:'" + cost_scale + "',child_scale:'" + price_scale + "',stock:'" + minStock + "',g_must_buy:'" + pMapList[0].group_item_id.ToString().Split(',').Length + "'}]}";


                    }
                    else
                    {
                        if (prod.Combination == 0)
                        {
                            jsonStr = "{success:false,msg:\"" + Resources.OrderAdd.PRODUCT_NOT_USE_IN_ORDER + "\"}";
                            this.Response.Clear();
                            this.Response.Write(jsonStr);
                            this.Response.End();
                            return this.Response;
                        }
                        else
                        {
                            //單一商品
                            ProductItem pItem = new ProductItem();
                            pItem.Item_Id = UInt32.Parse(pMapList[0].item_id.ToString());


                            List<ProductItem> pItemList = _proItemMgr.Query(pItem);
                            List<Product> pList = _proMgr.Query(new Product { Product_Id = uint.Parse(pItemList[0].Product_Id.ToString()) });
                            //補貨時還能販賣
                            if (pList[0].Shortage == 1)
                            {
                                this.Response.Clear();
                                this.Response.Write("{success:false,msg:'" + Resources.OrderAdd.SHPRT_AGE + "'}");
                                this.Response.End();
                                return this.Response;
                            }
                            if (pItemList.Count() == 0)
                            {
                                jsonStr = "{success:false}";
                                this.Response.Clear();
                                this.Response.Write(jsonStr);
                                this.Response.End();
                                return this.Response;
                            }


                            List<OrderAddCustom> oalist = new List<OrderAddCustom>();

                            OrderAddCustom oc = new OrderAddCustom();

                            oc.Spec_Id_1 = pItemList[0].Spec_Id_1;
                            oc.Spec_Id_2 = pItemList[0].Spec_Id_2;
                            oc.Item_Stock = pItemList[0].Item_Stock;
                            oc.ignore_stock = pList[0].Ignore_Stock;

                            IProductSpecImplMgr _specMgr = new BLL.gigade.Mgr.ProductSpecMgr(connectionString);
                            ProductSpec spec1 = _specMgr.query(Convert.ToInt32(pItemList[0].Spec_Id_1));
                            ProductSpec spec2 = _specMgr.query(Convert.ToInt32(pItemList[0].Spec_Id_2));
                            if (spec1 != null)
                            {
                                oc.Spec_Name_1 = spec1.spec_name;
                            }
                            if (spec2 != null)
                            {
                                oc.Spec_Name_2 = spec2.spec_name;
                            }

                            oalist.Add(oc);

                            StringBuilder stb = new StringBuilder();

                            stb.Append("[");
                            stb.Append("{product_id:'" + pMapList[0].channel_detail_id + "',item_id:" + pMapList[0].item_id + ",product_name:'" + HttpUtility.HtmlEncode(pMapList[0].product_name) + "',spec1:'" + oc.Spec_Id_1.ToString() + "',");
                            stb.Append("spec2:'" + oc.Spec_Id_2.ToString() + "',spec1_show:'" + oc.Spec_Name_1 + "',spec2_show:'" + oc.Spec_Name_2 + "',product_cost:" + pMapList[0].product_price + ",");
                            stb.Append("stock:" + oc.Item_Stock + ",price_master_id:" + oc.price_master_id + ",ignore_stock:" + oc.ignore_stock + "}");
                            stb.Append("]");

                            jsonStr = "{success:true,data:" + stb.ToString() + "}";
                        }
                    }
                }
                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);
                }
            }
            else
            {
                jsonStr = "{success:true,data:{product_id:''}}";
            }

            this.Response.Clear();
            this.Response.Write(jsonStr);
            this.Response.End();
            return this.Response;
        }