Пример #1
0
        public static bool Save(TOffice entity)
        {
            using (MainDataContext dbContext = new MainDataContext())
            {
                try
                {
                    var model = dbContext.TOffice.FirstOrDefault(t => t.编码 == entity.编码);
                    if (model == null)  //添加
                    {
                        //添加到数据库
                        InsertReceiver(entity, dbContext);
                        dbContext.TOffice.InsertOnSubmit(entity);
                    }
                    else  //修改
                    {
                        //再插入人员接收反馈表

                        #region 发送目的地信息
                        ////先删除发送目的地信息
                        //var lisS = from b in dbContext.TOfficeSend
                        //           where b.办公编码 == entity.编码
                        //           select b;
                        //foreach (var l in lisS)
                        //{
                        //    dbContext.TOfficeSend.Remove(l);
                        //}

                        ////再插入发送目的地表
                        //var squery = from am in dbContext.TOfficeSend
                        //    select am.编码;
                        //int scode = 0;
                        //if (squery.Any())
                        //    scode = squery.Max() + 1;
                        //else
                        //    scode = 1;
                        ////int scode = 0;//主键
                        //int j = 0;
                        //TOfficeSend s;
                        //switch (entity.发送类型编码)
                        //{
                        //    case 2:
                        //        string[] branchIDs = entity.接收部门编码.Split(',');
                        //        foreach (string branchID in branchIDs)
                        //        {
                        //            s = new TOfficeSend();
                        //            s.编码 = scode + j;
                        //            s.办公编码 = entity.编码;
                        //            s.发送类型编码 = 2;
                        //            s.目的地编码 = branchID;
                        //            dbContext.TOfficeSend.Add(s);
                        //            j++;
                        //        }
                        //        break;
                        //    case 3:
                        //        foreach (string personID in personIDs)
                        //        {
                        //            s = new TOfficeSend();
                        //            s.编码 = scode + j;
                        //            s.办公编码 = entity.编码;
                        //            s.发送类型编码 = 3;
                        //            s.目的地编码 = personID;
                        //            dbContext.TOfficeSend.Add(s);
                        //            j++;
                        //        }
                        //        break;
                        //    default:
                        //        break;
                        //}
                        #endregion

                        #region 接收反馈信息
                        //先删除人员接收反馈信息
                        var lisR = from b in dbContext.TOfficeReceive
                                   where b.办公编码 == entity.编码
                                   select b;
                        foreach (var l in lisR)
                        {
                            dbContext.TOfficeReceive.DeleteOnSubmit(l);
                        }

                        InsertReceiver(entity, dbContext);
                        #endregion

                        ReflectionUtility.CopyObjectProperty <TOffice>(entity, model);
                    }

                    dbContext.SubmitChanges();

                    return(true);
                }
                catch (Exception ex)
                {
                    Log4Net.LogError("Office Save", ex.ToString());

                    return(false);
                }
            }
        }