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

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

namespace IRaCIS.Core.Application.Service
{
	/// <summary>
	/// 用户WL模板
	/// </summary>	
	[ApiExplorerSettings(GroupName = "Reading")]
	public class UserWLTemplateService : BaseService
	{

		private readonly IRepository<UserWLTemplate> _userWLTemplateRepository;
		private readonly IRepository<User> _userRepository;

		public UserWLTemplateService(IRepository<UserWLTemplate> userWLTemplateRepository,
			IRepository<User> userRepository
			)
		{
			_userWLTemplateRepository = userWLTemplateRepository;
			this._userRepository = userRepository;
		}

		/// <summary>
		/// 获取模板
		/// </summary>
		/// <param name="inQuery"></param>
		/// <returns></returns>
		public async Task<List<UserWLTemplateView>> GetUserWLTemplateList(UserWLTemplateQuery inQuery)
		{
			var userWLTemplateQueryable = _userWLTemplateRepository
			.Where(x=>x.UserId==_userInfo.Id)
			.ProjectTo<UserWLTemplateView>(_mapper.ConfigurationProvider);
			return await userWLTemplateQueryable.ToListAsync();
		}

		/// <summary>
		/// 新增修改模板
		/// </summary>
		/// <param name="addOrEditUserWLTemplate"></param>
		/// <returns></returns>
		public async Task<IResponseOutput> AddOrUpdateUserWLTemplate(UserWLTemplateAddOrEdit addOrEditUserWLTemplate)
		{
			addOrEditUserWLTemplate.UserId = _userInfo.Id;
			if (addOrEditUserWLTemplate.Id == null)
			{
			   var count =	await _userWLTemplateRepository.Where(x => x.UserId == _userInfo.Id).CountAsync();
				if (count >= 10)
				{
                    throw new BusinessValidationFailedException(_localizer["UserWLTS_MaxTemplate"]);
                }

            }

		   if(await _userWLTemplateRepository.AnyAsync(x=> x.UserId == _userInfo.Id&&x.TemplateName== addOrEditUserWLTemplate.TemplateName&&x.Id!= addOrEditUserWLTemplate.Id))
		   {
                throw new BusinessValidationFailedException(_localizer["UserWLTS_NameRepeated"]);
            }

            if (await _userWLTemplateRepository.AnyAsync(x => x.UserId == _userInfo.Id && x.WW == addOrEditUserWLTemplate.WW && x.WL == addOrEditUserWLTemplate.WL && x.Id != addOrEditUserWLTemplate.Id))
            {
                throw new BusinessValidationFailedException(_localizer["UserWLTS_ContentRepeated"]);
            }

            var entity = await _userWLTemplateRepository.InsertOrUpdateAsync(addOrEditUserWLTemplate, true);
			return ResponseOutput.Ok(entity.Id.ToString());
		}

		/// <summary>
		/// 删除模板
		/// </summary>
		/// <param name="userWLTemplateId"></param>
		/// <returns></returns>
		[HttpDelete("{userWLTemplateId:guid}")]
		public async Task<IResponseOutput> DeleteUserWLTemplate(Guid userWLTemplateId)
		{
			var success = await _userWLTemplateRepository.DeleteFromQueryAsync(t => t.Id == userWLTemplateId, true);
			return ResponseOutput.Ok();
		}

		/// <summary>
		/// 获取自动切换任务配置
		/// </summary>
		/// <returns></returns>
		[HttpPost]
		public async Task<object> GetAutoCutNextTask() 
		{

			return await _userRepository.Where(x=>x.Id==_userInfo.Id).Select(x => new
			{
				AutoCutNextTask = x.AutoCutNextTask
			}).FirstNotNullAsync();
		}


		/// <summary>
		/// 设置自动切换任务
		/// </summary>
		/// <param name="inDto"></param>
		/// <returns></returns>
		[HttpPost]
		public async Task<IResponseOutput> SetAutoCutNextTask(SetAutoCutNextTaskInDto inDto)
		{
			await _userRepository.UpdatePartialNowNoQueryAsync(_userInfo.Id, x => new User()
			{

				AutoCutNextTask = inDto.AutoCutNextTask
			});

			await _userRepository.SaveChangesAsync ();

			return ResponseOutput.Ok();
		}
	}
}
