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

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

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

        private readonly IRepository<Internationalization> _internationalizationRepository;

        public InternationalizationService(IRepository<Internationalization> internationalizationRepository)
        {
            _internationalizationRepository = internationalizationRepository;
        }


        [AllowAnonymous]

        public async Task<List<InternationalizationSimpleDto>> GetFrontInternationalizationList()
        {

            var list = await _internationalizationRepository.Where(t => t.InternationalizationType == 0).Select(t => new InternationalizationSimpleDto()
            {
                Code = t.Code,
                Value = t.Value,
                ValueCN = t.ValueCN
            }).ToListAsync();

            return list;
        }

        [HttpPost]
        public async Task<PageOutput<InternationalizationView>> GetInternationalizationList(InternationalizationQuery inQuery)
        {

            var internationalizationQueryable = _internationalizationRepository
                .WhereIf(inQuery.Description != null, t => t.Description.Contains(inQuery.Description))
                .WhereIf(inQuery.Code != null, t => t.Code.Contains(inQuery.Code))
                .WhereIf(inQuery.State != null, t => t.State == inQuery.State)
                .WhereIf(inQuery.InternationalizationType != null, t => t.InternationalizationType == inQuery.InternationalizationType)
                .WhereIf(inQuery.Value != null, t => t.Value.Contains(inQuery.Value))
                .WhereIf(inQuery.ValueCN != null, t => t.ValueCN.Contains(inQuery.ValueCN))
                .ProjectTo<InternationalizationView>(_mapper.ConfigurationProvider);

            var pageList = await internationalizationQueryable
            .ToPagedListAsync(inQuery.PageIndex, inQuery.PageSize, string.IsNullOrWhiteSpace(inQuery.SortField) ? nameof(InternationalizationView.State) : inQuery.SortField,
            inQuery.Asc);

            return pageList;
        }

        [HttpPost]
        public async Task<IResponseOutput> BatchAddInternationalization(BatchAddInternationalization batchAdd)
        {
            foreach (var item in batchAdd.AddList)
            {

                var mapItem = _mapper.Map<InternationalizationAddOrEdit>(item);

                mapItem.InternationalizationType = batchAdd.InternationalizationType;
                mapItem.State = batchAdd.State;

                var verifyExp1 = new EntityVerifyExp<Internationalization>()
                {
                    VerifyExp = t => t.Code == mapItem.Code && t.InternationalizationType == mapItem.InternationalizationType,

                    VerifyMsg = $"该类型已有{item.Code}名称的国际化标识",
                    IsVerify = true
                };

                var entity = await _internationalizationRepository.InsertOrUpdateAsync(mapItem, false, verifyExp1);
            }

            await _internationalizationRepository.SaveChangesAsync();


            return ResponseOutput.Ok();
        }

        public async Task<IResponseOutput> AddOrUpdateInternationalization(InternationalizationAddOrEdit addOrEditInternationalization)
        {
            var verifyExp1 = new EntityVerifyExp<Internationalization>()
            {
                VerifyExp = t => t.Code == addOrEditInternationalization.Code && t.InternationalizationType == addOrEditInternationalization.InternationalizationType,

                VerifyMsg = $"该类型已有{addOrEditInternationalization.Code}名称的国际化标识",
                IsVerify = true
            };

            var entity = await _internationalizationRepository.InsertOrUpdateAsync(addOrEditInternationalization, true, verifyExp1);

            if (addOrEditInternationalization.InternationalizationType == 1)
            {
                await InternationalizationHelper.AddOrUpdateJsonKeyValueAsync(entity.Code, addOrEditInternationalization.Value, addOrEditInternationalization.ValueCN);

            }

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

        }


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


    }
}
