using FHT.Agency.CommonTypes;
using FHT.Agency.Queries.ImplementedInterfaces;
using FHT.Agency.Services.ImplementedInterfaces;
using FHT.Agency.UI.InputModels.Order;
using FHT.Agency.UI.ViewModels.Agent;
using FHT.Agency.UI.ViewModels.Feature;
using FHT.ApiCommon;
using FHT.Agency.Web.Common;
using FHT.Agency.Web.Filters;
using Microsoft.AspNet.Identity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System.Web.Http.Description;
using System.Threading.Tasks;
using FHT.Agency.Web.Extensions;
using FHT.Agency.UI.ViewModels;
using FHT.DDD;
using FHT.Agency.UI.ViewModels.Order;
using FHT.Core.CommonTypes;
using FHT.Utilities;
using System.Web.Http.Cors;
namespace FHT.Agency.Web.ApiControllers
{
[Authorize]
[RoutePrefix("api/orders")]
[EnableCors(origins: "*", headers: "*", methods: "*")]
public class OrderApiController : ApiController
{
private readonly IOrderQuery _orderQuery;
private readonly IOrderService _orderService;
private readonly IPackageService _packageService;
private readonly IFeatureQuery _featureQuery;
private readonly IUserQuery _userQuery;
private readonly IAgentClientQuery _agentClientQuery;
private readonly IKeywordQuery _keywordQuery;
public OrderApiController(IOrderQuery orderQuery,
IOrderService orderService, IPackageService packageService,
IFeatureQuery featureQuery, IUserQuery userQuery,
IAgentClientQuery agentClientQuery, IKeywordQuery keywordQuery)
{
_orderQuery = orderQuery;
_orderService = orderService;
_packageService = packageService;
_featureQuery = featureQuery;
_userQuery = userQuery;
_agentClientQuery = agentClientQuery;
_keywordQuery = keywordQuery;
}
#region 订单查询
///
/// 渠道查询订单
///
[HttpGet]
[ClaimsAuthorize(FhtUserClaimType.ViewOrders)]
[ResponseType(typeof(QueryResult))]
[Route("_search")]
public async Task SearchOrderAsync(int? clientId = null, int? agentOrganizationId = null, long? orderId = null,
OrderStatus? orderStatus = null, DateTime? beginTime = null, DateTime? endTime = null, int? channelUserId = null, BuyOrderType? buyOrderType = null,
int currPage=1,int limit=20)
{
int offset = (currPage < 2 ? 0 : currPage - 1) * limit;
int authenticatedUserId = User.Identity.GetUserId();
UserViewModel currUser = await _userQuery.GetAsync(authenticatedUserId);
var allChannelUsers = new List();
if (User.UserRole() == FhtUserRoleType.CustomerServiceStaff)
{
allChannelUsers = await _userQuery.GetCustomerServiceChannelUsersAsync(authenticatedUserId);
}
else
{
allChannelUsers = await _userQuery.GetChannelUsersByCurrUserAsync(authenticatedUserId);
}
if (channelUserId.HasValue && allChannelUsers.Count(u => u.UserId == channelUserId.Value) == 0)//非法请求
{
throw new InvalidOperationException("非法请求!");
}
QueryResult orders = await _orderQuery.ChannelUserSearchOrdersAsync(clientId, agentOrganizationId, orderId,
orderStatus, beginTime, endTime,
User.UserRole(), authenticatedUserId, channelUserId, buyOrderType, limit, offset);
return Ok(orders);
}
[HttpGet]
[ClaimsAuthorize(FhtUserClaimType.ManageOrders, FhtUserClaimType.AuditOrder)]
[ResponseType(typeof(QueryResult))]
[Route("_SearchOrderAudit")]
public async Task SearchOrderAuditAsync(int? clientId = null, long? orderId = null,int? packageId=null,
OrderStatus? orderStatus = null, DateTime? beginTime = null, DateTime? endTime = null, BuyOrderType? buyOrderType = null,
int currPage = 1, int limit = 20)
{
int offset = (currPage < 2 ? 0 : currPage - 1) * limit;
int authenticatedUserId = User.Identity.GetUserId();
QueryResult result = await _orderQuery.GetOrdersAsync(clientId, orderId, packageId,
orderStatus, beginTime, endTime,
User.UserRole(), authenticatedUserId,limit, offset, buyOrderType);
return Ok(result);
}
[HttpGet]
[ClaimsAuthorize(FhtUserClaimType.ReturnOrder)]
[ResponseType(typeof(QueryResult))]
[Route("_SearchOrderReturn")]
public async Task SearchOrderReturnAsync(int? clientId =null, long? orderId = null, int? packageId = null,
OrderStatus? orderStatus = null, DateTime? beginTime = null, DateTime? endTime = null, BuyOrderType? buyOrderType = null,
int currPage = 1, int limit = 20)
{
int authenticatedUserId = User.Identity.GetUserId();
int offset = (currPage < 2 ? 0 : currPage - 1) * limit;
QueryResult orders = await _orderQuery.GetOrdersAsync(clientId, orderId, packageId,
orderStatus, beginTime, endTime,
User.UserRole(), authenticatedUserId, limit, offset, buyOrderType);
return Ok(orders);
}
[HttpGet]
[ClaimsAuthorize(FhtUserClaimType.AuditReturnOrder)]
[ResponseType(typeof(QueryResult))]
[Route("_SearchAuditReturnOrder")]
public async Task SearchAuditReturnOrderAsync(int? clientId = null, long? orderId = null, int? packageId = null,
OrderStatus? orderStatus = null, DateTime? beginTime = null, DateTime? endTime = null, BuyOrderType? buyOrderType = null,
int currPage = 1, int limit = 20)
{
int authenticatedUserId = User.Identity.GetUserId();
int offset = (currPage < 2 ? 0 : currPage - 1) * limit;
QueryResult orders = await _orderQuery.GetOrdersAsync(clientId, orderId, packageId,
orderStatus, beginTime, endTime,
User.UserRole(), authenticatedUserId, limit, offset, buyOrderType);
return Ok(orders);
}
[HttpGet]
[ClaimsAuthorize(FhtUserClaimType.RenewalOrder)]
[ResponseType(typeof(QueryResult))]
[Route("_SearchAuditReturnOrder")]
public async Task SearchRenewalOrderAsync(int? clientId = null, long? orderId = null, int? packageId = null,
OrderStatus? orderStatus = null, DateTime? beginTime = null, DateTime? endTime = null, BuyOrderType? buyOrderType = null,
int currPage = 1, int limit = 20)
{
int authenticatedUserId = User.Identity.GetUserId();
int offset = (currPage < 2 ? 0 : currPage - 1) * limit;
QueryResult orders = await _orderQuery.GetOrdersAsync(clientId, orderId, packageId,orderStatus, beginTime, endTime, User.UserRole(), authenticatedUserId,limit, offset, buyOrderType);
return Ok(orders);
}
[HttpGet]
[Route("{orderId:long:min(1)}")]
[ResponseType(typeof(OrderInputModel))]
[ApiClaimsAuthorize(FhtUserClaimType.ManageOrders, FhtUserClaimType.CreateOrder, FhtUserClaimType.ViewOrders, FhtUserClaimType.AuditOrder)]
public IHttpActionResult GetOrder(long orderId)
{
OrderInputModel order = _orderService.GetOrder(orderId);
return Ok(order);
}
[HttpGet]
[Route("_getorderdetail")]
[ResponseType(typeof(OrderViewModel))]
[ClaimsAuthorize(FhtUserClaimType.ManageOrders, FhtUserClaimType.ViewOrders, FhtUserClaimType.AuditOrder)]
public async Task GetOrderDetailAsync(long orderId)
{
OrderViewModel order =await _orderQuery.GetOrderAsync(orderId);
return Ok(order);
}
[HttpGet]
[Route("_autoCompleteSearch")]
[ResponseType(typeof(List))]
public async Task AutoCompleteSearchAsync(string q="", long? id=null)
{
int authenticatedUserId = User.Identity.GetUserId();
var data =new List();
UserViewModel user = await _userQuery.GetAsync(authenticatedUserId);
if (id.HasValue)
{
SelectListItemViewModel order = await _orderQuery.GetSelectListItemByIdAsync(id.Value);
data.Add(order);
}
else
{
data = await _orderQuery.SearchSelectListItemAsync(q, user.AgentOrganizationId.Value);
}
return Ok(data);
}
[HttpGet]
[Route("_autoCompleteSearchByAdvRevise")]
[ResponseType(typeof(List))]
public async Task AutoCompleteSearchByAdvReviseAsync(string q="", long? id=null)
{
int authenticatedUserId = User.Identity.GetUserId();
var data = new List();
UserViewModel user = await _userQuery.GetAsync(authenticatedUserId);
if (id.HasValue)
{
SelectListItemViewModel order = await _orderQuery.GetSelectListItemByIdAsync(id.Value);
data.Add(order);
}
else
{
data = await _orderQuery.SearchSelectListItemByAdvReviseAsync(q, user.AgentOrganizationId.Value);
}
return Ok(data);
}
[HttpGet]
[Route("_getChartData")]
[ResponseType(typeof(HomeChartViewModel))]
public async Task GetChartDataAsync()
{
HomeChartViewModel res = await _orderQuery.GetOrderChartAsync();
return Ok(res);
}
#endregion
[HttpPost]
[Route("_create")]
[ResponseType(typeof(long))]
[ValidateModel]
[ApiClaimsAuthorize(FhtUserClaimType.CreateOrder)]
public async Task< IHttpActionResult> CreateOrderAsync(OrderInputModel orderInputModel)
{
int authenticatedUserId = User.Identity.GetUserId();
long orderId =await _orderService.CreateOrderAsync(authenticatedUserId, orderInputModel);
return Ok(orderId);
}
[HttpPost]
[Route("_modify")]
[ResponseType(typeof(void))]
[ValidateModel]
[ApiClaimsAuthorize(FhtUserClaimType.CreateOrder)]
public async Task< IHttpActionResult> EditOrderAsync( OrderInputModel orderInputModel)
{
int authenticatedUserId = User.Identity.GetUserId();
await _orderService.EditOrderAsync(authenticatedUserId, orderInputModel);
return Ok();
}
///
/// 检索代理商客户
///
///
///
[HttpGet]
[ResponseType(typeof(List))]
[Route("AutoCompleteClientCompanyName")]
[ApiClaimsAuthorize(FhtUserClaimType.AgentManagement, FhtUserClaimType.CreateOrder, FhtUserClaimType.ManageOrders)]
public async Task AutoCompleteClientCompanyNameAsync(string q = null, int? clientCompanyId = null)
{
if (q == null)
{
q = "";
}
int currentUserId = User.Identity.GetUserId();
UserViewModel user =await _userQuery.GetAsync(currentUserId);
if (!user.AgentOrganizationId.HasValue)
{
throw new ArgumentException("只有代理商才能购买订单!");
}
if (clientCompanyId.HasValue)
{
SelectListItemViewModel company =await _agentClientQuery.GetAgentClientByIdAsync(clientCompanyId.Value);
return Ok(new List {
new AgentClientCompanyViewModel
{
CompanyId= int.Parse(company.Value.ToString()),
CompanyName = company.Text,
}
});
}
List companyList =await _agentClientQuery.SearchCompaniesAsync(user.AgentOrganizationId.Value, q);
return Ok(companyList);
}
///
///
///
///
///
///
[HttpGet]
[ResponseType(typeof(Dictionary))]
[Route("GetSingleFeatureName")]
[ApiClaimsAuthorize(FhtUserClaimType.AgentManagement, FhtUserClaimType.CreateOrder)]
public async Task< IHttpActionResult> GetSingleFeatureNameAsync(FeatureType featureType)
{
if (FeatureHelp.GetExcludedSingleSellFeatureTypes().Contains(featureType))
{
throw new ArgumentOutOfRangeException("不能获取当前类型的功能服务");
}
Dictionary singleFeatureNames =await _featureQuery.GetSingleFeatureNamesAsync(featureType);
Dictionary jsonResult = singleFeatureNames.AsEnumerable().Select(s => new { key = s.Key, value = s.Value }).ToDictionary(k=>k.key,v=>v.value);
return Ok(jsonResult);
}
[HttpGet]
[ResponseType(typeof(FeatureViewModel))]
[Route("GetSingleFeatureContent/{featureDefinitionId:long:min(1)}")]
[ApiClaimsAuthorize(FhtUserClaimType.AgentManagement, FhtUserClaimType.CreateOrder)]
public async Task GetSingleFeatureContentAsync(long featureDefinitionId)
{
object jsonData;
FeatureViewModel featureDefinition =await _featureQuery.GetFeatureDefinitionAsync(featureDefinitionId);
if (featureDefinition.FeatureDefinition.FeatureType == FeatureType.SMS ||
featureDefinition.FeatureDefinition.FeatureType == FeatureType.Member ||
featureDefinition.FeatureDefinition.FeatureType == FeatureType.Advertisement)
{
return Ok(featureDefinition);
}
else
{
throw new ArgumentOutOfRangeException("没有找到对应的功能服务内容!");
}
}
[HttpGet]
[ResponseType(typeof(int))]
[Route("_agentDiscount")]
[ApiClaimsAuthorize(FhtUserClaimType.AgentManagement, FhtUserClaimType.CreateOrder)]
public async Task GetAgentOrderDiscountAsync()
{
int authenticatedUserId = User.Identity.GetUserId();
int discount =await _orderService.GetAgentOrderDiscountAsync(authenticatedUserId);
return Ok(discount);
}
[HttpGet]
[ResponseType(typeof(List))]
[Route("GetAdvertisementBoothNumbers")]
[ApiClaimsAuthorize(FhtUserClaimType.AgentManagement, FhtUserClaimType.CreateOrder)]
public async Task GetAdvertisementBoothNumbersAsync(long featureDefinitionId, string keyword=null)
{
try
{
List BoothNumberInfoOfSold =await _orderService.GetBoothNumberInfoOfSoldAsync(keyword, featureDefinitionId, null);
List boothNumbers = BoothNumberInfoOfSold.Where(u => !u.IsSell).
Select(s => s.BoothNumber).OrderBy(o => o).ToList();
return Ok(boothNumbers);
}catch (Exception ex)
{
throw;
}
}
[HttpGet]
[ResponseType(typeof(List))]
[Route("AutocompletionKeyword")]
[ApiClaimsAuthorize(FhtUserClaimType.AgentManagement, FhtUserClaimType.CreateOrder)]
public async Task< IHttpActionResult> AutoCompleteKeywordAsync(string q)
{
List keywords =(await _keywordQuery.AutoCompleteAsync(q)).Select(s => s.Name).ToList();
keywords.Remove(q);
keywords.Insert(0, q);
return Ok(keywords);
}
#region 支付 、审核 、退单
[ClaimsAuthorize(FhtUserClaimType.CreateOrder)]
[Route("_pay")]
[HttpPost]
[ResponseType(typeof(void))]
public void PayOrder(long orderId)
{
int authenticatedUserId = User.Identity.GetUserId();
_orderService.PayOrder(authenticatedUserId, orderId);
}
[HttpPost]
[ClaimsAuthorize(FhtUserClaimType.AuditOrder)]
[ResponseType(typeof(void))]
[Route("_audit")]
[ValidateModel]
public async Task AuditAsync(AuditDetailInputModel auditDetail)
{
int authenticatedUserId = User.Identity.GetUserId();
UserViewModel user = await _userQuery.GetAsync(authenticatedUserId);
if (auditDetail.AuditAction == AuditAction.Accept)
{
_orderService.AuditAcceptOrder(authenticatedUserId, auditDetail);
}
else if (auditDetail.AuditAction == AuditAction.Reject)
{
_orderService.AuditRejectOrder(authenticatedUserId, auditDetail);
}
return Ok();
}
[ResponseType(typeof(Tuple))]
[Route("_statisticsordernum")]
[ClaimsAuthorize(FhtUserClaimType.ManageOrders)]
public async Task StatisticsOrderNumAsync()
{
int authenticatedUserId = User.Identity.GetUserId();
UserViewModel user = await _userQuery.GetAsync(authenticatedUserId);
Tuple tup = await _orderQuery.StatisticsOrderNumAsync(user.AgentOrganizationId.Value);
return Ok(tup);
}
[ResponseType(typeof(List))]
[Route("_initialization")]
public IHttpActionResult Initialization(BuyOrderType? type = null)
{
var buyServiceTypes = new List();
EnumHelpers.Enum2Dictionary().OrderBy(m => m.Key).ToList().ForEach(et =>
{
buyServiceTypes.Add(new SelectListItemViewModel
{
Value = ((int)et.Key),
Text = et.Key.GetEnumDescription(),
Selected = (int)et.Key == (type.HasValue ? (int)type : (int)BuyOrderType.Package)
});
});
return Ok(buyServiceTypes);
}
[HttpPost]
[ClaimsAuthorize(FhtUserClaimType.ReturnOrder)]
[ResponseType(typeof(void))]
[Route("_return")]
[ValidateModel]
public IHttpActionResult ReturnOrder(ReturnOrderInputModel model)
{
int authenticatedUserId = User.Identity.GetUserId();
_orderService.ReturnOrder(authenticatedUserId, model);
return Ok();
}
[HttpPost]
[ClaimsAuthorize(FhtUserClaimType.AuditReturnOrder)]
[ResponseType(typeof(void))]
[Route("_auditReturn")]
[ValidateModel]
public IHttpActionResult AuditReturnOrder(ReturnOrderInputModel model)
{
int authenticatedUserId = User.Identity.GetUserId();
_orderService.AuditReturnOrder(authenticatedUserId, model);
return Ok();
}
#endregion
}
}