417 lines
17 KiB
C#
417 lines
17 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_evaluation_achievementService : Ieva_evaluation_achievementService
|
|
{
|
|
private IBaseRepository2<eva_evaluation_achievementEntity, int> _repository;
|
|
private IMyDatabase db;
|
|
private Iexternal_linkageService ext;
|
|
|
|
public eva_evaluation_achievementService(IBaseRepository2<eva_evaluation_achievementEntity, int> repository,
|
|
IMyDatabase mydb, Iexternal_linkageService inext)
|
|
{
|
|
_repository = repository;
|
|
db = mydb;
|
|
ext = inext;
|
|
}
|
|
|
|
#region Private Functions
|
|
private eva_evaluation_achievementEntity GetEntity(eva_evaluation_achievementInputModel model)
|
|
{
|
|
return Mapper.Map<eva_evaluation_achievementEntity>(model);
|
|
}
|
|
private List<eva_evaluation_achievementEntity> GetEntityList(List<eva_evaluation_achievementInputModel> models)
|
|
{
|
|
return Mapper.Map<List<eva_evaluation_achievementEntity>>(models);
|
|
}
|
|
private eva_evaluation_achievementViewModel GetDto(eva_evaluation_achievementEntity entity)
|
|
{
|
|
return Mapper.Map<eva_evaluation_achievementViewModel>(entity);
|
|
}
|
|
private List<eva_evaluation_achievementViewModel> GetDtoList(List<eva_evaluation_achievementEntity> entities)
|
|
{
|
|
return Mapper.Map<List<eva_evaluation_achievementViewModel>>(entities);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Public Functions
|
|
#region Query Functions
|
|
|
|
public eva_evaluation_achievementViewModel Get(int id)
|
|
{
|
|
var entity = _repository.Get(id);
|
|
|
|
return GetDto(entity);
|
|
}
|
|
public eva_evaluation_achievementWithSelectionViewModel GetWithSelection(int id)
|
|
{
|
|
var entity = _repository.Get(id);
|
|
var i = Mapper.Map<eva_evaluation_achievementWithSelectionViewModel>(entity);
|
|
|
|
|
|
return i;
|
|
}
|
|
public eva_evaluation_achievementWithSelectionViewModel GetBlankItem()
|
|
{
|
|
var i = new eva_evaluation_achievementWithSelectionViewModel();
|
|
|
|
|
|
return i;
|
|
}
|
|
|
|
public List<eva_evaluation_achievementViewModel> GetListBycreate_evaluation_detail_id(int? create_evaluation_detail_id)
|
|
{
|
|
var model = new eva_evaluation_achievementSearchModel();
|
|
model.create_evaluation_detail_id = create_evaluation_detail_id;
|
|
return GetListBySearch(model);
|
|
}
|
|
|
|
public List<eva_evaluation_achievementViewModel> GetListBySearch(eva_evaluation_achievementSearchModel model)
|
|
{
|
|
var data = (
|
|
from m_eva_evaluation_achievement in _repository.Context.eva_evaluation_achievement
|
|
|
|
join fk_eva_create_evaluation_detail1 in _repository.Context.eva_create_evaluation_detail on m_eva_evaluation_achievement.create_evaluation_detail_id equals fk_eva_create_evaluation_detail1.id
|
|
into eva_create_evaluation_detailResult1
|
|
from fk_eva_create_evaluation_detailResult1 in eva_create_evaluation_detailResult1.DefaultIfEmpty()
|
|
|
|
|
|
where 1 == 1
|
|
&& (m_eva_evaluation_achievement.create_evaluation_detail_id == model.create_evaluation_detail_id || !model.create_evaluation_detail_id.HasValue)
|
|
|
|
|
|
orderby m_eva_evaluation_achievement.created
|
|
select new eva_evaluation_achievementViewModel()
|
|
{
|
|
id = m_eva_evaluation_achievement.id,
|
|
create_evaluation_detail_id = m_eva_evaluation_achievement.create_evaluation_detail_id,
|
|
achievement = m_eva_evaluation_achievement.achievement,
|
|
weight = m_eva_evaluation_achievement.weight,
|
|
thefile = m_eva_evaluation_achievement.thefile,
|
|
thefileDisplay = m_eva_evaluation_achievement.thefileDisplay,
|
|
|
|
create_evaluation_detail_id_eva_create_evaluation_detail_create_evaluation_id = fk_eva_create_evaluation_detailResult1.create_evaluation_id,
|
|
|
|
target_score1 = m_eva_evaluation_achievement.target_score1,
|
|
target_score2 = m_eva_evaluation_achievement.target_score2,
|
|
target_score3 = m_eva_evaluation_achievement.target_score3,
|
|
target_score4 = m_eva_evaluation_achievement.target_score4,
|
|
target_score5 = m_eva_evaluation_achievement.target_score5,
|
|
|
|
isActive = m_eva_evaluation_achievement.isActive,
|
|
Created = m_eva_evaluation_achievement.created,
|
|
Updated = m_eva_evaluation_achievement.updated
|
|
}
|
|
).ToList();
|
|
|
|
return data;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Manipulation Functions
|
|
|
|
public int GetNewPrimaryKey()
|
|
{
|
|
int? newkey = 0;
|
|
|
|
var x = (from i in _repository.Context.eva_evaluation_achievement
|
|
orderby i.id descending
|
|
select i).Take(1).ToList();
|
|
|
|
if (x.Count > 0)
|
|
{
|
|
newkey = x[0].id + 1;
|
|
}
|
|
|
|
return newkey.Value;
|
|
}
|
|
|
|
private void ManageDetail(eva_evaluation_achievementEntity entity)
|
|
{
|
|
var oldDetail = from x in _repository.Context.eva_evaluation_achievement_detail
|
|
where x.achievement_id == entity.id
|
|
select x;
|
|
_repository.Context.eva_evaluation_achievement_detail.RemoveRange(oldDetail);
|
|
|
|
if (!string.IsNullOrEmpty(entity.achievement))
|
|
{
|
|
var sp = entity.achievement.Split("^");
|
|
int i = 0;
|
|
foreach (var s in sp)
|
|
{
|
|
if (i == 0)
|
|
{
|
|
entity.achievement_line1 = s.Trim();
|
|
}
|
|
else
|
|
{
|
|
var n = new eva_evaluation_achievement_detailEntity();
|
|
n.id = Guid.NewGuid();
|
|
n.achievement_order = i;
|
|
n.achievement_id = entity.id;
|
|
n.achievement_detail = s.Trim();
|
|
_repository.Context.Add(n);
|
|
}
|
|
i++;
|
|
}
|
|
_repository.Context.SaveChanges();
|
|
}
|
|
}
|
|
|
|
public eva_evaluation_achievementViewModel Insert(eva_evaluation_achievementInputModel model)
|
|
{
|
|
var entity = GetEntity(model);
|
|
entity.id = GetNewPrimaryKey();
|
|
|
|
var current_sum = (from i in _repository.Context.eva_evaluation_achievement
|
|
where i.create_evaluation_detail_id == model.create_evaluation_detail_id
|
|
select i.weight).Sum();
|
|
|
|
if (current_sum + model.weight > 100)
|
|
{
|
|
throw new Exception("ไม่สามารถบันทึกค่าน้ำหนักในส่วนผลสัมฤทธิ์ของงาน ได้เกิน 100");
|
|
}
|
|
|
|
var current_detail = (from i in _repository.Context.eva_create_evaluation_detail
|
|
where i.id == model.create_evaluation_detail_id
|
|
select i).FirstOrDefault();
|
|
|
|
if (current_detail.status_self == "Y")
|
|
{
|
|
throw new Exception("คุณนำส่งข้อตกลงการประเมินไปแล้ว ไม่สามารถบันทึกได้");
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(model.thefile))
|
|
{
|
|
//Move file from temp to physical
|
|
string thefileFileName = FileUtil.MoveTempUploadFileToActualPath(
|
|
model.thefile, FilePathConstant.DirType.FilesTestUpload, entity.id);
|
|
entity.thefile = thefileFileName;
|
|
}
|
|
|
|
var inserted = _repository.Insert(entity);
|
|
ManageDetail(inserted);
|
|
|
|
return Get(inserted.id);
|
|
}
|
|
|
|
public eva_evaluation_achievementViewModel Update(int id, eva_evaluation_achievementInputModel model)
|
|
{
|
|
var current_sum = (from i in _repository.Context.eva_evaluation_achievement
|
|
where i.create_evaluation_detail_id == model.create_evaluation_detail_id
|
|
select i.weight).Sum();
|
|
|
|
var current_item_weight = (from i in _repository.Context.eva_evaluation_achievement
|
|
where i.id == id
|
|
select i.weight).Sum();
|
|
|
|
if (current_sum - current_item_weight + model.weight > 100)
|
|
{
|
|
throw new Exception("ไม่สามารถบันทึกค่าน้ำหนักในส่วนผลสัมฤทธิ์ของงาน ได้เกิน 100");
|
|
}
|
|
|
|
var current_detail = (from i in _repository.Context.eva_create_evaluation_detail
|
|
where i.id == model.create_evaluation_detail_id
|
|
select i).FirstOrDefault();
|
|
|
|
if (current_detail.status_self == "Y")
|
|
{
|
|
throw new Exception("คุณนำส่งข้อตกลงการประเมินไปแล้ว ไม่สามารถบันทึกได้");
|
|
}
|
|
|
|
var existingEntity = _repository.Get(id);
|
|
if (existingEntity != null)
|
|
{
|
|
existingEntity.create_evaluation_detail_id = model.create_evaluation_detail_id;
|
|
existingEntity.achievement = model.achievement;
|
|
existingEntity.weight = model.weight;
|
|
if (!string.IsNullOrEmpty(model.thefile))
|
|
{
|
|
if (model.thefile.StartsWith("Uploads"))
|
|
{
|
|
var thefileFileName = FileUtil.MoveTempUploadFileToActualPath(
|
|
model.thefile, FilePathConstant.DirType.FilesTestUpload, existingEntity.id, existingEntity.thefile);
|
|
existingEntity.thefile = thefileFileName;
|
|
}
|
|
else
|
|
{
|
|
existingEntity.thefile = model.thefile;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
existingEntity.thefile = null;
|
|
}
|
|
existingEntity.target_score1 = model.target_score1;
|
|
existingEntity.target_score2 = model.target_score2;
|
|
existingEntity.target_score3 = model.target_score3;
|
|
existingEntity.target_score4 = model.target_score4;
|
|
existingEntity.target_score5 = model.target_score5;
|
|
|
|
|
|
var updated = _repository.Update(id, existingEntity);
|
|
ManageDetail(updated);
|
|
return Get(updated.id);
|
|
}
|
|
else
|
|
throw new NotificationException("No data to update");
|
|
}
|
|
|
|
public void reset_achievement(int id)
|
|
{
|
|
var current_detail = (from i in _repository.Context.eva_create_evaluation_detail
|
|
where i.id == id
|
|
select i).FirstOrDefault();
|
|
|
|
if(current_detail == null) throw new NotificationException("No data to update");
|
|
|
|
if (current_detail.status_self == "Y")
|
|
{
|
|
throw new Exception("คุณนำส่งข้อตกลงการประเมินไปแล้ว ไม่สามารถบันทึกได้");
|
|
}
|
|
|
|
var current_achievement = (from i in _repository.Context.eva_evaluation_achievement
|
|
where i.create_evaluation_detail_id == id
|
|
select i);
|
|
_repository.Context.RemoveRange(current_achievement);
|
|
|
|
var opt = (from i in _repository.Context.eva_evaluation_operating_agreement
|
|
where i.create_evaluation_detail_id == id
|
|
orderby i.mission_no
|
|
select i).ToList();
|
|
|
|
var newid = GetNewPrimaryKey();
|
|
var enter = "\n";
|
|
var start_id = newid;
|
|
|
|
foreach (var i in opt)
|
|
{
|
|
var n = new eva_evaluation_achievementEntity();
|
|
n.id = newid;
|
|
n.create_evaluation_detail_id = id;
|
|
n.achievement = "รายละเอียดภารกิจ" + enter + i.mission_detail + enter + enter + "เป้าหมาย" + enter + i.target + enter + enter + "ตัวชี้วัด" + enter + i.indicators;
|
|
n.weight = 0;
|
|
n.created = DateTime.Now.AddMinutes(newid - start_id).AddMinutes(opt.Count * -1);
|
|
newid++;
|
|
_repository.Context.Add(n);
|
|
}
|
|
|
|
_repository.Context.SaveChanges();
|
|
}
|
|
|
|
public string UpdateMultiple(List<eva_evaluation_achievementInputModel> 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)
|
|
{
|
|
var current_detail = (from k in _repository.Context.eva_create_evaluation_detail
|
|
where k.id == existingEntity.create_evaluation_detail_id
|
|
select k).FirstOrDefault();
|
|
|
|
if (current_detail.status_self == "Y")
|
|
{
|
|
throw new Exception("ผู้รับการประเมิน ส่งแบบประเมินไปแล้ว บันทึกไม่ได้");
|
|
}
|
|
|
|
existingEntity.create_evaluation_detail_id = i.create_evaluation_detail_id;
|
|
existingEntity.achievement = i.achievement;
|
|
existingEntity.weight = i.weight;
|
|
if (!string.IsNullOrEmpty(i.thefile))
|
|
{
|
|
if (i.thefile.StartsWith("Uploads"))
|
|
{
|
|
var thefileFileName = FileUtil.MoveTempUploadFileToActualPath(
|
|
i.thefile, FilePathConstant.DirType.FilesTestUpload, existingEntity.id, existingEntity.thefile);
|
|
existingEntity.thefile = thefileFileName;
|
|
}
|
|
else
|
|
{
|
|
existingEntity.thefile = i.thefile;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
existingEntity.thefile = null;
|
|
}
|
|
existingEntity.target_score1 = i.target_score1;
|
|
existingEntity.target_score2 = i.target_score2;
|
|
existingEntity.target_score3 = i.target_score3;
|
|
existingEntity.target_score4 = i.target_score4;
|
|
existingEntity.target_score5 = i.target_score5;
|
|
|
|
|
|
_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_evaluation_achievementViewModel SetAsActive(int id)
|
|
{
|
|
var updated = _repository.SetAsActive(id);
|
|
|
|
return Get(updated.id);
|
|
}
|
|
public eva_evaluation_achievementViewModel 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
|
|
}
|
|
}
|
|
|
|
|