using AutoMapper;
using IMCS.CCS.Entitys;
using IMCS.CCS.Service;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace IMCS.CCS.Service
{
///
/// 项目服务
///
public class ProjectService : IProjectService
{
private readonly IDataService _fileDataService;
private readonly IMapper _mapper;
private readonly IDataStorageConfigurationService _dataStorageConfigurationService;
public ProjectService(IConfiguration configuration, IDataService fileDataService, IMapper mapper, IDataStorageConfigurationService dataStorageConfigurationService)
{
_fileDataService = fileDataService;
//初始化 FileData服务
_fileDataService.Init(dataStorageConfigurationService.GetProjectPathOrKey());
_mapper = mapper;
_dataStorageConfigurationService = dataStorageConfigurationService;
}
///
/// 查询列表
///
///
public async Task> FindListAsync()
=> await _fileDataService.ReadDataAsync();
///
/// 保存
///
///
///
public async Task SaveAsync(Project form)
{
var data = (await this.FindListAsync())?.ToList() ?? new List();
var project = data.Find(w => w.Id == form.Id);
if (project == null)
{
form.Id = Guid.NewGuid();
project = _mapper.Map(form);
project.CreateTime = DateTime.Now;
data.Add(project);
}
else
{
project = _mapper.Map(form, project);
}
await _fileDataService.WriteDataAsync(data);
return project;
}
///
/// 删除数据
///
///
public async Task DeleteAsync(Guid id)
{
var data = (await this.FindListAsync())?.ToList() ?? new List();
var project = data.Find(w => w.Id == id);
if (project == null) return true;
data.RemoveAt(data.IndexOf(project));
await _fileDataService.WriteDataAsync(data);
return true;
}
///
/// 根据Id 查询 任务
///
///
///
public async Task FindByIdAsync(Guid id)
{
var data = (await this.FindListAsync())?.ToList() ?? new List();
return data.Find(w => w.Id == id) ?? new Project();
}
}
}