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.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 byId = orderService.getById(orderId); if (byId.getPaymentStatus()!=1){ return R.fail("订单状态异常"); } byId.setPaymentStatus(3); byId.setCancelTime(LocalDateTime.now()); orderService.updateById(byId); return R.ok(); } @ApiOperation(value = "课程详情",tags = "后台-订单管理") @PostMapping(value = "/course/detail") public R list(Long id,Long goodId) { TCourse course = courseService.getById(goodId); TRegion byId = regionService.getById(course.getRegionId()); course.setRegionName(byId.getProvinceName()+"-"+byId.getName()); TTechnicalTitle byId1 = tTechnicalTitleService.getById(course.getTechnicalId() ); TTitleMajor byId2 = majorService.getById(course.getMajorId()); course.setTechnicalName(byId1.getTitileName()+"-"+byId2.getMajorName()); TOrder byId3 = orderService.getById(id); TAppUser byId4 = appUserService.getById(byId3.getUserId()); byId3.setName(byId4.getName()); byId3.setPhone(byId4.getPhone()); byId3.setAvatar(byId4.getAvatar()); course.setOrder(byId3); return R.ok(course); } @ApiOperation(value = "工作总结详情",tags = "后台-订单管理") @PostMapping("/generate/detail") public R detail(Long id){ TOrder byId = orderService.getById(id); TGeneratedRecords one = generatedRecordsService.lambdaQuery().eq(TGeneratedRecords::getUserId, byId.getUserId()).orderByDesc(TGeneratedRecords::getCreateTime).last("limit 1").one(); if (one == null) { TGeneratedRecords generatedRecords = new TGeneratedRecords(); generatedRecords.setOrder(byId); return R.ok(generatedRecords); } TOrder byId3 = orderService.getById(id); TAppUser byId4 = appUserService.getById(byId3.getUserId()); byId3.setName(byId4.getName()); byId3.setPhone(byId4.getPhone()); byId3.setAvatar(byId4.getAvatar()); one.setOrder(byId3); return R.ok(one); } @ApiOperation(value = "资料详情",tags = "后台-订单管理") @PostMapping(value = "/order/information/detail") public R informationR1(Long goodId,Long id) { TInformation record = informationService.getById(goodId); TRegion byId = regionService.getById(record.getRegionId()); record.setRegionName(byId.getProvinceName()+"-"+byId.getName()); TTechnicalTitle byId1 = tTechnicalTitleService.getById(record.getTechnicalId()); TTitleMajor byId2 = majorService.getById(record.getMajorId()); record.setTechnicalName(byId1.getTitileName()+"-"+byId2.getMajorName()); TOrder byId3 = orderService.getById(id); TAppUser byId4 = appUserService.getById(byId3.getUserId()); byId3.setName(byId4.getName()); byId3.setPhone(byId4.getPhone()); byId3.setAvatar(byId4.getAvatar()); record.setOrder(byId3); return R.ok(record); } @ApiOperation(value = "资料详情",tags = "后台-资料管理") @PostMapping(value = "/information/detail") public R informationR(Long goodId) { TInformation record = informationService.getById(goodId); TRegion byId = regionService.getById(record.getRegionId()); record.setRegionName(byId.getProvinceName()+"-"+byId.getName()); TTechnicalTitle byId1 = tTechnicalTitleService.getById(record.getTechnicalId()); TTitleMajor byId2 = majorService.getById(record.getMajorId()); record.setTechnicalName(byId1.getTitileName()+"-"+byId2.getMajorName()); return R.ok(record); } @ApiOperation(value = "退款",tags = "后台-资料管理") @PostMapping(value = "/refund") public R refund(Long orderId) throws AlipayApiException { TOrder byId = orderService.getById(orderId); if (byId.getPaymentStatus()!=2){ return R.fail("订单状态异常"); } if (byId.getPaymentType()==1){ payMoneyUtil.wxRefund(byId.getSerialNumber(), byId.getCode(),byId.getPaymentAmount().toString(), byId.getPaymentAmount().toString(),"/"); } if (byId.getPaymentType()==2){ Map stringStringMap = payMoneyUtil.aliRefund(byId.getCode(), byId.getPaymentAmount().toString()); if ("10000".equals(stringStringMap.get("code"))){ byId.setPaymentStatus(4); orderService.updateById(byId); } } return R.ok(); } @ApiOperation(value = "导出",tags = "后台-订单管理") @PostMapping(value = "/export") public void mealGeneratorExport(@RequestBody OrderQuery orderQuery) { orderQuery.setPageNum(1); orderQuery.setPageSize(9999); PageInfo tOrderPageInfo = orderService.pageList(orderQuery); for (TOrder record : tOrderPageInfo.getRecords()) { record.getFormattedCreateTime(); if (record.getGoodType()==1){ TCourse byId = courseService.getById(record.getGoodId()); record.setGoodName(byId.getCourseName()); }else if (record.getGoodType()==2){ TInformation byId = informationService.getById(record.getGoodId()); record.setGoodName(byId.getInformationName()); }else { record.setGoodName("工作总结订单"); } } Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), TOrder.class,tOrderPageInfo.getRecords() ); 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(); } } } }