CoachController.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. using Ant.Data;
  2. using Ant.ORM;
  3. using Ant.SuperSocket.Common;
  4. using Ant.Service.Common;
  5. using Ant.Service.Common.Enums;
  6. using Central.Control.Domain;
  7. using MES.Production.Entity;
  8. using MES.Production.Entity.Entity;
  9. using MES.Production.Entity.Enum;
  10. using Ant.Service.Mongodb;
  11. using MES.Production.Service.IService;
  12. using MES.Production.Service.ServiceImp;
  13. using Ant.Service.Utility;
  14. using ChangFa.Machinery.WebPage.Areas.SysManage.Models;
  15. using ChangFa.Machinery.WebPage.Controllers;
  16. using MongoDB.Driver.Builders;
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Data;
  20. using System.IO;
  21. using System.Linq;
  22. using System.Text;
  23. using System.Web;
  24. using System.Web.Mvc;
  25. using JsonHelper = Ant.Service.Common.JsonHelper;
  26. namespace ChangFa.Machinery.WebPage.Areas.SysManage.Controllers
  27. {
  28. public class CoachController : BaseController
  29. {
  30. [UserAuthorizeAttribute(ModuleAlias = "coachList", OperaAction = "View")]
  31. public ActionResult CoachList()
  32. {
  33. try
  34. {
  35. PageInfo pageInfo = new PageInfo(base.page, base.pagesize, 0, JsonConverter.JsonClass(new List<EntYW_Bicycle>()));
  36. #region 处理查询参数
  37. var nameOrProject = Request.QueryString["nameOrProject"];
  38. #endregion
  39. #region 加载列表
  40. using (AntORM orm = new AntORM())
  41. {
  42. orm.db = DataAccessFactory.CreateDataConnection("CyclingItem");
  43. RequestModel res = new RequestModel();
  44. res.newSt = new SqlNote() { Author = "季健国", NewSt = new System.Diagnostics.StackTrace(true), SqlDesc = "获取教练员列表" };
  45. var coachList = orm.Queryable<YM_Coach>();
  46. int pageno = (base.page - 1) * base.pagesize;
  47. if (!nameOrProject.IsEmpty())
  48. {
  49. coachList = coachList.Where(p => (!string.IsNullOrEmpty(p.Name) && p.Name.Contains(nameOrProject)) || (!string.IsNullOrEmpty(p.Name) && p.Project.Contains(nameOrProject)));
  50. }
  51. if (CurrentUser.USERTYPE == UserRoleType.School.GetHashCode())
  52. {
  53. if (CurrentUser.DptInfo.IfNotNull())
  54. {
  55. if (!CurrentUser.DptInfo.ID.IsNullOrEmpty())
  56. {
  57. var schoolID = CurrentUser.DptInfo.ID;
  58. coachList = coachList.Where(p => p.SchoolID == schoolID);
  59. }
  60. }
  61. }
  62. var reslut = coachList.OrderByDesc(p => p.UpdateTime).Skip(pageno).Top(pagesize).ToList(res);
  63. if (reslut.IsSuccess)
  64. {
  65. reslut.RecordNum = coachList.Count();
  66. pageInfo = new PageInfo(base.page, base.pagesize, reslut.RecordNum, JsonConverter.JsonClass(reslut.ResultModel));
  67. }
  68. }
  69. ViewBag.Search = base.keywords;
  70. ViewData["nameOrProject"] = nameOrProject;
  71. return View(pageInfo);
  72. #endregion
  73. }
  74. catch (Exception e)
  75. {
  76. WriteLog(enumOperator.Select, "获取教练员列表:", e);
  77. throw e.InnerException;
  78. }
  79. }
  80. public ActionResult Delete(string idList)
  81. {
  82. JsonHelper json = new JsonHelper() { Status = "n", Msg = "删除教练成功" };
  83. try
  84. {
  85. //是否为空
  86. if (string.IsNullOrEmpty(idList)) { json.Msg = "未找到要删除的教练"; return Json(json); }
  87. string[] id = idList.Trim(',').Split(',');
  88. for (int i = 0; i < id.Length; i++)
  89. {
  90. var userId = (id[i]);
  91. using (AntORM orm = new AntORM())
  92. {
  93. orm.db = DataAccessFactory.CreateDataConnection("CyclingItem");
  94. RequestModel request = new RequestModel
  95. {
  96. newSt = new SqlNote() { Author = "季健国", NewSt = new System.Diagnostics.StackTrace(true), SqlDesc = "删除教练" },
  97. Oid = userId.ToString()
  98. };
  99. var reslut = orm.DeleteById<YM_Coach>(request);
  100. if (reslut.IsSuccess)
  101. {
  102. json.Status = "y";
  103. WriteLog(enumOperator.Remove, json.Msg, enumLog4net.WARN);
  104. }
  105. }
  106. }
  107. }
  108. catch (Exception e)
  109. {
  110. json.Msg = "删除教练发生内部错误!";
  111. WriteLog(enumOperator.Remove, "删除教练:", e);
  112. }
  113. return Json(json);
  114. }
  115. /// <summary>
  116. ///
  117. /// </summary>
  118. /// <param name="entity"></param>
  119. /// <returns></returns>
  120. [UserAuthorize(ModuleAlias = "coachList", OperaAction = "Add,Edit")]
  121. [ValidateInput(false)]
  122. [HttpPost]
  123. public ActionResult Save(YM_Coach entity)
  124. {
  125. JsonHelper json = new JsonHelper() { Msg = "保存教练成功", Status = "n", ReUrl = "/Sys/Coach/CoachList" };
  126. try
  127. {
  128. using (AntORM orm = new AntORM())
  129. {
  130. orm.db = DataAccessFactory.CreateDataConnection("CyclingItem");
  131. RequestModel request = new RequestModel();
  132. request.newSt = new SqlNote() { Author = "季健国", NewSt = new System.Diagnostics.StackTrace(true), SqlDesc = "保存教练" };
  133. entity.Project = Request["Projects"].ToString();
  134. if (CurrentUser.USERTYPE == UserRoleType.School.GetHashCode())//用户是学校角色
  135. {
  136. if (CurrentUser.DptInfo.IfNotNull())
  137. {
  138. if (!CurrentUser.DptInfo.ID.IsNullOrEmpty())
  139. {
  140. var schoolID = CurrentUser.DptInfo.ID;
  141. entity.SchoolID = schoolID;
  142. entity.SchoolName = CurrentUser.DptInfo.Name;
  143. }
  144. }
  145. }
  146. if (!entity.IDCard.IsEmpty())
  147. {
  148. if (!CodeHelper.CheckIDCard(entity.IDCard))
  149. {
  150. json.Status = "n";
  151. json.Msg = "身份证无效,请重新核对!";
  152. return Json(json);
  153. }
  154. }
  155. if (!entity.MobilePhone.IsEmpty())
  156. {
  157. if (!CodeHelper.IsPhoneNo(entity.MobilePhone))
  158. {
  159. json.Status = "n";
  160. json.Msg = "手机号无效,请重新核对!";
  161. return Json(json);
  162. }
  163. }
  164. entity.Updator = entity.Creator;
  165. entity.UpdateTime = entity.CreateTime;
  166. if (entity.ID > 0)
  167. {
  168. request.Oid = entity.ID.ToString();
  169. var reslut = orm.UpdateById(entity, request);
  170. if (reslut.IsSuccess)
  171. {
  172. json.Status = "y";
  173. }
  174. else
  175. {
  176. json.Msg = "保存教练失败";
  177. }
  178. }
  179. else
  180. {
  181. entity.Creator = this.CurrentUser.Name;
  182. entity.CreateTime = DateTime.Now;
  183. var reslut = orm.Save(entity, request);
  184. if (reslut.IsSuccess)
  185. {
  186. json.Status = "y";
  187. }
  188. else
  189. {
  190. json.Msg = "保存教练失败";
  191. }
  192. }
  193. }
  194. }
  195. catch (Exception ex)
  196. {
  197. json.Msg = "保存教练发生内部错误!";
  198. WriteLog(enumOperator.None, "保存教练:", ex);
  199. }
  200. return Json(json);
  201. }
  202. [UserAuthorize(ModuleAlias = "coachList", OperaAction = "Detail")]
  203. public ActionResult Detail(int? id)
  204. {
  205. YM_CoachExted mod = new YM_CoachExted();
  206. if (id != null)
  207. {
  208. using (AntORM orm = new AntORM())
  209. {
  210. try
  211. {
  212. orm.db = DataAccessFactory.CreateDataConnection("CyclingItem");
  213. RequestModel request = new RequestModel
  214. {
  215. newSt = new SqlNote() { Author = "季健国", NewSt = new System.Diagnostics.StackTrace(true), SqlDesc = "查询教练的单个实体方法" },
  216. Oid = id.ToString()
  217. };
  218. var reslut = orm.GetEntity<YM_Coach>(request);
  219. if (reslut.IsSuccess)
  220. {
  221. mod = JsonHelper.FromJson<YM_CoachExted>(JsonHelper.ToJson(reslut.ResultModel));
  222. mod.Projects = (mod.Project ?? string.Empty).Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
  223. }
  224. }
  225. catch (Exception e)
  226. {
  227. }
  228. }
  229. }
  230. List<SelectListItem> items = new List<SelectListItem>();
  231. items.Add(new SelectListItem { Text = "请选择", Value = "" });
  232. using (AntORM orm = new AntORM())
  233. {
  234. orm.db = DataAccessFactory.CreateDataConnection("CyclingItem");
  235. RequestModel res = new RequestModel
  236. {
  237. newSt = new SqlNote() { Author = "季健国", NewSt = new System.Diagnostics.StackTrace(true), SqlDesc = "查询学校" },
  238. };
  239. var result = orm.Queryable<YM_School>().ToList(res);
  240. foreach (var item in result.ResultModel)
  241. {
  242. items.Add(new SelectListItem { Text = item.Name, Value = item.ID.ToString() });
  243. }
  244. }
  245. ViewBag.Schools = items;
  246. items = new List<SelectListItem>();
  247. items.Add(new SelectListItem { Text = "请选择", Value = "" });
  248. var sysCodes = CommonEnumHelper.LoadDic(CommonCodeType.性别);
  249. foreach (var item in sysCodes)
  250. {
  251. items.Add(new SelectListItem { Text = item.NAMETEXT, Value = item.NAMETEXT });
  252. }
  253. ViewBag.Sexs = items;
  254. items = new List<SelectListItem>();
  255. sysCodes = CommonEnumHelper.LoadDic(CommonCodeType.运动分类);
  256. foreach (var item in sysCodes)
  257. {
  258. if (!string.IsNullOrWhiteSpace(mod.Project) && mod.Project.IndexOf(item.NAMETEXT) >= 0)
  259. {
  260. items.Add(new SelectListItem { Text = item.NAMETEXT, Selected = true, Value = item.NAMETEXT });
  261. }
  262. else
  263. {
  264. items.Add(new SelectListItem { Text = item.NAMETEXT, Value = item.NAMETEXT });
  265. }
  266. }
  267. MultiSelectList multiList = new MultiSelectList(items, "Value", "Text");
  268. ViewBag.MultiProjects = multiList;
  269. return View(mod);
  270. }
  271. public FileResult ExportExecl()
  272. {
  273. try
  274. {
  275. NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
  276. //添加一个sheet
  277. NPOI.SS.UserModel.ISheet sheet1 = book.CreateSheet("武进区学校体育教练员统计表");
  278. //给sheet1添加第一行的头部标题
  279. NPOI.SS.UserModel.IRow row1 = sheet1.CreateRow(0);
  280. row1.CreateCell(0).SetCellValue("教练姓名");
  281. row1.CreateCell(1).SetCellValue("性别");
  282. row1.CreateCell(2).SetCellValue("学校");
  283. row1.CreateCell(3).SetCellValue("职称");
  284. row1.CreateCell(4).SetCellValue("指导项目");
  285. #region 处理查询参数
  286. var nameOrProject = Request.QueryString["nameOrProject"];
  287. #endregion
  288. #region 加载列表
  289. using (AntORM orm = new AntORM())
  290. {
  291. orm.db = DataAccessFactory.CreateDataConnection("CyclingItem");
  292. RequestModel res = new RequestModel();
  293. res.newSt = new SqlNote() { Author = "季健国", NewSt = new System.Diagnostics.StackTrace(true), SqlDesc = "获取教练员列表" };
  294. var coachList = orm.Queryable<YM_Coach>()
  295. .InnerJoin<YM_School>((coach, school) => coach.SchoolID == school.ID);
  296. int pageno = (base.page - 1) * base.pagesize;
  297. var views = coachList.Select((coach, school) => new
  298. {
  299. Coach = coach,
  300. School = school
  301. });
  302. if (!nameOrProject.IsEmpty())
  303. {
  304. views = views.Where(p => (!string.IsNullOrEmpty(p.Coach.Name) && p.Coach.Name.Contains(nameOrProject)) || (!string.IsNullOrEmpty(p.Coach.Name) && p.Coach.Project.Contains(nameOrProject)));
  305. }
  306. var reslut = views.OrderByDesc(p => p.Coach.UpdateTime).Skip(pageno).Top(pagesize).ToList(res);
  307. if (reslut.IsSuccess)
  308. {
  309. int rowIndex = 1;
  310. foreach (var item in reslut.ResultModel)
  311. {
  312. NPOI.SS.UserModel.IRow rowtemp = sheet1.CreateRow(rowIndex);
  313. rowtemp.CreateCell(0).SetCellValue(item.Coach.Name);
  314. rowtemp.CreateCell(1).SetCellValue(item.Coach.Sex);
  315. rowtemp.CreateCell(2).SetCellValue(item.School.Name);
  316. rowtemp.CreateCell(3).SetCellValue(item.Coach.Title);
  317. rowtemp.CreateCell(4).SetCellValue(item.Coach.Project);
  318. rowIndex++;
  319. }
  320. }
  321. }
  322. #endregion
  323. // 写入到客户端
  324. System.IO.MemoryStream ms = new System.IO.MemoryStream();
  325. book.Write(ms);
  326. ms.Seek(0, SeekOrigin.Begin);
  327. return File(ms, "application/vnd.ms-excel", "武进区学校体育教练员统计表.xls");
  328. }
  329. catch (Exception e)
  330. {
  331. throw e.InnerException;
  332. }
  333. }
  334. }
  335. }