zhengyiming
18 小时以前 f7bb0825bc06b8cea32caa44d2326dde51990e77
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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;
    }
}