Пример #1
0
        public HttpResponseBase specTempSave()
        {
            string resultStr = "{success:true}";
            bool result = true;
            try
            {

                Caller _caller = (Session["caller"] as Caller);
                string specType = Request.Params["specType"];
                string spec1Name = Request.Params["spec1Name"];
                string spec1Result = Request.Params["spec1Result"];
                string spec2Name = "";
                string spec2Result;

                _specMgr = new ProductSpecMgr(connectionString);
                _specTempMgr = new ProductSpecTempMgr(connectionString);
                _productTempMgr = new ProductTempMgr(connectionString);
                _productItemMgr = new ProductItemMgr(connectionString);
                _productItemTempMgr = new ProductItemTempMgr(connectionString);
                _serialMgr = new SerialMgr(connectionString);

                if (!string.IsNullOrEmpty(Request.Params["ProductId"]))
                {
                    uint proId = uint.Parse(Request.Params["ProductId"]);

                    _functionMgr = new FunctionMgr(connectionString);
                    string function = Request.Params["function"] ?? "";
                    Function fun = _functionMgr.QueryFunction(function, "/Product/ProductSave");
                    int functionid = fun == null ? 0 : fun.RowId;
                    HistoryBatch batch = new HistoryBatch { functionid = functionid };
                    batch.batchno = Request.Params["batch"] ?? "";
                    batch.kuser = (Session["caller"] as Caller).user_email;

                    #region 正式表修改
                    List<ProductSpec> spec1List = null;
                    List<ProductSpec> spec2List = null;
                    List<ProductSpec> specUpdateList = new List<ProductSpec>();
                    List<ProductSpec> specAddList = new List<ProductSpec>();
                    JavaScriptSerializer jss = new JavaScriptSerializer();
                    spec1List = jss.Deserialize<List<ProductSpec>>(spec1Result);
                    if (spec1List != null)
                    {
                        //規格一處理
                        spec1List.ForEach(m =>
                        {
                            m.product_id = proId;
                            m.spec_type = 1;
                            if (m.spec_id != 0)
                            {
                                specUpdateList.Add(m);
                            }
                            else
                            {
                                specAddList.Add(m);
                            }
                        });
                    }

                    //規格二處理
                    if (specType.Equals("2"))
                    {
                        spec2Name = Request.Params["spec2Name"];
                        spec2Result = Request.Params["spec2Result"];
                        spec2List = jss.Deserialize<List<ProductSpec>>(spec2Result);

                        spec2List.ForEach(m =>
                        {
                            m.product_id = proId;
                            m.spec_type = 2;
                            if (m.spec_id != 0)
                            {
                                specUpdateList.Add(m);
                            }
                            else
                            {
                                specAddList.Add(m);
                            }
                        });
                    }

                    Product proModel = new Product();
                    _productMgr = new ProductMgr(connectionString);
                    _tableHistoryMgr = new TableHistoryMgr(connectionString);
                    proModel = _productMgr.Query(new Product { Product_Id = proId }).FirstOrDefault();
                    proModel.Spec_Title_1 = spec1Name;
                    proModel.Spec_Title_2 = spec2Name;

                    if (specUpdateList.Count() > 0)
                    {
                        for (int i = 0, j = specUpdateList.Count(); i < j; i++)
                        {
                            ArrayList sqls = new ArrayList();
                            sqls.Add(_specMgr.Update(specUpdateList[i]));
                            if (i == 0)
                            {
                                sqls.Add(_productMgr.Update(proModel));
                            }
                            if (!_tableHistoryMgr.SaveHistory<ProductSpec>(specUpdateList[i], batch, sqls))
                            {
                                result = false;
                            }
                        }
                    }

                    if (specAddList.Count() > 0)
                    {

                        List<ProductItem> saveItemList = new List<ProductItem>();
                        List<ProductSpec> spec1ExistList = spec1List.Where(m => m.spec_id != 0).ToList();     //規格一中原本就存在的規格
                        specAddList.ForEach(p => p.spec_id = uint.Parse(_serialMgr.NextSerial(18).ToString()));
                        ProductItem saveTemp;
                        List<PriceMaster> list_price = new List<PriceMaster>();
                        PriceMasterMgr pm = new PriceMasterMgr(connectionString);
                        list_price = pm.GetPriceMasterInfoByID2(proId.ToString());//獲取同一個ID下其他產品的價格信息
                        PriceMaster _p = list_price.FirstOrDefault();//add by wangwei0216w 2014/9/19 獲取同一個ID下其他產品的信息
                        //List<ProductItem> pro_list = new List<ProductItem>();
                        //ProductItemMgr pig = new ProductItemMgr(connectionString);
                        //pro_list = pig.GetProductItemByID(Convert.ToInt32(proId));
                        //ProductItem _product = pro_list.FirstOrDefault();//獲取同一個ID下其他產品的信息
                        foreach (var m in specAddList)
                        {
                            if (specType.Equals("1"))
                            {
                                if (m.spec_type == 1)
                                {
                                    saveTemp = new ProductItem();
                                    if (_p.same_price == 1)             //add by wangwei 0216w 2014/9/19 如果同價 就將價格賦予新增規格
                                    {
                                        //saveTemp.Item_Cost = Convert.ToUInt32(_p.cost);
                                        //saveTemp.Item_Money = Convert.ToUInt32(_p.price);
                                    }
                                    else
                                    {
                                        resultStr = "{success:true,Msg:true}";
                                    }
                                    saveTemp.Spec_Id_1 = m.spec_id;
                                    saveTemp.Product_Id = proId;
                                    // saveTemp.Item_Stock = 10;
                                    saveTemp.Item_Alarm = 1;
                                    saveItemList.Add(saveTemp);
                                }
                                else
                                {
                                    saveTemp = new ProductItem();
                                    if (_p.same_price == 1)             //add by wangwei 0216w 2014/9/19 如果同價 就將價格賦予新增規格
                                    {
                                        //saveTemp.Item_Cost = Convert.ToUInt32(_p.cost);
                                        //saveTemp.Item_Money = Convert.ToUInt32(_p.price);
                                    }
                                    else
                                    {
                                        resultStr = "{success:true,Msg:true}";
                                    }
                                    saveTemp.Spec_Id_2 = m.spec_id;
                                    saveTemp.Product_Id = proId;
                                    //saveTemp.Item_Stock = 10;
                                    saveTemp.Item_Alarm = 1;
                                    saveItemList.Add(saveTemp);
                                }
                            }
                            else
                            {
                                if (m.spec_type == 1)
                                {
                                    foreach (ProductSpec item in spec2List)
                                    {
                                        saveTemp = new ProductItem();
                                        if (_p.same_price == 1)             //add by wangwei 0216w 2014/9/19 如果同價 就將價格賦予新增規格
                                        {
                                            //saveTemp.Item_Cost = Convert.ToUInt32(_p.cost);
                                            //saveTemp.Item_Money = Convert.ToUInt32(_p.price);
                                        }
                                        else
                                        {
                                            resultStr = "{success:true,Msg:true}";
                                        }
                                        saveTemp.Spec_Id_1 = m.spec_id;
                                        saveTemp.Spec_Id_2 = item.spec_id;
                                        //saveTemp.Item_Stock = 10;
                                        saveTemp.Item_Alarm = 1;
                                        saveTemp.Product_Id = proId;
                                        saveItemList.Add(saveTemp);
                                    }
                                }
                                else
                                {
                                    foreach (ProductSpec item2 in spec1ExistList)
                                    {
                                        saveTemp = new ProductItem();
                                        if (_p.same_price == 1)             //add by wangwei 0216w 2014/9/19 如果同價 就將價格賦予新增規格
                                        {
                                            //saveTemp.Item_Cost = Convert.ToUInt32(_p.cost);
                                            //saveTemp.Item_Money = Convert.ToUInt32(_p.price);
                                        }
                                        else
                                        {
                                            resultStr = "{success:true,Msg:true}";
                                        }
                                        saveTemp.Spec_Id_1 = item2.spec_id;
                                        saveTemp.Spec_Id_2 = m.spec_id;
                                        //saveTemp.Item_Stock = 10;
                                        saveTemp.Item_Alarm = 1;
                                        saveTemp.Product_Id = proId;
                                        saveItemList.Add(saveTemp);
                                    }

                                }
                            }
                        }

                        _specMgr.Save(specAddList);
                        _productItemMgr.Save(saveItemList);
                        ProductItem pro;
                        if (proModel.Product_Status != 0) //add by wangwei0216w 2014/9/18 作用:將Export_flag的值設置為1
                        {
                            pro = new ProductItem() { Product_Id = proModel.Product_Id, Export_flag = 1 };
                            _productItemMgr.UpdateExportFlag(pro);
                        }
                        if (result) //如果之前保存規格,插入臨時表登操作有誤,則不執行插入itemprice的操作
                        {
                            List<ProductItem> item_list = _productItemMgr.GetProductNewItem_ID(Convert.ToInt32(proModel.Product_Id));   //得到product_item的新增ID
                            ItemPrice i = new ItemPrice();
                            ItemPriceMgr ipm = new ItemPriceMgr(connectionString);
                            ArrayList liststr = new ArrayList();
                            foreach (PriceMaster p in list_price)
                            {
                                foreach (ProductItem pi in item_list)
                                {
                                    if (p.same_price == 1)
                                    {
                                        i.price_master_id = p.price_master_id;
                                        i.item_id = pi.Item_Id;
                                        i.item_money = Convert.ToUInt32(p.price);
                                        i.item_cost = Convert.ToUInt32(p.cost);
                                        i.event_cost = Convert.ToUInt32(p.event_cost);
                                        i.event_money = Convert.ToUInt32(p.event_price);
                                    }
                                    else
                                    {
                                        i.price_master_id = p.price_master_id;
                                        i.item_id = pi.Item_Id;
                                        i.item_money = 0;
                                        i.item_cost = 0;
                                        i.event_money = 0;
                                        i.event_cost = 0;
                                        resultStr = "{success:true,Msg:true}";
                                    }
                                    liststr.Add(ipm.Save(i));
                                }
                            }
                            ipm.AddItemPricBySpec(liststr, connectionString);
                        }
                    }
                    //若為單一商品,則把product_item.export_flag改為2 edit by xiangwang0413w 2014/06/30
                    //if (proModel.Combination == 1)
                    //{
                    //    _productItemMgr = new ProductItemMgr(connectionString);
                    //    ProductItem pro_Item = new ProductItem() { Product_Id = proModel.Product_Id, Export_flag = 2 };
                    //    _productItemMgr.UpdateExportFlag(pro_Item);
                    //}

                    #endregion
                }
                else
                {
                    #region 臨時表修改
                    _productTempMgr = new ProductTempMgr(connectionString); //add by xiangwang 2014.09.26 可修改庫存預設值為99

                    string product_id = "0";
                    if (!string.IsNullOrEmpty(Request.Form["OldProductId"]))
                    {
                        product_id = Request.Form["OldProductId"];
                    }

                    //add by xiangwang 2014.09.26 可修改庫存預設值為99
                    ProductTemp query = _productTempMgr.GetProTemp(new ProductTemp { Writer_Id = _caller.user_id, Combo_Type = COMBO_TYPE, Product_Id = product_id });


                    if (!specType.Equals("0") && !string.IsNullOrEmpty(specType))
                    {
                        #region 有規格
                        List<ProductSpecTemp> spec1List;
                        List<ProductSpecTemp> spec2List;
                        List<ProductSpecTemp> specAllList = new List<ProductSpecTemp>();

                        JavaScriptSerializer jss = new JavaScriptSerializer();
                        spec1List = jss.Deserialize<List<ProductSpecTemp>>(spec1Result);

                        foreach (ProductSpecTemp item in spec1List)
                        {
                            //   specid = _serialMgr.NextSerial(18);
                            //item.spec_id = uint.Parse(specid.ToString());
                            item.Writer_Id = _caller.user_id;
                            item.product_id = product_id;
                            item.spec_type = 1;
                            item.spec_image = "";
                            specAllList.Add(item);
                        }

                        if (specType.Equals("2"))
                        {
                            spec2Name = Request.Params["spec2Name"];
                            spec2Result = Request.Params["spec2Result"];
                            spec2List = jss.Deserialize<List<ProductSpecTemp>>(spec2Result);

                            foreach (ProductSpecTemp item in spec2List)
                            {
                                // specid = _serialMgr.NextSerial(18);
                                //item.spec_id = uint.Parse(specid.ToString());
                                item.Writer_Id = _caller.user_id;
                                item.product_id = product_id;
                                item.spec_type = 2;
                                item.spec_image = "";
                                specAllList.Add(item);
                            }
                        }

                        List<ProductSpecTemp> tempList = _specTempMgr.Query(new ProductSpecTemp { Writer_Id = _caller.user_id, product_id = product_id });
                        if (tempList == null || tempList.Count() <= 0)
                        {
                            #region 保存

                            specAllList.ForEach(p => p.spec_id = uint.Parse(_serialMgr.NextSerial(18).ToString()));

                            bool saveSpecResult = _specTempMgr.Save(specAllList);

                            if (saveSpecResult)
                            {
                                _productItemTempMgr.Delete(new ProductItemTemp { Writer_Id = _caller.user_id, Product_Id = product_id });
                                #region 保存ProductItemTemp

                                List<ProductSpecTemp> specAllResultList = _specTempMgr.Query(new ProductSpecTemp { Writer_Id = _caller.user_id, product_id = product_id });
                                List<ProductSpecTemp> spec1ResultList = specAllResultList.Where(m => m.spec_type == 1).ToList();
                                List<ProductSpecTemp> spec2ResultList = specAllResultList.Where(m => m.spec_type == 2).ToList();

                                List<ProductItemTemp> saveItemList = new List<ProductItemTemp>();

                                if (specType.Equals("1"))
                                {
                                    foreach (ProductSpecTemp specTemp1 in spec1ResultList)
                                    {
                                        ProductItemTemp itemTemp = new ProductItemTemp();
                                        itemTemp.Writer_Id = _caller.user_id;
                                        itemTemp.Product_Id = product_id;
                                        itemTemp.Spec_Id_1 = specTemp1.spec_id;
                                        //itemTemp.Item_Stock = 10;
                                        itemTemp.Item_Alarm = 1;
                                        saveItemList.Add(itemTemp);

                                    }
                                }
                                else if (specType.Equals("2"))
                                {
                                    foreach (ProductSpecTemp specTemp1 in spec1ResultList)
                                    {
                                        foreach (ProductSpecTemp specTemp2 in spec2ResultList)
                                        {
                                            ProductItemTemp itemTemp = new ProductItemTemp();
                                            itemTemp.Writer_Id = _caller.user_id;
                                            itemTemp.Product_Id = product_id;
                                            itemTemp.Spec_Id_1 = specTemp1.spec_id;
                                            itemTemp.Spec_Id_2 = specTemp2.spec_id;
                                            //itemTemp.Item_Stock = 10;
                                            itemTemp.Item_Alarm = 1;
                                            itemTemp.Item_Code = "";
                                            itemTemp.Barcode = "";
                                            saveItemList.Add(itemTemp);
                                        }
                                    }
                                }

                                //add by xiangwang 2014.09.26 可修改庫存預設值為99
                                //saveItemList.ForEach(m => m.SetDefaultItemStock(query));

                                bool saveItemResult = _productItemTempMgr.Save(saveItemList);

                                if (!saveItemResult)
                                {
                                    result = false;
                                }

                                #endregion

                            }
                            else
                            {
                                result = false;
                            }
                            #endregion
                        }
                        else
                        {
                            #region 更新
                            string strSpecInit = Request.Params["specInit"];
                            string[] specs = strSpecInit.Split(',');

                            List<ProductSpecTemp> addList = specAllList.Where(p => p.spec_id == 0).ToList();
                            if (addList.Count() > 0)
                            {
                                addList.ForEach(p => p.spec_id = uint.Parse(_serialMgr.NextSerial(18).ToString()));

                                List<ProductSpecTemp> specAllResultList = _specTempMgr.Query(new ProductSpecTemp { Writer_Id = _caller.user_id, product_id = product_id });
                                List<ProductSpecTemp> spec1ResultList = specAllResultList.Where(m => m.spec_type == 1).ToList();
                                List<ProductSpecTemp> spec2ResultList = specAllResultList.Where(m => m.spec_type == 2).ToList();
                                List<ProductItemTemp> saveItemList = new List<ProductItemTemp>();
                                foreach (ProductSpecTemp item in addList)
                                {
                                    if (specType.Equals("1"))
                                    {
                                        if (item.spec_type == 1)
                                        {
                                            ProductItemTemp saveTemp = new ProductItemTemp();
                                            saveTemp.Writer_Id = _caller.user_id;
                                            saveTemp.Spec_Id_1 = item.spec_id;
                                            saveTemp.Product_Id = product_id;
                                            //saveTemp.Item_Stock = 10;
                                            saveTemp.Item_Alarm = 1;
                                            saveItemList.Add(saveTemp);
                                        }
                                        else
                                        {
                                            ProductItemTemp saveTemp = new ProductItemTemp();
                                            saveTemp.Writer_Id = _caller.user_id;
                                            saveTemp.Spec_Id_2 = item.spec_id;
                                            saveTemp.Product_Id = product_id;
                                            // saveTemp.Item_Stock = 10;
                                            saveTemp.Item_Alarm = 1;
                                            saveItemList.Add(saveTemp);
                                        }
                                    }
                                    else
                                    {
                                        if (item.spec_type == 1)
                                        {
                                            foreach (ProductSpecTemp item1 in spec2ResultList)
                                            {
                                                ProductItemTemp saveTemp = new ProductItemTemp();
                                                saveTemp.Writer_Id = _caller.user_id;
                                                saveTemp.Spec_Id_1 = item.spec_id;
                                                saveTemp.Spec_Id_2 = item1.spec_id;
                                                //saveTemp.Item_Stock = 10;
                                                saveTemp.Item_Alarm = 1;
                                                saveTemp.Product_Id = product_id;
                                                saveItemList.Add(saveTemp);
                                            }

                                            foreach (ProductSpecTemp item1 in addList.Where(p => p.spec_type == 2).ToList())
                                            {
                                                ProductItemTemp saveTemp = new ProductItemTemp();
                                                saveTemp.Writer_Id = _caller.user_id;
                                                saveTemp.Spec_Id_1 = item.spec_id;
                                                saveTemp.Spec_Id_2 = item1.spec_id;
                                                //saveTemp.Item_Stock = 10;
                                                saveTemp.Item_Alarm = 1;
                                                saveTemp.Product_Id = product_id;
                                                saveItemList.Add(saveTemp);
                                            }
                                        }
                                        else
                                        {
                                            foreach (ProductSpecTemp item2 in spec1ResultList)
                                            {
                                                ProductItemTemp saveTemp = new ProductItemTemp();
                                                saveTemp.Writer_Id = _caller.user_id;
                                                saveTemp.Spec_Id_1 = item2.spec_id;
                                                saveTemp.Spec_Id_2 = item.spec_id;
                                                saveTemp.Product_Id = product_id;
                                                // saveTemp.Item_Stock = 10;
                                                saveTemp.Item_Alarm = 1;
                                                saveItemList.Add(saveTemp);
                                            }

                                        }
                                    }
                                }
                                _specTempMgr.Save(addList);

                                //add by xiangwang 2014.09.26 可修改庫存預設值為99
                                //saveItemList.ForEach(m => m.SetDefaultItemStock(query));

                                _productItemTempMgr.Save(saveItemList);

                            }

                            if (specs.Length > 0)
                            {
                                List<ProductSpecTemp> updateList = new List<ProductSpecTemp>();
                                foreach (string initSpecId in specs)
                                {
                                    ProductSpecTemp nowItem = specAllList.Where(p => p.spec_id == uint.Parse(initSpecId)).FirstOrDefault();
                                    if (nowItem != null)
                                    {
                                        updateList.Add(nowItem);
                                    }
                                    else
                                    {

                                        ProductItemTemp delTemp = new ProductItemTemp { Writer_Id = _caller.user_id, Product_Id = product_id };
                                        uint spectype = _specTempMgr.Query(new ProductSpecTemp { spec_id = uint.Parse(initSpecId), product_id = product_id })[0].spec_type;
                                        if (spectype == 1)
                                        {
                                            delTemp.Spec_Id_1 = uint.Parse(initSpecId);
                                        }
                                        else if (spectype == 2)
                                        {
                                            delTemp.Spec_Id_2 = uint.Parse(initSpecId);
                                        }
                                        if (!_productItemTempMgr.Delete(delTemp))
                                        {
                                            result = false;
                                        }
                                        if (!_specTempMgr.Delete(new ProductSpecTemp { spec_id = uint.Parse(initSpecId), Writer_Id = _caller.user_id, product_id = product_id }))
                                        {
                                            result = false;
                                        }
                                        DeletePicOnServer(false, true, false, uint.Parse(initSpecId), product_id);
                                    }

                                }
                                if (!_specTempMgr.Update(updateList, "spec"))
                                {
                                    result = false;
                                }
                            }

                            #endregion
                        }

                        #region 更新Product

                        ProductTemp proTemp = new ProductTemp();
                        proTemp.Writer_Id = _caller.user_id;
                        proTemp.Product_Spec = uint.Parse(specType);
                        proTemp.Spec_Title_1 = spec1Name;
                        proTemp.Spec_Title_2 = spec2Name;
                        proTemp.Combo_Type = COMBO_TYPE;
                        proTemp.Product_Id = product_id;
                        bool saveProductResult = _productTempMgr.SpecInfoSave(proTemp);
                        if (!saveProductResult)
                        {
                            result = false;
                        }
                        #endregion

                        #endregion
                    }
                    else
                    {
                        #region 無規格
                        List<ProductItemTemp> saveList = new List<ProductItemTemp>();
                        //如果原數據有規格
                        if (query.Product_Spec != 0)
                        {
                            //刪除服務器上對應的圖片
                            DeletePicOnServer(false, true, false, 0, product_id);

                            _productItemTempMgr.Delete(new ProductItemTemp { Writer_Id = _caller.user_id, Product_Id = product_id });

                            _specTempMgr.Delete(new ProductSpecTemp { Writer_Id = _caller.user_id, product_id = product_id });

                            _productTempMgr.SpecInfoSave(new ProductTemp { Product_Spec = 0, Spec_Title_1 = "", Spec_Title_2 = "", Writer_Id = _caller.user_id, Combo_Type = COMBO_TYPE, Product_Id = product_id });

                            saveList = new List<ProductItemTemp>();
                            saveList.Add(new ProductItemTemp { Writer_Id = _caller.user_id, Product_Id = product_id/*, Item_Stock = 10*/, Item_Alarm = 1 });
                        }
                        else
                        {
                            List<ProductItemTemp> itemQuery = _productItemTempMgr.Query(new ProductItemTemp { Writer_Id = _caller.user_id, Product_Id = product_id });
                            if (itemQuery.Count() <= 0)
                            {
                                saveList = new List<ProductItemTemp>();
                                saveList.Add(new ProductItemTemp { Writer_Id = _caller.user_id, Product_Id = product_id, /*Item_Stock = 10,*/ Item_Alarm = 1 });
                                // _productItemTempMgr.Save(saveList);
                            }
                        }

                        //add by xiangwang 2014.09.26 可修改庫存預設值為99
                        //saveList.ForEach(m => m.SetDefaultItemStock(query));
                        _productItemTempMgr.Save(saveList);

                        #endregion
                    }
                    #endregion

                    #region 調度或自出商品,商品庫存預設為99
                    var proditemTemp = new ProductItemTemp { Product_Id = product_id, Writer_Id = _caller.user_id };
                    proditemTemp.SetDefaultItemStock(query);
                    _productItemTempMgr.UpdateItemStock(proditemTemp);
                    #endregion
                }
            }
            catch (Exception ex)
            {
                result = false;
                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);
            }

            if (!result)
            {
                resultStr = "{success:false,Msg:false}";
            }

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