//--------------------------------------------------------------------
//     此代码由T4模板自动生成  byzhouhang 20210918
//	   生成时间 2023-06-27 15:47:06 
//     对此文件的更改可能会导致不正确的行为，并且如果重新生成代码，这些更改将会丢失。
//--------------------------------------------------------------------

using IRaCIS.Core.Domain.Models;
using Microsoft.AspNetCore.Mvc;
using IRaCIS.Core.Application.Interfaces;
using IRaCIS.Core.Application.ViewModel;
using IRaCIS.Application.Contracts;

namespace IRaCIS.Core.Application.Service
{
    /// <summary>
    /// PublishLogService
    /// </summary>	
    [ApiExplorerSettings(GroupName = "Common")]
    public class PublishLogService : BaseService, IPublishLogService
    {

        private readonly IRepository<PublishLog> _publishLogRepository;

        public PublishLogService(IRepository<PublishLog> publishLogRepository)
        {
            _publishLogRepository = publishLogRepository;
        }

        [HttpPost]
        public async Task<PageOutput<PublishLogView>> GetPublishLogList(PublishLogQuery inQuery)
        {

            var publishLogQueryable = _publishLogRepository
                .WhereIf(!string.IsNullOrEmpty(inQuery.Version) ,  t => t.Version.Contains(inQuery.Version))
                .WhereIf(!string.IsNullOrEmpty(inQuery.UpdateContent), t => t.UpdateContent.Contains(inQuery.UpdateContent))
             .ProjectTo<PublishLogView>(_mapper.ConfigurationProvider);

            var pageList = await publishLogQueryable
                .ToPagedListAsync(inQuery.PageIndex, inQuery.PageSize, inQuery.SortField, inQuery.Asc);

            return pageList;
        }


        public async Task<IResponseOutput> AddOrUpdatePublishLog(PublishLogAddOrEdit addOrEditPublishLog)
        {
            Version version;

            if (!Version.TryParse(addOrEditPublishLog.Version, out version))
            {
                return ResponseOutput.NotOk(_localizer["PublishLog_VersionNotMatch"]);
            }
                     
            var verifyExp1 = new EntityVerifyExp<PublishLog>()
            {
                VerifyExp = u => u.Version == addOrEditPublishLog.Version ,
               
                VerifyMsg = "发布编号不能重复"
            };



            var entity = await _publishLogRepository.InsertOrUpdateAsync(addOrEditPublishLog, true, verifyExp1);

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

        }


        [HttpDelete("{publishLogId:guid}")]
        public async Task<IResponseOutput> DeletePublishLog(Guid publishLogId)
        {
            var success = await _publishLogRepository.DeleteFromQueryAsync(t => t.Id == publishLogId, true);
            return ResponseOutput.Ok();
        }


    }
}
