123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- using System;
- using System.Collections.Generic;
- using System.Threading.Tasks;
- using AutoMapper;
- using Dapper;
- using Datory;
- using GxPress.Common.AppOptions;
- using GxPress.Common.Page;
- using GxPress.Common.Tools;
- using GxPress.Repository.Interface.Order;
- using GxPress.Request.Order;
- using GxPress.Result.Order;
- using Microsoft.Extensions.Options;
- namespace GxPress.Repository.Implement.Order
- {
- public class OrderRepository : IOrderRepository
- {
- private readonly Repository<Entity.Order.Order> _repository;
- private readonly IMapper _mapper;
- private readonly string _connectionString;
- private readonly string _databaseTypestr;
- public OrderRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
- {
- _databaseTypestr = dbOptionsAccessor.CurrentValue.DatabaseType;
- _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
- var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
- var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
- _repository = new Repository<Entity.Order.Order>(database);
- _mapper = mapper;
- }
- public IDatabase Database => _repository.Database;
- public string TableName => _repository.TableName;
- public List<TableColumn> TableColumns => _repository.TableColumns;
- /// <summary>
- /// 查询订单
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<PagedList<Entity.Order.Order>> GetAllOrderAsync(OrderSearchRequest request)
- {
- var result = new PagedList<Entity.Order.Order>();
- var query = Q.NewQuery();
- if (!string.IsNullOrEmpty(request.KeyWord))
- {
- query.WhereLike(nameof(Entity.Order.Order.OrderNumber), $"%{request.KeyWord}%").OrWhereLike(nameof(Entity.Order.Order.Explain), $"%{request.KeyWord}%");
- }
- if (request.PayWay > 0)
- query.Where(nameof(Entity.Order.Order.PayWay), request.PayWay);
- if (!string.IsNullOrEmpty(request.BeginTime) && !string.IsNullOrEmpty(request.EndTime))
- {
- if (DateTime.TryParse(request.BeginTime, out var BeginTime) && DateTime.TryParse(request.EndTime, out var EndTime))
- {
- query.WhereDate(nameof(Entity.Order.Order.CreatedDate), ">", BeginTime);
- query.WhereDate(nameof(Entity.Order.Order.CreatedDate), "<", EndTime);
- }
- }
- if (request.UserId > 0)
- query.Where(nameof(Entity.Order.Order.UserId), request.UserId);
- result.Total = await _repository.CountAsync(query);
- query.OrderByDesc(nameof(Entity.Order.Order.CreatedDate));
- result.Items = await _repository.GetAllAsync(query.ForPage(request.Page, request.PerPage));
- return result;
- }
- /// <summary>
- /// 财务中心
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<OrderFinanceResult<Entity.Order.Order>> GetAllOrderFinnceAsync(OrderFinanceSearchRequest request)
- {
- var result = new OrderFinanceResult<Entity.Order.Order>();
- var query = Q.NewQuery();
- if (!string.IsNullOrEmpty(request.KeyWord))
- query.WhereLike(nameof(Entity.Order.Order.OrderNumber), $"%{request.KeyWord}%");
- if (request.PayWay > 0)
- query.Where(nameof(Entity.Order.Order.PayWay), request.PayWay);
- if (request.OrderType > 0)
- query.Where(nameof(Entity.Order.Order.OrderType), request.OrderType);
- if (!string.IsNullOrEmpty(request.BeginTime) && !string.IsNullOrEmpty(request.EndTime))
- {
- if (DateTime.TryParse(request.BeginTime, out var BeginTime) && DateTime.TryParse(request.EndTime, out var EndTime))
- {
- query.WhereDate(nameof(Entity.Order.Order.CreatedDate), ">", BeginTime);
- query.WhereDate(nameof(Entity.Order.Order.CreatedDate), "<", EndTime);
- }
- }
- result.Total = await _repository.CountAsync(query);
- //显示价格
- result.ShowPrice = await _repository.SumAsync(nameof(Entity.Order.Order.Price), query);
- //集合
- result.Items = await _repository.GetAllAsync(query.ForPage(request.Page, request.PerPage));
- //所有价格
- result.AllPrice = await _repository.SumAsync(nameof(Entity.Order.Order.Price));
- //订单数
- result.AllOrderCount = await _repository.CountAsync(Q.Where(nameof(Entity.Order.Order.Id), ">", 0));
- //VIP人数
- var databaseType = _databaseTypestr.ToEnum<DatabaseType>(DatabaseType.MySql);
- var database = new Database(databaseType, _connectionString);
- var connection = database.GetConnection();
- var sql = $"select count(1) from tede_user where IsVip=1 and id in(select userid from tede_order)";
- result.VipUserCount = await connection.ExecuteScalarAsync<int>(sql);
- // Vip总额
- sql = $" select Sum(Price) from tede_order where UserId IN(select Id from tede_user where IsVip=1)";
- result.VipAllPrice = await connection.ExecuteScalarAsync<int>(sql);
- //VIP订单数
- sql = $" select count(1) from tede_order where UserId IN(select Id from tede_user where IsVip=1)";
- result.VipAllOrderCount = await connection.ExecuteScalarAsync<int>(sql);
- return result;
- }
- /// <summary>
- /// 添加
- /// </summary>
- /// <param name="order"></param>
- /// <returns></returns>
- public async Task<int> InsertAsync(Entity.Order.Order order)
- {
- return await _repository.InsertAsync(order);
- }
- /// <summary>
- ///获取订单
- /// </summary>
- /// <param name="orderNumber"></param>
- /// <returns></returns>
- public async Task<Entity.Order.Order> GetOrderAsync(string orderNumber)
- {
- return await _repository.GetAsync(Q.Where(nameof(Entity.Order.Order.OrderNumber), orderNumber));
- }
- public async Task<bool> UpdateAsync(Entity.Order.Order order)
- {
- return await _repository.UpdateAsync(order);
- }
- public async Task<bool> GetExistsAsync(int userId, int mediaId)
- {
- return await _repository.ExistsAsync(Q.Where(nameof(Entity.Order.Order.UserId), userId).Where(nameof(Entity.Order.Order.OrderType), 1).Where(nameof(Entity.Order.Order.MediaId), mediaId));
- }
- }
- }
|