using NCC.Common.Enum;
using NCC.Common.Extension;
using NCC.Common.Filter;
using NCC.Common.Util;
using NCC.Dependency;
using NCC.DynamicApiController;
using NCC.Extend.Entitys.common_extend;
using NCC.Extend.Entitys.Dto.ProjectGantt;
using NCC.FriendlyException;
using NCC.System.Interfaces.Permission;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace NCC.Extend
{
///
/// 项目计划
/// 版 本:V1.20.15
/// 版 权:Wesley(https://www.NCCsoft.com)
/// 作 者:NCC开发平台组
/// 日 期:2022-03-16
///
[ApiDescriptionSettings(Tag = "Extend", Name = "ProjectGantt", Order = 600)]
[Route("api/extend/[controller]")]
public class ProjectGanttService : IDynamicApiController, ITransient
{
private readonly ISqlSugarRepository _projectGanttRepository;
private readonly IUsersService _usersService;
///
///
///
///
///
public ProjectGanttService(ISqlSugarRepository projectGanttRepository, IUsersService usersService)
{
_projectGanttRepository = projectGanttRepository;
_usersService = usersService;
}
#region GET
///
/// 项目列表
///
/// 请求参数
///
[HttpGet("")]
public async Task GetList([FromQuery] KeywordInput input)
{
var data = await _projectGanttRepository.Entities.Where(x => x.Type == 1 && x.DeleteMark == null).WhereIF(input.keyword.IsNotEmptyOrNull(), x => x.FullName.Contains(input.keyword)).OrderBy(x => x.CreatorTime, OrderByType.Desc).ToListAsync();
var output = data.Adapt>();
await GetManagersInfo(output);
return new { list = output };
}
///
/// 任务列表
///
/// 项目Id
/// 请求参数
///
[HttpGet("{projectId}/Task")]
public async Task GetTaskList([FromQuery] KeywordInput input, string projectId)
{
var data = await _projectGanttRepository.Entities.Where(x => x.Type == 2 && x.ProjectId == projectId && x.DeleteMark == null).OrderBy(x => x.CreatorTime, OrderByType.Desc).ToListAsync();
data.Add(await _projectGanttRepository.FirstOrDefaultAsync(x => x.Id == projectId));
if (!string.IsNullOrEmpty(input.keyword))
{
data = data.TreeWhere(t => t.FullName.Contains(input.keyword), t => t.Id, t => t.ParentId);
}
var output = data.Adapt>();
return new { list = output.ToTree() };
}
///
/// 任务树形
///
/// 项目Id
///
[HttpGet("{projectId}/Task/Selector/{id}")]
public async Task GetTaskTreeView(string projectId,string id)
{
var data = (await _projectGanttRepository.Entities.Where(x => x.Type == 2 && x.ProjectId == projectId && x.DeleteMark == null).OrderBy(x => x.CreatorTime, OrderByType.Desc).ToListAsync());
data.Add(await _projectGanttRepository.FirstOrDefaultAsync(x => x.Id == projectId));
if (!id.Equals("0"))
{
data.RemoveAll(x=>x.Id== id);
}
var output = data.Adapt>();
return new { list = output.ToTree() };
}
///
/// 信息
///
/// 主键值
///
[HttpGet("{id}")]
public async Task GetInfo(string id)
{
var data = (await _projectGanttRepository.FirstOrDefaultAsync(x=>x.Id==id&&x.DeleteMark==null)).Adapt();
return data;
}
///
/// 项目任务信息
///
/// 主键值
///
[HttpGet("Task/{taskId}")]
public async Task GetTaskInfo(string taskId)
{
var data = (await _projectGanttRepository.FirstOrDefaultAsync(x => x.Id == taskId && x.DeleteMark == null)).Adapt();
return data;
}
#endregion
#region POST
///
/// 删除
///
/// 主键值
///
[HttpDelete("{id}")]
public async Task Delete(string id)
{
if (await _projectGanttRepository.AnyAsync(x => x.ParentId != id&&x.DeleteMark==null))
{
var entity = await _projectGanttRepository.FirstOrDefaultAsync(x=>x.Id==id&&x.DeleteMark==null);
if (entity != null)
{
var isOk= await _projectGanttRepository.Context.Updateable(entity).IgnoreColumns(ignoreAllNullColumns: true).CallEntityMethod(m => m.Delete()).ExecuteCommandAsync();
if (isOk < 1)
throw NCCException.Oh(ErrorCode.COM1002);
}
else
{
throw NCCException.Oh(ErrorCode.COM1005);
}
}
else
{
throw NCCException.Oh(ErrorCode.D1007);
}
}
///
/// 创建
///
/// 实体对象
///
[HttpPost("")]
public async Task Create([FromBody] ProjectGanttCrInput input)
{
if (await _projectGanttRepository.AnyAsync(x => x.EnCode == input.enCode && x.DeleteMark == null) || await _projectGanttRepository.AnyAsync(x => x.FullName == input.fullName && x.DeleteMark == null))
throw NCCException.Oh(ErrorCode.COM1004);
var entity = input.Adapt();
entity.Type = 1;
entity.ParentId = "0";
var isOk= await _projectGanttRepository.Context.Insertable(entity).CallEntityMethod(m => m.Creator()).ExecuteCommandAsync();
if (isOk < 1)
throw NCCException.Oh(ErrorCode.COM1000);
}
///
/// 编辑
///
/// 主键值
/// 实体对象
///
[HttpPut("{id}")]
public async Task Update(string id, [FromBody] ProjectGanttUpInput input)
{
if (await _projectGanttRepository.AnyAsync(x => x.Id != id && x.EnCode == input.enCode && x.DeleteMark == null) || await _projectGanttRepository.AnyAsync(x => x.Id != id && x.FullName == input.fullName && x.DeleteMark == null))
throw NCCException.Oh(ErrorCode.COM1004);
var entity = input.Adapt();
var isOk= await _projectGanttRepository.Context.Updateable(entity).IgnoreColumns(ignoreAllNullColumns: true).CallEntityMethod(m => m.LastModify()).ExecuteCommandAsync();
if (isOk < 1)
throw NCCException.Oh(ErrorCode.COM1001);
}
///
/// 创建
///
/// 实体对象
///
[HttpPost("Task")]
public async Task CreateTask([FromBody] ProjectGanttTaskCrInput input)
{
var entity = input.Adapt();
entity.Type = 2;
var isOk = await _projectGanttRepository.Context.Insertable(entity).CallEntityMethod(m => m.Creator()).ExecuteCommandAsync();
if (isOk < 1)
throw NCCException.Oh(ErrorCode.COM1000);
}
///
/// 编辑
///
/// 主键值
/// 实体对象
///
[HttpPut("Task/{id}")]
public async Task UpdateTask(string id, [FromBody] ProjectGanttTaskUpInput input)
{
var entity = input.Adapt();
var isOk = await _projectGanttRepository.Context.Updateable(entity).IgnoreColumns(ignoreAllNullColumns: true).CallEntityMethod(m => m.LastModify()).ExecuteCommandAsync();
if (isOk < 1)
throw NCCException.Oh(ErrorCode.COM1001);
}
#endregion
#region PrivateMethod
///
/// 项目参与人员
///
///
///
private async Task GetManagersInfo(List outputList)
{
var userList = await _usersService.GetList();
foreach (var output in outputList)
{
List managerIds = new List(output.managerIds.Split(","));
foreach (var id in managerIds)
{
var managerInfo = new ManagersInfo();
var userInfo = userList.Find(x => x.Id == id);
if (userInfo != null)
{
managerInfo.account = userInfo.RealName + "/" + userInfo.Account;
managerInfo.headIcon = string.IsNullOrEmpty(userInfo.HeadIcon) ? "" : "/api/file/Image/userAvatar/" + userInfo.HeadIcon;
output.managersInfo.Add(managerInfo);
}
}
}
}
#endregion
}
}