//--------------------------------------------------------------------
//     此代码由T4模板自动生成  byzhouhang 20210918
//	   生成时间 2022-03-31 13:18:56 
//     对此文件的更改可能会导致不正确的行为,并且如果重新生成代码,这些更改将会丢失。
//--------------------------------------------------------------------

using IRaCIS.Core.Domain.Models;
using Microsoft.AspNetCore.Mvc;
using IRaCIS.Core.Application.Interfaces;
using IRaCIS.Core.Application.ViewModel;
namespace IRaCIS.Core.Application.Service
{
    /// <summary>
    /// 系统模板文档配置表   
    /// </summary>	
    [ApiExplorerSettings(GroupName = "Common")]
    public class CommonDocumentService : BaseService, ICommonDocumentService
    {

        private readonly IRepository<CommonDocument> _commonDocumentRepository;

        public CommonDocumentService(IRepository<CommonDocument> commonDocumentRepository)
        {
            _commonDocumentRepository = commonDocumentRepository;
        }

        [HttpPost]
        public async Task<PageOutput<CommonDocumentView>> GetCommonDocumentList(CommonDocumentQuery queryCommonDocument)
        {

            var commonDocumentQueryable = _commonDocumentRepository.AsQueryable(true)
                .WhereIf( queryCommonDocument.ModuleTypeId!=null ,t=>t.ModuleTypeId == queryCommonDocument.ModuleTypeId)
                .WhereIf(queryCommonDocument.FileTypeId != null, t => t.FileTypeId == queryCommonDocument.FileTypeId)
                .WhereIf( string.IsNullOrEmpty(queryCommonDocument.Code) , t => t.Code.Contains(queryCommonDocument.Code) )
                .WhereIf(string.IsNullOrEmpty(queryCommonDocument.Name), t => t.Name.Contains(queryCommonDocument.Name))
                .ProjectTo<CommonDocumentView>(_mapper.ConfigurationProvider,new { token = _userInfo.UserToken, userId = _userInfo.Id });

            return await commonDocumentQueryable.ToPagedListAsync(queryCommonDocument.PageIndex, queryCommonDocument.PageSize, String.IsNullOrEmpty(queryCommonDocument.SortField) ? nameof(CommonDocument.Code) : queryCommonDocument.SortField, queryCommonDocument.Asc); ;
        }


        public async Task<IResponseOutput> AddOrUpdateCommonDocument(CommonDocumentAddOrEdit addOrEditCommonDocument)
        {
            var verifyExp1 = new EntityVerifyExp<CommonDocument>()
            {
                VerifyExp = t => t.Code == addOrEditCommonDocument.Code,
                VerifyMsg = "文档的Code不能够重复。"
            };

            var entity = await _commonDocumentRepository.InsertOrUpdateAsync(addOrEditCommonDocument, true, verifyExp1);

            return ResponseOutput.Ok(entity.Id.ToString());

        }

    }
}