using Microsoft.Extensions.Caching.Distributed;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Admin.NET.Core.Service;
using Flurl.Http;
namespace Admin.NET.Application.Service;
///
/// vip卡服务
///
[ApiDescriptionSettings(ApplicationConst.ZYGroupName, Order = 900)]
[Route("api/Zyvip")]
public class ZYVipService : IDynamicApiController, ITransient
{
private readonly UserManager _userManager;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly SqlSugarRepository _vipCardInfo;
private readonly SqlSugarRepository _dictdata;
///
///
///
private readonly SqlSugarRepository _wechatUserExtend;
public ZYVipService(UserManager userManager, IHttpContextAccessor httpContextAccessor, SqlSugarRepository wechatUserExtend, SqlSugarRepository vipCardInfo, SqlSugarRepository dictdata)
{
_userManager = userManager;
_httpContextAccessor = httpContextAccessor;
_wechatUserExtend = wechatUserExtend;
_vipCardInfo = vipCardInfo;
_dictdata = dictdata;
}
///
/// 绑定vip
///
///
///
[HttpPost]
public async Task Bind([FromBody] VipBindInput dto)
{
try
{
if (string.IsNullOrWhiteSpace(dto.CardPwd) || string.IsNullOrWhiteSpace(dto.CardCode))
{
throw Oops.Oh("必填信息为空");
}
var cardinfo = await _vipCardInfo.AsQueryable().FirstAsync(e => e.Code == dto.CardCode && e.Pwd == dto.CardPwd);
if (cardinfo == null)
throw Oops.Oh("卡号或密码错误");
if (cardinfo.IsBind == 1)
throw Oops.Oh("卡号已被绑定");
//更新用户拓展信息表状态
if (cardinfo != null)
{
var single = await _wechatUserExtend.GetSingleAsync(e => e.WxId == _userManager.UserId);
single.VipCode = dto.CardCode;
single.IsVIP = true;
single.UpdateTime = DateTime.Now;
single.UpdateUserId = _userManager.UserId;
cardinfo.IsBind = 1;
cardinfo.UpdateTime = DateTime.Now;
cardinfo.UpdateUserId = _userManager.UserId;
cardinfo.UpdateUserName = _userManager.RealName;
await _vipCardInfo.UpdateAsync(cardinfo);
return await _wechatUserExtend.UpdateAsync(single);
}
else
{
throw Oops.Oh("卡号或密码错误");
}
}
catch (Exception ex)
{
throw Oops.Oh("卡号或密码错误");
}
}
///
/// 获取用户VIP绑定信息
///
///
[HttpGet]
public async Task GetBingInfo()
{
var userbindinfo = new UserVipBindDto();
//var single = await _wechatUserExtend.GetFirstAsync(e => e.WxId == _userManager.UserId && e.IsVIP == false);
//if (single == null)
//{
// throw Oops.Oh("用户未绑定");
//}
var cardinfo = await _vipCardInfo.GetFirstAsync(e => e.IsBind == 1 && e.UpdateUserId == _userManager.UserId);
if (cardinfo == null)
{
throw Oops.Oh("用户未绑定");
}
var cardtypeinfo = await _dictdata.GetFirstAsync(e => e.Id == cardinfo.CardTypeId);
userbindinfo.CarTypeName = cardtypeinfo.Name;
userbindinfo.CardCode = cardinfo.Code;
userbindinfo.EndTime = cardinfo.EndTime;
userbindinfo.CreateTime = cardinfo.UpdateTime.Value;
userbindinfo.Money = cardinfo.Money;
userbindinfo.Day = (cardinfo.EndTime - cardinfo.CreateTime.Value).Days;
return userbindinfo;
}
}