public string SaveListTraineeScore(List<Tra_TraineeScoreImport> lstTraineeScore, out List<Tra_TraineeScoreImport> DataReviewOutput) { using (var context = new VnrHrmDataContext()) { var unitOfWork = (IUnitOfWork)(new UnitOfWork(context)); var repoTra_Class = new CustomBaseRepository<Tra_Class>(unitOfWork); var repoTra_Trainee = new CustomBaseRepository<Tra_Trainee>(unitOfWork); var repoTra_TraineeTopic = new CustomBaseRepository<Tra_TraineeTopic>(unitOfWork); var repoTra_TraineeScore = new CustomBaseRepository<Tra_TraineeScore>(unitOfWork); var repoTra_ScoreType = new CustomBaseRepository<Tra_ScoreType>(unitOfWork); var traineeScoreServices = new Tra_TraineeScoreServices(); string message = string.Empty; List<Tra_TraineeScoreEntity> entityNew = new List<Tra_TraineeScoreEntity>(); List<Tra_TraineeScore> entityUpdate = new List<Tra_TraineeScore>(); List<Tra_TraineeScore> resultNew = new List<Tra_TraineeScore>(); List<Tra_TraineeScore> resultUpdate= new List<Tra_TraineeScore>(); DataReviewOutput = new List<Tra_TraineeScoreImport>(); List<string> ClassCode = lstTraineeScore.Select(m => m.ClassCode).Distinct().ToList(); //Lấy DS ClassID List<Guid> ClassID = repoTra_Class.GetAll().Where(m => m.IsDelete == null && ClassCode.Contains(m.Code)).Select(m => m.ID).ToList<Guid>(); //Lấy DS TraineeID List<Guid> TraineeID = repoTra_Trainee.GetAll().Where(m => m.IsDelete == null && ClassID.Contains(m.ClassID)).Select(m => m.ID).ToList<Guid>(); //Lấy DS TraineetopicID List<Guid> lstTraineeTopicID = repoTra_TraineeTopic.GetAll().Where(m => m.IsDelete == null && TraineeID.Contains(m.TraineeID ?? Guid.Empty)).Select(m => m.ID).ToList<Guid>(); //Lấy DS TraineeTopic var TraineeTopicAll = repoTra_TraineeTopic.GetAll().Where(m => m.IsDelete == null && lstTraineeTopicID.Contains(m.ID)) .Select(m => new { m.ID, TopicCode = m.Cat_Topic.Code, CodeEmp = m.Tra_Trainee.Hre_Profile.CodeEmp, ClassCode = m.Tra_Trainee.Tra_Class.Code }); //Lấy DS TraineeScore List<Tra_TraineeScore> LstTraineeScoreAll = repoTra_TraineeScore.GetAll().Where(m => m.IsDelete == null && lstTraineeTopicID.Contains(m.TraineeTopicID ?? Guid.Empty)).ToList<Tra_TraineeScore>(); List<string> ScoreTypeCode = lstTraineeScore.Select(m => m.TypeScore).Distinct().ToList<string>(); List<Tra_ScoreType> lstTypeScoreAll = repoTra_ScoreType.GetAll().Where(m => m.IsDelete == null && ScoreTypeCode.Contains(m.Code)).ToList<Tra_ScoreType>(); var stt = 1; foreach (var TraineeScore in lstTraineeScore) { var TraineeTopic = TraineeTopicAll.Where(m => m.CodeEmp == TraineeScore.CodeEmp && m.TopicCode == TraineeScore.TopicCode).FirstOrDefault(); Tra_ScoreType ScoreType = lstTypeScoreAll.Where(m => m.Code == TraineeScore.TypeScore).FirstOrDefault(); if (TraineeTopic == null) //Loi import { TraineeScore.Stt = stt++; TraineeScore.TypeError = "Không Có Môn Học Của Học Viên"; DataReviewOutput.Add(TraineeScore); } else if (ScoreType == null) { TraineeScore.Stt = stt++; TraineeScore.TypeError = "Không Có Loại Điểm Của Môn Học"; DataReviewOutput.Add(TraineeScore); } else { TraineeScore.Stt = stt++; DataReviewOutput.Add(TraineeScore); //Kiem tra diem da co trong DB chua //neu co roi thi update neu chua thi tao moi Guid TraineeTopicID = TraineeTopic.ID; Tra_TraineeScore TraineeScore_Current = LstTraineeScoreAll.Where(m => m.TraineeTopicID == TraineeTopicID && m.ScoreTypeID == ScoreType.ID).FirstOrDefault(); bool isNew = false; if (TraineeScore_Current == null) { isNew = true; TraineeScore_Current = new Tra_TraineeScore(); TraineeScore_Current.ID = Guid.NewGuid(); } TraineeScore_Current.TraineeTopicID = TraineeTopicID; TraineeScore_Current.ScoreTypeID = ScoreType.ID; TraineeScore_Current.Score = TraineeScore.Score; if (isNew) { resultNew.Add(TraineeScore_Current); } else { var entityEdit = new Tra_TraineeScoreEntity { ID = TraineeScore_Current.ID, TraineeTopicID = TraineeScore_Current.TraineeTopicID, ScoreTypeID = ScoreType.ID, Score = TraineeScore.Score }; resultUpdate.Add(TraineeScore_Current); message = traineeScoreServices.Edit(entityEdit); } } } entityNew = resultNew.Translate<Tra_TraineeScoreEntity>(); entityUpdate = resultUpdate.Translate<Tra_TraineeScore>(); if (entityNew.Count > 0) { message += traineeScoreServices.Add(entityNew); } return message; } }
public ActionResult UpdateScoreInTraineeTopic(Dictionary<string, object> model) { string status = string.Empty; string message = string.Empty; var DictraineeID = model.ElementAt(0); string[] traineeID = (string[])DictraineeID.Value; // var entity = new Tra_TraineeTopicEntity(); var lstEntity = new List<Tra_TraineeTopicEntity>(); var topicServices = new ActionService(UserLogin); var objTopic = new List<object>(); objTopic.Add(null); objTopic.Add(null); objTopic.Add(null); objTopic.Add(null); objTopic.Add(1); objTopic.Add(int.MaxValue - 1); var lstTopic = topicServices.GetData<Cat_TopicEntity>(objTopic, ConstantSql.hrm_cat_sp_get_Topic, ref status).ToList(); var traineeScoreServices = new Tra_TraineeScoreServices(); var objTraineeScore = new List<object>(); objTraineeScore.Add(1); objTraineeScore.Add(int.MaxValue - 1); var lstTraineeScore = topicServices.GetData<Tra_TraineeScoreEntity>(objTraineeScore, ConstantSql.hrm_tra_sp_get_TraineeScore, ref status).ToList(); var scoreTopicServices = new ActionService(UserLogin); var objScoreTopic = new List<object>(); objScoreTopic.Add(1); objScoreTopic.Add(int.MaxValue - 1); var lstScoreTopic = scoreTopicServices.GetData<Tra_ScoreTopicEntity>(objScoreTopic, ConstantSql.hrm_tra_sp_get_ScoreTopic, ref status).ToList(); var traineeTopicServices = new Tra_TraineeTopicServices(); var objTraineeTopic = new List<object>(); objTraineeTopic.Add(1); objTraineeTopic.Add(int.MaxValue - 1); var lstTraineeTopic = topicServices.GetData<Tra_TraineeTopicEntity>(objTraineeTopic, ConstantSql.hrm_tra_sp_get_TraineeTopic, ref status).ToList(); if (model.Count > 0) { for (int i = 3; i < model.Count; i++) { var entity = new Tra_TraineeTopicEntity(); var item = model.ElementAt(i); var strTitle = item.Key.Split('_'); string[] score = (string[])item.Value; var topicEnity = lstTopic.Where(s => item.Key.Contains(s.Code)).FirstOrDefault(); if (topicEnity != null) { var scoreTopicEntity = lstScoreTopic.Where(s => topicEnity.ID == s.TopicID && strTitle[1] == s.Code).FirstOrDefault(); var traineeTopicEntity = lstTraineeTopic.Where(s => s.TraineeID != null && topicEnity.ID == s.TopicID && Guid.Parse(traineeID[0]) == s.TraineeID.Value).FirstOrDefault(); if (traineeTopicEntity != null) { //Edit cột score trong bảng traineeTopic if (!string.IsNullOrEmpty(score[0])) { traineeTopicEntity.Score = double.Parse(score[0]); } else { traineeTopicEntity.Score = null; } message = traineeTopicServices.Edit(traineeTopicEntity); //Kiểm tra trong bảng TraineeScoree. Nếu chưa có record thì tạo mới nguoc lai thì edit record var traineeScoreEntity = lstTraineeScore.Where(s => s.TraineeTopicID == traineeTopicEntity.ID && scoreTopicEntity.ScoreTypeID == s.ScoreTypeID).FirstOrDefault(); if (traineeScoreEntity == null) { Tra_TraineeScoreEntity newTraineeScoreEntity = new Tra_TraineeScoreEntity(); newTraineeScoreEntity.ScoreTypeID = scoreTopicEntity.ScoreTypeID; newTraineeScoreEntity.TraineeTopicID = traineeTopicEntity.ID; newTraineeScoreEntity.Score = traineeTopicEntity.Score; message = traineeScoreServices.Add(newTraineeScoreEntity); } else { traineeScoreEntity.Score = traineeTopicEntity.Score; message = traineeScoreServices.Edit(traineeScoreEntity); } // lstEntity.Add(traineeTopicEntity); } } } //message = traineeTopicServices.Edit(lstEntity); } return null; }