using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using NCC.Common.Core.Manager; using NCC.Common.Enum; using NCC.Common.Filter; using NCC.Dependency; using NCC.DynamicApiController; using NCC.Extend.Entitys.Dto.LqStudyClass; using NCC.Extend.Entitys.lq_study_class; using NCC.Extend.Entitys.lq_study_student; using NCC.Extend.Entitys.lq_study_record; using NCC.Extend.Interfaces.LqStudyClass; using NCC.FriendlyException; using NCC.System.Entitys.Permission; using SqlSugar; using Yitter.IdGenerator; using NCC.Extend.Entitys.Enum; namespace NCC.Extend { /// /// 学习班级服务 /// [ApiDescriptionSettings(Tag = "绿纤学习班级管理", Name = "LqStudyClass", Order = 200)] [Route("api/Extend/LqStudyClass")] public class LqStudyClassService : IDynamicApiController, ITransient, ILqStudyClassService { private readonly IUserManager _userManager; private readonly ILogger _logger; private readonly ISqlSugarClient _db; /// /// 构造函数 /// /// 用户管理器 /// 日志记录器 /// 数据库客户端 public LqStudyClassService(IUserManager userManager, ILogger logger, ISqlSugarClient db) { _userManager = userManager; _logger = logger; _db = db; } #region 创建学习班级并添加学员 /// /// 创建学习班级并添加学员 /// /// 创建输入 /// 创建结果 [HttpPost("CreateClassWithStudents")] public async Task CreateClassWithStudentsAsync([FromBody] LqStudyClassCreateWithStudentsInput input) { try { _db.Ado.BeginTran(); // 创建班级 var classId = YitIdHelper.NextId().ToString(); var classEntity = new LqStudyClassEntity { Id = classId, ClassName = input.ClassName, TeacherId = input.TeacherId, StartTime = input.StartTime, EndTime = input.EndTime, Remark = input.Remark, CreateUser = _userManager.UserId, CreateTime = DateTime.Now, IsEffective = StatusEnum.有效.GetHashCode() }; await _db.Insertable(classEntity).ExecuteCommandAsync(); // 创建学员 var studentEntities = new List(); foreach (var student in input.Students) { var studentEntity = new LqStudyStudentEntity { Id = YitIdHelper.NextId().ToString(), EmployeeName = student.EmployeeName, EmployeePhone = student.EmployeePhone, EmployeeId = student.EmployeeId, AdmissionTime = student.AdmissionTime, ClassId = classId, ClassName = input.ClassName, HrBelong = student.HrBelong, CreateUser = _userManager.UserId, CreateTime = DateTime.Now, IsEffective = StatusEnum.有效.GetHashCode() }; studentEntities.Add(studentEntity); } if (studentEntities.Any()) { await _db.Insertable(studentEntities).ExecuteCommandAsync(); } _db.Ado.CommitTran(); return new { success = true, data = new { classId = classId, className = input.ClassName, studentCount = studentEntities.Count, message = $"成功创建班级'{input.ClassName}'并添加{studentEntities.Count}名学员" }, message = "创建成功" }; } catch (Exception ex) { _db.Ado.RollbackTran(); _logger.LogError(ex, "创建学习班级并添加学员失败"); throw NCCException.Oh($"创建失败:{ex.Message}"); } } #endregion #region 向现有班级添加学员 /// /// 向现有班级添加学员 /// /// 添加学员输入 /// 添加结果 [HttpPost("AddStudentsToClass")] public async Task AddStudentsToClassAsync([FromBody] LqStudyClassAddStudentsInput input) { try { // 验证班级是否存在 var classInfo = await _db.Queryable() .Where(x => x.Id == input.ClassId && x.IsEffective == StatusEnum.有效.GetHashCode()) .FirstAsync(); if (classInfo == null) { throw NCCException.Oh("班级不存在或已失效"); } _db.Ado.BeginTran(); // 创建学员 var studentEntities = new List(); foreach (var student in input.Students) { var studentEntity = new LqStudyStudentEntity { Id = YitIdHelper.NextId().ToString(), EmployeeName = student.EmployeeName, EmployeePhone = student.EmployeePhone, EmployeeId = student.EmployeeId, AdmissionTime = student.AdmissionTime, ClassId = input.ClassId, ClassName = classInfo.ClassName, HrBelong = student.HrBelong, CreateUser = _userManager.UserId, CreateTime = DateTime.Now, IsEffective = StatusEnum.有效.GetHashCode() }; studentEntities.Add(studentEntity); } if (studentEntities.Any()) { await _db.Insertable(studentEntities).ExecuteCommandAsync(); } _db.Ado.CommitTran(); return new { classId = input.ClassId, className = classInfo.ClassName, addedStudentCount = studentEntities.Count, message = $"成功向班级'{classInfo.ClassName}'添加{studentEntities.Count}名学员" }; } catch (Exception ex) { _db.Ado.RollbackTran(); _logger.LogError(ex, "向班级添加学员失败"); throw NCCException.Oh($"添加失败:{ex.Message}"); } } #endregion #region 获取所有班级列表 /// /// 获取所有班级列表 /// /// 查询输入 /// 班级列表 [HttpGet("GetClassList")] public async Task GetClassListAsync([FromQuery] LqStudyClassListQueryInput input) { try { var sidx = input.sidx == null ? "id" : input.sidx; // 查询班级信息 var data = await _db.Queryable() .WhereIF(!string.IsNullOrWhiteSpace(input.ClassName), x => x.ClassName.Contains(input.ClassName)) .WhereIF(!string.IsNullOrWhiteSpace(input.TeacherId), x => x.TeacherId == input.TeacherId) .WhereIF(input.StartTime.HasValue, x => x.StartTime >= input.StartTime.Value) .WhereIF(input.EndTime.HasValue, x => x.StartTime <= input.EndTime.Value) .WhereIF(input.IsEffective.HasValue, x => x.IsEffective == input.IsEffective.Value) .Select(x => new LqStudyClassListOutput { id = x.Id, className = x.ClassName, teacherId = x.TeacherId, teacherName = SqlFunc.Subqueryable().Where(u => u.Id == x.TeacherId).Select(u => u.RealName), startTime = x.StartTime, endTime = x.EndTime, remark = x.Remark, }) .MergeTable() .OrderBy(sidx + " " + input.sort) .ToPagedListAsync(input.currentPage, input.pageSize); return PageResult.SqlSugarPageResult(data); } catch (Exception ex) { _logger.LogError(ex, "获取班级列表失败"); throw NCCException.Oh($"获取班级列表失败:{ex.Message}"); } } #endregion #region 获取班级下所有学员信息 /// /// 获取班级下所有学员信息(分页) /// /// 查询输入 /// 学员列表 [HttpGet("GetStudentListByClassId")] public async Task GetStudentListByClassIdAsync([FromQuery] LqStudyStudentListQueryInput input) { try { var sidx = input.sidx == null ? "id" : input.sidx; // 查询学员信息 var data = await _db.Queryable() .Where(x => x.ClassId == input.ClassId) .WhereIF(!string.IsNullOrWhiteSpace(input.EmployeeName), x => x.EmployeeName.Contains(input.EmployeeName)) .WhereIF(!string.IsNullOrWhiteSpace(input.EmployeePhone), x => x.EmployeePhone.Contains(input.EmployeePhone)) .WhereIF(!string.IsNullOrWhiteSpace(input.EmployeeId), x => x.EmployeeId == input.EmployeeId) .WhereIF(!string.IsNullOrWhiteSpace(input.HrBelong), x => x.HrBelong.Contains(input.HrBelong)) .WhereIF(input.IsEffective.HasValue, x => x.IsEffective == input.IsEffective.Value) .Select(x => new LqStudyStudentListOutput { id = x.Id, employeeName = SqlFunc.Subqueryable().Where(u => u.Id == x.EmployeeId).Select(u => u.RealName), employeePhone = SqlFunc.Subqueryable().Where(u => u.Id == x.EmployeeId).Select(u => u.MobilePhone), employeeId = x.EmployeeId, admissionTime = x.AdmissionTime, classId = x.ClassId, className = x.ClassName }) .MergeTable() .OrderBy(sidx + " " + input.sort) .ToPagedListAsync(input.currentPage, input.pageSize); return PageResult.SqlSugarPageResult(data); } catch (Exception ex) { _logger.LogError(ex, "获取班级学员列表失败"); throw NCCException.Oh($"获取班级学员列表失败:{ex.Message}"); } } #endregion #region 学习记录管理 /// /// 添加学习记录 /// /// 学习记录输入 /// 添加结果 [HttpPost("AddStudyRecord")] public async Task AddStudyRecordAsync([FromBody] LqStudyRecordCreateInput input) { // 验证员工是否存在 var employee = await _db.Queryable() .Where(x => x.Id == input.EmployeeId && x.RealName == input.EmployeeName) .FirstAsync(); if (employee == null) { throw NCCException.Oh("员工不存在,请检查员工ID和姓名是否正确"); } // 如果选择下店协助,验证门店信息 if (input.IsStoreAssist == 1) { if (string.IsNullOrWhiteSpace(input.StoreId) || string.IsNullOrWhiteSpace(input.StoreName)) { throw NCCException.Oh("选择下店协助时,门店ID和门店名称不能为空"); } } // 创建学习记录 var recordEntity = new LqStudyRecordEntity { Id = YitIdHelper.NextId().ToString(), EmployeeName = input.EmployeeName, EmployeeId = input.EmployeeId, StudyType = input.StudyType, TransportFee = input.TransportFee, StudyDate = input.StudyDate, DailyStatus = input.DailyStatus, Remark = input.Remark, IsStoreAssist = input.IsStoreAssist, StoreId = input.IsStoreAssist == 1 ? input.StoreId : null, StoreName = input.IsStoreAssist == 1 ? input.StoreName : null, CreateUser = _userManager.UserId, CreateTime = DateTime.Now, IsEffective = 1 }; var isOk = await _db.Insertable(recordEntity).ExecuteCommandAsync(); if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1000); } #endregion #region 获取学习记录列表 /// /// 获取学习记录列表 /// /// 查询输入 /// 学习记录列表 [HttpGet("GetStudyRecordList")] public async Task GetStudyRecordListAsync([FromQuery] LqStudyRecordListQueryInput input) { try { var sidx = input.sidx == null ? "id" : input.sidx; // 查询学习记录信息 var data = await _db.Queryable() .WhereIF(!string.IsNullOrWhiteSpace(input.EmployeeName), x => x.EmployeeName.Contains(input.EmployeeName)) .WhereIF(!string.IsNullOrWhiteSpace(input.EmployeeId), x => x.EmployeeId == input.EmployeeId) .WhereIF(!string.IsNullOrWhiteSpace(input.StudyType), x => x.StudyType.Contains(input.StudyType)) .WhereIF(input.StudyDateStart.HasValue, x => x.StudyDate >= input.StudyDateStart.Value) .WhereIF(input.StudyDateEnd.HasValue, x => x.StudyDate <= input.StudyDateEnd.Value) .WhereIF(!string.IsNullOrWhiteSpace(input.DailyStatus), x => x.DailyStatus.Contains(input.DailyStatus)) .WhereIF(input.IsStoreAssist.HasValue, x => x.IsStoreAssist == input.IsStoreAssist.Value) .WhereIF(!string.IsNullOrWhiteSpace(input.StoreId), x => x.StoreId == input.StoreId) .WhereIF(input.IsEffective.HasValue, x => x.IsEffective == input.IsEffective.Value) .Select(x => new LqStudyRecordListOutput { id = x.Id, employeeName = x.EmployeeName, employeeId = x.EmployeeId, studyType = x.StudyType, transportFee = x.TransportFee, studyDate = x.StudyDate, dailyStatus = x.DailyStatus, remark = x.Remark, isStoreAssist = x.IsStoreAssist, storeId = x.StoreId, storeName = x.StoreName, createUser = x.CreateUser, createUserName = SqlFunc.Subqueryable().Where(u => u.Id == x.CreateUser).Select(u => u.RealName), createTime = x.CreateTime, updateUser = x.UpdateUser, updateUserName = SqlFunc.Subqueryable().Where(u => u.Id == x.UpdateUser).Select(u => u.RealName), updateTime = x.UpdateTime, isEffective = x.IsEffective }) .MergeTable() .OrderBy(sidx + " " + input.sort) .ToPagedListAsync(input.currentPage, input.pageSize); return PageResult.SqlSugarPageResult(data); } catch (Exception ex) { _logger.LogError(ex, "获取学习记录列表失败"); throw NCCException.Oh($"获取学习记录列表失败:{ex.Message}"); } } #endregion #region 作废学习记录 /// /// 作废学习记录 /// /// 学习记录ID /// 作废结果 [HttpPost("CancelStudyRecord")] public async Task CancelStudyRecordAsync([FromBody] string id) { var record = await _db.Queryable().Where(x => x.Id == id).FirstAsync(); if (record == null) throw NCCException.Oh("学习记录不存在"); record.IsEffective = StatusEnum.无效.GetHashCode(); record.UpdateTime = DateTime.Now; record.UpdateUser = _userManager.UserId; var isOk = await _db.Updateable(record).ExecuteCommandAsync(); if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1000); } #endregion } }