Files
hrm_eva/Models/eva_create_evaluation/eva_create_evaluationService.cs

342 lines
14 KiB
C#

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_create_evaluationService : Ieva_create_evaluationService
{
private IBaseRepository2<eva_create_evaluationEntity, int> _repository;
private IMyDatabase db;
private Iexternal_linkageService ext;
private Iexternal_employeeService emp;
private Ieva_create_evaluation_detailService create_detail;
public eva_create_evaluationService(IBaseRepository2<eva_create_evaluationEntity, int> repository, IMyDatabase mydb,
Iexternal_linkageService inext, Iexternal_employeeService inemp,
Ieva_create_evaluation_detailService in_create_detail)
{
_repository = repository;
db = mydb;
ext = inext;
emp = inemp;
create_detail = in_create_detail;
}
#region Private Functions
private eva_create_evaluationEntity GetEntity(eva_create_evaluationInputModel model)
{
return Mapper.Map<eva_create_evaluationEntity>(model);
}
private List<eva_create_evaluationEntity> GetEntityList(List<eva_create_evaluationInputModel> models)
{
return Mapper.Map<List<eva_create_evaluationEntity>>(models);
}
private eva_create_evaluationViewModel GetDto(eva_create_evaluationEntity entity)
{
return Mapper.Map<eva_create_evaluationViewModel>(entity);
}
private List<eva_create_evaluationViewModel> GetDtoList(List<eva_create_evaluationEntity> entities)
{
return Mapper.Map<List<eva_create_evaluationViewModel>>(entities);
}
#endregion
#region Public Functions
#region Query Functions
public eva_create_evaluationViewModel Get(int id)
{
var entity = _repository.Get(id);
return GetDto(entity);
}
public eva_create_evaluationWithSelectionViewModel GetWithSelection(int id)
{
var all_emp = emp.GetAllEmployee();
var entity = _repository.Get(id);
var i = Mapper.Map<eva_create_evaluationWithSelectionViewModel>(entity);
i.item_performance_plan_id = (from x in _repository.Context.eva_performance_plan orderby x.fiscal_year descending, x.theTime descending select x).ToList();
i.item_employee_id = all_emp;
i.item_evaluation_group_id = (from x in _repository.Context.eva_evaluation_group select x).ToList();
i.item_supervisor1_id = all_emp;
i.item_supervisor2_id = all_emp;
i.item_supervisor3_id = all_emp;
return i;
}
public eva_create_evaluationWithSelectionViewModel GetBlankItem()
{
var all_emp = emp.GetAllEmployee();
var i = new eva_create_evaluationWithSelectionViewModel();
i.item_performance_plan_id = (from x in _repository.Context.eva_performance_plan orderby x.fiscal_year descending, x.theTime descending select x).ToList();
i.item_employee_id = all_emp;
i.item_evaluation_group_id = (from x in _repository.Context.eva_evaluation_group select x).ToList();
i.item_supervisor1_id = all_emp;
i.item_supervisor2_id = all_emp;
i.item_supervisor3_id = all_emp;
return i;
}
public List<eva_create_evaluationViewModel> GetListByperformance_plan_id(Guid? performance_plan_id)
{
var model = new eva_create_evaluationSearchModel();
model.performance_plan_id = performance_plan_id;
return GetListBySearch(model);
}
public List<eva_create_evaluationViewModel> GetListBySearch(eva_create_evaluationSearchModel model)
{
var all_emp = emp.GetListByemployee_type(null, null);
var data = (
from m_eva_create_evaluation in _repository.Context.eva_create_evaluation
join fk_eva_performance_plan1 in _repository.Context.eva_performance_plan on m_eva_create_evaluation.performance_plan_id equals fk_eva_performance_plan1.id
into eva_performance_planResult1
from fk_eva_performance_planResult1 in eva_performance_planResult1.DefaultIfEmpty()
join fk_external_linkage2 in all_emp on m_eva_create_evaluation.employee_id equals fk_external_linkage2.id
into external_linkageResult2
from fk_external_linkageResult2 in external_linkageResult2.DefaultIfEmpty()
join fk_eva_evaluation_group5 in _repository.Context.eva_evaluation_group on m_eva_create_evaluation.evaluation_group_id equals fk_eva_evaluation_group5.id
into eva_evaluation_groupResult5
from fk_eva_evaluation_groupResult5 in eva_evaluation_groupResult5.DefaultIfEmpty()
join fk_external_linkage6 in all_emp on m_eva_create_evaluation.supervisor1_id equals fk_external_linkage6.id
into external_linkageResult6
from fk_external_linkageResult6 in external_linkageResult6.DefaultIfEmpty()
join fk_external_linkage7 in all_emp on m_eva_create_evaluation.supervisor2_id equals fk_external_linkage7.id
into external_linkageResult7
from fk_external_linkageResult7 in external_linkageResult7.DefaultIfEmpty()
join fk_external_linkage8 in all_emp on m_eva_create_evaluation.supervisor3_id equals fk_external_linkage8.id
into external_linkageResult8
from fk_external_linkageResult8 in external_linkageResult8.DefaultIfEmpty()
where 1==1
&& (m_eva_create_evaluation.performance_plan_id == model.performance_plan_id || !model.performance_plan_id.HasValue)
&& (m_eva_create_evaluation.evaluation_group_id == model.evaluation_group_id || !model.evaluation_group_id.HasValue)
orderby fk_eva_performance_planResult1.fiscal_year descending, fk_eva_performance_planResult1.theTime descending, fk_eva_evaluation_groupResult5.code ascending
select new eva_create_evaluationViewModel()
{
id = m_eva_create_evaluation.id,
performance_plan_id = m_eva_create_evaluation.performance_plan_id,
employee_id = m_eva_create_evaluation.employee_id,
score1 = m_eva_create_evaluation.score1,
score2 = m_eva_create_evaluation.score2,
evaluation_group_id = m_eva_create_evaluation.evaluation_group_id,
supervisor1_id = m_eva_create_evaluation.supervisor1_id,
supervisor2_id = m_eva_create_evaluation.supervisor2_id,
supervisor3_id = m_eva_create_evaluation.supervisor3_id,
performance_plan_id_eva_performance_plan_fiscal_year = fk_eva_performance_planResult1.display_text,
employee_id_external_linkage_external_name = fk_external_linkageResult2.fullname,
evaluation_group_id_eva_evaluation_group_code = fk_eva_evaluation_groupResult5.code,
evaluation_group_id_eva_evaluation_group_name = fk_eva_evaluation_groupResult5.thegroup,
supervisor1_id_external_linkage_external_name = fk_external_linkageResult6.fullname,
supervisor2_id_external_linkage_external_name = fk_external_linkageResult7.fullname,
supervisor3_id_external_linkage_external_name = fk_external_linkageResult8.fullname,
isActive = m_eva_create_evaluation.isActive,
Created = m_eva_create_evaluation.created,
Updated = m_eva_create_evaluation.updated
}
).ToList();
return data;
}
#endregion
#region Manipulation Functions
public int GetNewPrimaryKey()
{
int? newkey = 0;
var x = (from i in _repository.Context.eva_create_evaluation
orderby i.id descending
select i).Take(1).ToList();
if(x.Count > 0)
{
newkey = x[0].id + 1;
}
return newkey.Value;
}
public eva_create_evaluationViewModel Insert(eva_create_evaluationInputModel model)
{
var entity = GetEntity(model);
entity.id = GetNewPrimaryKey();
var inserted = _repository.Insert(entity);
var selected_emp = (from i in _repository.Context.eva_evaluation_group_detail
where i.evaluation_group_id == model.evaluation_group_id
select i.employee_id.Value).ToList();
create_detail.AddMultiple(entity.id, selected_emp);
return Get(inserted.id);
}
public eva_create_evaluationViewModel Update(int id, eva_create_evaluationInputModel model)
{
var existingEntity = _repository.Get(id);
if (existingEntity != null)
{
existingEntity.performance_plan_id = model.performance_plan_id;
existingEntity.employee_id = model.employee_id;
existingEntity.score1 = model.score1;
existingEntity.score2 = model.score2;
existingEntity.evaluation_group_id = model.evaluation_group_id;
existingEntity.supervisor1_id = model.supervisor1_id;
existingEntity.supervisor2_id = model.supervisor2_id;
existingEntity.supervisor3_id = model.supervisor3_id;
var updated = _repository.Update(id, existingEntity);
return Get(updated.id);
}
else
throw new NotificationException("No data to update");
}
public string UpdateMultiple(List<eva_create_evaluationInputModel> 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.performance_plan_id = i.performance_plan_id;
existingEntity.employee_id = i.employee_id;
existingEntity.score1 = i.score1;
existingEntity.score2 = i.score2;
existingEntity.evaluation_group_id = i.evaluation_group_id;
existingEntity.supervisor1_id = i.supervisor1_id;
existingEntity.supervisor2_id = i.supervisor2_id;
existingEntity.supervisor3_id = i.supervisor3_id;
_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_create_evaluationViewModel SetAsActive(int id)
{
var updated = _repository.SetAsActive(id);
return Get(updated.id);
}
public eva_create_evaluationViewModel SetAsInactive(int id)
{
var updated = _repository.SetAsInActive(id);
return Get(updated.id);
}
public void Delete(int id)
{
var p = from i in _repository.Context.eva_adjust_postponement
where i.create_evaluation_id == id
select i;
foreach(var j in p)
{
var p2 = from i in _repository.Context.eva_adjust_postponement_detail
where i.adjust_postponement_id == j.id
select i;
_repository.Context.eva_adjust_postponement_detail.RemoveRange(p2);
}
_repository.Context.eva_adjust_postponement.RemoveRange(p);
var p3 = from i in _repository.Context.eva_create_evaluation_detail
where i.create_evaluation_id == id
select i;
foreach(var k in p3)
{
var p4 = from i in _repository.Context.eva_evaluation_behavior
where i.create_evaluation_detail_id == k.id
select i;
_repository.Context.eva_evaluation_behavior.RemoveRange(p4);
var p5 = from i in _repository.Context.eva_evaluation_achievement
where i.create_evaluation_detail_id == k.id
select i;
_repository.Context.eva_evaluation_achievement.RemoveRange(p5);
var p6 = from i in _repository.Context.eva_evaluation_operating_agreement
where i.create_evaluation_detail_id == k.id
select i;
_repository.Context.eva_evaluation_operating_agreement.RemoveRange(p6);
}
_repository.Context.eva_create_evaluation_detail.RemoveRange(p3);
foreach (var j in p)
{
var p2 = from i in _repository.Context.eva_adjust_postponement_detail
where i.adjust_postponement_id == j.id
select i;
_repository.Context.eva_adjust_postponement_detail.RemoveRange(p2);
}
_repository.Delete(id);
return;
}
#endregion
#region Match Item
#endregion
#endregion
}
}