108 lines
		
	
	
		
			3.7 KiB
		
	
	
	
		
			C#
		
	
	
			
		
		
	
	
			108 lines
		
	
	
		
			3.7 KiB
		
	
	
	
		
			C#
		
	
	
| using IRaCIS.Application.Contracts;
 | ||
| using IRaCIS.Application.Interfaces;
 | ||
| using Microsoft.AspNetCore.Mvc;
 | ||
| using Panda.DynamicWebApi.Attributes;
 | ||
| 
 | ||
| namespace IRaCIS.Core.Application.Service
 | ||
| {
 | ||
| 
 | ||
|     [ApiExplorerSettings(GroupName = "Financial")]
 | ||
|     public class ExchangeRateService(IRepository<ExchangeRate> _exchangeRateRepository,
 | ||
|         IRepository<Payment> _paymentRepository, IMapper _mapper, IUserInfo _userInfo, IStringLocalizer _localizer) : BaseService, IExchangeRateService
 | ||
|     {
 | ||
| 
 | ||
| 
 | ||
|         [NonDynamicMethod]
 | ||
|         public async Task<IResponseOutput> AddOrUpdateExchangeRate(ExchangeRateCommand model)
 | ||
|         {
 | ||
|             if (model.Id == Guid.Empty || model.Id == null)
 | ||
|             {
 | ||
|                 var existItem = await _exchangeRateRepository.FirstOrDefaultAsync(u => u.YearMonth == model.YearMonth);
 | ||
|                 if (existItem != null)
 | ||
|                 {
 | ||
|                     //---The exchange rate of the same month already existed.
 | ||
|                     return ResponseOutput.NotOk(_localizer["ExR_SameMthExist"]);
 | ||
|                 }
 | ||
|                 var rate = _mapper.Map<ExchangeRate>(model);
 | ||
|                 rate = await _exchangeRateRepository.AddAsync(rate);
 | ||
|                 if (await _exchangeRateRepository.SaveChangesAsync())
 | ||
|                 {
 | ||
|                     return ResponseOutput.Ok(rate.Id.ToString());
 | ||
|                 }
 | ||
|                 else
 | ||
|                 {
 | ||
|                     return ResponseOutput.NotOk();
 | ||
|                 }
 | ||
| 
 | ||
|             }
 | ||
|             else
 | ||
|             {
 | ||
|                 var success = await _exchangeRateRepository.BatchUpdateNoTrackingAsync(t => t.Id == model.Id, u => new ExchangeRate()
 | ||
|                 {
 | ||
|                     //YearMonth = model.YearMonth,
 | ||
|                     Rate = model.Rate,
 | ||
|                     UpdateTime = DateTime.Now
 | ||
|                 });
 | ||
|                 return ResponseOutput.Result(success);
 | ||
|             }
 | ||
|         }
 | ||
| 
 | ||
|         /// <summary>
 | ||
|         /// 根据记录Id,删除汇率记录
 | ||
|         /// </summary>
 | ||
|         /// <param name="id">汇率记录Id</param>
 | ||
| 
 | ||
|         [HttpDelete("{id:guid}")]
 | ||
|         public async Task<IResponseOutput> DeleteExchangeRate(Guid id)
 | ||
|         {
 | ||
|             var monthInfo = await _exchangeRateRepository.FirstOrDefaultAsync(t => t.Id == id);
 | ||
| 
 | ||
|             if (await _paymentRepository.AnyAsync(t => t.YearMonth == monthInfo.YearMonth))
 | ||
|             {
 | ||
|                 //---The exchange rate has been used in monthly payment
 | ||
|                 return ResponseOutput.NotOk(_localizer["ExR_MthPymtRate"]);
 | ||
|             }
 | ||
| 
 | ||
| 
 | ||
|             var success = await _exchangeRateRepository.BatchDeleteNoTrackingAsync(t => t.Id == id);
 | ||
| 
 | ||
|             return ResponseOutput.Ok(success);
 | ||
|         }
 | ||
| 
 | ||
| 
 | ||
|         [NonDynamicMethod]
 | ||
|         public async Task<decimal> GetExchangeRateByMonth(string month)
 | ||
|         {
 | ||
|             //var rate = _exchangeRateRepository.FindSingleOrDefault(u => u.YearMonth.Equals(month));
 | ||
|             //if (rate == null)
 | ||
|             //{
 | ||
|             //    return 0;
 | ||
|             //}
 | ||
|             //return rate.Rate;
 | ||
| 
 | ||
|             var rate = await _exchangeRateRepository.FirstOrDefaultAsync(t => t.YearMonth == month);
 | ||
|             if (rate == null)
 | ||
|             {
 | ||
|                 return 0;
 | ||
|             }
 | ||
|             return rate.Rate;
 | ||
|         }
 | ||
| 
 | ||
|         [HttpPost]
 | ||
|         public async Task<PageOutput<ExchangeRateCommand>> GetExchangeRateList(ExchangeRateQueryDTO inQuery)
 | ||
|         {
 | ||
| 
 | ||
|             var yearMonth = inQuery.SearchMonth?.ToString("yyyy-MM");
 | ||
| 
 | ||
|             var exchangeRateQueryable = _exchangeRateRepository.AsQueryable()
 | ||
|                 .WhereIf(inQuery.SearchMonth != null, o => o.YearMonth == yearMonth)
 | ||
|                 .ProjectTo<ExchangeRateCommand>(_mapper.ConfigurationProvider);
 | ||
| 
 | ||
|             return await exchangeRateQueryable.ToPagedListAsync(inQuery);
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
|         }
 | ||
|     }
 | ||
| }
 |