package com.ruoyi.account.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.account.api.model.UserPoint;
import com.ruoyi.account.mapper.UserPointMapper;
import com.ruoyi.account.service.UserPointService;
import com.ruoyi.account.vo.*;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.page.PageInfo;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.order.feignClient.OrderClient;
import com.ruoyi.order.model.Order;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.YearMonth;
import java.util.Collection;
import java.util.List;
import java.util.Map;
/**
*
* 前端控制器
*
*
* @author luodangjia
* @since 2024-11-21
*/
@RestController
@RequestMapping("/user-point")
@Api(tags = "个人积分")
public class UserPointController extends BaseController {
@Resource
private UserPointService userPointService;
@Resource
private OrderClient orderClient;
@Resource
private UserPointMapper userPointMapper;
/**
* 获取个人积分
*/
@GetMapping("/getUserPoint")
@ApiOperation("获取个人积分")
public R getUserPoint() {
return R.ok(userPointService.getUserPoint(SecurityUtils.getUserId()));
}
/**
* 获取变更明细
*/
@GetMapping("/getUserPointDetail")
@ApiOperation("获取变更明细")
public R> getUserPointDetail(@ApiParam("指定日期") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date,
@ApiParam("变动类型(4=兑换商品,12=他人赠送,13=赠与他人,16=取消订单,17=充值 )") Integer type, Integer pageCurr, Integer pageSize) {
LocalDateTime startTime = null;
LocalDateTime endTime = null;
if (date != null) {
// 将 createTime 设置为当天的开始时间 (00:00)
startTime = date.atStartOfDay();
// 使用 YearMonth 来获取该月的最后一天
YearMonth yearMonth = YearMonth.from(date);
LocalDate lastDayOfMonth = yearMonth.atEndOfMonth();
// 将最后一天转换为 LocalDateTime,并设置为当天的最后一秒 (23:59:59.999)
endTime = lastDayOfMonth.atTime(LocalTime.MAX);
}
PageInfo userPointDetail = userPointService.getUserPointDetail(SecurityUtils.getUserId(), startTime, endTime, type, pageCurr, pageSize);
return R.ok(userPointDetail);
}
/**
* 转赠积分
*/
@PostMapping("/transferPoint")
@ApiOperation("转赠积分")
public R transferPoint(@RequestBody TransferPoint transferPoint) {
return userPointService.transferPoint(transferPoint.getPoint(), transferPoint.getPhone());
}
/**
* 保存积分流水记录
*
* @param userPoint
* @return
*/
@PostMapping("/saveUserPoint")
public R saveUserPoint(@RequestBody UserPoint userPoint) {
userPointService.save(userPoint);
return R.ok();
}
/**
* 删除用户积分流水
*/
@DeleteMapping("/deleteUserPoint")
public R deleteUserPoint(@RequestParam("orderId") Long orderId) {
userPointService.remove(new LambdaQueryWrapper().eq(UserPoint::getObjectId, orderId));
return R.ok();
}
/**
* 积分统计
*/
@GetMapping("/statistics")
@ApiOperation(value = "积分统计", tags = "管理后台-财务统计-用户积分统计")
public R statistics(UserPoint userPoint) {
return R.ok(userPointService.getStatistics(userPoint));
}
/**
* 变更记录
*/
@GetMapping("/list")
@ApiOperation(value = "积分变更记录", tags = "管理后台-财务统计-用户积分统计")
public R> list(UserPoint userPoint) {
IPage userPointPage = userPointService.getUserPointPage(Page.of(userPoint.getPageNum(), userPoint.getPageSize()), userPoint);
return R.ok(userPointPage);
}
@GetMapping("/user/list")
@ApiOperation(value = "积分管理-用户积分明细(必传用户id)", tags = "后台")
public R> userlist(UserPoint userPoint) {
Page page = userPointService.lambdaQuery()
.eq(userPoint.getType()!=null,UserPoint::getType, userPoint.getType())
.eq(UserPoint::getAppUserId, userPoint.getAppUserId())
.orderByDesc(UserPoint::getCreateTime)
.page(Page.of(userPoint.getPageNum(), userPoint.getPageSize()));
for (UserPoint record : page.getRecords()) {
if (record.getType()==1 || record.getType()==11){
Order data = orderClient.getOrderById(record.getObjectId()).getData();
if (data!=null){
record.setExtention(data.getOrderNumber());
}
}
int i = record.getHistoricalPoint() - record.getBalance();
if (i>0){
record.setVariableType(2);
}else if (i<0){
record.setVariableType(1);
}else{
record.setVariableType(0);
}
}
return R.ok(page);
}
/**
* 导出
*/
@GetMapping("/export")
@ApiOperation(value = "积分变更记录导出", tags = "管理后台-财务统计-用户积分统计")
public void export(HttpServletResponse response, UserPoint userPoint) {
IPage userPointPage = userPointService.getUserPointPage(Page.of(1, Integer.MAX_VALUE), userPoint);
List userPointList = userPointPage.getRecords();
ExcelUtil util = new ExcelUtil<>(UserPoint.class);
util.exportExcel(response, userPointList, "用户积分统计");
}
/**
* 获取积分变动记录
* @param userPoint
* @return
*/
@PostMapping("/getUserPointList")
public R> getUserPointList(@RequestBody UserPoint userPoint){
LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
if(null != userPoint.getType()){
queryWrapper.eq(UserPoint::getType, userPoint.getType());
}
if(null != userPoint.getObjectId()){
queryWrapper.eq(UserPoint::getObjectId, userPoint.getObjectId());
}
if(null != userPoint.getAppUserId()){
queryWrapper.eq(UserPoint::getAppUserId, userPoint.getAppUserId());
}
List list = userPointService.list(queryWrapper);
return R.ok(list);
}
/**
* 获取用户积分变更详情
*/
@GetMapping("/getUserPontDetailPageList")
@ApiOperation(value = "用户积分详情", tags = "管理后台-财务统计-用户积分统计")
public R> getUserPontDetailPageList(@RequestParam(value = "types",required = false) Collection types,
@RequestParam(value = "objectIdStr",required = false) String objectIdStr,
@RequestParam(value = "id") Long id,
@ApiParam("当前页")@RequestParam("pageCurr") Integer pageCurr,
@ApiParam("分页大小")@RequestParam("pageSize") Integer pageSize) {
PageInfo pageInfo=userPointService.getUserPontDetailPageList(types, objectIdStr,id,pageCurr,pageSize);
return R.ok(pageInfo);
}
/**
* 用户积分统计
*/
@GetMapping("/getUserPointStatisticsPageList")
@ApiOperation(value = "用户积分统计", tags = "管理后台-财务统计-用户积分统计")
public R> getUserPointStatisticsPageList(@RequestParam(value = "types",required = false) Collection types,
@RequestParam(value = "name",required = false) String name,
@RequestParam(value = "phone",required = false) String phone,
@RequestParam(value = "beginTime",required = false)@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime beginTime,
@RequestParam(value = "endTime",required = false)@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime,
@ApiParam("当前页")@RequestParam("pageCurr") Integer pageCurr,
@ApiParam("分页大小")@RequestParam("pageSize") Integer pageSize) {
//查找记录
PageInfo pageInfo=userPointService.getUserPointStatisticsPageList(types,name,phone,beginTime,endTime,pageCurr,pageSize);
return R.ok(pageInfo);
}
/**
* 用户积分统计
*/
@GetMapping("/getUserPointStatisticsTop")
@ApiOperation(value = "用户积分统计-顶部", tags = "管理后台-财务统计-用户积分统计")
public R getUserPointStatisticsTop(@RequestParam(value = "types",required = false) Collection types,
@RequestParam(value = "name",required = false) String name,
@RequestParam(value = "phone",required = false) String phone,
@RequestParam(value = "beginTime",required = false)@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime beginTime,
@RequestParam(value = "endTime",required = false)@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime
) {
UserPointStatisticsTopVO userPointStatisticsOutVO = new UserPointStatisticsTopVO();
//充值绿电分
Integer chargePoint=userPointService.selectRechargeAndUse(name,phone,beginTime,endTime,17);
// 消费绿电分
Integer exchangePoint =userPointService.selectRechargeAndUse(name,phone,beginTime,endTime,4);
Integer cancelPoint = userPointService.selectRechargeAndUse(name,phone,beginTime,endTime,16);
Integer usePoint=(exchangePoint==null?0:exchangePoint )-( cancelPoint==null?0:cancelPoint);
userPointStatisticsOutVO.setChargeTotalPoint(chargePoint==null?0:chargePoint);
userPointStatisticsOutVO.setUseTotalPoint(usePoint);
return R.ok(userPointStatisticsOutVO);
}
/**
* 导出店铺余额列表
*/
@GetMapping("/userPointExcel")
@ApiOperation(value = "导出", tags = {"管理后台-财务统计-用户积分统计"})
void userPointExcel(HttpServletResponse response,
@RequestParam(value = "types",required = false) Collection types,
@RequestParam(value = "name",required = false) String name,
@RequestParam(value = "phone",required = false) String phone,
@RequestParam(value = "beginTime",required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime beginTime,
@RequestParam(value = "endTime",required = false)@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime){
List exportList =userPointMapper.userPointExcel(types,name,phone,beginTime,endTime);
exportList.forEach(x->{
if (x.getType()==12||x.getType()==17){
x.setVariablePointStr("+"+x.getVariablePoint()+"绿电分");
}else {
x.setVariablePointStr("-"+x.getVariablePoint()+"绿电分");
}
});
ExcelUtil util = new ExcelUtil(UserPointExcel.class);
util.exportExcel(response, exportList, "店铺余额列表数据");
}
@PostMapping("/save")
R save(@RequestBody UserPoint userPoint){
userPointService.save(userPoint);
return R.ok();
}
}