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 Newtonsoft.Json.Linq; using Tiobon.Core.Common; using Tiobon.Core.Model; using Newtonsoft.Json; using Tiobon.Core.Common.Helper; using Org.BouncyCastle.Crypto; namespace Tiobon.Core.Services; /// /// 必选修规则 (服务) /// public class Ghre_StudyRuleServices : BaseServices, IGhre_StudyRuleServices { private readonly IBaseRepository _dal; private readonly IGhre_CourseServices _ghre_CourseServices; private readonly IGhre_CourseSceneServices _ghre_CourseSceneServices; private readonly IGhre_StudyRuleStaffServices _ghre_StudyRuleStaffServices; public Ghre_StudyRuleServices(ICaching caching, IGhre_CourseServices ghre_CourseServices, IGhre_CourseSceneServices ghre_CourseSceneServices, IGhre_StudyRuleStaffServices ghre_StudyRuleStaffServices, IBaseRepository dal) { this._dal = dal; base.BaseDal = dal; base._caching = caching; _ghre_CourseServices = ghre_CourseServices; _ghre_CourseSceneServices = ghre_CourseSceneServices; _ghre_StudyRuleStaffServices = ghre_StudyRuleStaffServices; } public override async Task> QueryFilterPage(QueryBody filter, string condition, bool? IsEnable = true) { if (string.IsNullOrWhiteSpace(filter.orderBy)) filter.orderBy = "CreateTime1 DESC"; if (filter.pageSize == 0) filter.pageSize = 10000; var countSql = @$" SELECT COUNT(1) FROM Ghre_StudyRule_V"; var sql = @$" SELECT * FROM Ghre_StudyRule_V A"; string conditions = " WHERE 1=1 "; if (IsEnable == true) conditions += " AND IsEnable = 1"; else if (IsEnable == false) conditions += " AND IsEnable = 0"; if (!string.IsNullOrWhiteSpace(condition)) conditions += " AND " + condition; 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 (name == "Date") //{ // var jsonParam = JsonConvert.DeserializeObject(value); // conditions += $" AND (Date BETWEEN '{jsonParam.columnValue[0]}' AND '{jsonParam.columnValue[1]}')"; // 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; case "GreaterOrEqual"://大于等于 conditions += $" AND {name} >='{jsonParam.columnValue}'"; break; case "Greater"://大于 conditions += $" AND {name} >'{jsonParam.columnValue}'"; break; case "LessOrEqual"://小于等于 conditions += $" AND {name} <='{jsonParam.columnValue}'"; break; case "Less"://小于 conditions += $" AND {name} <'{jsonParam.columnValue}'"; break; default: break; } } } sql += conditions; countSql += conditions; int total = await Db.Ado.GetIntAsync(countSql); sql = "SELECT * FROM (SELECT *, ROW_NUMBER() OVER (ORDER BY " + filter.orderBy + ") NUM FROM (SELECT * FROM (" + sql + " "; sql += ") A ) B ) C"; sql += " WHERE NUM <= " + filter.pageNum * filter.pageSize + " AND NUM >" + (filter.pageNum - 1) * filter.pageSize; var entitys = await Db.Ado.SqlQueryAsync(sql); var ids = entitys.Select(x => x.Id).ToList(); var staffs = await _ghre_StudyRuleStaffServices.Query(x => x.StudyRuleId != null && ids.Contains(x.StudyRuleId.Value)); entitys.ForEach(x => { x.StaffNames = string.Join(",", staffs.Where(o => o.StudyRuleId == x.Id).Select(o => o.StaffName)); }); return new ServicePageResult(filter.pageNum, total, filter.pageSize, entitys); } public override async Task Add(InsertGhre_StudyRuleInput entity) { var result = await base.Add(entity); if (entity.Staffs != null && entity.Staffs.Any()) { var insertStaffs = entity.Staffs.Select(x => new InsertGhre_StudyRuleStaffInput { StaffId = x, StudyRuleId = result }).ToList(); await _ghre_StudyRuleStaffServices.Add(insertStaffs); } string sql = @"UPDATE A SET A.StaffName = B.StaffName FROM Ghre_StudyRuleStaff A LEFT JOIN Ghra_Staff B ON A.StaffId = B.StaffID WHERE A.StaffName IS NULL"; await Db.Ado.ExecuteCommandAsync(sql); return result; } public override async Task Update(long Id, EditGhre_StudyRuleInput editModel) { var result = await base.Update(Id, editModel); await _ghre_StudyRuleStaffServices.Delete(x => x.StudyRuleId == Id); if (editModel.Staffs != null && editModel.Staffs.Any()) { var insertStaffs = editModel.Staffs.Select(x => new InsertGhre_StudyRuleStaffInput { StaffId = x, StudyRuleId = Id }).ToList(); await _ghre_StudyRuleStaffServices.Add(insertStaffs); } string sql = @"UPDATE A SET A.StaffName = B.StaffName FROM Ghre_StudyRuleStaff A LEFT JOIN Ghra_Staff B ON A.StaffId = B.StaffID WHERE A.StaffName IS NULL"; await Db.Ado.ExecuteCommandAsync(sql); return result; } public override async Task> QueryForm(QueryForm body) { var result = await base.QueryForm(body); if (result.result.DT_TableDataT1.Any()) { var staffs = await _ghre_StudyRuleStaffServices.Query(x => x.StudyRuleId != null && x.StudyRuleId == result.result.DT_TableDataT1[0].Id); var DT_TableDataT1 = result.result.DT_TableDataT1; DT_TableDataT1.ForEach(t => { t.Staffs = staffs.Where(x => x.StaffId != null).Select(x => x.StaffId.Value).ToList(); }); result.result.DT_TableDataT1 = DT_TableDataT1; } return result; } }