using APT.Infrastructure.Core;
using APT.BaseData.Domain.Entities.FM;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using APT.BaseData.Domain.IServices.FM;
using APT.Utility;
using APT.BaseData.Domain.Entities.BD;
using System.Linq;
using APT.BaseData.Domain.Enums;
using System;
using System.Linq.Expressions;
namespace APT.FM.WebApi.Controllers.Api.FM
{
///
/// 部门排班表
///
[Route("api/FM/DepartmentScheduling")]
public partial class DepartmentSchedulingController : AuthorizeApiController
{
///
/// 更新
///
///
///
[HttpPost, Route("FullUpdate")]
public JsonActionResult FullUpdate([FromBody] T_FM_DEPARTMENT_SCHEDULING entity)
{
return SafeExecute(() =>
{
//if (entity == null || !entity.Nav_DepartmentSchedulingDetail.Any(i => !i.IS_DELETED))
//{
// this.ThrowError("040004");
//}
var detail = entity.Nav_DepartmentSchedulingDetail;
var repeatPersonIds = detail.Where(t => t.IS_DELETED == false).GroupBy(x => x.PERSON_ID).Where(x => x.Count() > 1).Select(t => t.Key);
if (repeatPersonIds.Any())
{
BaseFilter baseFilter = new BaseFilter(entity.ORG_ID);
baseFilter.SelectField = new string[] { "NAME" };
var repeatPersonNames = GetEntities(t => repeatPersonIds.Contains(t.ID), baseFilter);
string repeatNames = string.Join(",", repeatPersonNames.Select(i => i.NAME));
throw new Exception(repeatNames + "重复,不允许重复添加人员!");
}
entity.Nav_DepartmentSchedulingDetail = null;
UnifiedCommit(() =>
{
this.UpdateEntityNoCommit(entity);
this.BantchSaveEntityNoCommit(detail);
});
return true;
});
}
///
/// 保存配置信息
///
///
///
[HttpPost, Route("ClearRun")]
public JsonActionResult ClearRun([FromBody] KeywordFilter filter)
{
return SafeExecute(() =>
{
var baseConfig = this.GetEntityByRedis(filter.GetOrgId().ToString(), filter.GetOrgId());
if (baseConfig == null)
this.ThrowError("010002");
List updateShiftData = new List();//删除上一个周期的几条
List updateShiftDataDetail = new List();//删除上一个周期的几条
//清除数据后重新生成
if (!string.IsNullOrWhiteSpace(filter.Keyword))
{
var deleteShiftPersonIds = new List();
var deleteShiftDataIds = new List();
var configIds = filter.Parameter1?.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(i => Guid.Parse(i));
Expression> express = i => i.DEPARTMENT_ID == new Guid(filter.Keyword)
&& i.ENABLE_STATUS == (int)FMEnableStatusEnum.启用 && i.END_TIME > DateTime.Now;
if (configIds.Any())
{
express = express.And(i => configIds.Contains(i.ID));
}
var departmentConfig = this.GetEntities(express, new BaseFilter(filter.GetOrgId()));
if (departmentConfig != null && departmentConfig.Any())
{
var configId = departmentConfig.FirstOrDefault().ID;
var cycle = departmentConfig.FirstOrDefault().CYCLE;
var currTime = departmentConfig.FirstOrDefault().CURR_TIME;
var departIds = departmentConfig.Select(t => t.DEPARTMENT_ID).Distinct().ToList();
deleteShiftDataIds = this.GetEntities(s => s.DATE_TIME.Date >= currTime && departIds.Contains(s.DEPARTMENT_ID), new BaseFilter(filter.OrgId)).Select(m => m.ID).ToList();
deleteShiftPersonIds = this.GetEntities(s => deleteShiftDataIds.Contains(s.DEPARTMENT_SCHEDULING_ID), new BaseFilter(filter.OrgId)).Select(m => m.ID).ToList();
//departmentConfig.ForEach(x => x.CURR_TIME = DateTime.Now.Date.AddDays(1));
//排序排班的最后几条记录
BasePageFilter pageFilter = new BasePageFilter(1, cycle);
pageFilter.OrgId = filter.OrgId;
pageFilter.Order = DbOrder.DESC;
pageFilter.Sort = "DATE_TIME";
var shiftDataTemp = this.GetEntities(s => s.DEPARTMENT_CALENDAR_CONFIG_ID == configId
&& s.DATE_TIME < currTime && s.DATE_TIME >= currTime.AddDays(-cycle) && departIds.Contains(s.DEPARTMENT_ID)&& s.IS_DELETED == false, new BaseFilter(filter.OrgId)).ToList();
updateShiftData.AddRange(shiftDataTemp);
updateShiftData.ForEach(x => x.IS_DELETED = true);
//var updateShiftIds = updateShiftData.Select(m => m.ID).ToList();
//updateShiftDataDetail = this.GetEntities(s => updateShiftIds.Contains(s.DEPARTMENT_SCHEDULING_ID) && s.IS_DELETED == false, new BaseFilter(filter.OrgId)).ToList();
//updateShiftDataDetail.ForEach(m => m.IS_DELETED = true);
}
this.UnifiedCommit(() =>
{
if (deleteShiftPersonIds != null && deleteShiftPersonIds.Any())
this.BantchDeleteEntityNoCommit(deleteShiftPersonIds);
if (deleteShiftDataIds != null && deleteShiftDataIds.Any())
this.BantchDeleteEntityNoCommit(deleteShiftDataIds);
//if (updateShiftDataDetail != null && updateShiftDataDetail.Any())
//{
// this.BantchUpdateEntity_noneBase(updateShiftDataDetail);
//}
if (updateShiftData != null && updateShiftData.Any())
{
this.BantchUpdateEntity_noneBase(updateShiftData);
}
// this.DeleteEntityNoCommit(i => i.Nav_DepartmentScheduling.DEPARTMENT_ID == new Guid(filter.Keyword)
//&& configIds.Contains(i.Nav_DepartmentScheduling.DEPARTMENT_CALENDAR_CONFIG_ID.Value) && i.Nav_DepartmentScheduling.DATE_TIME > DateTime.Now);
// this.DeleteEntityNoCommit(i => i.DEPARTMENT_ID == new Guid(filter.Keyword)
// && configIds.Contains(i.DEPARTMENT_CALENDAR_CONFIG_ID.Value) && i.DATE_TIME > DateTime.Now);
//if (departmentConfig != null && departmentConfig.Any())
//{
// this.BantchUpdateEntityNoCommit(departmentConfig);
//}
});
}
return true;
});
}
///
/// 更新
///
///
///
[HttpPost, Route("FullUpdateDetail")]
public JsonActionResult FullUpdateDetail([FromBody] T_FM_DEPARTMENT_SCHEDULING entity)
{
return SafeExecute(() =>
{
//if (entity == null || !entity.Nav_DepartmentSchedulingDetail.Any(i => !i.IS_DELETED))
//{
// this.ThrowError("040004");
//}
var detail = entity.Nav_DepartmentSchedulingDetail;
if (detail != null && detail.Any())
{
foreach (var item in detail)
{
item.Nav_DepartmentScheduling = null;
item.Nav_Person = null;
}
var repeatPersonIds = detail.Where(t => t.IS_DELETED == false).GroupBy(x => x.PERSON_ID).Where(x => x.Count() > 1).Select(t => t.Key);
if (repeatPersonIds.Any())
{
BaseFilter baseFilter = new BaseFilter(entity.ORG_ID);
baseFilter.SelectField = new string[] { "NAME" };
var repeatPersonNames = GetEntities(t => repeatPersonIds.Contains(t.ID), baseFilter);
string repeatNames = string.Join(",", repeatPersonNames.Select(i => i.NAME));
throw new Exception(repeatNames + "重复,不允许重复添加人员!");
}
}
entity.Nav_DepartmentSchedulingDetail = null;
UnifiedCommit(() =>
{
//this.UpdateEntityNoCommit(entity);
this.BantchSaveEntityNoCommit(detail);
});
return true;
});
}
}
}