/// <summary>
        /// 算法:根据组合产品的ProductID和其子产品的ChildrenProductID唯一确定一条记录,删除再插入。删除插入一并做,会大大简化程序和客户端的各种判断。
        /// CreateDate: 2013年11月19日11:43:22
        /// </summary>
        /// <param name="rModel"></param>
        /// <param name="User_Account"></param>
        /// <returns>如果新增成功,返回新的ID</returns>
        public long AddNewHM4Group(CMS_HMGroup_Relation_Model rModel, String User_Account)
        {
            using (PermaisuriCMSEntities db = new PermaisuriCMSEntities())
            {
                //这种方法如果表结构改动,会大大增加维护成本。根据实际情况,可能组合产品的新增操作不是很频繁,并不会很大程度上影响性能,所以还是用EF来做
                //using (TransactionScope transaction = new TransactionScope())
                //{
                //  //db.Database.ExecuteSqlCommand("delete from CMS_HMGroup_Relation where ProductID = @ProductID and ChildrenProductID", new SqlParameter("@ProductID", rModel.ProductID));
                //}
                //if (rModel.RID > 0)//说明是在原有的基础上编辑而不是新增,直接删除了吧...2013年11月20日14:42:23
                //{
                //    var relation = new CMS_HMGroup_Relation { RID = rModel.RID };
                //    db.Set<CMS_HMGroup_Relation>().Attach(relation);
                //    db.CMS_HMGroup_Relation.Remove(relation);
                //}

                //var query = db.CMS_HMGroup_Relation.Where(r => r.ProductID == rModel.ProductID && r.ChildrenProductID == rModel.ChildrenProductID).FirstOrDefault();
                //if (query != null)
                //{
                //    //db.Set<CMS_HMGroup_Relation>().Attach(query);
                //    db.CMS_HMGroup_Relation.Remove(query);
                //}

                //var rel = db.CMS_HMGroup_Relation.FirstOrDefault(r => r.RID == rModel.RID);

                /*使用RID来做判断,在2014年4月28日 下发生产环境的时候遇到很多问题,最典型的一个就是前端使用复制HMNUM,
                 * 然后选择下拉单,然后离开鼠标,客户端触发了2次RID为0的AJAX数据,造成该条数据重复插入两次。经过调试发现,ProductID是可以当成一个KEY读取,比如一个组合产品的ProductID
                 * 只会有一个,即使下一次再Create一个同样名称的组合产品,由于库表是自动增长的ID,所以名称一样没关系,ID不一样 2014年4月29日9:26:55
                 */
                var rel = db.CMS_HMGroup_Relation.FirstOrDefault(r => r.ProductID == rModel.ProductID && r.ChildrenProductID == rModel.ChildrenProductID);
                if (rel == null)
                {
                    var newModel = new CMS_HMGroup_Relation
                    {
                        ProductID         = rModel.ProductID,
                        ChildrenProductID = rModel.ChildrenProductID,
                        SellSets          = rModel.SellSets,
                        CreateBy          = User_Account,
                        CreateOn          = DateTime.Now
                    };
                    db.CMS_HMGroup_Relation.Add(newModel);
                    db.SaveChanges();
                    return(newModel.RID);
                }
                else
                {
                    rel.ProductID         = rModel.ProductID;
                    rel.SellSets          = rModel.SellSets;
                    rel.ChildrenProductID = rModel.ChildrenProductID;
                    db.SaveChanges();
                    return(rel.RID);
                }
            }
        }
        /// <summary>
        /// 为组合产品新增基础产品,用于Create HM Group 页面的使用
        /// CraeteTime:2013年11月19日11:37:55
        /// </summary>
        /// <param name="rModel"></param>
        /// <returns>如果新增成功,返回新的ID,和价格信息,价格信息用于第三阶段Costing的展示...</returns>
        public ActionResult AddNewHM4Group(CMS_HMGroup_Relation_Model rModel)
        {
            try
            {
                if (rModel.ProductID < 1 || rModel.ChildrenProductID < 1)
                {
                    return(Json(new NBCMSResultJson
                    {
                        Status = StatusType.Error,
                        Data = "Request is illegal!"
                    }));
                }

                User_Profile_Model curUserInfo = new CommonController().GetCurrentUserbyCookie(Request[ConfigurationManager.AppSettings["userInfoCookiesKey"]]);

                HMGroupCreateServices gpSvr = new HMGroupCreateServices();
                string errMsg = string.Empty;
                long   newID  = gpSvr.AddNewHM4Group(rModel, curUserInfo.User_Account);
                if (newID > 0)
                {
                    return(Json(new NBCMSResultJson
                    {
                        Status = StatusType.OK,
                        Data = new
                        {
                            newID = newID,
                            //根据当前主产品的信息获取其子产品的价格,一个一个返回,客户端要判断到傻眼,删除新增编辑都要用JS判断...
                            //2013年11月19日17:41:54
                            ChildrenCostList = gpSvr.GetChildrenCost(rModel)
                        }
                    }));
                }
                return(Json(new NBCMSResultJson
                {
                    Status = StatusType.Error,
                    Data = errMsg == string.Empty ? "Faile to add HM# Group" : errMsg
                }));
            }
            catch (Exception ex)
            {
                NBCMSLoggerManager.Error("");
                NBCMSLoggerManager.Error(ex.Message);
                NBCMSLoggerManager.Error(ex.Source);
                NBCMSLoggerManager.Error(ex.StackTrace);
                NBCMSLoggerManager.Error("");
                return(Json(new NBCMSResultJson
                {
                    Status = StatusType.Exception,
                    Data = ex.Message
                }));
            }
        }
 /// <summary>
 /// 删除组合产品中的某一个子产品
 /// </summary>
 /// <param name="rModel"></param>
 /// <param name="errMsg"></param>
 /// <returns></returns>
 public bool DeleteChildrenHM(CMS_HMGroup_Relation_Model rModel, ref string errMsg)
 {
     using (PermaisuriCMSEntities db = new PermaisuriCMSEntities())
     {
         var query = db.CMS_HMGroup_Relation.FirstOrDefault(r => r.RID == rModel.RID);
         if (query == null)
         {
             errMsg = "This item does not exist";
             return(false);
         }
         db.CMS_HMGroup_Relation.Remove(query);
         return(db.SaveChanges() > 0);
     }
 }
 /// <summary>
 /// 根据关系表的ID删除当前组合产品的下的某【一个】子产品
 /// Change:删除也返回当前组合产品对于的各种子产品的价格,用于前端价格的动态展示。2013年11月20日15:08:42
 /// </summary>
 /// <param name="rModel"></param>
 /// <returns></returns>
 public ActionResult DeleteChildrenHM(CMS_HMGroup_Relation_Model rModel)
 {
     try
     {
         if (rModel.RID < 1)
         {
             return(Json(new NBCMSResultJson
             {
                 Status = StatusType.Error,
                 Data = "Request is illegal!"
             }));
         }
         HMGroupCreateServices gpSvr = new HMGroupCreateServices();
         string errMsg = string.Empty;
         if (gpSvr.DeleteChildrenHM(rModel, ref errMsg))
         {
             return(Json(new NBCMSResultJson
             {
                 Status = StatusType.OK,
                 Data = new
                 {
                     ChildrenCostList = gpSvr.GetChildrenCost(rModel)
                 }
             }));
         }
         return(Json(new NBCMSResultJson
         {
             Status = StatusType.Error,
             Data = errMsg == string.Empty ? "Faile to delete this item" : errMsg
         }));
     }
     catch (Exception ex)
     {
         NBCMSLoggerManager.Error("");
         NBCMSLoggerManager.Error(ex.Message);
         NBCMSLoggerManager.Error(ex.Source);
         NBCMSLoggerManager.Error(ex.StackTrace);
         NBCMSLoggerManager.Error("");
         return(Json(new NBCMSResultJson
         {
             Status = StatusType.Exception,
             Data = ex.Message
         }));
     }
 }
        /// <summary>
        /// 通过组合产品的ProductID来获取其子产品的各个价格,用于创建组合产品的时候,成功添加组合产品之后需要返回价格信息给前端展示使用
        /// CreateDate:2013年11月19日17:53:40
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public List <CMS_HM_Costing_Model> GetChildrenCost(CMS_HMGroup_Relation_Model model)
        {
            using (PermaisuriCMSEntities db = new PermaisuriCMSEntities())
            {
                var query = db.CMS_HMNUM.FirstOrDefault(c => c.ProductID == model.ProductID);
                if (query == null)
                {
                    return(null);
                }
                var costing = query.CMS_HMGroup_Relation.Select(r => new CMS_HM_Costing_Model
                {
                    HMNUM           = r.CMS_HMNUM_Children.CMS_HM_Costing.HMNUM,
                    FirstCost       = r.CMS_HMNUM_Children.CMS_HM_Costing.FirstCost.ConvertToNotNull().ToString("C", new CultureInfo("en-US")),
                    LandedCost      = r.CMS_HMNUM_Children.CMS_HM_Costing.LandedCost.ConvertToNotNull().ToString("C", new CultureInfo("en-US")),
                    EstimateFreight = r.CMS_HMNUM_Children.CMS_HM_Costing.EstimateFreight.ConvertToNotNull().ToString("C", new CultureInfo("en-US")),

                    OceanFreight      = r.CMS_HMNUM_Children.CMS_HM_Costing == null ? "$0.00" : r.CMS_HMNUM_Children.CMS_HM_Costing.OceanFreight.ConvertToNotNull().ToString("C", new CultureInfo("en-US")),
                    USAHandlingCharge = r.CMS_HMNUM_Children.CMS_HM_Costing == null ? "$0.00" : r.CMS_HMNUM_Children.CMS_HM_Costing.USAHandlingCharge.ConvertToNotNull().ToString("C", new CultureInfo("en-US")),
                    Drayage           = r.CMS_HMNUM_Children.CMS_HM_Costing == null ? "$0.00" : r.CMS_HMNUM_Children.CMS_HM_Costing.Drayage.ConvertToNotNull().ToString("C", new CultureInfo("en-US")),
                    SellSets          = r.SellSets
                }).ToList();
                return(costing);
            }
        }