.NET Core API Swagger 接口(WEB API)实现批量操作(批量添加、批量修改、批量删除 )

前端批量传值格式

 

 

 后端批量接受参数

 

 

 

代码 

 

  1 using System.Diagnostics;
  2 
  3 
  4 
  5 /// <summary>
  6         /// 批量添加班级(无判断)  json批量传参测试
  7         /// </summary>
  8         /// <param name="CM"></param>
  9         /// <returns></returns>
 10         [HttpPost]
 11         public ActionResult BatchAddClass([FromBody]List<Class_Model> CM) {
 12 
 13             List<Class> list = new List<Class>();
 14             int result = 0;
 15             string str="";
 16             foreach (var item in CM)
 17             {
 18                 Class c = new Class();
 19                 c.CId = item.CId;
 20                 c.CName = item.CName;
 21                 c.CMark = item.CMark;
 22                 c.CAddress = item.CAddress;
 23                 list.Add(c);
 24             }
 25 
 26 
 27             #region 批量添加班级事物
 28             using (TransactionScope transaction = new TransactionScope())
 29             {
 30                 try
 31                 {
 32                     Stopwatch watch = Stopwatch.StartNew();
 33                     _context.BulkInsert(list);
 34                     _context.BulkSaveChanges();
 35                     watch.Stop();
 36 
 37                     transaction.Complete();
 38                     result = 1;
 39 
 40                 }
 41                 catch (Exception)
 42                 {
 43                     result = 0;
 44                 }
 45             }
 46             #endregion
 47             if (result == 1)
 48             {
 49                 str = "批量添加成功";
 50             }
 51             else
 52             {
 53                 str = "批量添加失败";
 54             }
 55             return Content(str);
 56         }
 57 
 58         /// <summary>
 59         /// 批量修改班级
 60         /// </summary>
 61         /// <param name="CM"></param>
 62         /// <returns></returns>
 63         [HttpPost]
 64         public ActionResult BatchUpdateClass([FromBody] List<Class_Model> CM)
 65         {
 66 
 67             List<Class> list = new List<Class>();
 68 
 69             string Id_ISNull = "";
 70             int result = 0;
 71 
 72             foreach (var item in CM)
 73             {
 74                 Class s = _context.Classes.Where(e => e.CId == item.CId).FirstOrDefault();
 75                 if (s != null)//判断此班级是否存在,若存在即可修改班级
 76                 {
 77                     Class c = new Class();
 78                     c.CId = s.CId;//班级编号为主键,不可修改
 79                     c.CName = item.CName;
 80                     c.CMark = item.CMark;
 81                     c.CAddress = item.CAddress;
 82                     list.Add(c);
 83                 }
 84                 else {
 85                     //判断有哪些ID不存在
 86                     Id_ISNull = Id_ISNull + item.CId+" ";
 87                 }
 88 
 89             }
 90 
 91             #region 批量修改班级事物
 92             using (TransactionScope transaction = new TransactionScope())
 93             {
 94                 try
 95                 {
 96                     Stopwatch watch = Stopwatch.StartNew();
 97                     _context.BulkUpdate(list);
 98                     _context.BulkSaveChanges();
 99                     watch.Stop();
100 
101                     transaction.Complete();
102                     result = 1;
103 
104                 }
105                 catch (Exception)
106                 {
107                     result = 0;
108                 }
109             }
110             #endregion
111 
112             //ID不存在提示文本
113             if (Id_ISNull != "")
114             {
115                 Id_ISNull = ",ID为" + Id_ISNull + "的班级不存在";
116             }
117             if (result == 1)
118             {
119                 Id_ISNull = "批量修改成功" + Id_ISNull;
120             }
121             else
122             {
123                 Id_ISNull = "批量修改失败" + Id_ISNull;
124             }
125             return Content("执行完成, "+ Id_ISNull);
126         }
127 
128 
129         /// <summary>
130         /// 批量删除班级
131         /// </summary>
132         /// <param name="CM"></param>
133         /// <returns></returns>
134         [HttpPost]
135         public ActionResult BatchDeleteClass([FromBody] List<ID_Model> CM)
136         {
137             List<Class> Class_list = new List<Class>();
138             List<Student> Student_list = new List<Student>();
139             string Id_ISNull = "";
140             int result = 0;
141 
142             foreach (var item in CM)
143             {
144                 #region 添加批量需要删除班级列表
145 
146                 Class s = _context.Classes.Where(e => e.CId == item.ID).FirstOrDefault();
147                 if (s != null)
148                 {
149                     Class c = new Class();
150                     c.CId = item.ID;
151                     Class_list.Add(c);
152                 }
153                 else
154                 {
155                     //判断有哪些ID不存在
156                     Id_ISNull = Id_ISNull + item.ID + " ";
157                 }
158                 #endregion
159 
160                 #region 添加批量需要删除的学生列表
161                 List<Student> stu = _context.Students.Where(e => e.CId == item.ID).ToList();
162                 foreach (var item_stu in stu)
163                 {
164                     Student st = new Student();
165                     st.SId = item_stu.SId;
166                     Student_list.Add(st);
167                 }
168                 #endregion
169             }
170             #region 批量删除班级事物
171             using (TransactionScope transaction = new TransactionScope())
172             {
173                 try
174                 {
175                     Stopwatch watch = Stopwatch.StartNew();
176                     _context.BulkDelete(Student_list);
177                     //_context.BulkSaveChanges();
178                     _context.BulkDelete(Class_list);
179                     _context.BulkSaveChanges();//只需要更新执行一次
180                     watch.Stop();
181 
182                     transaction.Complete();
183                     result = 1;
184 
185                 }
186                 catch (Exception)
187                 {
188                     result = 0;
189                 }
190             }
191             #endregion
192 
193 
194             //ID不存在提示文本
195             if (Id_ISNull != "")
196             {
197                 Id_ISNull = ",ID为" + Id_ISNull + "的班级不存在";
198             }
199             if (result == 1)
200             {
201                 Id_ISNull = "批量删除班级成功" + Id_ISNull;
202             }
203             else
204             {
205                 Id_ISNull = "批量删除班级失败" + Id_ISNull;
206             }
207             return Content("执行完成, " + Id_ISNull);
208 
209 
210         }

 

posted @ 2022-08-30 11:17  Bruce_Sun  阅读(702)  评论(0)    收藏  举报