sunpengfei
2025-12-04 484de131314a90144cceac6ea721e345ad014f08
LifePayment/LifePayment.Application/LifePay/LifePayRateService.cs
New file
@@ -0,0 +1,159 @@
using LifePayment.Application.Contracts;
using LifePayment.Application.Contracts.LifePay;
using LifePayment.Domain;
using LifePayment.Domain.LifePay;
using LifePayment.Domain.Models;
using LifePayment.Domain.Shared;
using Microsoft.EntityFrameworkCore;
using Nest;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;
using Z.EntityFramework.Plus;
using ZeroD.Util;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
namespace LifePayment.Application.LifePay;
public class LifePayRateService : ApplicationService, ILifePayRateService
{
    private readonly IRepository<LifePayRateChannel, Guid> _lifePayRateChannelRepository;
    public LifePayRateService(IRepository<LifePayRateChannel, Guid> lifePayRateChannelRepository)
    {
        _lifePayRateChannelRepository = lifePayRateChannelRepository;
    }
    /// <summary>
    /// 获取折扣通道配置分页
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<PageOutput<CreateEditRateChannelOutput>> GetLifePayRateChannelPage(PageInput input)
    {
        return await GetLifePayRateChannelListFilter().GetPageResult(input.PageModel);
    }
    /// <summary>
    /// 获取折扣通道配置列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<List<CreateEditRateChannelOutput>> GetLifePayRateChannelAllList(QueryRateChannelInput input)
    {
        return await GetLifePayRateChannelListFilter()
            .WhereIf(input.LifePayOrderType.HasValue, x => x.LifePayOrderType == input.LifePayOrderType)
            .WhereIf(input.Status.HasValue, x => x.Status == input.Status)
            .OrderBy(r => r.CreationTime).ToListAsync();
    }
    /// <summary>
    /// 新增编辑折扣通道配置
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<int> CreateOrEditLifePayRateChannel(CreateEditRateChannelInput input)
    {
        CheckExtensions.IfTrueThrowUserFriendlyException(input.SupplierRate < 0.01m, "供应商折扣设置错误");
        CheckExtensions.IfTrueThrowUserFriendlyException(input.PlatformRate < 0.01m, "平台折扣设置错误");
        CheckExtensions.IfTrueThrowUserFriendlyException(input.PlatformRate < input.SupplierRate, "平台折扣无法低于供应商折扣");
        bool isExist = await _lifePayRateChannelRepository.AnyAsync(r => (!input.Id.HasValue || r.Id != input.Id.Value) && r.Code == input.Code);
        CheckExtensions.IfTrueThrowUserFriendlyException(isExist, "ID已存在");
        if (input.Id.HasValue)
        {
            var dto = await _lifePayRateChannelRepository.FirstOrDefaultAsync(r => r.Id == input.Id.Value);
            CheckExtensions.IfTrueThrowUserFriendlyException(dto == null, "未获取到折扣通道数据");
            dto.LifePayOrderType = input.LifePayOrderType;
            dto.RateChannelName = input.RateChannelName;
            dto.Code = input.Code;
            dto.SupplierRate = input.SupplierRate;
            dto.PlatformRate = input.PlatformRate;
            dto.Status = input.Status;
            dto.Remark = input.Remark;
        }
        else
        {
            input.Id = Guid.NewGuid();
            var entity = ObjectMapper.Map<CreateEditRateChannelInput, LifePayRateChannel>(input);
            await _lifePayRateChannelRepository.InsertAsync(entity);
        }
        return Constant.SUCCESS;
    }
    /// <summary>
    /// 设置折扣通道状态
    /// </summary>
    /// <param name="id"></param>
    /// <param name="status"></param>
    /// <returns></returns>
    public async Task<int> SetRateChannelStatus(Guid id, LifePayRateChannelStatus status)
    {
        await _lifePayRateChannelRepository.Where(r => r.Id == id).UpdateAsync(r => new LifePayRateChannel
        {
            Status = status,
        });
        return Constant.SUCCESS;
    }
    /// <summary>
    /// 删除折扣通道
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public async Task<int> DeleteRateChannel(Guid id)
    {
        await _lifePayRateChannelRepository.DeleteAsync(s => s.Id == id);
        return Constant.SUCCESS;
    }
    public async Task<CreateEditRateChannelOutput> GetRateChannelByCode(string code)
    {
        var dto = await _lifePayRateChannelRepository.FirstOrDefaultAsync(x => x.Code == code);
        if (dto == null)
        {
            return new CreateEditRateChannelOutput();
        }
        else
        {
            return new CreateEditRateChannelOutput
            {
                Id = dto.Id,
                LifePayOrderType = dto.LifePayOrderType,
                RateChannelName = dto.RateChannelName,
                Code = dto.Code,
                SupplierRate = dto.SupplierRate,
                PlatformRate = dto.PlatformRate,
                Status = dto.Status,
                Remark = dto.Remark,
                CreationTime = dto.CreationTime,
            };
        }
    }
    private IQueryable<CreateEditRateChannelOutput> GetLifePayRateChannelListFilter()
    {
        var query = from x in _lifePayRateChannelRepository
               select new CreateEditRateChannelOutput
               {
                   Id = x.Id,
                   LifePayOrderType = x.LifePayOrderType,
                   RateChannelName = x.RateChannelName,
                   Code = x.Code,
                   SupplierRate = x.SupplierRate,
                   PlatformRate = x.PlatformRate,
                   Status = x.Status,
                   Remark = x.Remark,
                   CreationTime = x.CreationTime,
               };
        return query;
    }
}