秒杀模块初始化

Administrator 0 2023-09-29

1.在WeifuwuDemo.Seckill.Domain项目中创建Seckills项目文件夹并添加聚合根,实体等:

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

namespace WeifuwuDemo.Seckill.Seckills
{
    public class SeckillInfo:FullAuditedAggregateRoot<Guid>
    {
        public int SeckillType { set; get; } // 秒杀类型
        public string SeckillName { set; get; } // 秒杀名称
        public string SeckillUrl { set; get; } // 秒杀URL
        public decimal SeckillPrice { set; get; } // 秒杀价格
        public int SeckillStock { set; get; } // 秒杀库存
        public string SeckillPercent { set; get; } // 秒杀百分比
        public Guid TimeId { set; get; } // 秒杀时间编号
        public Guid ProductId { set; get; } // 商品编号
        public int SeckillLimit { set; get; } // 秒杀限制数量
        public string SeckillDescription { set; get; } // 秒杀描述
        public int SeckillIstop { set; get; } // 秒杀是否结束
        public int SeckillStatus { set; get; } // 秒杀状态

        public virtual ICollection<SeckillRecords> SeckillRecords { get; set; }
    }
}
using System;
using System.Collections.Generic;
using System.Text;
using Volo.Abp.Domain.Entities.Auditing;

namespace WeifuwuDemo.Seckill.Seckills
{
    public class SeckillRecords:FullAuditedEntity<Guid>
    {
        public DateTime Createtime { set; get; }      // 秒杀记录创建时间
        public DateTime Updatetime { set; get; }      // 秒杀记录更新时间
        public decimal RecordTotalprice { set; get; }        // 秒杀记录总价
        public Guid SeckillId { set; get; }               // 秒杀活动编号
        public int SeckillNum { set; get; }              // 秒杀数量
        public decimal SeckillPrice { set; get; }            // 秒杀价格
        public decimal ProductPrice { set; get; }            // 商品原价
        public string OrderSn { set; get; }                 // 订单号
        public Guid UserId { set; get; }                  // 用户编号
        public int RecordStatus { set; get; }            // 秒杀记录状态
    }
}
using System;
using System.Collections.Generic;
using System.Text;
using Volo.Abp.Domain.Entities.Auditing;

namespace WeifuwuDemo.Seckill.Seckills
{
    public class SeckillTime:FullAuditedAggregateRoot<Guid>
    {
        public string TimeTitleUrl { set; get; } // 秒杀时间主题url
        public string SeckillDate { set; get; } // 秒杀日期(2020/8/10)
        public string SeckillStarttime { set; get; } // 秒杀开始时间点(20:00)
        public string SeckillEndtime { set; get; } // 秒杀结束时间点(22:00)
        public int TimeStatus { set; get; } // 秒杀时间状态(0:启动,1:禁用)
    }
}

并在WeifuwuDemo.Seckill.EntityFrameworkCore项目中

SeckillDbContext文件下添加:

public DbSet<SeckillInfo> SeckillInfo { get; set; }
    public DbSet<SeckillTime> SeckillTime { get; set; }

在SeckillDbContextModelCreatingExtensions文件中添加:

builder.Entity<SeckillInfo>(b =>
        {
            b.ConfigureByConvention();
            b.HasMany(u => u.SeckillRecords).WithOne().HasForeignKey(ur => ur.SeckillId).IsRequired();
        });

        // 1、配置秒杀时间表
        builder.Entity<SeckillTime>(b =>
        {
            b.ConfigureByConvention();
        });

2.执行数据库迁移:

在WeifuwuDemo.Seckill.HttpApi.Host中cmd执行迁移命令:

dotnet ef migrations add seckilltable

dotnet ef database update

3.在WeifuwuDemo.Seckill.Domain下的Seckills文件夹下分别新建两个仓储接口:

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

namespace WeifuwuDemo.Seckill.Seckills
{
    public interface ISeckillInfoAbpRepository:IRepository<SeckillInfo,Guid>
    {
    }
}
using System;
using System.Collections.Generic;
using System.Text;
using Volo.Abp.Domain.Repositories;

namespace WeifuwuDemo.Seckill.Seckills
{
    public interface ISeckillTimeAbpRepository:IRepository<SeckillTime,Guid>
    {

    }
}

4.在WeifuwuDemo.Seckill.EntityFrameworkCore项目下分别新建Seckills文件夹和SeckillTables文件夹,分别实现仓储接口:

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.Seckill.EntityFrameworkCore;

namespace WeifuwuDemo.Seckill.Seckills
{
    public class SeckillInfoAbpRepository:EfCoreRepository<SeckillDbContext,SeckillInfo,Guid>,ISeckillInfoAbpRepository
    {
        public SeckillInfoAbpRepository(IDbContextProvider<SeckillDbContext> dbContextProvider):base(dbContextProvider)
        {
            
        }
    }
}
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.Seckill.EntityFrameworkCore;
using WeifuwuDemo.Seckill.Seckills;

namespace WeifuwuDemo.Seckill.SEckillTimes
{
    public class SeckillTimeAbpRepository : EfCoreRepository<SeckillDbContext, SeckillTime, Guid>, ISeckillTimeAbpRepository
    {
        public SeckillTimeAbpRepository(IDbContextProvider<SeckillDbContext> dbContextProvider) : base(dbContextProvider)
        {
            
        }
    }
}

5.在WeifuwuDemo.Seckill.Application.Contracts中分别新建Seckills文件夹和SeckillTables文件夹,分别添加服务接口和dto:

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

namespace WeifuwuDemo.Seckill.Seckills
{
    public interface ISeckillInfoAppService:ICrudAppService<SeckillInfoDto,Guid,PagedAndSortedResultRequestDto,CreateSeckillInfoDto,UpdateSeckillInfoDto>
    {

    }
}
using System;
using System.Collections.Generic;
using System.Text;

namespace WeifuwuDemo.Seckill.Seckills
{
    public class CreateSeckillInfoDto
    {
        public int SeckillType { set; get; } // 秒杀类型
        public string SeckillName { set; get; } // 秒杀名称
        public string SeckillUrl { set; get; } // 秒杀URL
        public decimal SeckillPrice { set; get; } // 秒杀价格
        public int SeckillStock { set; get; } // 秒杀库存
        public string SeckillPercent { set; get; } // 秒杀百分比
        public Guid TimeId { set; get; } // 秒杀时间编号
        public Guid ProductId { set; get; } // 商品编号
        public int SeckillLimit { set; get; } // 秒杀限制数量
        public string SeckillDescription { set; get; } // 秒杀描述
        public int SeckillIstop { set; get; } // 秒杀是否结束
        public int SeckillStatus { set; get; } // 秒杀状态
    }
}
using System;
using System.Collections.Generic;
using System.Text;

namespace WeifuwuDemo.Seckill.Seckills
{
    public class DeleteSeckillInfoDto
    {
        public Guid Id { set; get; } //  '订单号',
    }
}
using System;
using System.Collections.Generic;
using System.Text;
using Volo.Abp.Application.Dtos;

namespace WeifuwuDemo.Seckill.Seckills
{
    public class SeckillInfoDto:EntityDto<Guid>
    {
        public int SeckillType { set; get; } // 秒杀类型
        public string SeckillName { set; get; } // 秒杀名称
        public string SeckillUrl { set; get; } // 秒杀URL
        public decimal SeckillPrice { set; get; } // 秒杀价格
        public int SeckillStock { set; get; } // 秒杀库存
        public string SeckillPercent { set; get; } // 秒杀百分比
        public Guid TimeId { set; get; } // 秒杀时间编号
        public Guid ProductId { set; get; } // 商品编号
        public int SeckillLimit { set; get; } // 秒杀限制数量
        public string SeckillDescription { set; get; } // 秒杀描述
        public int SeckillIstop { set; get; } // 秒杀是否结束
        public int SeckillStatus { set; get; } // 秒杀状态
    }
}
using System;
using System.Collections.Generic;
using System.Text;

namespace WeifuwuDemo.Seckill.Seckills
{
    public class UpdateSeckillInfoDto
    {
        public string SeckillPrice { set; get; } //  '支付金额',
        public string SeckillStatus { set; get; } //  '支付状态',
        public Guid OrderId { set; get; } //  '订单号',
        public int SeckillType { set; get; }// 支付类型
        public string SeckillMethod { set; get; } //  '支付方式',
        public DateTime Createtime { set; get; } //  '支付创建时间',
        public DateTime Updatetime { set; get; } //  '支付更新时间',
        public string SeckillRemark { set; get; } //  '支付备注',
        public string SeckillUrl { set; get; } //  '支付url',
        public string SeckillReturnUrl { set; get; } //  '支付回调url',
        public string SeckillCode { set; get; }//  '支付单号',
        public Guid UserId { set; get; } //  '用户Id',
        public string SeckillErrorNo { set; get; } //  '支付错误编号',
        public string SeckillErrorInfo { set; get; } //  '支付错误信息',
    }
}

SeckillTables添加:

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

namespace WeifuwuDemo.Seckill.SeckillTimes
{
    public interface ISeckillTimeAppService:ICrudAppService<SeckillTimeDto,Guid,PagedAndSortedResultRequestDto,CreateSeckillTimeDto,UpdateSeckillTimeDto>
    {
    }
}
using System;
using System.Collections.Generic;
using System.Text;

namespace WeifuwuDemo.Seckill.SeckillTimes
{
    public class CreateSeckillTimeDto
    {
        public string TimeTitleUrl { set; get; } // 秒杀时间主题url
        public string SeckillDate { set; get; } // 秒杀日期(2020/8/10)
        public string SeckillStarttime { set; get; } // 秒杀开始时间点(20:00)
        public string SeckillEndtime { set; get; } // 秒杀结束时间点(22:00)
        public int TimeStatus { set; get; } // 秒杀时间状态(0:启动,1:禁用)
    }
}
using System;
using System.Collections.Generic;
using System.Text;

namespace WeifuwuDemo.Seckill.SeckillTimes
{
    public class DeleteSeckillTimeDto
    {
        public Guid Id { set; get; } //  '订单号',
    }
}
using System;
using System.Collections.Generic;
using System.Text;
using Volo.Abp.Application.Dtos;

namespace WeifuwuDemo.Seckill.SeckillTimes
{
    public class SeckillTimeDto:EntityDto<Guid>
    {
        public string TimeTitleUrl { set; get; } // 秒杀时间主题url
        public string SeckillDate { set; get; } // 秒杀日期(2020/8/10)
        public string SeckillStarttime { set; get; } // 秒杀开始时间点(20:00)
        public string SeckillEndtime { set; get; } // 秒杀结束时间点(22:00)
        public int TimeStatus { set; get; } // 秒杀时间状态(0:启动,1:禁用)
    }
}
using System;
using System.Collections.Generic;
using System.Text;

namespace WeifuwuDemo.Seckill.SeckillTimes
{
    public class UpdateSeckillTimeDto
    {
        public string TimeTitleUrl { set; get; } // 秒杀时间主题url
        public string SeckillDate { set; get; } // 秒杀日期(2020/8/10)
        public string SeckillStarttime { set; get; } // 秒杀开始时间点(20:00)
        public string SeckillEndtime { set; get; } // 秒杀结束时间点(22:00)
        public int TimeStatus { set; get; } // 秒杀时间状态(0:启动,1:禁用)
    }
}

6.在WeifuwuDemo.Seckill.Application项目中分别新建Seckills文件夹和SeckillTables文件夹,分别实现服务接口:

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.Seckill.Seckills
{
    public class SeckillInfoAppService:CrudAppService<SeckillInfo,SeckillInfoDto,Guid,PagedAndSortedResultRequestDto,CreateSeckillInfoDto,UpdateSeckillInfoDto>,ISeckillInfoAppService
    {
        private readonly ISeckillInfoAbpRepository _seckillInfoAbpRepository;

        public SeckillInfoAppService(ISeckillInfoAbpRepository seckillInfoAbpRepository):base(seckillInfoAbpRepository)
        {
            _seckillInfoAbpRepository = seckillInfoAbpRepository;
        }
    }
}
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;
using WeifuwuDemo.Seckill.Seckills;

namespace WeifuwuDemo.Seckill.SeckillTimes
{
    public class SeckillTimeAppService:CrudAppService<SeckillTime,SeckillTimeDto,Guid,PagedAndSortedResultRequestDto,CreateSeckillTimeDto,UpdateSeckillTimeDto>,ISeckillTimeAppService
    {
        private readonly ISeckillTimeAbpRepository _seckillTimeAbpRepository;

        public SeckillTimeAppService(ISeckillTimeAbpRepository seckillTimeAbpRepository):base(seckillTimeAbpRepository)
        {
            _seckillTimeAbpRepository = seckillTimeAbpRepository;
        }
    }
}

并且在SeckillApplicationAutoMapperProfile文件中添加dto映射关系:

using AutoMapper;
using Volo.Abp.Application.Dtos;
using WeifuwuDemo.Seckill.Seckills;
using WeifuwuDemo.Seckill.SeckillTimes;

namespace WeifuwuDemo.Seckill;

public class SeckillApplicationAutoMapperProfile : Profile
{
    public SeckillApplicationAutoMapperProfile()
    {

        CreateMap<SeckillInfo, SeckillInfoDto>();
        CreateMap<PagedAndSortedResultRequestDto, SeckillInfo>();
        CreateMap<CreateSeckillInfoDto, SeckillInfo>();
        CreateMap<UpdateSeckillInfoDto, SeckillInfo>();

        CreateMap<SeckillTime, SeckillTimeDto>();
        CreateMap<PagedAndSortedResultRequestDto, SeckillTime>();
        CreateMap<CreateSeckillTimeDto, SeckillTime>();
        CreateMap<UpdateSeckillTimeDto, SeckillTime>();

        /* You can configure your AutoMapper mapping configuration here.
         * Alternatively, you can split your mapping configurations
         * into multiple profile classes for a better organization. */
    }
}

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

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

namespace WeifuwuDemo.Seckill;

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

7.在WeifuwuDemo.Seckill.HttpApi中分别新建Seckills文件夹和SeckillTables文件夹,添加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.Seckill.Seckills
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class SeckillInfoController:SeckillController,ISeckillInfoAppService
    {
        private readonly ISeckillInfoAppService _seckillInfoAppService;

        public SeckillInfoController(ISeckillInfoAppService seckillInfoAppService)
        {
            _seckillInfoAppService = seckillInfoAppService;
        }
        [HttpPost]
        public Task<SeckillInfoDto> CreateAsync(CreateSeckillInfoDto input)
        {
            return _seckillInfoAppService.CreateAsync(input);
        }
        [HttpDelete]
        public Task DeleteAsync(Guid id)
        {
            return _seckillInfoAppService.DeleteAsync(id);
        }
        [HttpGet]
        public Task<SeckillInfoDto> GetAsync(Guid id)
        {
            return _seckillInfoAppService.GetAsync(id);
        }
        [HttpGet]
        public Task<PagedResultDto<SeckillInfoDto>> GetListAsync([FromQuery]PagedAndSortedResultRequestDto input)
        {
            return _seckillInfoAppService.GetListAsync(input);
        }
        [HttpPut]
        public Task<SeckillInfoDto> UpdateAsync(Guid id, UpdateSeckillInfoDto input)
        {
            return _seckillInfoAppService.UpdateAsync(id, input);
        }
    }
}