using Learun.Application.Organization; using Learun.Application.TwoDevelopment.EducationalAdministration; using Learun.Application.TwoDevelopment.LR_Desktop; using Learun.Util; using Learun.Util.Operat; using Microsoft.AspNet.SignalR.Client; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Linq; using System.Threading.Tasks; using System.Web; using System.Web.Mvc; namespace Learun.Application.Web.Areas.EducationalAdministration.Controllers { /// /// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园 /// Copyright (c) 2013-2018 北京泉江科技有限公司 /// 创 建:超级管理员 /// 日 期:2019-02-27 11:05 /// 描 述:排课 /// public class ArrangeLessonTermController : MvcControllerBase { private ArrangeLessonTermIBLL arrangeLessonTermIBLL = new ArrangeLessonTermBLL(); private ArrangeLessonTermOfElectiveIBLL arrangeLessonTermOfElectiveIBLL = new ArrangeLessonTermOfElectiveBLL(); private Sys_InformationPushIBLL sys_InformationPushIBLL = new Sys_InformationPushBLL(); private UserIBLL userIbll = new UserBLL(); private WeChatConfigIBLL weChatConfigIbll = new WeChatConfigBLL(); private WeChatTempletIBLL weChatTempletIbll = new WeChatTempletBLL(); private SchoolCalendarIBLL schoolCalendarIbll = new SchoolCalendarBLL(); private StuInfoBasicIBLL stuInfoBasicIbll = new StuInfoBasicBLL(); private EmpInfoIBLL empInfoIbll = new EmpInfoBLL(); #region 视图功能 /// /// 主页面 /// /// [HttpGet] public ActionResult Index() { return View(); } /// /// 表单页 /// /// [HttpGet] public ActionResult Form() { return View(); } /// /// 按条件清空排课数据 /// /// [HttpGet] public ActionResult EmptyByConditionForm() { return View(); } /// /// 按条件同步排课数据 /// /// [HttpGet] public ActionResult SyncByConditionForm() { return View(); } [HttpGet] public ActionResult ClearByConditionForm() { return View(); } [HttpGet] public ActionResult IndexLessonTerm() { return View(); } /// /// 学生课表 /// /// [HttpGet] public ActionResult IndexLessonTermStu() { var LogInfo = LoginUserInfo.Get(); if (LogInfo.Description == "学生") { var StuInfo = stuInfoBasicIbll.GetAllList().Where(x => x.StuNo == LogInfo.account); if (StuInfo != null) { ViewBag.ClassNo = StuInfo.FirstOrDefault().ClassNo; } } return View(); } /// /// 教师课表 /// /// [HttpGet] public ActionResult IndexLessonTermTeach() { var LogInfo = LoginUserInfo.Get(); if (LogInfo.Description == "教师") { var empInfo = empInfoIbll.GetAllList().Where(x => x.EmpNo == LogInfo.account); if (empInfo != null) { ViewBag.EmpNo = empInfo.FirstOrDefault().EmpNo; } } return View(); } #endregion #region 获取数据 /// /// 获取列表数据 /// /// [HttpGet] [AjaxOnly] public ActionResult GetList(string queryJson) { var data = arrangeLessonTermIBLL.GetList(queryJson).ToList() .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); return Success(data); } /// /// 获取列表分页数据 /// 分页参数 /// /// [HttpGet] [AjaxOnly] public ActionResult GetPageList(string pagination, string queryJson) { Pagination paginationobj = pagination.ToObject(); var data = arrangeLessonTermIBLL.GetPageList(paginationobj, queryJson); var jsonData = new { rows = data, total = paginationobj.total, page = paginationobj.page, records = paginationobj.records }; return Success(jsonData); } /// /// 排课管理 获取左侧树结构 /// /// [HttpGet] [AjaxOnly] public ActionResult GetTree() { var data = arrangeLessonTermIBLL.GetTree(); return Success(data); } /// /// 获取表单数据 /// 主键 /// /// [HttpGet] [AjaxOnly] public ActionResult GetFormData(string keyValue) { var data = arrangeLessonTermIBLL.GetEntity(keyValue); return Success(data); } /// /// 获取周次 /// /// [HttpGet] [AjaxOnly] public ActionResult GetWeekTime() { int weekTimes = 0; int curWeek = 0; //开始时间 var startdate = DateTime.Today; var startDate = Common.CalculateFirstDateOfWeek(startdate).ToString("yyyy-MM-dd"); //var endDate = Common.CalculateLastDateOfWeek(startdate).ToString("yyyy-MM-dd"); var semesterAndYear = Common.GetSemesterAndYear(startDate); //var strAcademicYear = semesterAndYear.AcademicYearShort; //var strSemester = semesterAndYear.Semester; var entity = schoolCalendarIbll.GetSchoolCalendarEntityByNo(semesterAndYear.AcademicYearShort, semesterAndYear.Semester); if (entity.StartTime.HasValue && entity.EndTime.HasValue) { weekTimes = GetYearWeekCount(entity.StartTime.Value, entity.EndTime.Value); curWeek = WeekOfYear(startdate, entity.StartTime.Value, entity.EndTime.Value); } var listObj = new List(); for (int i = 1; i <= weekTimes; i++) { listObj.Add(new { text = i + "周", value = i }); } var jsonData = new { weekList = listObj, curWeek = curWeek, }; return Success(jsonData); } /// /// 有多少周 /// 返回 int /// /// int private static int GetYearWeekCount(DateTime startTime, DateTime endTime) { string returnStr = ""; var startDate = DateTime.Parse(Common.CalculateFirstDateOfWeek(startTime).ToString("yyyy-MM-dd")); //int k = Convert.ToInt32(startTime.DayOfWeek);//得到开始时间的第一天是周几 int countDay = endTime.Subtract(startDate).Days; int countWeek = countDay / 14 + 1; return countWeek; } /// /// 求当前日期是第几周 /// /// /// private static int WeekOfYear(DateTime curDay, DateTime startTime, DateTime endTime) { int firstdayofweek = Convert.ToInt32(startTime.DayOfWeek); var startDate = DateTime.Parse(Common.CalculateFirstDateOfWeek(startTime).ToString("yyyy-MM-dd")); int k = Convert.ToInt32(startTime.DayOfWeek);//得到开始时间的第一天是周几 int days = curDay.Subtract(startTime).Days; //int days = curDay.DayOfYear; int daysOutOneWeek = days - (14 - firstdayofweek); if (daysOutOneWeek <= 0) { return 1; } else { int weeks = daysOutOneWeek / 14; if (daysOutOneWeek % 14 != 0) weeks++; return weeks + 1; } } /// /// 课程表【教务】 /// /// 班级 /// 当前第几周 /// [HttpGet] [AjaxOnly] public ActionResult GetDataInEducation(string classNo, int curWeek) { var userInfo = LoginUserInfo.Get(); //开始时间 var startdate = DateTime.Today; var startDate = Common.CalculateFirstDateOfWeek(startdate).ToString("yyyy-MM-dd"); var endDate = Common.CalculateLastDateOfWeek(startdate).ToString("yyyy-MM-dd"); var semesterAndYear = Common.GetSemesterAndYear(startDate); var strAcademicYear = semesterAndYear.AcademicYearLong; var strSemester = semesterAndYear.Semester; if (string.IsNullOrEmpty(classNo)) { if (userInfo.Description != "教师") { var res = new { schoolName = "课程表", semester = $"{strAcademicYear}学年度 第{strSemester}学期", weekList = "" }; return JsonResult(res); } } //校历 var entity = schoolCalendarIbll.GetSchoolCalendarEntityByNo(semesterAndYear.AcademicYearShort, semesterAndYear.Semester); var StartTime = entity.StartTime.Value; //根据第几周,计算查询的开始和结束日期 startDate = Common.CalculateFirstDateOfWeek(StartTime.AddDays(((curWeek - 1) * 14))).ToString("yyyy-MM-dd");//StartTime.AddDays(((curWeek - 1) * 14)).ToString("yyyy-MM-dd"); if (curWeek == 1 && Convert.ToDateTime(startDate) < StartTime) { startDate = StartTime.ToString("yyyy-MM-dd"); } endDate = Common.CalculateFirstDateOfWeek(StartTime.AddDays(((curWeek - 1) * 14))).AddDays(10) .ToString("yyyy-MM-dd");// StartTime.AddDays(((curWeek - 1) * 14)).AddDays(10).ToString("yyyy-MM-dd"); var timeTableList = new List(); //课程表 var data = arrangeLessonTermIBLL.GetTimeTableInEducation(startDate, endDate, classNo, "", ""); if (!string.IsNullOrEmpty(classNo)) { var PKRoleId = Config.GetValue("PKRoleId"); var loginInfoRoleIds = LoginUserInfo.Get().roleIds; if (loginInfoRoleIds.IndexOf(',') == -1) { if (loginInfoRoleIds == PKRoleId) { data = arrangeLessonTermIBLL.GetTimeTableInEducation(startDate, endDate, classNo, "", ""); } } else { if (loginInfoRoleIds.Split(',').Contains(PKRoleId)) { data = arrangeLessonTermIBLL.GetTimeTableInEducation(startDate, endDate, classNo, "", ""); } } } else { if (userInfo.Description == "学生") { data = arrangeLessonTermIBLL.GetTimeTableInEducation(startDate, endDate, classNo, "", ""); } else if (userInfo.Description == "教师") { var empInfo = empInfoIbll.GetAllList().Where(x => x.EmpNo == userInfo.account); if (empInfo != null) { data = arrangeLessonTermIBLL.GetTimeTableInEducation(startDate, endDate, "", empInfo.FirstOrDefault().EmpNo, ""); } } else { data = arrangeLessonTermIBLL.GetTimeTableInEducation(startDate, endDate, classNo, "", ""); } } timeTableList.AddRange(data); //选修课课程表 //var dataOfElective = arrangeLessonTermOfElectiveIBLL.GetTimeTableInEducation(startDate, endDate, classNo, "", ""); //timeTableList.AddRange(dataOfElective); var timeTables = timeTableList.ToList(); var noDataResult = new { schoolName = "课程表", semester = $"{strAcademicYear}学年度 第{strSemester}学期", weekList = "" }; if (!timeTables.Any()) { return JsonResult(noDataResult); } var formatData = from d in timeTables.AsEnumerable() let tt = d.LessonTime.Split('-')[1] group d by tt into g orderby g.Key select new { time = g.Key, list = from e in timeTables.AsEnumerable() let ee = e.LessonTime.Split('-')[1] where ee == g.Key select new { ALTId = e.ALTId, day = e.LessonTime.Split('-')[0], curriculum = e.LessonSortNo == "2" ? e.LessonName + "[选修]" : e.LessonName, teacher = e?.EmpName, classRoom = string.IsNullOrEmpty(e.ClassroomName) ? "" : e.ClassroomName.Trim(), academicyear = semesterAndYear.AcademicYearShort, semester = strSemester, lessonNo = e?.LessonNo, teachClassNo = e?.TeachClassNo, empno = e?.EmpNo, lessonTime = e.LessonTime, lessonDate = e.LessonDate.ToString("yyyy-MM-dd"), classRoomNo = string.IsNullOrEmpty(e.ClassRoomNo) ? "" : e.ClassRoomNo.Trim(), lessonSortNo = e.LessonSortNo, e?.OLPEId, className = string.IsNullOrEmpty(e.ClassName) ? "" : e.ClassName } } ; var result = new { schoolName = "课程表", semester = $"{strAcademicYear}学年度 第{strSemester}学期", weekList = formatData }; return JsonResult(result); } #endregion #region 提交数据 /// /// 删除实体数据 /// 主键 /// /// [HttpPost] [AjaxOnly] public ActionResult DeleteForm(string keyValue) { arrangeLessonTermIBLL.DeleteEntity(keyValue); return Success("删除成功!"); } /// /// 保存实体数据(新增、修改) /// 主键 /// /// [HttpPost] [ValidateAntiForgeryToken] [AjaxOnly] public ActionResult SaveForm(string keyValue, ArrangeLessonTermEntity entity) { arrangeLessonTermIBLL.SaveEntity(keyValue, entity); return Success("保存成功!"); } /// /// 删除课程 /// 主键 /// /// [HttpPost] [AjaxOnly] public ActionResult DeleteLessonTerm(string keyValue, string WeekTime) { arrangeLessonTermIBLL.DeleteLessonTerm(keyValue, WeekTime); return Success("删除成功!"); } /// ///修改课程 /// 主键 /// /// [HttpPost] [AjaxOnly] public ActionResult UpdateLessonTerm(string keyValue, string postdata) { UpdateLessonTermEntity model = postdata.ToObject(); var res = arrangeLessonTermIBLL.UpdateLessonTerm(keyValue, model); if (!string.IsNullOrEmpty(res)) { return Fail(res); } return Success("操作成功!"); } #endregion /// /// 当前学期排课数据同步 /// /// [HttpGet] [AjaxOnly] public async Task AsyncArrangeLessonData() { var data = await arrangeLessonTermIBLL.AsyncArrangeLessonData(); return Success(data); } /// /// 清空当前学期排课数据 /// /// [HttpGet] [AjaxOnly] public async Task AsyncModifyArrangeLessonData() { var data = await arrangeLessonTermIBLL.AsyncModifyArrangeLessonData(); return Success(data); } /// /// 按条件清空排课数据 /// /// [HttpPost] [AjaxOnly] public async Task AsyncModifyArrangeLessonDataByCondition(ArrangeLessonTermEntity entity) { var data = await arrangeLessonTermIBLL.AsyncModifyArrangeLessonDataByCondition(entity); return Success(data); } /// /// 按条件同步排课数据 /// /// [HttpPost] [AjaxOnly] public async Task AsyncArrangeLessonDataByCondition(ArrangeLessonTermEntity entity) { var data = await arrangeLessonTermIBLL.AsyncArrangeLessonDataByCondition(entity); if (data) { //读取信息推送管理-课表同步推送(09)的配置 var informationPushEntity = sys_InformationPushIBLL.GetEntityByPushItem("09"); if (informationPushEntity != null && informationPushEntity.Status == true) { var title = string.Format("{0}学年第{1}学期必修课课表同步", entity.AcademicYearNo, entity.Semester); var needpostuserlist = userIbll.GetAllList().Where(m => m.F_DeleteMark != 1 && m.F_EnabledMark == 1 && m.F_Description == "教师").ToList(); //微信推送 try { PushWeixin(needpostuserlist, title); } catch (Exception e) { } //飞星推送 await Task.Run(async () => { using (var hubConnection = new HubConnection(ConfigurationManager.AppSettings["CommunicationServeraddress"])) { var hubProxy = hubConnection.CreateHubProxy("SignalRHub"); await hubConnection.Start(); 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)), ""); } }); } } return Success(data); } /// /// 按条件重置基础数据同步状态 /// /// /// [HttpPost] [AjaxOnly] public ActionResult InitAsyncDataByCondition(ArrangeLessonTermEntity entity) { var data = arrangeLessonTermIBLL.InitAsyncDataByCondition(entity); return Success(data); } public void PushWeixin(List needpostuserlist, string title) { var WeChatConfigentity = weChatConfigIbll.GetEnableEntity(); string appid = WeChatConfigentity.APPId; string secret = WeChatConfigentity.secret; var wechatemplete = weChatTempletIbll.GetWeChatTemplateEntityByCodeConfigId(WeChatConfigentity.ID, "task"); string weixintaskurl = wechatemplete.TUrl; string weixintasktempid = wechatemplete.TempId; var responsejson = Util.HttpMethods.HttpGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret); OperateLogModel operateLogModel = new OperateLogModel(); operateLogModel.title = title; operateLogModel.type = OperationType.Other; operateLogModel.url = "ArrangeLessonTermController"; operateLogModel.sourceObjectId = "002"; operateLogModel.sourceContentJson = responsejson; OperatorHelper.Instance.WriteOperateLog(operateLogModel); foreach (UserEntity userinfo in needpostuserlist) { if (userinfo != null && !string.IsNullOrEmpty(userinfo.OpenIdForWeixin)) { //执行推送任务 if (!string.IsNullOrEmpty(appid) && !string.IsNullOrEmpty(secret) && !string.IsNullOrEmpty(weixintaskurl) && !string.IsNullOrEmpty(weixintasktempid)) { if (!string.IsNullOrEmpty(responsejson)) { var weixintokenobj = JsonConvert.DeserializeObject(responsejson); if (string.IsNullOrEmpty(weixintokenobj.errcode)) { string access_token = weixintokenobj.access_token; string jsondata = "{\"touser\":\"" + userinfo.OpenIdForWeixin + "\"," + "\"template_id\":\"" + weixintasktempid + "\"," + "\"url\":\"" + weixintaskurl + "\"," + "\"data\":{" + "\"first\": {\"value\":\"您有新的课表同步\",\"color\":\"#173177\"}," + "\"keyword1\":{\"value\":\"课表同步\",\"color\":\"#173177\"}," + "\"keyword2\": {\"value\":\"" + title + "\",\"color\":\"#173177\"}," + "\"keyword3\": {\"value\":\"待查看\",\"color\":\"#173177\"}," + "\"keyword4\": {\"value\":\"您有新的课表同步【" + title + "】\",\"color\":\"#173177\"}" + "}" + "}"; string pushresult = Util.HttpMethods.HttpPost("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + access_token, jsondata); operateLogModel.title = title; operateLogModel.type = OperationType.Other; operateLogModel.url = "ArrangeLessonTermController"; operateLogModel.sourceObjectId = "002"; operateLogModel.sourceContentJson = pushresult; OperatorHelper.Instance.WriteOperateLog(operateLogModel); } } } } } } } }