using AutoMapper; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using TTSW.EF; using TTSW.Utils; using TTSW.Constant; using TTSW.Common; using TodoAPI2.Models; using System.IO; using System.Web; using System.Net; using TTSW.Configure; using Microsoft.Extensions.Options; using System.Data; namespace TodoAPI2.Models { public class eva_adjust_postponementService : Ieva_adjust_postponementService { private IBaseRepository2 _repository; private IMyDatabase db; private Iexternal_linkageService ext; private Iexternal_employeeService emp; public eva_adjust_postponementService(IBaseRepository2 repository, IMyDatabase mydb, Iexternal_linkageService inext, Iexternal_employeeService inemp) { _repository = repository; db = mydb; ext = inext; emp = inemp; } #region Private Functions private eva_adjust_postponementEntity GetEntity(eva_adjust_postponementInputModel model) { return Mapper.Map(model); } private List GetEntityList(List models) { return Mapper.Map>(models); } private eva_adjust_postponementViewModel GetDto(eva_adjust_postponementEntity entity) { return Mapper.Map(entity); } private List GetDtoList(List entities) { return Mapper.Map>(entities); } #endregion #region Public Functions #region Query Functions public eva_adjust_postponementViewModel Get(int id) { var entity = _repository.Get(id); return GetDto(entity); } public eva_adjust_postponementWithSelectionViewModel GetWithSelection(int id) { var entity = _repository.Get(id); var i = Mapper.Map(entity); i.item_create_evaluation_id = (from x in _repository.Context.eva_create_evaluation select x).ToList(); var all_emp = emp.GetListByemployee_type(null, null); i.item_managed_by = all_emp.ToList(); return i; } public eva_adjust_postponementWithSelectionViewModel GetBlankItem() { var i = new eva_adjust_postponementWithSelectionViewModel(); i.item_create_evaluation_id = (from x in _repository.Context.eva_create_evaluation select x).ToList(); var all_emp = emp.GetListByemployee_type(null, null); i.item_managed_by = all_emp.ToList(); return i; } public List GetListByfiscal_year(int? fiscal_year) { var model = new eva_adjust_postponementSearchModel(); model.fiscal_year = fiscal_year; return GetListBySearch(model); } public List GetListBySearch(eva_adjust_postponementSearchModel model) { var all_emp = emp.GetListByemployee_type(null, null); var data = ( from m_eva_adjust_postponement in _repository.Context.eva_adjust_postponement join fk_eva_create_evaluation4 in _repository.Context.eva_create_evaluation on m_eva_adjust_postponement.create_evaluation_id equals fk_eva_create_evaluation4.id into eva_create_evaluationResult4 from fk_eva_create_evaluationResult4 in eva_create_evaluationResult4.DefaultIfEmpty() join fk_external_linkage11 in all_emp on m_eva_adjust_postponement.managed_by equals fk_external_linkage11.id into external_linkageResult11 from fk_external_linkageResult11 in external_linkageResult11.DefaultIfEmpty() where 1==1 //&& (m_eva_adjust_postponement.id == model.id || !model.id.HasValue) && (m_eva_adjust_postponement.fiscal_year == model.fiscal_year || !model.fiscal_year.HasValue) && (m_eva_adjust_postponement.theRound == model.theRound || !model.theRound.HasValue) orderby m_eva_adjust_postponement.created descending select new eva_adjust_postponementViewModel() { id = m_eva_adjust_postponement.id, fiscal_year = m_eva_adjust_postponement.fiscal_year, theDate = m_eva_adjust_postponement.theDate, theRound = m_eva_adjust_postponement.theRound, create_evaluation_id = m_eva_adjust_postponement.create_evaluation_id, limit = m_eva_adjust_postponement.limit, limit_frame = m_eva_adjust_postponement.limit_frame, limit_quota = m_eva_adjust_postponement.limit_quota, limit_frame_quota = m_eva_adjust_postponement.limit_frame_quota, percentage = m_eva_adjust_postponement.percentage, command_no = m_eva_adjust_postponement.command_no, managed_by = m_eva_adjust_postponement.managed_by, create_evaluation_id_eva_create_evaluation_performance_plan_id = fk_eva_create_evaluationResult4.performance_plan_id, managed_by_external_linkage_external_name = fk_external_linkageResult11.fullname, isActive = m_eva_adjust_postponement.isActive, Created = m_eva_adjust_postponement.created, Updated = m_eva_adjust_postponement.updated } ).Take(100).ToList(); return data; } #endregion #region Manipulation Functions public int GetNewPrimaryKey() { int? newkey = 0; var x = (from i in _repository.Context.eva_adjust_postponement orderby i.id descending select i).Take(1).ToList(); if(x.Count > 0) { newkey = x[0].id + 1; } return newkey.Value; } public eva_adjust_postponementViewModel Insert(eva_adjust_postponementInputModel model) { var entity = GetEntity(model); entity.id = GetNewPrimaryKey(); var inserted = _repository.Insert(entity); return Get(inserted.id); } public eva_adjust_postponementViewModel Update(int id, eva_adjust_postponementInputModel model) { var existingEntity = _repository.Get(id); if (existingEntity != null) { existingEntity.fiscal_year = model.fiscal_year; existingEntity.theDate = model.theDate; existingEntity.theRound = model.theRound; existingEntity.create_evaluation_id = model.create_evaluation_id; existingEntity.limit = model.limit; existingEntity.limit_frame = model.limit_frame; existingEntity.limit_quota = model.limit_quota; existingEntity.limit_frame_quota = model.limit_frame_quota; existingEntity.percentage = model.percentage; existingEntity.command_no = model.command_no; existingEntity.managed_by = model.managed_by; var updated = _repository.Update(id, existingEntity); return Get(updated.id); } else throw new NotificationException("No data to update"); } public string UpdateMultiple(List model) { foreach(var i in model) { if (i.active_mode == "1" && i.id.HasValue) // update { var existingEntity = _repository.Get(i.id.Value); if (existingEntity != null) { existingEntity.fiscal_year = i.fiscal_year; existingEntity.theDate = i.theDate; existingEntity.theRound = i.theRound; existingEntity.create_evaluation_id = i.create_evaluation_id; existingEntity.limit = i.limit; existingEntity.limit_frame = i.limit_frame; existingEntity.limit_quota = i.limit_quota; existingEntity.limit_frame_quota = i.limit_frame_quota; existingEntity.percentage = i.percentage; existingEntity.command_no = i.command_no; existingEntity.managed_by = i.managed_by; _repository.UpdateWithoutCommit(i.id.Value, existingEntity); } } else if (i.active_mode == "1" && !i.id.HasValue) // add { var entity = GetEntity(i); entity.id = GetNewPrimaryKey(); _repository.InsertWithoutCommit(entity); } else if (i.active_mode == "0" && i.id.HasValue) // remove { _repository.DeleteWithoutCommit(i.id.Value); } else if (i.active_mode == "0" && !i.id.HasValue) { // nothing to do } } _repository.Context.SaveChanges(); return model.Count().ToString(); } public eva_adjust_postponementViewModel SetAsActive(int id) { var updated = _repository.SetAsActive(id); return Get(updated.id); } public eva_adjust_postponementViewModel SetAsInactive(int id) { var updated = _repository.SetAsInActive(id); return Get(updated.id); } public void Delete(int id) { _repository.Delete(id); return; } #endregion #region Match Item #endregion #endregion } }