支付模块初始化

Administrator 1 2023-09-29

1.在WeifuwuDemo.Payment.Domain中新建Payments项目文件夹并添加实体:

using System;
using System.Collections.Generic;
using System.Text;
using Volo.Abp.Domain.Entities.Auditing;

namespace WeifuwuDemo.Payment.Payments
{
    /// <summary>
    /// 支付模型
    /// </summary>
    public class PaymentInfo:FullAuditedAggregateRoot<Guid>
    {
        public string PaymentPrice { set; get; } //  '支付金额',
        public string PaymentStatus { set; get; } //  '支付状态',
        public Guid OrderId { set; get; } //  '订单号',
        public int PaymentType { set; get; }// 支付类型
        public string PaymentMethod { set; get; } //  '支付方式',
        public DateTime Createtime { set; get; } //  '支付创建时间',
        public DateTime Updatetime { set; get; } //  '支付更新时间',
        public string PaymentRemark { set; get; } //  '支付备注',
        public string PaymentUrl { set; get; } //  '支付url',
        public string PaymentReturnUrl { set; get; } //  '支付回调url',
        public string PaymentCode { set; get; }//  '支付单号',
        public Guid UserId { set; get; } //  '用户Id',
        public string PaymentErrorNo { set; get; } //  '支付错误编号',
        public string PaymentErrorInfo { set; get; } //  '支付错误信息',
    }
}

2.在WeifuwuDemo.Payment.EntityFrameworkCore项目中的PaymentDbContext文件中添加:

public DbSet<PaymentInfo> PaymentInfo { get; set; }

在PaymentDbContextModelCreatingExtensions文件中添加:

builder.Entity<PaymentInfo>(b =>
        {
            b.ConfigureByConvention();
        });

3.在项目WeifuwuDemo.Payment.HttpApi.Host中cmd执行数据库迁移:

dotnet ef migrations add paymenttable

dotnet ef database update

4.在项目WeifuwuDemo.Payment.Domain下Payments文件夹下创建仓储接口:

using System;
using System.Collections.Generic;
using System.Text;
using Volo.Abp.Domain.Repositories;

namespace WeifuwuDemo.Payment.Payments
{
    public interface IPaymentInfoAbpRepository:IRepository<PaymentInfo,Guid>
    {
    }
}

在WeifuwuDemo.Payment.EntityFrameworkCore中创建Payments文件夹并实现仓储接口:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;
using WeifuwuDemo.Payment.EntityFrameworkCore;
using WeifuwuDemo.Payment.Payments;

namespace WeifuwuDemo.Payment.Payments
{
    public class PaymentInfoAbpRepository:EfCoreRepository<PaymentDbContext,PaymentInfo,Guid>,IPaymentInfoAbpRepository
    {
        public PaymentInfoAbpRepository(IDbContextProvider<PaymentDbContext> dbContextProvider):base(dbContextProvider)
        {
            
        }
    }
}

在WeifuwuDemo.Payment.Application.Contracts中创建服务接口和dto:

using System;
using System.Collections.Generic;
using System.Text;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;

namespace WeifuwuDemo.Payment.Payments
{
    public interface IPaymentInfoAppService:ICrudAppService<PaymentInfoDto,Guid,PagedAndSortedResultRequestDto,CreatePaymentInfoDto,UpdatePaymentInfoDto>
    {
    }
}
using System;
using System.Collections.Generic;
using System.Text;

namespace WeifuwuDemo.Payment.Payments
{
    public class CreatePaymentInfoDto
    {
        public Guid OrderId { set; get; } //  '订单号',
        public int PaymentType { set; get; }// 支付类型
        public string PaymentMethod { set; get; } //  '支付方式',
        public DateTime Createtime { set; get; } //  '支付创建时间',
        public DateTime Updatetime { set; get; } //  '支付更新时间',
        public string PaymentRemark { set; get; } //  '支付备注',
        public string PaymentUrl { set; get; } //  '支付url',
        public string PaymentReturnUrl { set; get; } //  '支付回调url',
        public string PaymentCode { set; get; }//  '支付单号',
        public Guid UserId { set; get; } //  '用户Id',
    }
}
using System;
using System.Collections.Generic;
using System.Text;

namespace WeifuwuDemo.Payment.Payments
{
    public class DeletePaymentInfoDto
    {
        public Guid OrderId { set; get; } //  '订单号',
        public Guid UserId { set; get; } //  '用户Id',
    }
}
using System;
using System.Collections.Generic;
using System.Text;
using Volo.Abp.Application.Dtos;

namespace WeifuwuDemo.Payment.Payments
{
    public class PaymentInfoDto:EntityDto<Guid>
    {
        public string PaymentPrice { set; get; } //  '支付金额',
        public string PaymentStatus { set; get; } //  '支付状态',
        public Guid OrderId { set; get; } //  '订单号',
        public int PaymentType { set; get; }// 支付类型
        public string PaymentMethod { set; get; } //  '支付方式',
        public DateTime Createtime { set; get; } //  '支付创建时间',
        public DateTime Updatetime { set; get; } //  '支付更新时间',
        public string PaymentRemark { set; get; } //  '支付备注',
        public string PaymentUrl { set; get; } //  '支付url',
        public string PaymentReturnUrl { set; get; } //  '支付回调url',
        public string PaymentCode { set; get; }//  '支付单号',
        public Guid UserId { set; get; } //  '用户Id',
        public string PaymentErrorNo { set; get; } //  '支付错误编号',
        public string PaymentErrorInfo { set; get; } //  '支付错误信息',
    }
}
using System;
using System.Collections.Generic;
using System.Text;

namespace WeifuwuDemo.Payment.Payments
{
    public class UpdatePaymentInfoDto
    {
        public string PaymentPrice { set; get; } //  '支付金额',
        public string PaymentStatus { set; get; } //  '支付状态',
        public Guid OrderId { set; get; } //  '订单号',
        public int PaymentType { set; get; }// 支付类型
        public string PaymentMethod { set; get; } //  '支付方式',
        public DateTime Createtime { set; get; } //  '支付创建时间',
        public DateTime Updatetime { set; get; } //  '支付更新时间',
        public string PaymentRemark { set; get; } //  '支付备注',
        public string PaymentUrl { set; get; } //  '支付url',
        public string PaymentReturnUrl { set; get; } //  '支付回调url',
        public string PaymentCode { set; get; }//  '支付单号',
        public Guid UserId { set; get; } //  '用户Id',
        public string PaymentErrorNo { set; get; } //  '支付错误编号',
        public string PaymentErrorInfo { set; get; } //  '支付错误信息',
    }
}

6.在WeifuwuDemo.Payment.Application中创建payments文件夹并实现服务接口:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;

namespace WeifuwuDemo.Payment.Payments
{
    public class PaymentInfoAppService:CrudAppService<PaymentInfo,PaymentInfoDto,Guid,PagedAndSortedResultRequestDto,CreatePaymentInfoDto,UpdatePaymentInfoDto>,IPaymentInfoAppService
    {
        private readonly IPaymentInfoAbpRepository _paymentInfoAbpRepository;

        public PaymentInfoAppService(IPaymentInfoAbpRepository paymentInfoAbpRepository):base(paymentInfoAbpRepository)
        {
            _paymentInfoAbpRepository = paymentInfoAbpRepository;
        }
    }
}

在PaymentApplicationAutoMapperProfile文件中添加dto map关系:

CreateMap<PaymentInfo, PaymentInfoDto>();
        CreateMap<PagedAndSortedResultRequestDto, PaymentInfo>();
        CreateMap<CreatePaymentInfoDto, PaymentInfo>();
        CreateMap<UpdatePaymentInfoDto, PaymentInfo>();

在PaymentApplicationModule文件中将验证实体改为false

using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.AutoMapper;
using Volo.Abp.Modularity;
using Volo.Abp.Application;

namespace WeifuwuDemo.Payment;

[DependsOn(
    typeof(PaymentDomainModule),
    typeof(PaymentApplicationContractsModule),
    typeof(AbpDddApplicationModule),
    typeof(AbpAutoMapperModule)
    )]
public class PaymentApplicationModule : AbpModule
{
    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        context.Services.AddAutoMapperObjectMapper<PaymentApplicationModule>();
        Configure<AbpAutoMapperOptions>(options =>
        {
            options.AddMaps<PaymentApplicationModule>(validate: false);
        });
    }
}

7.在WeifuwuDemo.Payment.HttpApi项目中添加payments文件夹并添加api:

using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;

namespace WeifuwuDemo.Payment.Payments
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class PaymentInfoController:PaymentController,IPaymentInfoAppService
    {
        private readonly IPaymentInfoAppService _paymentInfoAppService;

        public PaymentInfoController(IPaymentInfoAppService paymentInfoAppService)
        {
            _paymentInfoAppService = paymentInfoAppService;
        }
        [HttpPost]
        public Task<PaymentInfoDto> CreateAsync(CreatePaymentInfoDto input)
        {
            return _paymentInfoAppService.CreateAsync(input);
        }
        [HttpDelete]
        public Task DeleteAsync(Guid id)
        {
            return _paymentInfoAppService.DeleteAsync(id);
        }
        [HttpGet]
        public Task<PaymentInfoDto> GetAsync(Guid id)
        {
            return _paymentInfoAppService.GetAsync(id);
        }
        [HttpGet]
        public Task<PagedResultDto<PaymentInfoDto>> GetListAsync([FromQuery]PagedAndSortedResultRequestDto input)
        {
            return _paymentInfoAppService.GetListAsync(input);
        }
        [HttpPut]
        public Task<PaymentInfoDto> UpdateAsync(Guid id, UpdatePaymentInfoDto input)
        {
            return _paymentInfoAppService.UpdateAsync(id, input);
        }
    }
}