using APT.Infrastructure.Core; using APT.MS.Domain.Entities.SE; using APT.MS.Domain.Enums; using APT.Utility; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Linq; using APT.Infrastructure.Core; using APT.BaseData.Domain.ApiModel; using System.Data; using System.IO; using APT.BaseData.Domain.Entities.FM; using APT.BaseData.Domain.Entities; namespace APT.SE.WebApi.Controllers.Api { [Route("api/SE/SECoursewearlibrary")] public partial class SECoursewearlibraryController : AuthorizeApiController { /// /// 更新 /// /// /// [HttpPost, Route("FullUpdate")] public JsonActionResult FullUpdate([FromBody] T_SE_COURSEWEARLIBRARY entity) { return SafeExecute(() => { var Files = entity.Nav_Files; entity.Nav_Files = null; var Detail = entity.Nav_Detail; entity.Nav_Detail = null; if (Files != null && Files.Count() > 1) { throw new Exception("课件只能单一文件"); } if (entity.FILE_DB_ID.HasValue && Files != null && Files.Count > 0) { throw new Exception("视频和附件只能有一个!");//不然到时候 考试 看视频或者附件不好处理 } if (entity.SCOREPASS == 0) { throw new Exception("请输入及格分(不能为0)!"); } List listT = new List(); List listTPost = new List(); List listTPoint = new List(); if (Detail == null || Detail.Count < 1) { throw new Exception("请添加题目!"); } else { decimal ScorePer = 0; decimal sum = Detail.Where(e => !e.IS_DELETED).Sum(e => e.SCOREPER); if (sum != 100) { int dc = Detail.Where(e => !e.IS_DELETED).Count(); ScorePer = Math.Round(Convert.ToDecimal(100.0 / dc), 2); if (dc * ScorePer != 100) { throw new Exception("题数" + dc + "不能被100除尽!"); } } foreach (var item in Detail) { if (item.IS_DELETED) continue; if (ScorePer != 0) item.SCOREPER = ScorePer; if (item.EDU_CARD_ID == Guid.Empty) item.EDU_CARD_ID = entity.ID; if (item.TEST_ID == Guid.Empty) { throw new Exception("试题ID传参有误,课件试题只能通过导入的方式!"); } if (item.Nav_Test != null) { if (item.Nav_Test.Nav_Points != null && item.Nav_Test.Nav_Points.Count > 0) listTPost.AddRange(item.Nav_Test.Nav_Posts); // item.Nav_Test.Nav_Posts = null; //} if (item.Nav_Test.Nav_Posts != null && item.Nav_Test.Nav_Posts.Count > 0) listTPoint.AddRange(item.Nav_Test.Nav_Points); // item.Nav_Test.Nav_Points = null; //} listT.Add(item.Nav_Test); item.Nav_Test = null; } //if (item.TEST_ID == Guid.Empty) //{ // //手动添加信息 //} } } if (!entity.FILE_DB_ID.HasValue) { Guid? imgFileID = Files.Find(e => !e.IS_DELETED)?.IMG_FILE_ID; if (imgFileID.HasValue) { //去完善 var img = GetEntity(imgFileID.Value); if (img != null) { entity.FILE_PATH = img.FILE_PATH; entity.FILENAME = img.FILE_NAME; } } } if (entity.USER_ID == Guid.Empty) { entity.USER_ID = APT.Infrastructure.Api.AppContext.CurrentSession.UserID.Value; entity.DEPARTMENTID = APT.Infrastructure.Api.AppContext.CurrentSession.DepartmentID.Value; } UnifiedCommit(() => { UpdateEntityNoCommit(entity); if (Detail != null && Detail.Any()) BantchSaveEntityNoCommit(Detail); if (Files != null && Files.Any()) BantchSaveEntityNoCommit(Files); if (listT != null && listT.Any()) BantchSaveEntityNoCommit(listT); if (listTPost != null && listTPost.Any()) BantchSaveEntityNoCommit(listTPost); if (listTPoint != null && listTPoint.Any()) BantchSaveEntityNoCommit(listTPoint); }); return true; }); } /// /// 获得单条实体数据 /// /// 过滤实体 /// [HttpPost, Route("GetShow")] public JsonActionResult GetShow([FromBody] KeywordFilter filter) { return SafeExecute(() => { var result = GetEntity(null, filter, null); if (result.Nav_Detail != null && result.Nav_Detail.Count() > 0) { string ANSWERSHOW = string.Empty; foreach (var item in result.Nav_Detail) { ANSWERSHOW = string.Empty; GetAnswerShow(item.Nav_Test, ref ANSWERSHOW); item.Nav_Test.ANSWERSHOW = ANSWERSHOW; } } return result; }); } /// /// 获取分数显示 /// /// /// private void GetAnswerShow(T_SE_TEST Nav_Test, ref string ANSWERSHOW) { try { if (Nav_Test != null && Nav_Test.ANSWER != 0 && ((int)Nav_Test.TYPE) != 0) { switch (Nav_Test.ANSWER) { case 0: ANSWERSHOW = ""; break; case 1: ANSWERSHOW = "A"; break; case 2: ANSWERSHOW = "B"; break; case 3: ANSWERSHOW = "AB"; break; case 4: ANSWERSHOW = "C"; break; case 5: ANSWERSHOW = "AC"; break; case 6: ANSWERSHOW = "AB"; break; case 7: ANSWERSHOW = "ABC"; break; case 8: ANSWERSHOW = "D"; break; case 9: ANSWERSHOW = "AD"; break; case 10: ANSWERSHOW = "BD"; break; case 11: ANSWERSHOW = "ABD"; break; case 12: ANSWERSHOW = "CD"; break; case 13: ANSWERSHOW = "ACD"; break; case 14: ANSWERSHOW = "BCD"; break; case 15: ANSWERSHOW = "ABCD"; break; case 16: ANSWERSHOW = "E"; break; case 17: ANSWERSHOW = "AE"; break; case 18: ANSWERSHOW = "BE"; break; case 19: ANSWERSHOW = "ABE"; break; case 20: ANSWERSHOW = "CE"; break; case 21: ANSWERSHOW = "ACE"; break; case 22: ANSWERSHOW = "ABE"; break; case 23: ANSWERSHOW = "ABCE"; break; case 24: ANSWERSHOW = "DE"; break; case 25: ANSWERSHOW = "ADE"; break; case 26: ANSWERSHOW = "BDE"; break; case 27: ANSWERSHOW = "ABDE"; break; case 28: ANSWERSHOW = "CDE"; break; case 29: ANSWERSHOW = "ACDE"; break; case 30: ANSWERSHOW = "BCDE"; break; case 31: ANSWERSHOW = "ABCDE"; break; default: break; } } } catch { } } /// /// 获取导入数据 /// 参考 [Route("api/PF/Import")] /// /// [HttpPost, Route("GetImportData")] public JsonActionResult> GetImportData() { return SafeExecute>(() => { List listImport = new List(); var orgId = APT.Infrastructure.Api.AppContext.CurrentSession.OrgId; var httpRequest = this.HttpContext.Request; string orgIdStr = httpRequest.Form["OrgId"];// 获取 组织 //ImportDataModel result = new ImportDataModel(); var dic = Path.Combine(System.AppContext.BaseDirectory, "tempImportFiles"); if (!Directory.Exists(dic)) Directory.CreateDirectory(dic); int NUM = 0; Guid TEST_ID = Guid.Empty; Guid EDU_CARD_ID = Guid.Empty; if (httpRequest.Form.ContainsKey("EDU_CARD_ID")) { string strEduID = httpRequest.Form["EDU_CARD_ID"]; if (!string.IsNullOrEmpty(strEduID)) { try { EDU_CARD_ID = new Guid(strEduID); } catch { EDU_CARD_ID = Guid.Empty; } } } T_SE_TEST test = null; foreach (var key in httpRequest.Form.Files) // 文件键 { var postedFile = key; // 获取文件键对应的文件对象 string filePath = Path.Combine(dic, DateTime.Now.ToString("yyyy_MM_dd_hh_mm_ss") + "_" + postedFile.FileName); Byte[] fileData = new Byte[postedFile.Length]; Stream sr = postedFile.OpenReadStream();//创建数据流对象 sr.Read(fileData, 0, (int)postedFile.Length); using (FileStream fs = new FileStream(filePath, FileMode.CreateNew)) { fs.Write(fileData, 0, fileData.Length); fs.Flush(); fs.Close(); } //获取数据 Dictionary startRowIndexs = new Dictionary(); startRowIndexs.Add(0, 1);//根据Excel格式数据赋值 var dataTables = FileUtils.ReadExcelByOledb(filePath, startRowIndexs); string Msg = string.Empty; bool isOK = InsertModel(dataTables.Tables[0], orgId, EDU_CARD_ID, ref Msg, ref listImport); try { System.IO.File.Delete(filePath); } catch { } //foreach (var item in dataTables.Tables[0].Rows) //{ // //题目 // NUM++; // test = new T_SE_TEST() // { // ID = Guid.NewGuid(), // ORG_ID = orgId, // CREATE_TIME = DateTime.Now, // }; // listImport.Add(new T_SE_COURSEWEARLIBRARY_DETAIL() // { // ID = Guid.NewGuid(), // ORG_ID = orgId, // EDU_CARD_ID = EDU_CARD_ID, // NUM = NUM, // TEST_ID = test.ID, // Nav_Test = test, // CREATE_TIME = test.CREATE_TIME // }); //} } return listImport; }); } /// /// 数据插入 /// /// /// /// /// /// /// /// /// public bool InsertModel(DataTable dtSource, Guid? orgId, Guid EDU_CARD_ID, ref string Msg, ref List listImport, int rowIndex = 1) { var userId = APT.Infrastructure.Api.AppContext.CurrentSession.UserID; if (dtSource == null || dtSource.Rows.Count < rowIndex) { Msg = "未获取到导入数据"; throw new Exception(Msg); } List listNotEmpty = new List() { 0, 1, 7 };//, 8, 10 Dictionary> dicIndexEmpty = new Dictionary>(); int rowAll = dtSource.Rows.Count; int colAll = dtSource.Columns.Count; List mainTables = new List(); List sub1s = new List(); List sub2s = new List(); List sub1sPer = null; List sub2sPer = null; var posts = GetEntities(t => t.IS_DELETED == false, new BaseFilter(orgId)); var points = GetEntities(t => t.IS_DELETED == false, new BaseFilter(orgId)); T_FM_USER_POST modelPost = null; List listPostNameEmpty = new List(); List listAnswerErrorIndex = new List(); List listAnswerError = new List(); string strANSWER = string.Empty; T_SE_TEST_ENUM_POINT modelPoint = null; List listPointNameEmpty = new List(); DateTime dtNow = DateTime.Now; for (int i = 0; i < rowAll; i++) { #region 不能为空 List listi = null; for (int j = 0; j < colAll; j++) { if (!listNotEmpty.Contains(j)) continue; if (string.IsNullOrEmpty(dtSource.Rows[i][j].ToString().Trim())) { if (listi == null) { listi = new List(); } listi.Add(j); } } if (listi != null) dicIndexEmpty.Add(i + rowIndex, listi);//有空的 直接添加 #endregion if (dicIndexEmpty.Count < 1) { T_SE_TEST mainTable = new T_SE_TEST(); mainTable.ID = Guid.NewGuid(); mainTable.ORG_ID = orgId; var testName = dtSource.Rows[i][0].ToString().Trim(); if (!string.IsNullOrEmpty(testName)) { mainTable.NAME = testName; } else { continue; } var testType = dtSource.Rows[i][1].ToString().Trim(); if (!string.IsNullOrEmpty(testType)) { if (dtSource.Rows[i][1].ToString().Trim() == "是非题") { mainTable.TYPE = SETestTypeEnum.是非题; } else if (dtSource.Rows[i][1].ToString().Trim() == "单选题") { mainTable.TYPE = SETestTypeEnum.单选题; } else if (dtSource.Rows[i][1].ToString().Trim() == "多选题") { mainTable.TYPE = SETestTypeEnum.多选题; } else { throw new Exception("没有《" + testType + "》类型"); } } else { throw new Exception("题型不能为空!"); } if (!string.IsNullOrEmpty(dtSource.Rows[i][2].ToString().Trim())) { mainTable.OPTION_A = (dtSource.Rows[i][2].ToString().Trim()); } if (!string.IsNullOrEmpty(dtSource.Rows[i][3].ToString().Trim())) { mainTable.OPTION_B = (dtSource.Rows[i][3].ToString().Trim()); } if (!string.IsNullOrEmpty(dtSource.Rows[i][4].ToString().Trim())) { mainTable.OPTION_C = (dtSource.Rows[i][4].ToString().Trim()); } if (!string.IsNullOrEmpty(dtSource.Rows[i][5].ToString().Trim())) { mainTable.OPTION_D = (dtSource.Rows[i][5].ToString().Trim()); } if (!string.IsNullOrEmpty(dtSource.Rows[i][6].ToString().Trim())) { mainTable.OPTION_E = (dtSource.Rows[i][6].ToString().Trim()); } strANSWER = dtSource.Rows[i][7].ToString().Trim(); if (!string.IsNullOrEmpty(strANSWER)) { mainTable.ANSWERSHOW = strANSWER; if (mainTable.TYPE == SETestTypeEnum.是非题) { if (strANSWER == "是" || strANSWER == "A") { mainTable.ANSWER = 1; } else if (strANSWER == "否" || strANSWER == "B") { mainTable.ANSWER = 2; } else { if (listAnswerError.Contains(strANSWER)) { listAnswerError.Add(strANSWER); } listAnswerErrorIndex.Add((i + rowIndex).ToString()); } } if (mainTable.TYPE == SETestTypeEnum.单选题) { if (strANSWER == "A") { mainTable.ANSWER = 1; } else if (strANSWER == "B") { mainTable.ANSWER = 2; } else if (strANSWER == "C") { mainTable.ANSWER = 4; } else if (strANSWER == "D") { mainTable.ANSWER = 8; } else { if (listAnswerError.Contains(strANSWER)) { listAnswerError.Add(strANSWER); } listAnswerErrorIndex.Add((i + rowIndex).ToString()); } } if (mainTable.TYPE == SETestTypeEnum.多选题) { int answer = 0; if (strANSWER.Contains("A")) { answer = answer + 1; } if (strANSWER.Contains("B")) { answer = answer + 2; } if (strANSWER.Contains("C")) { answer = answer + 4; } if (strANSWER.Contains("D")) { answer = answer + 8; } if (strANSWER.Contains("E")) { answer = answer + 16; } mainTable.ANSWER = answer; } } else { if (listAnswerError.Contains(strANSWER)) { listAnswerError.Add(strANSWER); } listAnswerErrorIndex.Add((i + rowIndex).ToString()); } if (!string.IsNullOrEmpty(dtSource.Rows[i][8].ToString().Trim())) { int k = 0; if (dtSource.Rows[i][8].ToString().Contains("安全意识调查")) { k = k + 1; } if (dtSource.Rows[i][8].ToString().Contains("培训")) { k = k + 2; } if (dtSource.Rows[i][8].ToString().Contains("事故事件")) { k = k + 4; } mainTable.USAGE = k; } else { mainTable.USAGE = 2;//默认培训 } sub1sPer = new List(); sub2sPer = new List(); if (!string.IsNullOrEmpty(dtSource.Rows[i][9].ToString().Trim())) { var testPosts = dtSource.Rows[i][9].ToString().Trim().Split(';'); foreach (var postName in testPosts) { if (!string.IsNullOrEmpty(postName)) { modelPost = posts.FirstOrDefault(t => t.NAME == postName); if (modelPost == null) { if (!listPostNameEmpty.Contains(postName)) listPostNameEmpty.Add(postName); } else { T_SE_TEST_LIST_POST sub1 = new T_SE_TEST_LIST_POST { ID = Guid.NewGuid(), ORG_ID = orgId, TEST_ID = mainTable.ID, POST_ID = posts.FirstOrDefault(t => t.NAME == postName).ID }; sub1sPer.Add(sub1); } } } } if (!string.IsNullOrEmpty(dtSource.Rows[i][10].ToString().Trim())) { var testPoints = dtSource.Rows[i][10].ToString().Trim().Split(';'); foreach (var pointName in testPoints) { if (!string.IsNullOrEmpty(pointName)) { modelPoint = points.FirstOrDefault(t => t.NAME == pointName); if (modelPoint != null) { T_SE_TEST_LIST_POINT sub2 = new T_SE_TEST_LIST_POINT { ID = Guid.NewGuid(), ORG_ID = orgId, TEST_ID = mainTable.ID, POINT_ID = modelPoint.ID, }; sub2sPer.Add(sub2); } else { if (!listPointNameEmpty.Contains(pointName)) listPointNameEmpty.Add(pointName); } } } } if (sub1sPer != null && sub1sPer.Count > 0) sub1s.AddRange(sub1sPer); if (sub2sPer != null && sub2sPer.Count > 0) sub2s.AddRange(sub2sPer); if (!string.IsNullOrEmpty(dtSource.Rows[i][11].ToString().Trim())) { mainTable.TRAINNAME = dtSource.Rows[i][11].ToString().Trim(); } mainTables.Add((T_SE_TEST)mainTable.Clone()); if (sub2sPer != null && sub2sPer.Count > 0) mainTable.Nav_Points = sub2sPer;//知识点 if (sub1sPer != null && sub1sPer.Count > 0) mainTable.Nav_Posts = sub1sPer;//适用岗位 listImport.Add(new T_SE_COURSEWEARLIBRARY_DETAIL() { ID = Guid.NewGuid(), ORG_ID = orgId, EDU_CARD_ID = EDU_CARD_ID, NUM = i + 1, TEST_ID = mainTable.ID, Nav_Test = mainTable, CREATE_TIME = mainTable.CREATE_TIME }); } } #region 提示 string strEmptyError = string.Empty; if (listPostNameEmpty.Count > 0 || dicIndexEmpty.Count > 0 || listPointNameEmpty.Count > 0) { if (dicIndexEmpty.Count > 0) { int colIndex = 0; foreach (var item in dicIndexEmpty) { strEmptyError += "行:" + item.Key + ",列:"; colIndex = 0; foreach (var itemVal in item.Value) { strEmptyError += (colIndex > 0 ? "、" : "") + itemVal; colIndex++; } strEmptyError += ";"; } strEmptyError = strEmptyError.Substring(0, strEmptyError.Length - 1) + "不能为空!"; } if (listPostNameEmpty.Count > 0) { throw new Exception("岗位未找到:" + string.Join(",", listPostNameEmpty)); } if (listPointNameEmpty.Count > 0) { throw new Exception("知识点未找到:" + string.Join(",", listPointNameEmpty)); } if (listAnswerErrorIndex.Count > 0) { throw new Exception("答案有误,行:【" + string.Join(",", listAnswerErrorIndex) + "】值:" + string.Join(",", listAnswerError)); } } if (strEmptyError != string.Empty) throw new Exception(strEmptyError); #endregion UnifiedCommit(() => { if (mainTables != null && mainTables.Any()) BantchAddEntityNoCommit(mainTables); if (sub1s != null && sub1s.Any()) BantchAddEntityNoCommit(sub1s); if (sub2s != null && sub2s.Any()) BantchAddEntityNoCommit(sub2s); }); //Msg = "导入成功!"; return true; } } }