package com.ruoyi.study.controller;
import com.alipay.api.AlipayApiException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.constant.RedisConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.exception.GlobalException;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.PageInfo;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.common.security.auth.AuthUtil;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.management.api.feignClient.ManagementClient;
import com.ruoyi.management.api.model.*;
import com.ruoyi.management.api.query.UseGuideQuery;
import com.ruoyi.study.domain.*;
import com.ruoyi.study.dto.AppUserQuery;
import com.ruoyi.study.dto.UserInfoQuery;
import com.ruoyi.study.request.RegisterPhoneRequest;
import com.ruoyi.study.service.*;
import com.ruoyi.study.utils.PayMoneyUtil;
import com.ruoyi.study.utils.UUIDUtil;
import com.ruoyi.study.vo.*;
import com.ruoyi.system.api.model.LoginUserParent;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.*;
/**
*
* 用户管理 控制器
*
*
* @author 无关风月
* @since 2024-04-26
*/
@RestController
@RequestMapping("/base/user")
public class TUserController {
@Autowired
private ITUserService userService;
@Autowired
private IVipOrderService vipOrderService;
@Autowired
private ManagementClient managementClient;
@Autowired
private TokenService tokenService;
@Resource
private RedisService redisService;
@Resource
private ITUserShareService userShareService;
@Autowired
private ITIntegralRecordService integralRecordService;
@PostMapping("/getUserById/{id}")
@ApiOperation(value = "根据用户id 获取用户信息", tags = {"管理后台-用户管理"})
public R getUserById(@PathVariable("id") Integer id) {
TUser byId = userService.getById(id);
return R.ok(byId);
}
@PostMapping("/vipInfoStudy")
@ApiOperation(value = "会员中心-获取会员说明、当前登录用户是否为会员、会员购买规格", tags = {"学习端-个人中心"})
public R> vipInfoStudy() {
if (tokenService.getLoginUserStudy() == null) {
throw new GlobalException("登录失效!");
}
List vipInfoVOS = new ArrayList<>();
List data = managementClient.getVipSet1().getData();
for (TVipSet datum : data) {
VipInfoVO vipInfoVO = new VipInfoVO();
vipInfoVO.setInfo(datum.getInfo());
vipInfoVO.setId(datum.getId());
Integer userid = tokenService.getLoginUserStudy().getUserid();
TUser byId = userService.getById(userid);
// 先判断vipEndTime
if (byId.getVipEndTime() == null) {
vipInfoVO.setIsVip(0);
} else {
// 判断会员到期时间是否大于当前时间
if (byId.getVipEndTime().getTime() > System.currentTimeMillis()) {
vipInfoVO.setIsVip(1);
} else {
vipInfoVO.setIsVip(0);
}
}
vipInfoVO.setTime(datum.getTime());
vipInfoVO.setAmount(datum.getAmount());
vipInfoVOS.add(vipInfoVO);
}
return R.ok(vipInfoVOS);
}
@PostMapping("/vipInfo")
@ApiOperation(value = "会员中心-获取会员说明、当前登录用户是否为会员、会员购买规格", tags = {"家长端-个人中心"})
public R> vipInfo() {
if (tokenService.getLoginUser1() == null) {
throw new GlobalException("登录失效!");
}
List vipInfoVOS = new ArrayList<>();
List data = managementClient.getVipSet1().getData();
for (TVipSet datum : data) {
VipInfoVO vipInfoVO = new VipInfoVO();
vipInfoVO.setInfo(datum.getInfo());
vipInfoVO.setId(datum.getId());
Integer userid = tokenService.getLoginUser1().getUserid();
TUser byId = userService.getById(userid);
// 先判断vipEndTime
if (byId.getVipEndTime() == null) {
vipInfoVO.setIsVip(0);
} else {
// 判断会员到期时间是否大于当前时间
if (byId.getVipEndTime().getTime() > new Date().getTime()) {
vipInfoVO.setIsVip(1);
} else {
vipInfoVO.setIsVip(0);
}
}
vipInfoVO.setTime(datum.getTime());
vipInfoVO.setAmount(datum.getAmount());
vipInfoVOS.add(vipInfoVO);
}
return R.ok(vipInfoVOS);
}
@Autowired
private PayMoneyUtil payMoneyUtil;
@PostMapping("/pay")
@ApiOperation(value = "购买会员支付操作", tags = {"家长端-个人中心"})
@ApiImplicitParams({
@ApiImplicitParam(name = "Authorization", value = "Bearer eyJhbGciOiJIUzUxMiJ....", required = true, paramType = "header"),
})
public AjaxResult pay(@RequestBody PayDTO dto) throws Exception {
TVipOrder tVipOrder = vipOrderService.getById(dto.getOrderId());
switch (dto.getPayType()) {
case 1:
return payMoneyUtil.weixinpay
("购买会员", "",
dto.getId() + "_" + tVipOrder.getId() + "_" +
UUIDUtil.getRandomCode(8),
tVipOrder.getMoney().toString(),
"/base/user/wxPayBuyVip", "APP", "");
case 2:
return payMoneyUtil.alipay
("购买会员",
"购买会员下单支付",
"",
dto.getId() + "_" + tVipOrder.getId() + "_" +
UUIDUtil.getRandomCode(8),
tVipOrder.getMoney().toString(),
"/base/user/aliPayBuyVip");
}
return AjaxResult.success();
}
@PostMapping("/order")
@ApiOperation(value = "购买会员下单操作", tags = {"家长端-个人中心"})
@ApiImplicitParams({
@ApiImplicitParam(name = "Authorization", value = "Bearer eyJhbGciOiJIUzUxMiJ....", required = true, paramType = "header"),
@ApiImplicitParam(name = "payType", value = "支付类型 1=微信 2=支付宝", required = true),
@ApiImplicitParam(name = "id", value = "会员规格id", required = true),
})
public R order(Integer payType, Integer id) throws Exception {
if (tokenService.getLoginUser1() == null) {
return R.tokenError("登录失效");
}
Integer userid = tokenService.getLoginUser1().getUserid();
TVipOrder tVipOrder = new TVipOrder();
List data = managementClient.getVipSet1().getData();
Integer time = 0;
for (TVipSet datum : data) {
if (datum.getId() == id) {
tVipOrder.setMoney(datum.getAmount());
time = datum.getTime();
}
}
tVipOrder.setPayState(1);
tVipOrder.setUserId(userid);
tVipOrder.setPayType(payType);
tVipOrder.setCount(time);
vipOrderService.save(tVipOrder);
PayVO payVO = new PayVO();
payVO.setOrderId(tVipOrder.getId());
payVO.setId(id);
return R.ok(payVO);
}
@ResponseBody
@PostMapping("/aliPayBuyVip")
public void addVipPaymentAliCallback(HttpServletRequest request, HttpServletResponse response) {
try {
System.err.println("进入支付宝回调");
Map map = payMoneyUtil.alipayCallback(request);
System.err.println("返回参数" + map);
if (null != map) {
PrintWriter out = response.getWriter();
out.write("success");
out.flush();
out.close();
String out_trade_no = map.get("out_trade_no");
String trade_no = map.get("trade_no");
// 会员规格id
String s = out_trade_no.split("_")[0];
Integer integer = Integer.valueOf(s);
Integer time = 0;
for (TVipSet datum : managementClient.getVipSet1().getData()) {
if (datum.getId() == integer) {
time = datum.getTime();
}
}
// 订单id
String s1 = out_trade_no.split("_")[1];
Integer integer1 = Integer.valueOf(s1);
TVipOrder byId = vipOrderService.getById(integer1);
if (byId.getBackTime() != null) {
return;
}
byId.setPayState(2);
byId.setTransactionId(trade_no);
byId.setOutTradeNo(out_trade_no);
byId.setPayTime(new Date());
TUser byId1 = userService.getById(byId.getUserId());
if (byId1.getVipPayTime() == null) {
// 是否是首次充值会员
byId1.setVipPayTime(new Date());
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.MONTH, time);
Date dateAfterOneMonth = calendar.getTime();
dateAfterOneMonth.setHours(23);
dateAfterOneMonth.setMinutes(59);
dateAfterOneMonth.setSeconds(59);
byId1.setVipEndTime(dateAfterOneMonth);
byId.setTime(dateAfterOneMonth);
} else {
// 不是首次 判断vipEndTime 是否到期 如果没有 加指定月份时间 如果到期了 将会员到期时间从当前增加指定月份
if (byId1.getVipEndTime().getTime() < new Date().getTime()) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.MONTH, time);
Date dateAfterOneMonth = calendar.getTime();
dateAfterOneMonth.setHours(23);
dateAfterOneMonth.setMinutes(59);
dateAfterOneMonth.setSeconds(59);
byId.setTime(dateAfterOneMonth);
byId1.setVipEndTime(dateAfterOneMonth);
} else {
Calendar calendar = Calendar.getInstance();
calendar.setTime(byId1.getVipEndTime());
calendar.add(Calendar.MONTH, time);
Date dateAfterOneMonth = calendar.getTime();
dateAfterOneMonth.setHours(23);
dateAfterOneMonth.setMinutes(59);
dateAfterOneMonth.setSeconds(59);
byId.setTime(dateAfterOneMonth);
byId1.setVipEndTime(dateAfterOneMonth);
}
}
// 修改订单状态
vipOrderService.updateById(byId);
// 修改用户会员续期信息
userService.updateById(byId1);
}
} catch (Exception e) {
e.printStackTrace();
}
}
@ResponseBody
@PostMapping("/wxPayBuyVip")
public void wxPayBuyPackage(HttpServletRequest request, HttpServletResponse response) {
try {
Map map = payMoneyUtil.weixinpayCallback(request);
if (null != map) {
// 内部订单号
String out_trade_no = map.get("out_trade_no");
// 微信订单号
String transaction_id = map.get("transaction_id");
String result = map.get("result");
PrintWriter out = response.getWriter();
out.write(result);
out.flush();
out.close();
// 会员规格id
String s = out_trade_no.split("_")[0];
Integer integer = Integer.valueOf(s);
Integer time = 0;
for (TVipSet datum : managementClient.getVipSet1().getData()) {
if (datum.getId() == integer) {
time = datum.getTime();
}
}
// 订单id
String s1 = out_trade_no.split("_")[1];
Integer integer1 = Integer.valueOf(s1);
TVipOrder byId = vipOrderService.getById(integer1);
byId.setPayState(2);
byId.setTransactionId(transaction_id);
byId.setOutTradeNo(out_trade_no);
byId.setPayTime(new Date());
TUser byId1 = userService.getById(byId.getUserId());
if (byId1.getVipPayTime() == null) {
// 是否是首次充值会员
byId1.setVipPayTime(new Date());
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.MONTH, time);
Date dateAfterOneMonth = calendar.getTime();
dateAfterOneMonth.setHours(23);
dateAfterOneMonth.setMinutes(59);
dateAfterOneMonth.setSeconds(59);
byId.setTime(dateAfterOneMonth);
byId1.setVipEndTime(dateAfterOneMonth);
} else {
// 不是首次 判断vipEndTime 是否到期 如果没有 加指定月份时间 如果到期了 将会员到期时间从当前增加指定月份
if (byId1.getVipEndTime().getTime() < new Date().getTime()) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.MONTH, time);
Date dateAfterOneMonth = calendar.getTime();
dateAfterOneMonth.setHours(23);
dateAfterOneMonth.setMinutes(59);
dateAfterOneMonth.setSeconds(59);
byId.setTime(dateAfterOneMonth);
byId1.setVipEndTime(dateAfterOneMonth);
} else {
Calendar calendar = Calendar.getInstance();
calendar.setTime(byId1.getVipEndTime());
calendar.add(Calendar.MONTH, time);
Date dateAfterOneMonth = calendar.getTime();
dateAfterOneMonth.setHours(23);
dateAfterOneMonth.setMinutes(59);
dateAfterOneMonth.setSeconds(59);
byId.setTime(dateAfterOneMonth);
byId1.setVipEndTime(dateAfterOneMonth);
}
}
// 修改订单状态
vipOrderService.updateById(byId);
// 修改用户会员续期信息
userService.updateById(byId1);
}
} catch (Exception e) {
e.printStackTrace();
}
}
@PostMapping("/getPage")
@ApiOperation(value = "获取注意事项", tags = {"家长端-注意事项"})
public R getPage() {
List data = managementClient.getPage1().getData();
for (TPage datum : data) {
if (datum.getType() == 4) {
return R.ok(datum.getImg());
}
}
return R.ok();
}
@PostMapping("/getUserInfoParent")
@ApiOperation(value = "获取用户信息", tags = {"家长端-获取用户信息"})
public R getUserInfo() {
LoginUserParent loginUser1 = tokenService.getLoginUser1();
if (loginUser1 != null) {
TUser byId = userService.getById(loginUser1.getUserid());
if (byId.getVipEndTime() == null) {
byId.setIsVip(0);
} else if (byId.getVipEndTime().after(new Date())) {
byId.setIsVip(1);
} else {
byId.setIsVip(0);
}
return R.ok(byId);
} else {
return R.tokenError("登录失效");
}
}
@PostMapping("/studyPage")
@ApiOperation(value = "学习端", tags = {"启动页"})
public R studyPage() {
List data = managementClient.getPage1().getData();
for (TPage datum : data) {
if (datum.getType() == 1) {
return R.ok(datum.getImg());
}
}
return R.ok();
}
@PostMapping("/parentPage")
@ApiOperation(value = "平板", tags = {"启动页"})
public R parentPage() {
List data = managementClient.getPage1().getData();
for (TPage datum : data) {
if (datum.getType() == 3) {
return R.ok(datum.getImg());
}
}
return R.ok();
}
@PostMapping("/parentPage1")
@ApiOperation(value = "手机", tags = {"启动页"})
public R parentPage1() {
List data = managementClient.getPage1().getData();
for (TPage datum : data) {
if (datum.getType() == 2) {
return R.ok(datum.getImg());
}
}
return R.ok();
}
@PostMapping("/getProtocol")
@ApiOperation(value = "获取协议", tags = {"协议"})
@ApiImplicitParams({
@ApiImplicitParam(value = "类型 1用户 2隐私 3注销", name = "type", dataType = "string", required = true),
})
public R getProtocol(Integer type) {
String data = managementClient.agreement1(type).getData();
return R.ok(data);
}
@PostMapping("/useGuide")
@ApiOperation(value = "使用指南", tags = {"家长端-使用指南"})
public R> useGuide(@RequestBody UseGuideQuery query) {
PageInfo data = managementClient.useGuide1(query).getData();
for (TUseGuide record : data.getRecords()) {
String plainTextContent = record.getAnswer().replaceAll("\\<.*?\\>", "");
record.setCount(plainTextContent);
}
return R.ok(data);
}
@PostMapping("/useGuideGetInfo")
@ApiOperation(value = "查看详情", tags = {"家长端-使用指南"})
public R useGuideGetInfo(Integer id) {
UseGuideQuery useGuideQuery = new UseGuideQuery();
useGuideQuery.setPageNumber(1);
useGuideQuery.setPageSize(300);
PageInfo data = managementClient.useGuide1(useGuideQuery).getData();
for (TUseGuide record : data.getRecords()) {
if (record.getId() == id) {
return R.ok(record.getAnswer());
}
}
return R.ok();
}
@PostMapping("/feedBack")
@ApiOperation(value = "反馈", tags = {"家长端-意见反馈"})
public R feedBack(@RequestBody TFeedback dto) {
if (tokenService.getLoginUser1() == null) {
return R.tokenError("登录失效");
}
LoginUserParent loginUser1 = tokenService.getLoginUser1();
dto.setUserId(loginUser1.getUserid());
managementClient.addFeedBack(dto);
return R.ok("反馈成功");
}
@PostMapping("/parentLogin")
@ApiOperation(value = "登录", tags = {"家长端-登录"})
@ApiImplicitParams({
@ApiImplicitParam(value = "手机号", name = "phone", dataType = "string", required = true),
@ApiImplicitParam(value = "验证码", name = "phoneCode", dataType = "string", required = true)
})
public R