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_evaluation_groupService : Ieva_evaluation_groupService { private IBaseRepository _repository; private IMyDatabase db; private Iexternal_linkageService ext; private Iexternal_employeeService emp; public eva_evaluation_groupService(IBaseRepository repository, IMyDatabase mydb, Iexternal_linkageService inext, Iexternal_employeeService inemp) { _repository = repository; db = mydb; ext = inext; emp = inemp; } #region Private Functions private eva_evaluation_groupEntity GetEntity(eva_evaluation_groupInputModel model) { return Mapper.Map(model); } private List GetEntityList(List models) { return Mapper.Map>(models); } private eva_evaluation_groupViewModel GetDto(eva_evaluation_groupEntity entity) { return Mapper.Map(entity); } private List GetDtoList(List entities) { return Mapper.Map>(entities); } private List GetListWithoutBase64Fields(IQueryable listQuery) { return listQuery.Select(c => new eva_evaluation_groupEntity() { code = c.code, thegroup = c.thegroup, updated = c.updated, id = c.id, isActive = c.isActive }).ToList(); } #endregion #region Public Functions #region Query Functions private List getMainDept() { var a = emp.GetDeptMapping().Where(x => !x.id2.HasValue); var data = new List(); foreach(var i in a) { var n = new external_linkageViewModel(); n.external_id = i.id; n.external_name = i.department_name; n.external_code = i.department_code; data.Add(n); } return data; } public eva_evaluation_groupViewModel Get(Guid id) { var entity = _repository.Get(id); return GetDto(entity); } public eva_evaluation_groupWithSelectionViewModel GetWithSelection(Guid id) { var entity = _repository.Get(id); var i = Mapper.Map(entity); i.item_main_dept_id = getMainDept(); return i; } public eva_evaluation_groupWithSelectionViewModel GetBlankItem() { var i = new eva_evaluation_groupWithSelectionViewModel(); i.item_main_dept_id = getMainDept(); return i; } public List GetListBycode(string code) { var model = new eva_evaluation_groupSearchModel(); model.code = code; return GetListBySearch(model); } public List GetListALLeva_evaluation_group() { var data = ( from m_eva_evaluation_group in _repository.Context.eva_evaluation_group join fk_external_linkage3 in getMainDept() on m_eva_evaluation_group.main_dept_id equals fk_external_linkage3.id into external_linkageResult3 from fk_external_linkageResult3 in external_linkageResult3.DefaultIfEmpty() orderby m_eva_evaluation_group.code select new eva_evaluation_groupViewModel() { id = m_eva_evaluation_group.id, code = m_eva_evaluation_group.code, thegroup = m_eva_evaluation_group.thegroup, main_dept_id = m_eva_evaluation_group.main_dept_id, percentage = m_eva_evaluation_group.percentage, main_dept_id_external_linkage_external_name = fk_external_linkageResult3.external_name, isActive = m_eva_evaluation_group.isActive, Created = m_eva_evaluation_group.created, Updated = m_eva_evaluation_group.updated } ).ToList(); return data; } public List GetListBySearch(eva_evaluation_groupSearchModel model) { var data = ( from m_eva_evaluation_group in _repository.Context.eva_evaluation_group join fk_external_linkage3 in getMainDept() on m_eva_evaluation_group.main_dept_id equals fk_external_linkage3.id into external_linkageResult3 from fk_external_linkageResult3 in external_linkageResult3.DefaultIfEmpty() where 1 == 1 && (m_eva_evaluation_group.code == model.code || string.IsNullOrEmpty(model.code)) orderby m_eva_evaluation_group.code select new eva_evaluation_groupViewModel() { id = m_eva_evaluation_group.id, code = m_eva_evaluation_group.code, thegroup = m_eva_evaluation_group.thegroup, main_dept_id = m_eva_evaluation_group.main_dept_id, percentage = m_eva_evaluation_group.percentage, main_dept_id_external_linkage_external_name = fk_external_linkageResult3.external_name, isActive = m_eva_evaluation_group.isActive, Created = m_eva_evaluation_group.created, Updated = m_eva_evaluation_group.updated } ).ToList(); return data; } #endregion #region Manipulation Functions public eva_evaluation_groupViewModel Insert(eva_evaluation_groupInputModel model) { var entity = GetEntity(model); entity.id = Guid.NewGuid(); var inserted = _repository.Insert(entity); return Get(inserted.id); } public eva_evaluation_groupViewModel Update(Guid id, eva_evaluation_groupInputModel model) { var existingEntity = _repository.Get(id); if (existingEntity != null) { existingEntity.code = model.code; existingEntity.thegroup = model.thegroup; existingEntity.main_dept_id = model.main_dept_id; existingEntity.percentage = model.percentage; 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.code = i.code; existingEntity.thegroup = i.thegroup; existingEntity.main_dept_id = i.main_dept_id; existingEntity.percentage = i.percentage; _repository.UpdateWithoutCommit(i.id.Value, existingEntity); } } else if (i.active_mode == "1" && !i.id.HasValue) // add { var entity = GetEntity(i); entity.id = Guid.NewGuid(); _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_evaluation_groupViewModel SetAsActive(Guid id) { var updated = _repository.SetAsActive(id); return Get(updated.id); } public eva_evaluation_groupViewModel SetAsInactive(Guid id) { var updated = _repository.SetAsInActive(id); return Get(updated.id); } public void Delete(Guid id) { _repository.Delete(id); return; } #endregion #region Match Item #endregion #endregion } }