You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

534 lines
22 KiB

  1. using Learun.Application.Organization;
  2. using Learun.Application.TwoDevelopment.EducationalAdministration;
  3. using Learun.Application.TwoDevelopment.LR_Desktop;
  4. using Learun.Util;
  5. using Learun.Util.Operat;
  6. using Microsoft.AspNet.SignalR.Client;
  7. using Newtonsoft.Json;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Configuration;
  11. using System.Data;
  12. using System.Linq;
  13. using System.Threading.Tasks;
  14. using System.Web;
  15. using System.Web.Mvc;
  16. namespace Learun.Application.Web.Areas.EducationalAdministration.Controllers
  17. {
  18. /// <summary>
  19. /// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
  20. /// Copyright (c) 2013-2018 北京泉江科技有限公司
  21. /// 创 建:超级管理员
  22. /// 日 期:2019-02-27 11:05
  23. /// 描 述:排课
  24. /// </summary>
  25. public class ArrangeLessonTermController : MvcControllerBase
  26. {
  27. private ArrangeLessonTermIBLL arrangeLessonTermIBLL = new ArrangeLessonTermBLL();
  28. private ArrangeLessonTermOfElectiveIBLL arrangeLessonTermOfElectiveIBLL = new ArrangeLessonTermOfElectiveBLL();
  29. private Sys_InformationPushIBLL sys_InformationPushIBLL = new Sys_InformationPushBLL();
  30. private UserIBLL userIbll = new UserBLL();
  31. private WeChatConfigIBLL weChatConfigIbll = new WeChatConfigBLL();
  32. private WeChatTempletIBLL weChatTempletIbll = new WeChatTempletBLL();
  33. private SchoolCalendarIBLL schoolCalendarIbll = new SchoolCalendarBLL();
  34. #region 视图功能
  35. /// <summary>
  36. /// 主页面
  37. /// <summary>
  38. /// <returns></returns>
  39. [HttpGet]
  40. public ActionResult Index()
  41. {
  42. return View();
  43. }
  44. /// <summary>
  45. /// 表单页
  46. /// <summary>
  47. /// <returns></returns>
  48. [HttpGet]
  49. public ActionResult Form()
  50. {
  51. return View();
  52. }
  53. /// <summary>
  54. /// 按条件清空排课数据
  55. /// <summary>
  56. /// <returns></returns>
  57. [HttpGet]
  58. public ActionResult EmptyByConditionForm()
  59. {
  60. return View();
  61. }
  62. /// <summary>
  63. /// 按条件同步排课数据
  64. /// <summary>
  65. /// <returns></returns>
  66. [HttpGet]
  67. public ActionResult SyncByConditionForm()
  68. {
  69. return View();
  70. }
  71. [HttpGet]
  72. public ActionResult ClearByConditionForm()
  73. {
  74. return View();
  75. }
  76. [HttpGet]
  77. public ActionResult IndexLessonTerm()
  78. {
  79. return View();
  80. }
  81. #endregion
  82. #region 获取数据
  83. /// <summary>
  84. /// 获取列表数据
  85. /// <summary>
  86. /// <returns></returns>
  87. [HttpGet]
  88. [AjaxOnly]
  89. public ActionResult GetList(string queryJson)
  90. {
  91. var data = arrangeLessonTermIBLL.GetList(queryJson).ToList()
  92. .OrderBy(x => x.AcademicYearNo).ThenBy(x => x.Semester).ThenBy(x => x.DeptNo).ThenBy(x => x.MajorNo).ThenBy(x => x.LessonNo).ThenBy(x => x.TeachClassNo.Replace(x.LessonName, "")).ThenBy(x => x.EmpNo).ThenBy(x => x.ClassroomNo).ThenBy(x => x.LessonDate);
  93. return Success(data);
  94. }
  95. /// <summary>
  96. /// 获取列表分页数据
  97. /// <param name="pagination">分页参数</param>
  98. /// <summary>
  99. /// <returns></returns>
  100. [HttpGet]
  101. [AjaxOnly]
  102. public ActionResult GetPageList(string pagination, string queryJson)
  103. {
  104. Pagination paginationobj = pagination.ToObject<Pagination>();
  105. var data = arrangeLessonTermIBLL.GetPageList(paginationobj, queryJson);
  106. var jsonData = new
  107. {
  108. rows = data,
  109. total = paginationobj.total,
  110. page = paginationobj.page,
  111. records = paginationobj.records
  112. };
  113. return Success(jsonData);
  114. }
  115. /// <summary>
  116. /// 排课管理 获取左侧树结构
  117. /// <summary>
  118. /// <returns></returns>
  119. [HttpGet]
  120. [AjaxOnly]
  121. public ActionResult GetTree()
  122. {
  123. var data = arrangeLessonTermIBLL.GetTree();
  124. return Success(data);
  125. }
  126. /// <summary>
  127. /// 获取表单数据
  128. /// <param name="keyValue">主键</param>
  129. /// <summary>
  130. /// <returns></returns>
  131. [HttpGet]
  132. [AjaxOnly]
  133. public ActionResult GetFormData(string keyValue)
  134. {
  135. var data = arrangeLessonTermIBLL.GetEntity(keyValue);
  136. return Success(data);
  137. }
  138. /// <summary>
  139. /// 获取周次
  140. /// <summary>
  141. /// <returns></returns>
  142. [HttpGet]
  143. [AjaxOnly]
  144. public ActionResult GetWeekTime()
  145. {
  146. int weekTimes = 0;
  147. int curWeek = 0;
  148. //开始时间
  149. var startdate = DateTime.Today;
  150. var startDate = Common.CalculateFirstDateOfWeek(startdate).ToString("yyyy-MM-dd");
  151. //var endDate = Common.CalculateLastDateOfWeek(startdate).ToString("yyyy-MM-dd");
  152. var semesterAndYear = Common.GetSemesterAndYear(startDate);
  153. //var strAcademicYear = semesterAndYear.AcademicYearShort;
  154. //var strSemester = semesterAndYear.Semester;
  155. var entity = schoolCalendarIbll.GetSchoolCalendarEntityByNo(semesterAndYear.AcademicYearShort, semesterAndYear.Semester);
  156. if (entity.StartTime.HasValue && entity.EndTime.HasValue)
  157. {
  158. weekTimes = GetYearWeekCount(entity.StartTime.Value, entity.EndTime.Value);
  159. curWeek = WeekOfYear(startdate, entity.StartTime.Value, entity.EndTime.Value);
  160. }
  161. var listObj = new List<Object>();
  162. for (int i = 1; i <= weekTimes; i++)
  163. {
  164. listObj.Add(new { text = i + "周", value = i });
  165. }
  166. var jsonData = new
  167. {
  168. weekList = listObj,
  169. curWeek = curWeek,
  170. };
  171. return Success(jsonData);
  172. }
  173. /// <summary>
  174. /// 有多少周
  175. /// 返回 int
  176. /// </summary>
  177. /// <returns>int</returns>
  178. private static int GetYearWeekCount(DateTime startTime, DateTime endTime)
  179. {
  180. string returnStr = "";
  181. var startDate = DateTime.Parse(Common.CalculateFirstDateOfWeek(startTime).ToString("yyyy-MM-dd"));
  182. //int k = Convert.ToInt32(startTime.DayOfWeek);//得到开始时间的第一天是周几
  183. int countDay = endTime.Subtract(startDate).Days;
  184. int countWeek = countDay / 14 + 1;
  185. return countWeek;
  186. }
  187. /// <summary>
  188. /// 求当前日期是第几周
  189. /// </summary>
  190. /// <param name="date"></param>
  191. /// <returns></returns>
  192. private static int WeekOfYear(DateTime curDay, DateTime startTime, DateTime endTime)
  193. {
  194. int firstdayofweek = Convert.ToInt32(startTime.DayOfWeek);
  195. var startDate = DateTime.Parse(Common.CalculateFirstDateOfWeek(startTime).ToString("yyyy-MM-dd"));
  196. int k = Convert.ToInt32(startTime.DayOfWeek);//得到开始时间的第一天是周几
  197. int days = curDay.Subtract(startTime).Days;
  198. //int days = curDay.DayOfYear;
  199. int daysOutOneWeek = days - (14 - firstdayofweek);
  200. if (daysOutOneWeek <= 0)
  201. {
  202. return 1;
  203. }
  204. else
  205. {
  206. int weeks = daysOutOneWeek / 14;
  207. if (daysOutOneWeek % 14 != 0)
  208. weeks++;
  209. return weeks + 1;
  210. }
  211. }
  212. /// <summary>
  213. /// 课程表【教务】
  214. /// </summary>
  215. /// <param name="classNo">班级</param>
  216. /// <param name="curWeek">当前第几周</param>
  217. /// <returns></returns>
  218. [HttpGet]
  219. [AjaxOnly]
  220. public ActionResult GetDataInEducation(string classNo, int curWeek)
  221. {
  222. var userInfo = LoginUserInfo.Get();
  223. //开始时间
  224. var startdate = DateTime.Today;
  225. var startDate = Common.CalculateFirstDateOfWeek(startdate).ToString("yyyy-MM-dd");
  226. var endDate = Common.CalculateLastDateOfWeek(startdate).ToString("yyyy-MM-dd");
  227. var semesterAndYear = Common.GetSemesterAndYear(startDate);
  228. var strAcademicYear = semesterAndYear.AcademicYearLong;
  229. var strSemester = semesterAndYear.Semester;
  230. if (string.IsNullOrEmpty(classNo))
  231. {
  232. var res = new
  233. { schoolName = "课程表", semester = $"{strAcademicYear}学年度 第{strSemester}学期", weekList = "" };
  234. return JsonResult(res);
  235. }
  236. //校历
  237. var entity = schoolCalendarIbll.GetSchoolCalendarEntityByNo(semesterAndYear.AcademicYearShort, semesterAndYear.Semester);
  238. var StartTime = entity.StartTime.Value;
  239. //根据第几周,计算查询的开始和结束日期
  240. startDate = Common.CalculateFirstDateOfWeek(StartTime.AddDays(((curWeek - 1) * 14))).ToString("yyyy-MM-dd");//StartTime.AddDays(((curWeek - 1) * 14)).ToString("yyyy-MM-dd");
  241. if (curWeek == 1 && Convert.ToDateTime(startDate) < StartTime)
  242. {
  243. startDate = StartTime.ToString("yyyy-MM-dd");
  244. }
  245. endDate = Common.CalculateFirstDateOfWeek(StartTime.AddDays(((curWeek - 1) * 14))).AddDays(10)
  246. .ToString("yyyy-MM-dd");// StartTime.AddDays(((curWeek - 1) * 14)).AddDays(10).ToString("yyyy-MM-dd");
  247. var timeTableList = new List<TimeTable>();
  248. //课程表
  249. var data = arrangeLessonTermIBLL.GetTimeTableInEducation(startDate, endDate, classNo, "", "");
  250. timeTableList.AddRange(data);
  251. //选修课课程表
  252. //var dataOfElective = arrangeLessonTermOfElectiveIBLL.GetTimeTableInEducation(startDate, endDate, classNo, "", "");
  253. //timeTableList.AddRange(dataOfElective);
  254. var timeTables = timeTableList.ToList();
  255. var noDataResult = new
  256. { schoolName = "课程表", semester = $"{strAcademicYear}学年度 第{strSemester}学期", weekList = "" };
  257. if (!timeTables.Any())
  258. {
  259. return JsonResult(noDataResult);
  260. }
  261. var formatData = from d in timeTables.AsEnumerable()
  262. let tt = d.LessonTime.Split('-')[1]
  263. group d by tt into g
  264. orderby g.Key
  265. select new
  266. {
  267. time = g.Key,
  268. list = from e in timeTables.AsEnumerable()
  269. let ee = e.LessonTime.Split('-')[1]
  270. where ee == g.Key
  271. select new
  272. {
  273. ALTId = e.ALTId,
  274. day = e.LessonTime.Split('-')[0],
  275. curriculum = e.LessonSortNo == "2" ? e.LessonName + "[选修]" : e.LessonName,
  276. teacher = e?.EmpName,
  277. classRoom = string.IsNullOrEmpty(e.ClassroomName) ? "" : e.ClassroomName.Trim(),
  278. academicyear = semesterAndYear.AcademicYearShort,
  279. semester = strSemester,
  280. lessonNo = e?.LessonNo,
  281. teachClassNo = e?.TeachClassNo,
  282. empno = e?.EmpNo,
  283. lessonTime = e.LessonTime,
  284. lessonDate = e.LessonDate.ToString("yyyy-MM-dd"),
  285. classRoomNo = string.IsNullOrEmpty(e.ClassRoomNo) ? "" : e.ClassRoomNo.Trim(),
  286. lessonSortNo = e.LessonSortNo,
  287. e?.OLPEId,
  288. className = string.IsNullOrEmpty(e.ClassName) ? "" : e.ClassName
  289. }
  290. }
  291. ;
  292. var result = new
  293. { schoolName = "课程表", semester = $"{strAcademicYear}学年度 第{strSemester}学期", weekList = formatData };
  294. return JsonResult(result);
  295. }
  296. #endregion
  297. #region 提交数据
  298. /// <summary>
  299. /// 删除实体数据
  300. /// <param name="keyValue">主键</param>
  301. /// <summary>
  302. /// <returns></returns>
  303. [HttpPost]
  304. [AjaxOnly]
  305. public ActionResult DeleteForm(string keyValue)
  306. {
  307. arrangeLessonTermIBLL.DeleteEntity(keyValue);
  308. return Success("删除成功!");
  309. }
  310. /// <summary>
  311. /// 保存实体数据(新增、修改)
  312. /// <param name="keyValue">主键</param>
  313. /// <summary>
  314. /// <returns></returns>
  315. [HttpPost]
  316. [ValidateAntiForgeryToken]
  317. [AjaxOnly]
  318. public ActionResult SaveForm(string keyValue, ArrangeLessonTermEntity entity)
  319. {
  320. arrangeLessonTermIBLL.SaveEntity(keyValue, entity);
  321. return Success("保存成功!");
  322. }
  323. /// <summary>
  324. /// 删除课程
  325. /// <param name="keyValue">主键</param>
  326. /// <summary>
  327. /// <returns></returns>
  328. [HttpPost]
  329. [AjaxOnly]
  330. public ActionResult DeleteLessonTerm(string keyValue, string WeekTime)
  331. {
  332. arrangeLessonTermIBLL.DeleteLessonTerm(keyValue, WeekTime);
  333. return Success("删除成功!");
  334. }
  335. /// <summary>
  336. ///修改课程
  337. /// <param name="keyValue">主键</param>
  338. /// <summary>
  339. /// <returns></returns>
  340. [HttpPost]
  341. [AjaxOnly]
  342. public ActionResult UpdateLessonTerm(string keyValue, string postdata)
  343. {
  344. UpdateLessonTermEntity model = postdata.ToObject<UpdateLessonTermEntity>();
  345. var res = arrangeLessonTermIBLL.UpdateLessonTerm(keyValue, model);
  346. if (!string.IsNullOrEmpty(res))
  347. {
  348. return Fail(res);
  349. }
  350. return Success("操作成功!");
  351. }
  352. #endregion
  353. /// <summary>
  354. /// 当前学期排课数据同步
  355. /// </summary>
  356. /// <returns></returns>
  357. [HttpGet]
  358. [AjaxOnly]
  359. public async Task<ActionResult> AsyncArrangeLessonData()
  360. {
  361. var data = await arrangeLessonTermIBLL.AsyncArrangeLessonData();
  362. return Success(data);
  363. }
  364. /// <summary>
  365. /// 清空当前学期排课数据
  366. /// </summary>
  367. /// <returns></returns>
  368. [HttpGet]
  369. [AjaxOnly]
  370. public async Task<ActionResult> AsyncModifyArrangeLessonData()
  371. {
  372. var data = await arrangeLessonTermIBLL.AsyncModifyArrangeLessonData();
  373. return Success(data);
  374. }
  375. /// <summary>
  376. /// 按条件清空排课数据
  377. /// </summary>
  378. /// <returns></returns>
  379. [HttpPost]
  380. [AjaxOnly]
  381. public async Task<ActionResult> AsyncModifyArrangeLessonDataByCondition(ArrangeLessonTermEntity entity)
  382. {
  383. var data = await arrangeLessonTermIBLL.AsyncModifyArrangeLessonDataByCondition(entity);
  384. return Success(data);
  385. }
  386. /// <summary>
  387. /// 按条件同步排课数据
  388. /// </summary>
  389. /// <returns></returns>
  390. [HttpPost]
  391. [AjaxOnly]
  392. public async Task<ActionResult> AsyncArrangeLessonDataByCondition(ArrangeLessonTermEntity entity)
  393. {
  394. var data = await arrangeLessonTermIBLL.AsyncArrangeLessonDataByCondition(entity);
  395. if (data)
  396. {
  397. //读取信息推送管理-课表同步推送(09)的配置
  398. var informationPushEntity = sys_InformationPushIBLL.GetEntityByPushItem("09");
  399. if (informationPushEntity != null && informationPushEntity.Status == true)
  400. {
  401. var title = string.Format("{0}学年第{1}学期必修课课表同步", entity.AcademicYearNo, entity.Semester);
  402. var needpostuserlist = userIbll.GetAllList().Where(m => m.F_DeleteMark != 1 && m.F_EnabledMark == 1 && m.F_Description == "教师").ToList();
  403. //微信推送
  404. try
  405. {
  406. PushWeixin(needpostuserlist, title);
  407. }
  408. catch (Exception e)
  409. {
  410. }
  411. //飞星推送
  412. await Task.Run(async () =>
  413. {
  414. using (var hubConnection = new HubConnection(ConfigurationManager.AppSettings["CommunicationServeraddress"]))
  415. {
  416. var hubProxy = hubConnection.CreateHubProxy("SignalRHub");
  417. await hubConnection.Start();
  418. await hubProxy.Invoke("PushAnnouncement", LoginUserInfo.Get().userId, "课表同步", Util.Str.ReplaceHtml(HttpUtility.HtmlDecode(title)).Length < 20 ? Util.Str.ReplaceHtml(HttpUtility.HtmlDecode(title)) : Util.Str.ReplaceHtml(HttpUtility.HtmlDecode(title)).Substring(0, 20), "synclesson", string.Join(",", needpostuserlist.Select(m => m.F_UserId)), "");
  419. }
  420. });
  421. }
  422. }
  423. return Success(data);
  424. }
  425. /// <summary>
  426. /// 按条件重置基础数据同步状态
  427. /// </summary>
  428. /// <param name="entity"></param>
  429. /// <returns></returns>
  430. [HttpPost]
  431. [AjaxOnly]
  432. public ActionResult InitAsyncDataByCondition(ArrangeLessonTermEntity entity)
  433. {
  434. var data = arrangeLessonTermIBLL.InitAsyncDataByCondition(entity);
  435. return Success(data);
  436. }
  437. public void PushWeixin(List<UserEntity> needpostuserlist, string title)
  438. {
  439. var WeChatConfigentity = weChatConfigIbll.GetEnableEntity();
  440. string appid = WeChatConfigentity.APPId;
  441. string secret = WeChatConfigentity.secret;
  442. var wechatemplete = weChatTempletIbll.GetWeChatTemplateEntityByCodeConfigId(WeChatConfigentity.ID, "task");
  443. string weixintaskurl = wechatemplete.TUrl;
  444. string weixintasktempid = wechatemplete.TempId;
  445. var responsejson = Util.HttpMethods.HttpGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret);
  446. OperateLogModel operateLogModel = new OperateLogModel();
  447. operateLogModel.title = title;
  448. operateLogModel.type = OperationType.Other;
  449. operateLogModel.url = "ArrangeLessonTermController";
  450. operateLogModel.sourceObjectId = "002";
  451. operateLogModel.sourceContentJson = responsejson;
  452. OperatorHelper.Instance.WriteOperateLog(operateLogModel);
  453. foreach (UserEntity userinfo in needpostuserlist)
  454. {
  455. if (userinfo != null && !string.IsNullOrEmpty(userinfo.OpenIdForWeixin))
  456. {
  457. //执行推送任务
  458. if (!string.IsNullOrEmpty(appid) && !string.IsNullOrEmpty(secret) && !string.IsNullOrEmpty(weixintaskurl) && !string.IsNullOrEmpty(weixintasktempid))
  459. {
  460. if (!string.IsNullOrEmpty(responsejson))
  461. {
  462. var weixintokenobj = JsonConvert.DeserializeObject<dynamic>(responsejson);
  463. if (string.IsNullOrEmpty(weixintokenobj.errcode))
  464. {
  465. string access_token = weixintokenobj.access_token;
  466. string jsondata = "{\"touser\":\"" + userinfo.OpenIdForWeixin + "\"," +
  467. "\"template_id\":\"" + weixintasktempid + "\"," +
  468. "\"url\":\"" + weixintaskurl + "\"," +
  469. "\"data\":{" +
  470. "\"first\": {\"value\":\"您有新的课表同步\",\"color\":\"#173177\"}," +
  471. "\"keyword1\":{\"value\":\"课表同步\",\"color\":\"#173177\"}," +
  472. "\"keyword2\": {\"value\":\"" + title + "\",\"color\":\"#173177\"}," +
  473. "\"keyword3\": {\"value\":\"待查看\",\"color\":\"#173177\"}," +
  474. "\"keyword4\": {\"value\":\"您有新的课表同步【" + title + "】\",\"color\":\"#173177\"}" +
  475. "}" +
  476. "}";
  477. string pushresult = Util.HttpMethods.HttpPost("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + access_token, jsondata);
  478. operateLogModel.title = title;
  479. operateLogModel.type = OperationType.Other;
  480. operateLogModel.url = "ArrangeLessonTermController";
  481. operateLogModel.sourceObjectId = "002";
  482. operateLogModel.sourceContentJson = pushresult;
  483. OperatorHelper.Instance.WriteOperateLog(operateLogModel);
  484. }
  485. }
  486. }
  487. }
  488. }
  489. }
  490. }
  491. }