using Dapper;
using Learun.DataBase.Repository;
using Learun.Util;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
namespace Learun.Application.TwoDevelopment.EducationalAdministration
{
///
/// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
/// Copyright (c) 2013-2018 北京泉江科技有限公司
/// 创 建:超级管理员
/// 日 期:2019-05-14 10:02
/// 描 述:选修课课程信息
///
public class OpenLessonPlanOfElectiveService : RepositoryFactory
{
#region 获取数据
///
/// 获取页面显示列表数据
///
/// 查询参数
///
public IEnumerable GetPageList(Pagination pagination, string queryJson)
{
try
{
var strSql = new StringBuilder();
strSql.Append("SELECT t.* ");
strSql.Append(" FROM OpenLessonPlanOfElective t ");
strSql.Append(" WHERE 1=1 and Id not in(select OLPOEId from ElectiveMergeItem)");
var queryParam = queryJson.ToJObject();
// 虚拟参数
var dp = new DynamicParameters(new { });
if (!queryParam["AcademicYearNo"].IsEmpty())
{
dp.Add("AcademicYearNo", "%" + queryParam["AcademicYearNo"].ToString() + "%", DbType.String);
strSql.Append(" AND t.AcademicYearNo Like @AcademicYearNo ");
}
if (!queryParam["Semester"].IsEmpty())
{
dp.Add("Semester", queryParam["Semester"].ToString(), DbType.String);
strSql.Append(" AND t.Semester = @Semester ");
}
if (!queryParam["LessonNo"].IsEmpty())
{
dp.Add("LessonNo", queryParam["LessonNo"].ToString(), DbType.String);
strSql.Append(" AND t.LessonNo = @LessonNo ");
}
if (!queryParam["LessonName"].IsEmpty())
{
dp.Add("LessonName", "%" + queryParam["LessonName"].ToString() + "%", DbType.String);
strSql.Append(" AND t.LessonName Like @LessonName ");
}
if (pagination != null)
{
return this.BaseRepository("CollegeMIS").FindList(strSql.ToString(), dp, pagination);
}
else
{
return this.BaseRepository("CollegeMIS").FindList(strSql.ToString(), dp);
}
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 获取页面显示列表数据【学子在线-选课中心】
///
/// 查询参数
///
public IEnumerable GetPageListOfStudent(Pagination pagination, string queryJson)
{
try
{
var queryParam = queryJson.ToJObject();
// 虚拟参数
var dp = new DynamicParameters(new { });
//学号
if (!queryParam["StuNo"].IsEmpty())
{
dp.Add("StuNo", queryParam["StuNo"].ToString(), DbType.String);
}
//教学工作安排中“选课”工作设置的学年学期
var now = DateTime.Now;
var EADateArrangeEntityAboutElective = this.BaseRepository("CollegeMIS").FindEntity(x => x.WorkName == "选课" && x.MakeDate <= now && x.EndDate >= now && x.CheckMark == "1");
var strSql = new StringBuilder();
strSql.Append(@"select b.*,leif.LessonTypeId,c.StuNo,c.StuName,c.Status,cp.Status as StatusPre from ElectiveMajor a left join OpenLessonPlanOfElective b on a.OLPOEId=b.Id
left join StuSelectLessonListOfElective c on b.Id=c.OLPEId and c.StuNo=@StuNo
left join StuSelectLessonListOfElectivePre cp on b.Id=cp.OLPEId and cp.StuNo=@StuNo left join LessonInfo leif on b.LessonNo=leif.LessonNo
where not exists ( ");
//不显示与已选课程 相同课程名称相同教师的课程
strSql.Append(" select * from StuSelectLessonListOfElective s where s.AcademicYearNo='" + EADateArrangeEntityAboutElective?.AcademicYearNo + "' and s.Semester='" + EADateArrangeEntityAboutElective.Semester + "' and s.StuNo=@StuNo and s.Status <>3 and s.LessonName=b.LessonName and s.EmpNo=b.EmpNo and s.OLPEId!=b.Id ");
strSql.Append(" ) ");
strSql.Append(" and a.MajorId is not null and a.Grade is not null ");
strSql.Append(" AND b.AcademicYearNo='" + EADateArrangeEntityAboutElective?.AcademicYearNo + "' and b.Semester='" + EADateArrangeEntityAboutElective?.Semester + "' ");
if (!queryParam["StuMajorNo"].IsEmpty())
{
dp.Add("StuMajorNo", queryParam["StuMajorNo"].ToString(), DbType.String);
strSql.Append(" and a.MajorId=@StuMajorNo ");
}
else
{
strSql.Append(" and a.MajorId='' ");
}
if (!queryParam["StuGrade"].IsEmpty())
{
dp.Add("StuGrade", queryParam["StuGrade"].ToString(), DbType.String);
strSql.Append(" and a.Grade=@StuGrade ");
}
else
{
strSql.Append(" and a.Grade='' ");
}
//不显示与已选课程 相同课程名称的课程
//if (EADateArrangeEntityAboutElective != null)
//{
// strSql.Append(" AND b.AcademicYearNo='" + EADateArrangeEntityAboutElective?.AcademicYearNo + "' and b.Semester='" + EADateArrangeEntityAboutElective?.Semester + "'");
// strSql.Append(" and b.LessonName not in(select LessonName from StuSelectLessonListOfElective where AcademicYearNo='" + EADateArrangeEntityAboutElective?.AcademicYearNo + "' and Semester='" + EADateArrangeEntityAboutElective?.Semester + "' and StuNo='" + queryParam["StuNo"].ToString() + "' and Status<>3)");
//}
//else
//{
// strSql.Append(" AND b.AcademicYearNo='' and b.Semester=''");
//}
if (!queryParam["EmpName"].IsEmpty())
{
dp.Add("EmpName", queryParam["EmpName"].ToString(), DbType.String);
strSql.Append(" AND b.EmpName = @EmpName ");
}
if (!queryParam["LessonName"].IsEmpty())
{
dp.Add("LessonName", "%" + queryParam["LessonName"].ToString() + "%", DbType.String);
strSql.Append(" AND b.LessonName Like @LessonName ");
}
var list = this.BaseRepository("CollegeMIS").FindList(strSql.ToString(), dp, pagination);
foreach (var item in list)
{
//模式二:正式选课
var aa = this.BaseRepository("CollegeMIS").FindList(x => x.OLPEId == item.Id);
//已报名人数
item.StuNumOfApply = aa.Where(x => (x.Status == 1 || x.Status == 2)).Count();
//模式一:预选课
var aaa = this.BaseRepository("CollegeMIS").FindList(x => x.OLPEId == item.Id);
//已报名人数
item.StuNumOfApplyPre = aaa.Where(x => (x.Status == 1 || x.Status == 2)).Count();
}
return list;
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 获取OpenLessonPlanOfElective表实体数据
/// 主键
///
///
public OpenLessonPlanOfElectiveEntity GetOpenLessonPlanOfElectiveEntity(string keyValue)
{
try
{
return this.BaseRepository("CollegeMIS").FindEntity(keyValue);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
#endregion
#region 提交数据
///
/// 删除实体数据
/// 主键
///
///
public void DeleteEntity(string keyValue)
{
try
{
this.BaseRepository("CollegeMIS").Delete(t => t.Id == keyValue);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 保存实体数据(新增、修改)
/// 主键
///
///
public void SaveEntity(string keyValue, OpenLessonPlanOfElectiveEntity entity)
{
try
{
if (!string.IsNullOrEmpty(keyValue))
{
entity.Modify(keyValue);
this.BaseRepository("CollegeMIS").Update(entity);
}
else
{
entity.Create();
this.BaseRepository("CollegeMIS").Insert(entity);
}
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
#endregion
#region 扩展数据
///
/// 获取选课时间管理
///
///
public EADateArrangeEntity GetEADateArrangeEntityAboutElective()
{
try
{
var semesterAndYear = Common.GetSemesterAndYear();
var strAcademicYear = semesterAndYear.AcademicYearShort;
var strSemester = semesterAndYear.Semester;
var now = DateTime.Now;
//不判断学年学期
return this.BaseRepository("CollegeMIS").FindEntity(x => x.WorkName == "选课" && x.MakeDate <= now && x.EndDate >= now && x.CheckMark == "1");
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
public IEnumerable GetPageListOfMerge(Pagination pagination, string queryJson)
{
try
{
var strSql = new StringBuilder();
strSql.Append("SELECT ");
strSql.Append(@"
t.Id,
t.AcademicYearNo,
t.Semester,
t.LessonNo,
t.LessonNo as LessonNo2,
t.LessonName,
t.LessonSortNo,
t.LessonSection,
t.LessonTime,
t.StudyScore,
t.EmpNo,
t.EmpNo as EmpNo2,
t.EmpName,
t.ClassRoomNo,
t.ClassRoomName,
t.StuNumMax,
t.StuNum,
t.ModifyTime,
t.ModifyUserId,
t.ModifyUserName
");
strSql.Append(" FROM OpenLessonPlanOfElective t ");
strSql.Append(" WHERE 1=1 ");
var queryParam = queryJson.ToJObject();
// 虚拟参数
var dp = new DynamicParameters(new { });
if (!queryParam["AcademicYearNo"].IsEmpty())
{
dp.Add("AcademicYearNo", "%" + queryParam["AcademicYearNo"].ToString() + "%", DbType.String);
strSql.Append(" AND t.AcademicYearNo Like @AcademicYearNo ");
}
if (!queryParam["Semester"].IsEmpty())
{
dp.Add("Semester", queryParam["Semester"].ToString(), DbType.String);
strSql.Append(" AND t.Semester = @Semester ");
}
if (!queryParam["LessonNo"].IsEmpty())
{
dp.Add("LessonNo", queryParam["LessonNo"].ToString(), DbType.String);
strSql.Append(" AND t.LessonNo = @LessonNo ");
}
if (!queryParam["LessonName"].IsEmpty())
{
dp.Add("LessonName", "%" + queryParam["LessonName"].ToString() + "%", DbType.String);
strSql.Append(" AND t.LessonName Like @LessonName ");
}
var list = new List();
if (pagination != null)
{
list = this.BaseRepository("CollegeMIS").FindList(strSql.ToString(), dp, pagination).ToList();
}
else
{
list = this.BaseRepository("CollegeMIS").FindList(strSql.ToString(), dp).ToList();
}
//选课专业
var majorSql =
@"SELECT t.MajorName,t.MajorNo,a.* FROM ElectiveMajor a left join CdMajor t on a.majorid=t.id";
var electiveMajorList = this.BaseRepository("CollegeMIS").FindList(majorSql);
foreach (var item in list)
{
//模式二:正式选课
var aa = this.BaseRepository("CollegeMIS").FindList(x => x.OLPEId == item.Id);
//已报名人数
item.StuNumOfApply = aa.Where(x => (x.Status == 1 || x.Status == 2)).Count();
//审核通过人数
item.StuNum = aa.Where(x => x.Status == 2).Count();
//选课专业
//var strSql2 = new StringBuilder();
//strSql2.Append("select e.Id,e.OLPOEId,e.MajorId,e.Grade,m.MajorNo,m.MajorName from ElectiveMajor e left join CdMajor m on e.MajorId=m.ID where e.OLPOEId='" + item.Id + "' and e.MajorId is not null and e.Grade is not null ");
//item.ElectiveMajorList = this.BaseRepository("CollegeMIS").FindList(strSql2.ToString()).OrderBy(x => x.MajorNo).ThenBy(x => x.Grade).ToList();
//模式一:预选课
var aaa = this.BaseRepository("CollegeMIS").FindList(x => x.OLPEId == item.Id);
//已报名人数
item.StuNumOfApplyPre = aaa.Where(x => (x.Status == 1 || x.Status == 2)).Count();
//审核通过人数
item.StuNumPre = aaa.Where(x => x.Status == 2).Count();
//选课专业
item.ElectiveMajorList = electiveMajorList.Where(x => x.OLPOEId == item.Id).ToList();
}
return list;
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 选修课课程异动
/// 主键
///
///
public void DoElectiveChange(OpenLessonPlanOfElectiveChangeEntity entity)
{
var db = this.BaseRepository("CollegeMIS").BeginTrans();
try
{
//OpenLessonPlanOfElective,StuSelectLessonListOfElective,StuSelectLessonListOfElectivePre,
//ElectiveMergeItem,ElectiveMerge,OpenLessonPlanOfElectiveDel,ArrangeLessonTermOfElective
var OLPEIdOne = entity.OLPEId;
var EmId = db.FindEntity(x => x.OLPOEId == OLPEIdOne)?.EmId;
var OLPEIdTwo = db.FindEntity(x => x.EmId == EmId && x.OLPOEId != OLPEIdOne).OLPOEId;
var lessonInfoEntity = db.FindEntity(x => x.LessonNo == entity.AfterLessonNo);
//选修课开课计划
db.ExecuteBySql($"update OpenLessonPlanOfElective set LessonNo='{entity.AfterLessonNo}',LessonName='{entity.AfterLessonName}',EmpNo='{entity.AfterEmpNo}',EmpName='{entity.AfterEmpName}',ClassRoomNo='{entity.AfterClassRoomNo}',ClassRoomName='{entity.AfterClassRoomName}' where Id='{OLPEIdOne}' ");
//学生选课表
db.ExecuteBySql($"update StuSelectLessonListOfElective set LessonNo='{entity.AfterLessonNo}',LessonName='{entity.AfterLessonName}',EmpNo='{entity.AfterEmpNo}',EmpName='{entity.AfterEmpName}',ClassRoomNo='{entity.AfterClassRoomNo}',ClassRoomName='{entity.AfterClassRoomName}' where OLPEId='{OLPEIdOne}' ");
db.ExecuteBySql($"update StuSelectLessonListOfElectivePre set LessonNo='{entity.AfterLessonNo}',EmpNo='{entity.AfterEmpNo}',ClassRoomNo='{entity.AfterClassRoomNo}' where OLPEId='{OLPEIdOne}' ");
//选修合班明细
db.ExecuteBySql($"update ElectiveMergeItem set EmpNo='{entity.AfterEmpNo}',EmpName='{entity.AfterEmpName}',ClassRoomNo='{entity.AfterClassRoomNo}',ClassRoomName='{entity.AfterClassRoomName}' where EmId='{EmId}' ");
//合班合并结果
db.ExecuteBySql($"update ElectiveMerge set LessonId='{lessonInfoEntity?.LessonId}',LessonNo='{entity.AfterLessonNo}',LessonName='{entity.AfterLessonName}' where EMId='{EmId}' ");
//合班异动数据记录
db.ExecuteBySql($"update OpenLessonPlanOfElectiveDel set LessonNo='{entity.AfterLessonNo}',LessonName='{entity.AfterLessonName}',EmpNo='{entity.AfterEmpNo}',EmpName='{entity.AfterEmpName}',ClassRoomNo='{entity.AfterClassRoomNo}',ClassRoomName='{entity.AfterClassRoomName}' where Id='{OLPEIdTwo}' ");
//选修课程表
db.ExecuteBySql($"update ArrangeLessonTermOfElective set LessonNo='{entity.AfterLessonNo}',LessonName='{entity.AfterLessonName}',EmpNo='{entity.AfterEmpNo}',EmpName='{entity.AfterEmpName}',ClassRoomNo='{entity.AfterClassRoomNo}',ClassRoomName='{entity.AfterClassRoomName}' where PaiKeId='{OLPEIdOne}' or PaiKeId='{OLPEIdTwo}' ");
db.Commit();
}
catch (Exception ex)
{
db.Rollback();
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
#endregion
}
}