package com.gxwebsoft.bszx.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gxwebsoft.bszx.entity.BszxBm;
import com.gxwebsoft.bszx.entity.BszxPay;
import com.gxwebsoft.bszx.param.BszxPayParam;
import com.gxwebsoft.bszx.service.BszxBmService;
import com.gxwebsoft.bszx.service.BszxPayService;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.shop.entity.ShopOrder;
import com.gxwebsoft.shop.param.ShopOrderParam;
import com.gxwebsoft.shop.service.ShopOrderService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 百色中学-订单管理
 *
 * @author 科技小王子
 * @since 2025-03-06 22:50:25
 */
@Tag(name = "百色中学-订单管理")
@RestController
@RequestMapping("/api/bszx/bszx-order")
public class BszxOrderController extends BaseController {
  @Resource
  private BszxPayService bszxPayService;
  @Resource
  private BszxBmService bszxBmService;
  @Resource
  private ShopOrderService shopOrderService;

  @Operation(summary = "分页查询百色中学-订单列表")
  @GetMapping("/page")
  public ApiResult<PageResult<ShopOrder>> page(ShopOrderParam param) {
    // 使用关联查询
    final PageResult<ShopOrder> result = shopOrderService.pageRel(param);
    if(!CollectionUtils.isEmpty(result.getList())){
      final Set<Integer> userIds = result.getList().stream().map(ShopOrder::getUserId).collect(Collectors.toSet());
      final List<BszxBm> bmList = bszxBmService.list(new LambdaQueryWrapper<BszxBm>().in(BszxBm::getUserId, userIds).isNotNull(BszxBm::getName));
      final Map<Integer, List<BszxBm>> collect = bmList.stream().collect(Collectors.groupingBy(BszxBm::getUserId));
      final Set<String> orderNos = result.getList().stream().map(ShopOrder::getOrderNo).collect(Collectors.toSet());
      final BszxPayParam bszxPayParam = new BszxPayParam();
      bszxPayParam.setOrderNos(orderNos);
      final List<BszxPay> bszxPays = bszxPayService.listRel(bszxPayParam);
      final Map<String, List<BszxPay>> collectByOrderNo = bszxPays.stream().collect(Collectors.groupingBy(BszxPay::getOrderNo));

      result.getList().forEach(d -> {
        final List<BszxPay> pays = collectByOrderNo.get(d.getOrderNo());
        if(!CollectionUtils.isEmpty(pays)){
          d.setDeliveryStatus(20);
        }
        final List<BszxBm> bmList1 = collect.get(d.getUserId());
        if(!CollectionUtils.isEmpty(bmList1)){
          final BszxBm bm = bmList1.get(0);
          d.setBm(bm);
          d.setRealName(bm.getName());
          if(bm.getPhone() != null){
            d.setPhone(bm.getPhone());
          }
        }
      });
    }
    return success(result);
  }


  @Operation(summary = "统计订单总金额")
  @GetMapping("/total")
  public ApiResult<BigDecimal> total() {
    try {
      BigDecimal totalAmount = bszxPayService.total();
      return success(totalAmount);
    } catch (Exception e) {
      // 异常时返回0，保持接口稳定性
      return success(BigDecimal.ZERO);
    }
  }

}
