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-08-26 16:58
/// 描 述:选修合班设置
///
public class ElectiveMergeService : RepositoryFactory
{
#region 获取数据
///
/// 获取页面显示列表数据
///
/// 查询参数
///
public IEnumerable GetPageList(Pagination pagination, string queryJson)
{
try
{
var strSql = new StringBuilder();
strSql.Append(@"select distinct a.*,d.StudyScore from ElectiveMerge a left join
ElectiveMergeItem b
on a.EMId = b.EmId left join lessoninfo d on a.LessonId=d.LessonId
");
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 a.AcademicYearNo =@AcademicYearNo ");
}
if (!queryParam["Semester"].IsEmpty())
{
dp.Add("Semester", "" + queryParam["Semester"].ToString() + "", DbType.String);
strSql.Append(" AND a.Semester = @Semester ");
}
if (!queryParam["LessonName"].IsEmpty())
{
dp.Add("LessonName", "%" + queryParam["LessonName"].ToString() + "%", DbType.String);
strSql.Append(" AND a.LessonName Like @LessonName ");
}
return this.BaseRepository("CollegeMIS").FindList(strSql.ToString(), dp);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 获取ElectiveMerge表实体数据
/// 主键
///
///
public ElectiveMergeEntity GetElectiveMergeEntity(string keyValue)
{
try
{
var ementity = this.BaseRepository("CollegeMIS").FindEntity(keyValue);
var stumax = this.BaseRepository("CollegeMIS").FindObject(@"select distinct isnull(c.StuNumMax,0) from ElectiveMerge a left join
ElectiveMergeItem b
on a.EMId = b.EmId left join OpenLessonPlanOfElective c
on b.OLPOEId = c.Id where a.EMId = '"+keyValue+"'");
ementity.StuNumMax = Convert.ToInt32(stumax);
return ementity;
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
#endregion
#region 提交数据
///
/// 删除实体数据
/// 主键
///
///
public void DeleteEntity(string keyValue)
{
var db = this.BaseRepository("CollegeMIS");
try
{
db.BeginTrans();
var deldatalist = db.FindList(m => m.EmId == keyValue).OrderBy(m=>Convert.ToInt32(m.LessonSection));
var deldatafirst = deldatalist.First();
var deldatalast= deldatalist.Last();
var opfirst = db.FindEntity(m => m.Id == deldatafirst.OLPOEId);
opfirst.LessonSection = deldatafirst.LessonSection;
opfirst.LessonTime = deldatafirst.LessonTime;
db.Update(opfirst);
db.ExecuteBySql("insert into OpenLessonPlanOfElective select * from OpenLessonPlanOfElectiveDel where id='" + deldatalast.OLPOEId + "'");
db.ExecuteBySql("delete from OpenLessonPlanOfElectiveDel where id='" + deldatalast.OLPOEId + "'");
db.Delete(m => m.EmId == keyValue);
db.Delete(t => t.EMId == keyValue);
db.Commit();
}
catch (Exception ex)
{
db.Rollback();
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 保存实体数据(新增、修改)
/// 主键
///
///
public void SaveEntity(string keyValue, ElectiveMergeEntity entity, List electiveMergeItemEntity)
{
var db = this.BaseRepository("CollegeMIS");
try
{
if (!string.IsNullOrEmpty(keyValue))
{
entity.Modify(keyValue);
this.BaseRepository("CollegeMIS").Update(entity);
}
else
{
db.BeginTrans();
db.Insert(entity);
db.Insert(electiveMergeItemEntity);
var ids =string.Join(",",electiveMergeItemEntity.Select(n => n.OLPOEId));
var olplist = db.FindList(m =>
ids.Contains(m.Id)).OrderBy(c=>Convert.ToInt32(c.LessonSection));
var opfirst = olplist.First();
var oplast = olplist.Last();
opfirst.LessonSection +=","+ oplast.LessonSection;
opfirst.LessonTime += "," + oplast.LessonTime;
db.Update(opfirst);
db.ExecuteBySql("insert into OpenLessonPlanOfElectiveDel select * from OpenLessonPlanOfElective where id='"+ oplast.Id+ "'");
db.Delete(oplast);
db.Commit();
}
}
catch (Exception ex)
{
db.Rollback();
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
#endregion
public void SetStuNumMax(string keyValue, int entityStuNumMax)
{
var db = this.BaseRepository("CollegeMIS");
try
{
db.BeginTrans();
var datetimenow = DateTime.Now;
var loginuserinfo = LoginUserInfo.Get();
var olpl = db.FindList(@"select c.id from ElectiveMerge a left join
ElectiveMergeItem b
on a.EMId = b.EmId left join OpenLessonPlanOfElective c
on b.OLPOEId = c.Id where a.EMId = '"+ keyValue + "'");
foreach (var opitem in olpl)
{
opitem.StuNumMax = entityStuNumMax;
opitem.ModifyTime = datetimenow;
opitem.ModifyUserId = loginuserinfo.userId;
opitem.ModifyUserName = loginuserinfo.realName;
db.Update(opitem);
}
db.Commit();
}
catch (Exception ex)
{
db.Rollback();
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
}
}