using AutoMapper; using AutoMapper.QueryableExtensions; using IRaCIS.Application.ExpressionExtend; using IRaCIS.Core.Application.Contracts; using IRaCIS.Core.Application.Contracts.DTO; using IRaCIS.Core.Application.Contracts.RequestAndResponse; using IRaCIS.Core.Domain.Interfaces; using IRaCIS.Core.Domain.Models; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; namespace IRaCIS.Application.Services.QA { public class QATemplateService : IQATemplateService { private readonly IQATemplateRepository _qaTemplateRepository; private readonly IMapper _mapper; private readonly IDictionaryRepository _dictionaryRepository; private readonly IQATemplateDictionaryRepository _qaTemplateDictionaryRepository; public QATemplateService(IQATemplateRepository qaTemplateRepository, IMapper mapper, IDictionaryRepository dictionaryRepository, IQATemplateDictionaryRepository qaTemplateDictionaryRepository ) { _qaTemplateRepository = qaTemplateRepository; _mapper = mapper; _dictionaryRepository = dictionaryRepository; _qaTemplateDictionaryRepository = qaTemplateDictionaryRepository; } public IResponseOutput AddOrUpdateQATemplate(QATemplateCommand qaTemplateCommand) { if (qaTemplateCommand.Id == null) { var qaTemplate = _mapper.Map(qaTemplateCommand); _qaTemplateRepository.Add(qaTemplate); var success = _qaTemplateRepository.SaveChanges(); return ResponseOutput.Result(success, qaTemplate.Id); } else { var qaTemplate = _qaTemplateRepository.GetAll().FirstOrDefault(t => t.Id == qaTemplateCommand.Id); _mapper.Map(qaTemplateCommand, qaTemplate); _qaTemplateRepository.Update(qaTemplate); var success = _qaTemplateRepository.SaveChanges(); return ResponseOutput.Result(success); } } public IResponseOutput DeleteQATemplate(Guid qaTemplateId) { var success = _qaTemplateRepository.Delete(t => t.Id == qaTemplateId); return ResponseOutput.Result(success); } public List GetQaTemplateSelectList() { var query = _qaTemplateRepository.GetAll() .ProjectTo(_mapper.ConfigurationProvider); return query.ToList(); } public PageOutput GetQaTemplateList(QATemplateQueryDTO qaTemplateQuery) { Expression> qATemplateLambda = x => true; var query = _qaTemplateRepository.GetAll().Where(qATemplateLambda) .ProjectTo(_mapper.ConfigurationProvider); var count = query.Count(); var propName = qaTemplateQuery.SortField == string.Empty ? "Name" : qaTemplateQuery.SortField; query = qaTemplateQuery.Asc ? query.OrderBy(propName) : query.OrderByDescending(propName); query = query.Skip((qaTemplateQuery.PageIndex - 1) * qaTemplateQuery.PageSize).Take(qaTemplateQuery.PageSize); var list = query.ToList(); return new PageOutput(qaTemplateQuery.PageIndex, qaTemplateQuery.PageSize, count, list); } public List GetQaTemplateConfigList(Guid qaTemplateId) { var query = from dic in _dictionaryRepository.GetAll().Where(t => t.Type == "QATemplate") join qaDic in _qaTemplateDictionaryRepository.GetAll().Where(u => u.QATemplateId == qaTemplateId) on dic.Id equals qaDic.DictionaryId into tt from qaDic in tt.DefaultIfEmpty() select new QATemplateItemSelect { DictionaryId = dic.Id, DictionaryValue = dic.Value, IsSelect = qaDic != null }; return query.ToList(); } public IResponseOutput ConfigQATemplate(QATemplateConfigCommand qaTemplateConfigCommand) { var success = false; if (qaTemplateConfigCommand.IsSelect) { var qaTemplateDictionary = _mapper.Map(qaTemplateConfigCommand); _qaTemplateDictionaryRepository.Add(qaTemplateDictionary); success = _qaTemplateDictionaryRepository.SaveChanges(); } else { success = _qaTemplateDictionaryRepository .Delete(t => t.DictionaryId == qaTemplateConfigCommand.DictionaryId && t.QATemplateId == qaTemplateConfigCommand.QATemplateId); } return ResponseOutput.Result(success); } public List GetQaTemplateItemsById(Guid qaTemplateId) { var query = from dic in _dictionaryRepository.GetAll().Where(t => t.Type == "QATemplate") join qaDic in _qaTemplateDictionaryRepository.GetAll().Where(u => u.QATemplateId == qaTemplateId) on dic.Id equals qaDic.DictionaryId select new QATemplateItemDTO { DictionaryId = qaDic.DictionaryId, DictionaryValue = dic.Value }; return query.ToList(); } } }