public async Task <IActionResult> DeleteType(int[] type_id)
        {
            using (var tran = context.Database.BeginTransaction())
            {
                try
                {
                    var list  = new List <AppFixvalueType>();
                    var list1 = new List <AppFixvalue>();
                    for (int i = 0; i < type_id.Length; i++)
                    {
                        var single1 = await context.AppFixvalues.Where(u => u.FixvalueTypeId == type_id[i]).ToListAsync();

                        if (single1.Count > 0)
                        {
                            context.AppFixvalues.RemoveRange(single1);
                            context.SaveChanges();
                        }
                        var single = await context.AppFixvalueTypes.SingleOrDefaultAsync(u => u.FixvalueTypeId == type_id[i]);

                        if (single != null)
                        {
                            list.Add(single);
                        }
                    }
                    if (list.Count > 0)
                    {
                        context.AppFixvalueTypes.RemoveRange(list);
                        context.SaveChanges();
                    }
                    await tran.CommitAsync();

                    logger.LogInformation(HttpContext.Session.GetString("who") + "删除公共代码类型成功。");
                    return(Json(new { code = 200, msg = "删除成功" }));
                }
                catch (Exception ex)
                {
                    await tran.RollbackAsync();

                    logger.LogInformation(HttpContext.Session.GetString("who") + "删除公共代码类型失败。" + ex.Message);
                    return(Json(new { code = 300, msg = "删除失败" }));
                }
            }
        }
示例#2
0
        public async Task <IActionResult> Delete(int[] id)
        {
            using (var tran = context.Database.BeginTransaction())
            {
                try
                {
                    for (int i = 0; i < id.Length; i++)
                    {
                        var single = await context.AppCorps.SingleOrDefaultAsync(u => u.CorpId == id[i]);

                        if (single != null)
                        {
                            context.AppCorps.Remove(single);
                            context.SaveChanges();
                            //银行账号
                            var list1 = await context.AppCorpBanks.Where(u => u.CorpId == id[i]).ToListAsync();

                            if (list1.Count > 0)
                            {
                                context.AppCorpBanks.RemoveRange(list1);
                                context.SaveChanges();
                            }
                            //部门
                            var list2 = await context.AppDepts.Where(u => u.CorpId == id[i]).ToListAsync();

                            if (list2.Count > 0)
                            {
                                for (int j = 0; j < list2.Count; j++)
                                {
                                    //岗位
                                    var list3 = await context.AppPosts.Where(u => u.DeptId == list2[j].DeptId).ToListAsync();

                                    if (list3.Count > 0)
                                    {
                                        context.AppPosts.RemoveRange(list3);
                                        context.SaveChanges();
                                    }
                                }
                                context.AppDepts.RemoveRange(list2);
                                context.SaveChanges();
                            }
                            var list4 = await context.AppUsers.Where(u => u.CorpId == id[i]).ToListAsync();

                            if (list4.Count > 0)
                            {
                                for (int k = 0; k < list4.Count; k++)
                                {
                                    list4[i].CorpId           = -999;
                                    list4[i].DeptId           = -999;
                                    list4[i].PostId           = -999;
                                    list4[i].Status           = "失效";
                                    list4[4].LastModifiedDate = DateTime.Now;
                                    list4[i].LastModifiedUser = HttpContext.Session.GetInt32("user_id");
                                    context.AppUsers.Update(list4[i]);
                                    context.SaveChanges();
                                }
                            }
                        }
                    }
                    await tran.CommitAsync();

                    logger.LogInformation(HttpContext.Session.GetString("who") + "删除公司成功。");
                    return(Json(new { code = 200, msg = "删除成功" }));
                }
                catch (Exception ex)
                {
                    await tran.RollbackAsync();

                    logger.LogInformation(HttpContext.Session.GetString("who") + "删除公司失败。" + ex.Message);
                    return(Json(new { code = 300, msg = "删除失败" }));
                }
            }
        }