package com.ruoyi.web.controller.api;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.WebUtils;
import com.ruoyi.system.model.TBigGiftPackageOrder;
import com.ruoyi.system.model.TPhysicalGoods;
import com.ruoyi.system.model.TPhysicalGoodsOrder;
import com.ruoyi.system.query.BigGiftPackageQuery;
import com.ruoyi.system.service.TPhysicalGoodsOrderService;
import com.ruoyi.system.service.TPhysicalGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.*;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
/**
*
* 实物商品订单 前端控制器
*
*
* @author xiaochen
* @since 2025-12-08
*/
@RestController
@RequestMapping("/t-physical-goods-order")
@Api(tags = "(嘉视达)实物商品订单")
public class TPhysicalGoodsOrderController {
private final TPhysicalGoodsOrderService tPhysicalGoodsOrderService;
private final TPhysicalGoodsService tPhysicalGoodsService;
public TPhysicalGoodsOrderController(TPhysicalGoodsOrderService tPhysicalGoodsOrderService, TPhysicalGoodsService tPhysicalGoodsService) {
this.tPhysicalGoodsOrderService = tPhysicalGoodsOrderService;
this.tPhysicalGoodsService = tPhysicalGoodsService;
}
@ApiOperation(value = "实物商品订单列表")
@PostMapping(value = "/pageList")
public R> pageList(@RequestBody BigGiftPackageQuery query) {
Page page = tPhysicalGoodsOrderService.pageList(query);
return R.ok(page);
}
@ApiOperation(value = "实物商品订单详情")
@GetMapping(value = "/detail/{id}")
public R detail(@PathVariable("id") Long id) {
TPhysicalGoodsOrder order = tPhysicalGoodsOrderService.getById(id);
TPhysicalGoods goods = tPhysicalGoodsService.getById(order.getPhysicalGoodsId());
order.setPhysicalGoodsName(goods.getName());
order.setPhysicalGoodsCover(goods.getCoverImage());
order.setPhysicalGoodsPrice(goods.getTotalPrice());
order.setPhysicalGoodsIntro(goods.getIntroduction());
return R.ok(order);
}
@ApiOperation(value = "实物商品订单编辑")
@PostMapping(value = "/edit")
@Log(title = "实物商品订单-编辑", businessType = BusinessType.UPDATE)
public R edit(@RequestBody TPhysicalGoodsOrder order) {
TPhysicalGoodsOrder goodsOrder = tPhysicalGoodsOrderService.getById(order.getId());
if(goodsOrder.getStatus()!=1){
return R.fail("待付款状态才可以编辑");
}
goodsOrder.setOrderPrice(order.getOrderPrice());
boolean b = tPhysicalGoodsOrderService.updateById(goodsOrder);
return R.ok("操作成功");
}
@ApiOperation(value = "实物商品订单删除")
@DeleteMapping(value = "/delete/{id}")
@Log(title = "实物商品订单-删除", businessType = BusinessType.DELETE)
public R delete(@PathVariable("id") Integer id) {
TPhysicalGoodsOrder order = tPhysicalGoodsOrderService.getById(id);
order.setDelFlag(1);
boolean b = tPhysicalGoodsOrderService.updateById(order);
return R.ok("操作成功");
}
@ApiOperation(value = "实物商品订单导出")
@PostMapping(value = "/export")
@Log(title = "实物商品订单导出-导出", businessType = BusinessType.EXPORT)
public void export(@RequestBody BigGiftPackageQuery query){
List list = tPhysicalGoodsOrderService.export(query);
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), TPhysicalGoodsOrder.class, list);
HttpServletResponse response = WebUtils.response();
assert response != null;
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();
}
}
}
}