Files
hrm_eva/Models/eva_idp_plan/eva_idp_planService.cs

232 lines
7.1 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_idp_planService : Ieva_idp_planService
{
private IBaseRepository2<eva_idp_planEntity, int> _repository;
private IMyDatabase db;
private Iexternal_linkageService ext;
public eva_idp_planService(IBaseRepository2<eva_idp_planEntity, int> repository, IMyDatabase mydb, Iexternal_linkageService inext)
{
_repository = repository;
db = mydb;
ext = inext;
}
#region Private Functions
private eva_idp_planEntity GetEntity(eva_idp_planInputModel model)
{
return Mapper.Map<eva_idp_planEntity>(model);
}
private List<eva_idp_planEntity> GetEntityList(List<eva_idp_planInputModel> models)
{
return Mapper.Map<List<eva_idp_planEntity>>(models);
}
private eva_idp_planViewModel GetDto(eva_idp_planEntity entity)
{
return Mapper.Map<eva_idp_planViewModel>(entity);
}
private List<eva_idp_planViewModel> GetDtoList(List<eva_idp_planEntity> entities)
{
return Mapper.Map<List<eva_idp_planViewModel>>(entities);
}
#endregion
#region Public Functions
#region Query Functions
public eva_idp_planViewModel Get(int id)
{
var entity = _repository.Get(id);
return GetDto(entity);
}
public eva_idp_planWithSelectionViewModel GetWithSelection(int id)
{
var entity = _repository.Get(id);
var i = Mapper.Map<eva_idp_planWithSelectionViewModel>(entity);
return i;
}
public eva_idp_planWithSelectionViewModel GetBlankItem()
{
var i = new eva_idp_planWithSelectionViewModel();
return i;
}
public List<eva_idp_planViewModel> GetListBycreate_evaluation_detail_id(int? create_evaluation_detail_id)
{
var model = new eva_idp_planSearchModel();
model.create_evaluation_detail_id = create_evaluation_detail_id;
return GetListBySearch(model);
}
public List<eva_idp_planViewModel> GetListBySearch(eva_idp_planSearchModel model)
{
var data = (
from m_eva_idp_plan in _repository.Context.eva_idp_plan
where 1==1
//&& (m_eva_idp_plan.id == model.id || !model.id.HasValue)
&& (m_eva_idp_plan.create_evaluation_detail_id == model.create_evaluation_detail_id || !model.create_evaluation_detail_id.HasValue)
orderby m_eva_idp_plan.created descending
select new eva_idp_planViewModel()
{
id = m_eva_idp_plan.id,
create_evaluation_detail_id = m_eva_idp_plan.create_evaluation_detail_id,
develop = m_eva_idp_plan.develop,
development_method = m_eva_idp_plan.development_method,
start_date = m_eva_idp_plan.start_date,
end_date = m_eva_idp_plan.end_date,
isActive = m_eva_idp_plan.isActive,
Created = m_eva_idp_plan.created,
Updated = m_eva_idp_plan.updated
}
).Take(100).ToList();
return data;
}
#endregion
#region Manipulation Functions
public int GetNewPrimaryKey()
{
int? newkey = 0;
var x = (from i in _repository.Context.eva_idp_plan
orderby i.id descending
select i).Take(1).ToList();
if(x.Count > 0)
{
newkey = x[0].id + 1;
}
return newkey.Value;
}
public eva_idp_planViewModel Insert(eva_idp_planInputModel model)
{
var entity = GetEntity(model);
entity.id = GetNewPrimaryKey();
var inserted = _repository.Insert(entity);
return Get(inserted.id);
}
public eva_idp_planViewModel Update(int id, eva_idp_planInputModel model)
{
var existingEntity = _repository.Get(id);
if (existingEntity != null)
{
existingEntity.create_evaluation_detail_id = model.create_evaluation_detail_id;
existingEntity.develop = model.develop;
existingEntity.development_method = model.development_method;
existingEntity.start_date = model.start_date;
existingEntity.end_date = model.end_date;
var updated = _repository.Update(id, existingEntity);
return Get(updated.id);
}
else
throw new NotificationException("No data to update");
}
public string UpdateMultiple(List<eva_idp_planInputModel> 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.create_evaluation_detail_id = i.create_evaluation_detail_id;
existingEntity.develop = i.develop;
existingEntity.development_method = i.development_method;
existingEntity.start_date = i.start_date;
existingEntity.end_date = i.end_date;
_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_idp_planViewModel SetAsActive(int id)
{
var updated = _repository.SetAsActive(id);
return Get(updated.id);
}
public eva_idp_planViewModel 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
}
}