using Dapper;
using Learun.DataBase.Repository;
using Learun.Util;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Learun.Application.Base.AuthorizeModule;
using Learun.Application.Organization;
using System.Reflection;
using Learun.Application.Base.SystemModule;
using Learun.Application.TwoDevelopment.LogisticsManagement;
namespace Learun.Application.TwoDevelopment.EducationalAdministration
{
///
/// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
/// Copyright (c) 2013-2018 北京泉江科技有限公司
/// 创 建:超级管理员
/// 日 期:2019-02-21 16:53
/// 描 述:学生学籍
///
public class StuInfoBasicService : RepositoryFactory
{
#region 获取数据
///
/// 获取页面显示列表数据
///
/// 查询参数
///
public IEnumerable GetPageList(Pagination pagination, string queryJson)
{
try
{
var strSql = new StringBuilder();
strSql.Append("SELECT t.* ");
strSql.Append(" FROM StuInfoBasic t ");
strSql.Append(" WHERE 1=1 ");
var queryParam = queryJson.ToJObject();
// 虚拟参数
var dp = new DynamicParameters(new { });
if (!queryParam["StuNo"].IsEmpty())
{
dp.Add("StuNo", queryParam["StuNo"].ToString(), DbType.String);
strSql.Append(" AND t.StuNo = @StuNo ");
}
if (!queryParam["StuName"].IsEmpty())
{
dp.Add("StuName", "%" + queryParam["StuName"].ToString() + "%", DbType.String);
strSql.Append(" AND t.StuName Like @StuName ");
}
if (!queryParam["DeptNo"].IsEmpty())
{
dp.Add("DeptNo", "" + queryParam["DeptNo"].ToString() + "", DbType.String);
strSql.Append(" AND t.DeptNo=@DeptNo ");
}
if (!queryParam["MajorNo"].IsEmpty())
{
dp.Add("MajorNo", "" + queryParam["MajorNo"].ToString() + "", DbType.String);
strSql.Append(" AND t.MajorNo=@MajorNo ");
}
if (!queryParam["ClassNo"].IsEmpty())
{
dp.Add("ClassNo", "" + queryParam["ClassNo"].ToString() + "", DbType.String);
strSql.Append(" AND t.ClassNo=@ClassNo ");
}
if (!queryParam["Grade"].IsEmpty())
{
dp.Add("Grade", "" + queryParam["Grade"].ToString() + "", DbType.String);
strSql.Append(" AND t.Grade=@Grade ");
}
if (!queryParam["FinishSchoolMark"].IsEmpty())
{
dp.Add("FinishSchoolMark", "" + queryParam["FinishSchoolMark"].ToString() + "", DbType.String);
if (queryParam["FinishSchoolMark"].ToString() == "0")
{
strSql.Append(" AND (t.FinishSchoolMark is null or t.FinishSchoolMark='0') ");
}
else
{
strSql.Append(" AND t.FinishSchoolMark=@FinishSchoolMark ");
}
}
if (!queryParam["Remark"].IsEmpty())
{
dp.Add("Remark", "%" + queryParam["Remark"].ToString() + "%", DbType.String);
strSql.Append(" AND t.Remark Like @Remark ");
}
return this.BaseRepository("CollegeMIS").FindList(strSql.ToString(), dp, pagination);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
internal List GetSaveClassStudents(string account)
{
try
{
var classNO = this.BaseRepository("CollegeMIS").FindEntity(a => a.StuNo == account).ClassNo;
return this.BaseRepository("CollegeMIS").FindList(a => a.ClassNo == classNO).Select(a => a.StuNo).ToList();
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
internal bool GetAny()
{
try
{
return this.BaseRepository("CollegeMIS").FindList().ToList().Count > 0 ? true : false;
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
internal StuInfoBasicEntity GetStuInfoBasicEntityByStuNo(string enCode)
{
try
{
var data = this.BaseRepository("CollegeMIS").FindEntity(a => a.StuNo == enCode);
if (data != null && (data.Photo != null && data.Photo != ""))
{
var url = this.BaseRepository().FindEntity(a => a.F_FolderId == data.Photo)?.F_FilePath;
if (!string.IsNullOrEmpty(url))
{
url = "/" + url.Substring(url.IndexOf("Resource"));
data.Photo = url;
}
}
return data;
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
public StuInfoBasicEntity GetStuInfoBasicEntityByStuName(string name)
{
try
{
return this.BaseRepository("CollegeMIS").FindEntity(a => a.StuName == name);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 获取StuInfoBasic表实体数据
/// 主键
///
///
public StuInfoBasicEntity GetStuInfoBasicEntity(string keyValue)
{
try
{
return this.BaseRepository("CollegeMIS").FindEntity(keyValue);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 获取准许毕业的学生的专业
///
///
public List GetMajorInfoWithGraduation()
{
try
{
var stuInfo = this.BaseRepository("CollegeMIS")
.FindList(a => a.FinishSchoolMark == "1").ToList();
var majorNoList = stuInfo.GroupBy(a => a.MajorNo).Select(a => a.Key).ToList();
var majorList = this.BaseRepository("CollegeMIS")
.FindList(a => majorNoList.Contains(a.MajorNo)).ToList();
return majorList;
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 获取StuInfoBasic表实体数据
/// 主键
///
///
public void CreateGraduateNoByMajor(string CityCode, string SchoolCode, string MajorList)
{
try
{
var year = DateTime.Now.Year.ToString().Substring(2, 2);
var firstStr = year + CityCode + SchoolCode;
//获取最大的序号
var DiplomaNoList = this.BaseRepository("CollegeMIS")
.FindList(a => a.DiplomaNo.Contains(firstStr)).Select(a => a.DiplomaNo.Substring(a.DiplomaNo.Length - 4, 4).ToInt()).ToList();
var orderNo = 1;
if (DiplomaNoList.Count() > 0)
{
var MaxDiplomaNo = DiplomaNoList.Max(a => a);
orderNo = MaxDiplomaNo + 1;
}
var ListMajorNo = MajorList.Split(',').ToList();
foreach (var MajorNo in ListMajorNo)
{
var stuList = this.BaseRepository("CollegeMIS")
.FindList(a => a.MajorNo == MajorNo)
.Where(a => a.DiplomaNo == null || a.DiplomaNo == "");
if (stuList.Count() > 0)
{
foreach (var stuInfo in stuList)
{
stuInfo.DiplomaNo = firstStr + orderNo.ToString().PadLeft(4, '0');
this.BaseRepository("CollegeMIS").Update(stuInfo);
orderNo++;
}
}
}
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 获取StuInfoBasic表实体数据
/// 主键
///
///
public StuInfoBasicEntity GetStuNoByAccount(string keyValue)
{
try
{
return this.BaseRepository("CollegeMIS").FindEntity(a => a.StuNo == keyValue);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 获取树形数据
///
///
public DataTable GetSqlTree()
{
try
{
var ClassDiredctorNo = LoginUserInfo.Get().account;
return this.BaseRepository("CollegeMIS").FindTable($" SELECT * FROM dbo.ClassInfo where ClassDiredctorNo='{ClassDiredctorNo}' or ClassTutorNo='{ClassDiredctorNo}'");
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
#endregion
#region 提交数据
///
/// 审核全部
///
public void CheckAll()
{
try
{
this.BaseRepository("CollegeMIS").ExecuteBySql("update StuInfoBasic set CheckMark=1");
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 准许毕业操作
///
public void AllowGraduate(string stuNo, string status)
{
try
{
var list = stuNo.Split(',').ToList().Select(a => "'" + a + "'");
var stulist = string.Join(",", list);
this.BaseRepository("CollegeMIS").ExecuteBySql($"UPDATE dbo.StuInfoBasic SET FinishSchoolMark ='{status}' WHERE StuNo in({stulist})");
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
public void SynPhoto()
{
try
{
var stuList = this.BaseRepository("CollegeMIS").FindList()
.ToList();
var url = AppDomain.CurrentDomain.BaseDirectory;
foreach (var stuInfo in stuList)
{
//照片不为空
if (!string.IsNullOrEmpty(stuInfo.Photo))
{
var annex = this.BaseRepository()
.FindEntity(a => a.F_FolderId == stuInfo.Photo);
if (annex == null)
{
var annexEntity = new AnnexesFileEntity()
{
F_Id = Guid.NewGuid().ToString(),
F_FileName = stuInfo.IdentityCardNo + ".jpg",
F_FilePath = $"{url}/Resource/UserPhoto/{stuInfo.IdentityCardNo}.jpg",
F_FolderId = stuInfo.Photo
};
this.BaseRepository().Insert(annexEntity);
}
else
{
annex.F_FileName = stuInfo.IdentityCardNo + ".jpg";
annex.F_FilePath = $"{url}/Resource/UserPhoto/{stuInfo.IdentityCardNo}.jpg";
this.BaseRepository().Update(annex);
}
}
else
{
stuInfo.Photo = Guid.NewGuid().ToString();
var annexEntity = new AnnexesFileEntity()
{
F_Id = Guid.NewGuid().ToString(),
F_FileName = stuInfo.IdentityCardNo + ".jpg",
F_FilePath = $"{url}/Resource/UserPhoto/{stuInfo.IdentityCardNo}.jpg",
F_FolderId = stuInfo.Photo
};
annexEntity.Create();
this.BaseRepository("CollegeMIS").Update(stuInfo);
this.BaseRepository().Insert(annexEntity);
}
}
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 删除实体数据
/// 主键
///
///
public void DeleteEntity(string keyValue)
{
var db = this.BaseRepository("CollegeMIS").BeginTrans();
try
{
//单个删除
//this.BaseRepository("CollegeMIS").Delete(t => t.StuId == keyValue);
//多个删除
var keyValueArr = keyValue.Split(',');
foreach (var item in keyValueArr)
{
db.Delete(t => t.StuId == item);
}
db.Commit();
}
catch (Exception ex)
{
db.Rollback();
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 领取毕业证
/// 主键
///
///
public void GetCard(string keyValue)
{
var db = this.BaseRepository("CollegeMIS").BeginTrans();
try
{
var keyValueArr = keyValue.Split(',');
foreach (var item in keyValueArr)
{
var sql = $"UPDATE dbo.StuInfoBasic SET FinishSchoolMark='2',FinishSchoolDate=GETDATE() WHERE StuId='{item}'";
db.ExecuteBySql(sql);
}
db.Commit();
}
catch (Exception ex)
{
db.Rollback();
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 毕业生归档
///
///
public void StuStore()
{
var db = this.BaseRepository("CollegeMIS").BeginTrans();
var adb = this.BaseRepository().BeginTrans();
try
{
//归档前提为已经领取毕业证的学生
var stuInfos = db
.FindList(a => a.FinishSchoolMark == "2");
foreach (var item in stuInfos)
{
var StuInfoType = typeof(StuInfoBasicEntity);
var PropertyInfoStuInfo = StuInfoType.GetProperties();
var GraduateEntity = new StuInfoGraduateEntity();
var typegraduate = typeof(StuInfoGraduateEntity);
var PropertyInfoGraduate = typegraduate.GetProperties();
foreach (var itemStuInfo in PropertyInfoStuInfo)
{
var objStuInfoType = itemStuInfo.GetValue(StuInfoType);
foreach (var itemGraduate in PropertyInfoGraduate)
{
if (itemGraduate.Name == itemStuInfo.Name)
{
itemGraduate.SetValue(GraduateEntity, objStuInfoType, null);
}
}
}
var accountInfo = adb.FindEntity(a => a.F_Account == item.StuNo);
if (accountInfo != null)
{
//删除账户
adb.Delete(accountInfo);
}
//清空宿舍信息
var dormitory = db
.FindEntity(a => a.StudentID == item.StuId);
if (dormitory != null)
{
dormitory.StudentID = "";
dormitory.StuName = "";
db.Update(dormitory);
}
db.Delete(item);
db.Insert(GraduateEntity);
}
}
catch (Exception ex)
{
db.Rollback();
adb.Rollback();
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 保存实体数据(新增、修改)
/// 主键
///
///
public void SaveEntity(string keyValue, StuInfoBasicEntity entity)
{
try
{
UserIBLL userIBLL = new UserBLL();
if (!string.IsNullOrEmpty(keyValue))
{
entity.Modify(keyValue);
var oldEntity = this.BaseRepository("CollegeMIS").FindEntity(keyValue);
List list = new List();
var loginUser = LoginUserInfo.Get();
var tableInfos = this.BaseRepository("CollegeMIS").FindTable(@"SELECT t.[name] AS 表名,c.[name] AS 字段名,cast(ep.[value]
as varchar(100)) AS [字段说明]
FROM sys.tables AS t
INNER JOIN sys.columns
AS c ON t.object_id = c.object_id
LEFT JOIN sys.extended_properties AS ep
ON ep.major_id = c.object_id AND ep.minor_id = c.column_id WHERE ep.class =1
AND t.name='StuInfoBasic'");
PropertyInfo[] properties = oldEntity.GetType().GetProperties();
foreach (System.Reflection.PropertyInfo item in properties)
{
string name = item.Name;
object oldValue = item.GetValue(oldEntity);
object newValue = item.GetValue(entity);
if (oldValue == null || newValue == null)
{
continue;
}
if (!oldValue.Equals(newValue))
{
var columnName = "";
foreach (DataRow rows in tableInfos.Rows)
{
if (rows["字段名"].ToString() == item.Name)
{
columnName = rows["字段说明"].ToString();
}
}
var changeEntity = new StuInfoBasic_ChangeLogEntity
{
StuID = keyValue,
BeforeChange = oldValue.ToString(),
AfterChange = newValue.ToString(),
FieldName = columnName,
UpdateBy = loginUser.userId,
UpdateTime = DateTime.Now
};
changeEntity.Create();
list.Add(changeEntity);
}
}
if (list.Count > 0)
{
this.BaseRepository("CollegeMIS").Insert(list);
}
var annexesFileEntity = this.BaseRepository().FindEntity(a => a.F_FolderId == entity.Photo);
var url = "";
if (annexesFileEntity != null)
{
url = annexesFileEntity.F_FilePath;
url = "/" + url.Substring(url.IndexOf("Resource"), url.Length - url.IndexOf("Resource"));
}
var baseUser = userIBLL.GetEntityByAccount(entity.StuNo);
if (baseUser != null)
{
baseUser.F_HeadIcon = url;
userIBLL.SaveEntity(baseUser.F_UserId, baseUser);
}
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
public class TableInfo
{
public string TableName { get; set; }
public string ColumnName { get; set; }
public string ColumnDescription { get; set; }
}
public void GenerateAccout()
{
UserRelationIBLL userRelationIBLL = new UserRelationBLL();
UserIBLL userIBLL = new UserBLL();
try
{
var stuInfoBasicEntities = BaseRepository("CollegeMIS").FindList(m => m.CheckMark != "0");
var alluserlist = userIBLL.GetAllList().Where(m => m.F_Description == "学生");
var roleId = Config.GetValue("GenerateStudentsRoleId");
var defaultpwd = Config.GetValue("defaultpwd");
//读取默认密码配置中已启用的密码
var Sys_DefaultPwdConfigEntity = this.BaseRepository().FindEntity(x => x.IsEnabled == true);
if (Sys_DefaultPwdConfigEntity != null)
{
defaultpwd = Sys_DefaultPwdConfigEntity.Pwd;
}
var studentList = new List();
foreach (var tEntity in stuInfoBasicEntities)
{
if (alluserlist.Count(m => m.F_Account == tEntity.StuNo) > 0)
{
continue;
}
var annexesFileEntity = this.BaseRepository().FindEntity(a => a.F_FolderId == tEntity.Photo);
var url = "";
if (annexesFileEntity != null)
{
url = annexesFileEntity.F_FilePath;
url = "/" + url.Substring(url.IndexOf("Resource"));
}
UserEntity userbase = new UserEntity();
userbase.F_Account = tEntity.StuNo;
userbase.F_RealName = tEntity.StuName;
userbase.F_EnCode = tEntity.StuNo;
userbase.F_Password = Md5Helper.Encrypt(defaultpwd, 32).ToLower();
userbase.F_HeadIcon = string.IsNullOrEmpty(url) ? Config.GetValue("defaultheadimg") : url;
userbase.F_Gender = tEntity.GenderNo.HasValue ? Convert.ToInt32(tEntity.GenderNo.Value) : 1;
userbase.F_DeleteMark = 0;
userbase.F_EnabledMark = 1;
userbase.F_Mobile = tEntity.mobile;
userbase.F_Description = "学生";
userbase.F_CompanyId = tEntity.F_SchoolId;
userbase.F_DepartmentId = tEntity.DeptNo;
userbase.F_IdentityCardNo = tEntity.IdentityCardNo;
userIBLL.SaveEntity(null, userbase);
studentList.Add(userbase);
}
if (studentList.Any())
{
string s = studentList.Select(m => m.F_UserId).Aggregate((current, userEntity) => current + "," + userEntity);
userRelationIBLL.SaveEntityList2(roleId, 1, s);
}
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
public void UpdateAccount()
{
try
{
UserIBLL userIBLL = new UserBLL();
var teacherList = this.BaseRepository("CollegeMIS").FindList();
var baseTeacherList = userIBLL.GetAllList().Where(m => m.F_Description == "学生");
foreach (var baseTeacher in baseTeacherList)
{
var num = 0;
var teacher = teacherList.FirstOrDefault(a => a.StuNo == baseTeacher.F_Account);
if (teacher != null)
{
if (baseTeacher.F_RealName != teacher.StuName)
{
baseTeacher.F_RealName = teacher.StuName;
num++;
}
if (baseTeacher.F_Gender != Convert.ToInt32(teacher.GenderNo))
{
baseTeacher.F_Gender = Convert.ToInt32(teacher.GenderNo);
num++;
}
if (baseTeacher.F_Mobile != teacher.mobile)
{
baseTeacher.F_Mobile = teacher.mobile;
num++;
}
if (baseTeacher.F_CompanyId != teacher.F_SchoolId)
{
baseTeacher.F_CompanyId = teacher.F_SchoolId;
num++;
}
if (baseTeacher.F_DepartmentId != teacher.DeptNo)
{
baseTeacher.F_DepartmentId = teacher.DeptNo;
num++;
}
if (baseTeacher.F_IdentityCardNo != teacher.IdentityCardNo)
{
baseTeacher.F_IdentityCardNo = teacher.IdentityCardNo;
num++;
}
if (num > 0)
{
userIBLL.UpdateEntity(baseTeacher);
}
}
}
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
public IEnumerable GetAllList()
{
try
{
return this.BaseRepository("CollegeMIS").FindList(m => m.CheckMark == "1");
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
public IEnumerable GetStuInfoByClassNo(string classNo)
{
try
{
return this.BaseRepository("CollegeMIS").FindList(m => m.CheckMark == "1" && m.ClassNo == classNo);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
}
}