package com.ruoyi.web.controller.api;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.alipay.api.AlipayApiException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.basic.PageInfo;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.WebUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.query.AppUserQuery;
import com.ruoyi.system.query.OrderQuery;
import com.ruoyi.system.service.*;
import com.ruoyi.web.controller.query.CourseQuery;
import com.ruoyi.web.controller.query.InformationQuery;
import com.ruoyi.web.controller.tool.PayMoneyUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.security.core.parameters.P;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
*
* 订单表 前端控制器
*
*
* @author luodangjia
* @since 2024-09-19
*/
@RestController
@RequestMapping("/t-order")
public class TOrderController {
@Resource
private TOrderService orderService;
@Resource
private TCourseService courseService;
@Resource
private TRegionService regionService;
@Resource
private TTechnicalTitleService tTechnicalTitleService;
@Resource
private TTitleMajorService majorService;
@Resource
private TGeneratedRecordsService generatedRecordsService;
@Resource
private TInformationService informationService;
@Resource
private PayMoneyUtil payMoneyUtil;
@Resource
private TokenService tokenService;
@Resource
private TAppUserService appUserService;
//查询
@ApiOperation(value = "查询",tags = "后台-订单管理")
@PostMapping("/pageList")
public R> pageList(@RequestBody OrderQuery orderQuery){
return R.ok(orderService.pageList(orderQuery));
}
@ApiOperation(value = "删除",tags = "web-订单管理")
@PostMapping("/delete")
public R delete(@RequestParam Long orderId){
orderService.removeById(orderId);
return R.ok();
}
@ApiOperation(value = "订单查询",tags = "web-个人中心")
@PostMapping("/web/pageList")
public R> webpageList(@RequestBody OrderQuery orderQuery){
Long userId = tokenService.getLoginUser().getUserId();
orderQuery.setUserId(userId);
return R.ok(orderService.pageList(orderQuery));
}
@ApiOperation(value = "计数",tags = "后台-订单管理")
@PostMapping("/count")
public R>> count(@RequestBody OrderQuery orderQuery){
return R.ok(orderService.totalCount(orderQuery));
}
@ApiOperation(value = "取消订单",tags = {"后台-订单管理","web-个人中心"})
@PostMapping("/cancel")
public R cancel(Long orderId){
TOrder order = orderService.getById(orderId);
if (order.getPaymentStatus()!=1){
return R.fail("订单状态异常");
}
order.setPaymentStatus(3);
order.setCancelTime(LocalDateTime.now());
orderService.updateById(order);
return R.ok();
}
@ApiOperation(value = "课程详情",tags = "后台-订单管理")
@PostMapping(value = "/course/detail")
public R list(Long id,Long goodId) {
TCourse course = courseService.getById(goodId);
TRegion region = regionService.getById(course.getRegionId());
course.setRegionName(region.getProvinceName()+"-"+region.getName());
TTechnicalTitle tTechnicalTitle = tTechnicalTitleService.getById(course.getTechnicalId() );
TTitleMajor titleMajor = majorService.getById(course.getMajorId());
course.setTechnicalName(tTechnicalTitle.getTitileName()+"-"+titleMajor.getMajorName());
TOrder order = orderService.getById(id);
TAppUser appUser = appUserService.getById(order.getUserId());
order.setName(appUser.getName());
order.setPhone(appUser.getPhone());
order.setAvatar(appUser.getAvatar());
course.setOrder(order);
Long count = orderService.lambdaQuery()
.eq(TOrder::getGoodId, course.getId())
.eq(TOrder::getGoodType, 1)
.eq(TOrder::getPaymentStatus, 2).count();
course.setBuyNum(Integer.parseInt(String.valueOf(count)));
return R.ok(course);
}
@ApiOperation(value = "工作总结详情",tags = "后台-订单管理")
@PostMapping("/generate/detail")
public R detail(Long id){
TOrder order = orderService.getById(id);
TGeneratedRecords tGeneratedRecords = generatedRecordsService.lambdaQuery().eq(TGeneratedRecords::getUserId, order.getUserId()).orderByDesc(TGeneratedRecords::getCreateTime).last("limit 1").one();
if (tGeneratedRecords == null) {
TGeneratedRecords generatedRecords = new TGeneratedRecords();
generatedRecords.setOrder(order);
return R.ok(generatedRecords);
}
TOrder tOrder = orderService.getById(id);
TAppUser appUser = appUserService.getById(tOrder.getUserId());
tOrder.setName(appUser.getName());
tOrder.setPhone(appUser.getPhone());
tOrder.setAvatar(appUser.getAvatar());
tGeneratedRecords.setOrder(tOrder);
return R.ok(tGeneratedRecords);
}
@ApiOperation(value = "资料详情",tags = "后台-订单管理")
@PostMapping(value = "/order/information/detail")
public R informationR1(Long goodId,Long id) {
TInformation record = informationService.getById(goodId);
TRegion region = regionService.getById(record.getRegionId());
record.setRegionName(region.getProvinceName()+"-"+region.getName());
TTechnicalTitle technicalTitle = tTechnicalTitleService.getById(record.getTechnicalId());
TTitleMajor titleMajor = majorService.getById(record.getMajorId());
record.setTechnicalName(technicalTitle.getTitileName()+"-"+titleMajor.getMajorName());
TOrder order = orderService.getById(id);
TAppUser appUser = appUserService.getById(order.getUserId());
order.setName(appUser.getName());
order.setPhone(appUser.getPhone());
order.setAvatar(appUser.getAvatar());
record.setOrder(order);
return R.ok(record);
}
@ApiOperation(value = "资料详情",tags = "后台-资料管理")
@PostMapping(value = "/information/detail")
public R informationR(Long goodId) {
TInformation record = informationService.getById(goodId);
TRegion region = regionService.getById(record.getRegionId());
record.setRegionName(region.getProvinceName()+"-"+region.getName());
TTechnicalTitle technicalTitle = tTechnicalTitleService.getById(record.getTechnicalId());
TTitleMajor tTitleMajor = majorService.getById(record.getMajorId());
record.setTechnicalName(technicalTitle.getTitileName()+"-"+tTitleMajor.getMajorName());
return R.ok(record);
}
@ApiOperation(value = "退款",tags = "后台-资料管理")
@PostMapping(value = "/refund")
public R refund(Long orderId) throws AlipayApiException {
TOrder order = orderService.getById(orderId);
if (order.getPaymentStatus()!=2){
return R.fail("订单状态异常");
}
if (order.getPaymentType()==1){
payMoneyUtil.wxRefund(order.getSerialNumber(), order.getCode(),order.getPaymentAmount().toString(), order.getPaymentAmount().toString(),"/");
}
if (order.getPaymentType()==2){
Map stringStringMap = payMoneyUtil.aliRefund(order.getCode(), order.getPaymentAmount().toString());
if ("10000".equals(stringStringMap.get("code"))){
order.setPaymentStatus(4);
orderService.updateById(order);
}
}
return R.ok();
}
@ApiOperation(value = "导出",tags = "后台-订单管理")
@PostMapping(value = "/export")
public void mealGeneratorExport(@RequestBody OrderQuery orderQuery) {
int pageSize = 100;
int currentPage = 0;
List page = new ArrayList<>();
while (true) {
orderQuery.setPageNum(currentPage * pageSize);
orderQuery.setPageSize(pageSize);
PageInfo tOrderPageInfo = orderService.pageList(orderQuery);
List records = tOrderPageInfo.getRecords();
if (CollectionUtils.isEmpty(records)) {
break; // 没有更多数据,退出循环
}
page.addAll(records); // 累加当前页数据
currentPage++; // 进入下一页
}
List courses = courseService.lambdaQuery().list();
List informations = informationService.lambdaQuery().list();
for (TOrder record : page) {
record.getFormattedCreateTime();
if (record.getGoodType()==1){
TCourse course = courses.stream().filter(e -> e.getId().equals(record.getGoodId())).findFirst().orElse(null);
record.setGoodName(course.getCourseName());
}else if (record.getGoodType()==2){
TInformation information = informations.stream().filter(e -> e.getId().equals(record.getGoodId())).findFirst().orElse(null);
record.setGoodName(information.getInformationName());
}else {
record.setGoodName("工作总结订单");
}
}
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), TOrder.class,page);
HttpServletResponse response = WebUtils.response();
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
ServletOutputStream outputStream = null;
try {
String fileName = URLEncoder.encode("用户列表.xls", "utf-8");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
outputStream = response.getOutputStream();
workbook.write(outputStream);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}