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.
253 lines
11 KiB
253 lines
11 KiB
namespace Tiobon.Core.Services;
|
|
|
|
/// <summary>
|
|
/// 课程场景 (服务)
|
|
/// </summary>
|
|
public class Ghre_CourseSceneServices : BaseServices<Ghre_CourseScene, Ghre_CourseSceneDto, InsertGhre_CourseSceneInput, EditGhre_CourseSceneInput>, IGhre_CourseSceneServices
|
|
{
|
|
private readonly IBaseRepository<Ghre_CourseScene> _dal;
|
|
public Ghre_CourseSceneServices(ICaching caching, IBaseRepository<Ghre_CourseScene> dal)
|
|
{
|
|
this._dal = dal;
|
|
base.BaseDal = dal;
|
|
base._caching = caching;
|
|
}
|
|
public override async Task<ServicePageResult<Ghre_CourseSceneDto>> QueryFilterPage(QueryBody filter, string condition, bool? IsEnable = true)
|
|
{
|
|
var data = await base.QueryFilterPage(filter, condition, IsEnable);
|
|
data.result.DT_TableDataT1.ForEach(x =>
|
|
{
|
|
x.BuiltInLabel = x.BuiltIn == 1 ? "是" : "否";
|
|
});
|
|
return data;
|
|
}
|
|
|
|
public override async Task<ServiceFormResult<Ghre_CourseSceneDto>> QueryForm(QueryForm body)
|
|
{
|
|
var result = await base.QueryForm(body);
|
|
|
|
var DT_TableDataT1 = result.result.DT_TableDataT1;
|
|
DT_TableDataT1.ForEach(t =>
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(t.CourseId))
|
|
t.CourseIds = JsonHelper.JsonToObj<List<string>>(t.CourseId);
|
|
else
|
|
t.CourseIds = new List<string>();
|
|
});
|
|
if (body.id.IsNotEmptyOrNull())
|
|
{
|
|
var coures = await Db.Queryable<Ghre_Course>().Where(x => x.CourseSceneIds != null && (x.CourseSceneIds.Contains(body.id.ObjToString()) || x.CourseSceneId == body.id) && x.Status == Consts.DIC_COURSE_STATUS.RELEASED).ToListAsync();
|
|
if (DT_TableDataT1.Any())
|
|
DT_TableDataT1[0].Courses = coures;
|
|
}
|
|
result.result.DT_TableDataT1 = DT_TableDataT1;
|
|
return result;
|
|
}
|
|
|
|
|
|
public override async Task<Ghre_CourseSceneDto> QueryById(object objId)
|
|
{
|
|
var data = await base.QueryById(objId);
|
|
var DT_TableDataT1 = Mapper.Map(data).ToANew<Ghre_CourseSceneDto>();
|
|
var coures = await Db.Queryable<Ghre_Course>().Where(x => x.CourseSceneIds != null && x.CourseSceneIds.Contains(objId.ObjToString())).ToListAsync();
|
|
if (DT_TableDataT1.CreditRuleId != null)
|
|
DT_TableDataT1.CreditRuleName = (await Db.Queryable<Ghre_CreditRule>().Where(x => x.Id == DT_TableDataT1.CreditRuleId).FirstAsync())?.RuleName;
|
|
|
|
DT_TableDataT1.Courses = coures;
|
|
return DT_TableDataT1;
|
|
}
|
|
|
|
public override async Task<long> Add(InsertGhre_CourseSceneInput entity)
|
|
{
|
|
var courseIds = new List<long>();
|
|
if (entity.Courses != null && entity.Courses.Any())
|
|
{
|
|
courseIds = entity.Courses.Select(x => x.Id).ToList();
|
|
entity.CourseId = JsonHelper.ObjToJson(courseIds);
|
|
|
|
var list = Db.Queryable<Ghre_Course>().Where(x => courseIds.Contains(x.Id)).Select(x => x.CourseName).ToList();
|
|
|
|
entity.CourseName = string.Join("、", list.ToArray());
|
|
}
|
|
var id = await base.Add(entity);
|
|
var courses = Db.Queryable<Ghre_Course>().Where(x => courseIds.Contains(x.Id)).ToList();
|
|
for (int i = 0; i < courses.Count; i++)
|
|
{
|
|
var courseSceneIds = new List<long>();
|
|
if (courses[i].CourseSceneIds.IsNotEmptyOrNull())
|
|
courseSceneIds = JsonHelper.JsonToObj<List<long>>(courses[i].CourseSceneIds);
|
|
courseSceneIds.Add(id);
|
|
courses[i].CourseSceneIds = JsonHelper.ObjToJson(courseSceneIds);
|
|
await Db.Updateable(courses).UpdateColumns(it => new { it.CourseSceneIds }, true)//true表示追加AOP赋值列
|
|
.ExecuteCommandAsync();
|
|
}
|
|
return id;
|
|
}
|
|
|
|
public override async Task<bool> Update(long Id, EditGhre_CourseSceneInput editModel)
|
|
{
|
|
var courseIds = new List<long>();
|
|
if (editModel.Courses != null && editModel.Courses.Any())
|
|
{
|
|
courseIds = editModel.Courses.Select(x => x.Id).ToList();
|
|
editModel.CourseId = JsonHelper.ObjToJson(courseIds);
|
|
|
|
var list = Db.Queryable<Ghre_Course>().Where(x => courseIds.Contains(x.Id)).Select(x => x.CourseName).ToList();
|
|
|
|
editModel.CourseName = string.Join("、", list.Select(x => x).ToArray());
|
|
}
|
|
var result = await base.Update(Id, editModel);
|
|
|
|
var courses = await Db.Queryable<Ghre_Course>().Where(x => x.CourseSceneIds != null && x.CourseSceneIds.Contains(Id.ObjToString())).ToListAsync();
|
|
|
|
for (int j = 0; j < courses.Count; j++)
|
|
{
|
|
var courseSceneIds = new List<long>();
|
|
if (courses[j].CourseSceneIds.IsNotEmptyOrNull())
|
|
courseSceneIds = JsonHelper.JsonToObj<List<long>>(courses[j].CourseSceneIds);
|
|
courseSceneIds = courseSceneIds.Where(x => x != Id).ToList();
|
|
courses[j].CourseSceneIds = JsonHelper.ObjToJson(courseSceneIds);
|
|
await Db.Updateable(courses).UpdateColumns(it => new { it.CourseSceneIds }, true)//true表示追加AOP赋值列
|
|
.ExecuteCommandAsync();
|
|
}
|
|
courses = Db.Queryable<Ghre_Course>().Where(x => courseIds.Contains(x.Id)).ToList();
|
|
for (int i = 0; i < courses.Count; i++)
|
|
{
|
|
var courseSceneIds = new List<long>();
|
|
if (courses[i].CourseSceneIds.IsNotEmptyOrNull())
|
|
courseSceneIds = JsonHelper.JsonToObj<List<long>>(courses[i].CourseSceneIds);
|
|
courseSceneIds.Add(Id);
|
|
courses[i].CourseSceneIds = JsonHelper.ObjToJson(courseSceneIds);
|
|
await Db.Updateable(courses).UpdateColumns(it => new { it.CourseSceneIds }, true)//true表示追加AOP赋值列
|
|
.ExecuteCommandAsync();
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
#region Excel导入
|
|
|
|
|
|
public override async Task<ServiceResult<string>> DownloadExcel(string menuName)
|
|
{
|
|
var physicsPath = $"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}wwwroot";
|
|
var path = $"{$"{Path.DirectorySeparatorChar}files{Path.DirectorySeparatorChar}ExcelTemplate{Path.DirectorySeparatorChar}"}";
|
|
if (!Directory.Exists(physicsPath + path))
|
|
Directory.CreateDirectory(physicsPath + path);
|
|
|
|
Type entityType = typeof(Ghre_CourseScene);
|
|
var fileName = entityType.GetEntityTableName() + ".xlsx";
|
|
|
|
|
|
var physicsPath1 = physicsPath + path + fileName;
|
|
//if (dataSourceLists.Any())
|
|
// physicsPath1 = physicsPath + path + newFileName;
|
|
var result = ServiceResult<string>.OprateSuccess("课程场景_" + DateTimeHelper.ConvertToSecondString1(DateTime.Now) + ".xlsx", physicsPath1);
|
|
return result;
|
|
}
|
|
public override async Task<ServiceResult<ExcelData>> ImportExcel(IFormFile file, string menuName = null, long? MasterId = null)
|
|
{
|
|
var data = new ExcelData();
|
|
long id = SnowFlakeSingle.instance.getID();
|
|
var physicsPath = $"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}wwwroot";
|
|
var path = $"{$"{Path.DirectorySeparatorChar}files{Path.DirectorySeparatorChar}import{Path.DirectorySeparatorChar}{id}{Path.DirectorySeparatorChar}"}";
|
|
if (!Directory.Exists(physicsPath + path))
|
|
Directory.CreateDirectory(physicsPath + path);
|
|
|
|
var filepath = physicsPath + path + file.FileName;
|
|
using (var stream = File.Create(filepath))
|
|
{
|
|
await file.CopyToAsync(stream);
|
|
}
|
|
string extension = Path.GetExtension(filepath);
|
|
|
|
bool isExistError = false;
|
|
var id1 = SnowFlakeSingle.instance.getID();
|
|
string errorFileName = path + SnowFlakeSingle.instance.getID() + extension;
|
|
|
|
var dt = NPOIHelper.ImportExcel(filepath, "课程场景");
|
|
if (dt.Columns["Comments"] == null)
|
|
dt.Columns.Add("Comments", typeof(string));
|
|
|
|
for (int i = 0; i < dt.Rows.Count; i++)
|
|
{
|
|
var comments = new List<string>();
|
|
|
|
if (!dt.Columns.Contains("场景编号"))
|
|
{
|
|
comments.Add("未查询到【场景编号】列!");
|
|
data.ErrorCount++;
|
|
dt.Rows[i]["Comments"] = string.Join(";", comments.Select(a => a));
|
|
isExistError = true;
|
|
continue;
|
|
}
|
|
if (!dt.Columns.Contains("场景名称"))
|
|
{
|
|
comments.Add("未查询到【场景名称】列!");
|
|
data.ErrorCount++;
|
|
dt.Rows[i]["Comments"] = string.Join(";", comments.Select(a => a));
|
|
isExistError = true;
|
|
continue;
|
|
}
|
|
if (!dt.Columns.Contains("备注"))
|
|
{
|
|
comments.Add("未查询到【备注】列!");
|
|
data.ErrorCount++;
|
|
dt.Rows[i]["Comments"] = string.Join(";", comments.Select(a => a));
|
|
isExistError = true;
|
|
continue;
|
|
}
|
|
|
|
|
|
var SceneNo = dt.Rows[i]["场景编号"].ToString();
|
|
var SceneName = dt.Rows[i]["场景名称"].ToString();
|
|
if (SceneNo.IsNullOrEmpty() && SceneName.IsNullOrEmpty())
|
|
continue;
|
|
|
|
var remarkSz = dt.Rows[i]["备注"].ToString();
|
|
|
|
if (await base.AnyAsync(x => x.SceneName == SceneName && x.SceneNo == SceneNo))
|
|
{
|
|
comments.Add($"系统已存在相同编号名称课程场景数据!");
|
|
data.ErrorCount++;
|
|
dt.Rows[i]["Comments"] = string.Join(";", comments.Select(a => a));
|
|
isExistError = true;
|
|
continue;
|
|
}
|
|
|
|
|
|
var dict = new Dictionary<string, object>
|
|
{
|
|
{ "Id", SnowFlakeSingle.Instance.NextId() },
|
|
{ "CreateBy", App.User.ID },
|
|
{ "CreateTime", DateTime.Now },
|
|
{ "SceneNo", SceneNo },
|
|
{ "SceneName", SceneName },
|
|
{ "RemarkSz", remarkSz }
|
|
};
|
|
try
|
|
{
|
|
await Db.Insertable(dict).AS("Ghre_CourseScene").ExecuteCommandAsync();
|
|
data.SuccessCount++;
|
|
//data.SuccessCount += list.Count;
|
|
}
|
|
catch (Exception E)
|
|
{
|
|
comments.Add(E.Message);
|
|
data.ErrorCount++;
|
|
dt.Rows[i]["Comments"] = string.Join(";", comments.Select(a => a));
|
|
isExistError = true;
|
|
continue;
|
|
}
|
|
|
|
}
|
|
|
|
if (isExistError)
|
|
{
|
|
NPOIHelper.ExportExcel(dt, null, "课程场景", physicsPath + errorFileName);
|
|
data.filePath = "/Advanced" + errorFileName;
|
|
}
|
|
return ServiceResult<ExcelData>.OprateSuccess("导入成功!", data);
|
|
}
|
|
#endregion
|
|
} |