package com.ruoyi.web.controller.api.agent;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.basic.PageInfo;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.WebUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.dto.RefundApplyDTO;
import com.ruoyi.system.dto.TransferOrderDto;
import com.ruoyi.system.export.TBigGiftPackageOrderExport;
import com.ruoyi.system.model.*;
import com.ruoyi.system.query.AppUserListQuery;
import com.ruoyi.system.query.BigGiftPackageOrderQuery;
import com.ruoyi.system.query.OrderTransferQuery;
import com.ruoyi.system.query.RefundApplicationQuery;
import com.ruoyi.system.service.*;
import com.ruoyi.system.vo.TOrderTransferDetailVo;
import com.ruoyi.system.vo.TOrderTransferVo;
import com.ruoyi.system.vo.TRefundApplicationVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.context.annotation.Lazy;
import org.springframework.validation.annotation.Validated;
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.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
/**
*
* 大礼包订单 前端控制器
*
*
* @author xiaochen
* @since 2025-12-08
*/
@RestController
@RequestMapping("/agent/t-big-gift-package-order")
@Api(tags = "(嘉视达)供应商大礼包订单")
@RequiredArgsConstructor(onConstructor_ = @Lazy)
public class AgentBigGiftPackageOrderController {
private final TBigGiftPackageOrderService tBigGiftPackageOrderService;
private final TAppUserService tAppUserService;
private final TCompanyService tCompanyService;
private final TBigGiftPackageService tBigGiftPackageService;
private final TRefundApplicationService tRefundApplicationService;
@ApiOperation(value = "大礼包订单列表")
@PostMapping(value = "/pageList")
public R> pageList(@RequestBody BigGiftPackageOrderQuery query) {
if (!SecurityUtils.isAgentUser()) {
throw new ServiceException("请使用经销商/股东帐号访问");
}
Page tBigGiftPackageOrderPage = new Page<>(query.getPageNum(), query.getPageSize());
query.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
List list = tBigGiftPackageOrderService.pageList(tBigGiftPackageOrderPage, query);
tBigGiftPackageOrderPage.setRecords(list);
return R.ok(tBigGiftPackageOrderPage);
}
@ApiOperation(value = "大礼包订单详情")
@PostMapping(value = "/detail/{id}")
public R detail(@PathVariable Integer id) {
if (!SecurityUtils.isAgentUser()) {
throw new ServiceException("请使用经销商/股东帐号访问");
}
TBigGiftPackageOrder tBigGiftPackageOrder = tBigGiftPackageOrderService.getById(id);
Integer appUserId = tBigGiftPackageOrder.getAppUserId();
TAppUser appUser = tAppUserService.getById(appUserId);
Integer referrerUserId = appUser.getReferrerUserId();
if(referrerUserId!=null){
// 上级用户
TAppUser lastUser = tAppUserService.getById(referrerUserId);
if (lastUser != null && lastUser.getUserType()==2){
tBigGiftPackageOrder.setSaleName(lastUser.getName());
if(lastUser.getReferrerUserId()!=null){
// 上上级
TAppUser lastLastUser = tAppUserService.getById(lastUser.getReferrerUserId());
if (lastLastUser != null && lastLastUser.getUserType()==1){
tBigGiftPackageOrder.setPartnerName(lastLastUser.getName());
}
}
}
if (lastUser != null && lastUser.getUserType()==1){
tBigGiftPackageOrder.setPartnerName(lastUser.getName());
}
}
Integer bigGiftPackageId = tBigGiftPackageOrder.getBigGiftPackageId();
TBigGiftPackage tBigGiftPackage = tBigGiftPackageService.getById(bigGiftPackageId);
tBigGiftPackageOrder.setBigGiftPackageName(tBigGiftPackage.getName());
tBigGiftPackageOrder.setBigGiftPackageCover(tBigGiftPackage.getCoverImage());
tBigGiftPackageOrder.setBigGiftPackagePrice(tBigGiftPackage.getTotalPrice());
tBigGiftPackageOrder.setBigGiftPackageIntro(tBigGiftPackage.getIntroduction());
// 销量
long count = tBigGiftPackageOrderService.count(new LambdaQueryWrapper().eq(TBigGiftPackageOrder::getBigGiftPackageId, bigGiftPackageId).ne(TBigGiftPackageOrder::getStatus, 5));
tBigGiftPackageOrder.setBigGiftPackageSales((int)count);
Integer companyId = appUser.getCompanyId();
if(companyId!=null){
TCompany tCompany = tCompanyService.getById(companyId);
tBigGiftPackageOrder.setCompanyName(tCompany.getName());
}
return R.ok(tBigGiftPackageOrder);
}
@ApiOperation(value = "大礼包订单退款申请")
@PostMapping(value = "/applyRefund")
public R applyRefund(@Validated @RequestBody RefundApplyDTO dto) {
if (!SecurityUtils.isAgentUser()) {
throw new ServiceException("请使用经销商/股东帐号访问");
}
Integer userId = SecurityUtils.getLoginUser().getUser().getUserId().intValue();
tRefundApplicationService.applyRefund(dto, userId);
return R.ok();
}
@ApiOperation(value = "退款申请记录列表")
@PostMapping(value = "/refundList")
public R> refundList(@RequestBody RefundApplicationQuery query) {
if (!SecurityUtils.isAgentUser()) {
throw new ServiceException("请使用经销商/股东帐号访问");
}
query.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
Page page = tRefundApplicationService.pageRefundList(query);
return R.ok(page);
}
@ApiOperation(value = "退款申请详情")
@GetMapping(value = "/refundDetail/{id}")
public R refundDetail(@PathVariable Integer id) {
return R.ok(tRefundApplicationService.refundDetail(id));
}
}