using Tiobon.Core.IServices; using Tiobon.Core.Model.Models; using Tiobon.Core.Services.BASE; using Tiobon.Core.IRepository.Base; using Tiobon.Core.Common.Caches; using Tiobon.Core.Common; using Tiobon.Core.Model; using Newtonsoft.Json.Linq; using SqlSugar; using Newtonsoft.Json; using MySqlX.XDevAPI.Common; using System.Data; using System.Dynamic; using Tiobon.Core.Common.DB.Dapper.Extensions; using AgileObjects.AgileMapper; using Microsoft.AspNetCore.Http; using Tiobon.Core.Common.UserManager; using Tiobon.Core.Common.Helper; namespace Tiobon.Core.Services; /// /// 课程 (服务) /// public class Ghre_CourseServices : BaseServices, IGhre_CourseServices { private readonly IBaseRepository _dal; private IGhre_CourseClassServices _ghre_CourseClassServices; public Ghre_CourseServices(IBaseRepository dal, ICaching caching, IGhre_CourseClassServices ghre_CourseClassServices) { this._dal = dal; base.BaseDal = dal; _ghre_CourseClassServices = ghre_CourseClassServices; base._caching = caching; } public override async Task> QueryFilterPage(QueryBody filter) { RefAsync totalCount = 0; string sql = @"SELECT * FROM (SELECT A.*, B.ClassName CourseClassName, C.ClassName CourseClassName2, D.SceneName CourseSceneName, E.CourseWareName, F.StaffName ManagerStaffName, G.UserName CreateName, G.UserName UpdateName, CASE A.InOrOut WHEN 'In' THEN F.StaffName WHEN 'Out' THEN J.TeacherName END TeacherName, ISNULL(A.UpdateTime, A.CreateTime) CreateTime1 FROM Ghre_Course A LEFT JOIN Ghre_CourseClass B ON A.CourseClassId = B.Id LEFT JOIN Ghre_CourseClass C ON A.CourseClassId2 = C.Id LEFT JOIN Ghre_CourseScene D ON A.CourseSceneId = D.Id LEFT JOIN Ghre_CourseWare E ON A.CourseWareId = E.Id LEFT JOIN Ghra_Staff F ON A.ManagerId = F.StaffID LEFT JOIN Ghrs_User G ON A.CreateBy = G.UserId LEFT JOIN Ghrs_User H ON A.UpdateBy = H.UserId LEFT JOIN Ghra_Staff I ON A.TeacherId = I.StaffID LEFT JOIN Ghre_Teacher J ON A.SchoolTeacherId = I.StaffID WHERE A.IsEnable = 1) A"; if (string.IsNullOrWhiteSpace(filter.orderBy)) filter.orderBy = "CreateTime1 DESC"; string conditions = "1=1"; if (filter.jsonParam != null) foreach (JProperty jProperty in filter.jsonParam.Properties()) { var name = jProperty.Name; var value = jProperty.Value.ToString(); if (name == "page" || name == "pageSize") continue; if (!string.IsNullOrWhiteSpace(value)) { var jsonParam = JsonConvert.DeserializeObject(value); switch (jsonParam.operationKey) { case "Include": conditions += $" AND {name} LIKE '%{jsonParam.columnValue}%'"; break; case "NotInclude": conditions += $" AND {name} NOT LIKE '%{jsonParam.columnValue}%'"; break; case "IsNull": conditions += $" AND {name} IS NULL"; break; case "NotNull": conditions += $" AND {name} IS NOT NULL"; break; case "Equal": conditions += $" AND {name} ='{jsonParam.columnValue}'"; break; case "NotEqual": conditions += $" AND {name} !='{jsonParam.columnValue}'"; break; default: break; } } } if (filter.pageSize == 0) filter.pageSize = 10000; var data = await Db.SqlQueryable(sql) .OrderBy(filter.orderBy) .ToPageListAsync(filter.pageNum, filter.pageSize, totalCount); data.ForEach(async x => { x.ChargeMethodLabel = await GetParaLabel("CourseChargeMethod", x.ChargeMethod); x.InOrOutLabel = await GetParaLabel("CourseInOrOut", x.InOrOut); x.IsOPenLabel = x.IsOPen == true ? "是" : "否"; }); return new ServicePageResult(filter.pageNum, totalCount, filter.pageSize, data); } public async Task> QueryList(QueryBody filter, string status) { RefAsync totalCount = 0; string sql = @"SELECT * FROM (SELECT A.*, B.ClassName CourseClassName, C.ClassName CourseClassName2, D.SceneName CourseSceneName, E.CourseWareName, F.StaffName ManagerStaffName, G.UserName CreateName, H.UserName UpdateName, CASE A.InOrOut WHEN 'In' THEN F.StaffName WHEN 'Out' THEN J.TeacherName END TeacherName, ISNULL(A.UpdateTime, A.CreateTime) CreateTime1 FROM Ghre_Course A LEFT JOIN Ghre_CourseClass B ON A.CourseClassId = B.Id LEFT JOIN Ghre_CourseClass C ON A.CourseClassId2 = C.Id LEFT JOIN Ghre_CourseScene D ON A.CourseSceneId = D.Id LEFT JOIN Ghre_CourseWare E ON A.CourseWareId = E.Id LEFT JOIN Ghra_Staff F ON A.ManagerId = F.StaffID LEFT JOIN Ghrs_User G ON A.CreateBy = G.UserId LEFT JOIN Ghrs_User H ON A.UpdateBy = H.UserId LEFT JOIN Ghra_Staff I ON A.TeacherId = I.StaffID LEFT JOIN Ghre_Teacher J ON A.SchoolTeacherId = I.StaffID WHERE A.IsEnable = 1) A"; if (string.IsNullOrWhiteSpace(filter.orderBy)) filter.orderBy = "CreateTime1 DESC"; string conditions = " WHERE 1=1"; if (!string.IsNullOrWhiteSpace(status)) conditions += $" AND Status ='{status}'"; if (filter.jsonParam != null) foreach (JProperty jProperty in filter.jsonParam.Properties()) { var name = jProperty.Name; var value = jProperty.Value.ToString(); if (name == "page" || name == "pageSize") continue; if (!string.IsNullOrWhiteSpace(value)) { var jsonParam = JsonConvert.DeserializeObject(value); if (name == "CourseNoOrName") { conditions += $" AND ( CourseNo LIKE '%{jsonParam.columnValue}%' OR CourseName LIKE '%{jsonParam.columnValue}%')"; continue; } switch (jsonParam.operationKey) { case "Include": conditions += $" AND {name} LIKE '%{jsonParam.columnValue}%'"; break; case "NotInclude": conditions += $" AND {name} NOT LIKE '%{jsonParam.columnValue}%'"; break; case "IsNull": conditions += $" AND {name} IS NULL"; break; case "NotNull": conditions += $" AND {name} IS NOT NULL"; break; case "Equal": conditions += $" AND {name} ='{jsonParam.columnValue}'"; break; case "NotEqual": conditions += $" AND {name} !='{jsonParam.columnValue}'"; break; default: break; } } } if (filter.pageSize == 0) filter.pageSize = 10000; sql += conditions; var data = await Db.SqlQueryable(sql) .OrderBy(filter.orderBy) .ToPageListAsync(filter.pageNum, filter.pageSize, totalCount); data.ForEach(async x => { x.ChargeMethodLabel = await GetParaLabel("CourseChargeMethod", x.ChargeMethod); x.InOrOutLabel = await GetParaLabel("CourseInOrOut", x.InOrOut); x.IsOPenLabel = x.IsOPen == true ? "是" : "否"; }); return new ServicePageResult(filter.pageNum, totalCount, filter.pageSize, data); } public async Task> QueryDeptID(int StaffID) { string sql = $"SELECT A.DeptID from Ghra_Staff A LEFT JOIN Ghro_Dept B ON A.DeptID = B.DeptID WHERE A.StaffID='{StaffID}'"; int id = await Db.Ado.GetIntAsync(sql); return ServiceResult.OprateSuccess("查询成功!", id); } public async Task> QueryTeacher(long? linkId) { var result = new CommonSelect(); JArray TableColumn = new JArray(); JArray DT_TablePageInfoT1 = new JArray(); JArray DT_TableDataT1 = new JArray(); JObject searchItem = new JObject(); JObject item; DataTable dt; string sql; searchItem = [ new JProperty("defaultHidden", true), new JProperty("field","value"), new JProperty("label","讲师ID"), ]; TableColumn.Add(searchItem); searchItem = [ new JProperty("field","label"), new JProperty("label","讲师名称"), ]; TableColumn.Add(searchItem); sql = "SELECT Id, TeacherNo , TeacherName FROM Ghre_Teacher WHERE IsEnable=1 "; if (linkId != null) sql += $"AND SchoolId ='{linkId}'"; dt = await Db.Ado.GetDataTableAsync(sql); for (int i = 0; i < dt.Rows.Count; i++) { item = [ new JProperty("value",long.Parse(dt.Rows[i]["Id"].ToString())), new JProperty("label",dt.Rows[i]["TeacherNo"].ToString()+"-"+dt.Rows[i]["TeacherName"].ToString()) ]; DT_TableDataT1.Add(item); } item = [ new JProperty("ListMax",100), new JProperty("ListMin",10), new JProperty("PageNum",1), new JProperty("PageSize",99999), new JProperty("TotalCount",dt.Rows.Count), new JProperty("UIType","Auto") ]; DT_TablePageInfoT1.Add(item); result.JM_TableColumnT1.TableColumn = TableColumn; result.DT_TableDataT1 = DT_TableDataT1; result.DT_TablePageInfoT1 = DT_TablePageInfoT1; return new ServiceResult() { Success = true, Message = "查询成功", Data = result, }; } public override async Task> QueryForm(QueryForm body) { var result = await base.QueryForm(body); string examPaperId = result.result.DT_TableDataT1[0].ExamPaperId; if (!string.IsNullOrWhiteSpace(examPaperId)) result.result.DT_TableDataT1[0].ExamPaperIds = JsonConvert.DeserializeObject>(examPaperId); else result.result.DT_TableDataT1[0].ExamPaperIds = new List(); if (body.doType == "Copy") { result.result.DT_TableDataT1[0].CourseNo = null; result.result.DT_TableDataT1[0].CourseName = null; } return result; } public async Task UpdateStatus(long[] ids, string status) { if (status != "Released" && status != "Disabled" && status != "Draft") throw new Exception("无效的状态"); HttpRequest request = UserContext.Context.Request; var api = request.Path.ObjToString().TrimEnd('/').ToLower(); var ip = GetUserIp(UserContext.Context); var entities = new List(); foreach (var id in ids) { if (id == null || !BaseDal.Any(id)) continue; var entity = await BaseDal.QueryById(id); if (entity.Status == "Released") { string sql = $"SELECT count(0) FROM Ghre_Exam WHERE CourseId='{id}' and IsEnable=1"; if (await Db.Ado.GetIntAsync(sql) > 0) return ServiceResult.OprateFailed($"课程【{entity.CourseName}】已在考试中关联,不可{(status == "Disabled" ? "停用" : "取消发布")}!"); } BasePoco ent = entity; ent.UpdateIP = ip; ent.UpdateProg = api; if (status == "Released" || status == "Disabled" || status == "Draft") { entity.Status = status; entities.Add(entity); } } var result = await BaseDal.Update(entities); if (status == "Released") return ServiceResult.OprateSuccess("发布成功!"); else if (status == "Draft") return ServiceResult.OprateSuccess("取消发布成功!"); else return ServiceResult.OprateSuccess("停用成功!"); } public override async Task Add(InsertGhre_CourseInput entity) { entity.ExamPaperId = JsonHelper.ObjToJson(entity.ExamPaperIds); entity.Status = "Draft"; return await base.Add(entity); } public override async Task Update(long Id, EditGhre_CourseInput editModel) { editModel.ExamPaperId = JsonHelper.ObjToJson(editModel.ExamPaperIds); return await base.Update(Id, editModel); } #region 获取通用列表下拉 /// /// 获取通用列表下拉 /// /// /// public async Task> GetSelectAsync(long? linkId) { var result = new CommonSelect(); var TableColumn = new JArray(); var DT_TablePageInfoT1 = new JArray(); var DT_TableDataT1 = new JArray(); var searchItem = new JObject(); JObject item; DataTable dt; string sql; searchItem = [ new JProperty("defaultHidden", true), new JProperty("field","value"), new JProperty("label","课程ID"), ]; TableColumn.Add(searchItem); searchItem = [ new JProperty("field","label"), new JProperty("label","课程名称"), ]; TableColumn.Add(searchItem); sql = $"SELECT Id, CourseNo , CourseName FROM Ghre_Course WHERE Status ='Released' AND IsEnable=1"; if (linkId.HasValue) sql += $" AND CourseSceneId='{linkId}'"; sql += $" ORDER BY UpdateTime DESC, CreateTime DESC"; dt = await Db.Ado.GetDataTableAsync(sql); for (int i = 0; i < dt.Rows.Count; i++) { item = [ new JProperty("value",long.Parse(dt.Rows[i]["Id"].ToString())), new JProperty("label", dt.Rows[i]["CourseName"].ToString() + "("+dt.Rows[i]["CourseNo"].ToString()+")") ]; DT_TableDataT1.Add(item); } item = [ new JProperty("ListMax",100), new JProperty("ListMin",10), new JProperty("PageNum",1), new JProperty("PageSize",99999), new JProperty("TotalCount",dt.Rows.Count), new JProperty("UIType","Auto") ]; DT_TablePageInfoT1.Add(item); result.JM_TableColumnT1.TableColumn = TableColumn; result.DT_TableDataT1 = DT_TableDataT1; result.DT_TablePageInfoT1 = DT_TablePageInfoT1; return new ServiceResult() { Success = true, Message = "查询成功", Data = result, }; } #endregion }