package com.leromro.core.controller;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.leromro.common.core.domain.R;
import com.leromro.common.core.page.TableDataInfo;
import com.leromro.common.utils.SecurityUtils;
import com.leromro.core.domain.Address;
import com.leromro.core.domain.MainOrders;
import com.leromro.core.domain.SecondOrder;
import com.leromro.core.domain.VolunteerInfo;
import com.leromro.core.domain.dto.OrderRequestDTO;
import com.leromro.core.domain.dto.UserOrderReviewDTO;
import com.leromro.core.domain.vo.MainOrdersVO;
import com.leromro.core.domain.vo.OrdersInfoVO;
import com.leromro.core.domain.vo.VolunteerInfoVO;
import com.leromro.core.domain.vo.webMainOrderListVO;
import com.leromro.core.service.IAddressService;
import com.leromro.core.service.IMainOrderService;
import com.leromro.core.service.ISecondOrderService;
import com.leromro.core.service.IVolunteerInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.leromro.common.annotation.Log;
import com.leromro.common.core.controller.BaseController;
import com.leromro.common.core.domain.AjaxResult;
import com.leromro.common.enums.BusinessType;

/**
 * 订单主Controller
 *
 * @author ruoyi
 * @date 2025-04-09
 */
@RestController
@Api(tags = "用户订单")
@RequestMapping("/core/users/orders")
public class OrdersController extends BaseController {
    @Autowired
    private IMainOrderService mainOrderService;
    @Autowired
    private ISecondOrderService secondOrderService;
    @Autowired
    private IAddressService addressService;
    @Autowired
    private IVolunteerInfoService volunteerInfoService;


    /**
     * 后台管理web页面订单列表
     */
    @ApiOperation("后台管理web页面订单列表")
    @GetMapping("/web/mainOrderList")
    public TableDataInfo<webMainOrderListVO> webMainOrderList(MainOrdersVO mainOrdersVO) {
        startPage();
        List<webMainOrderListVO> list = mainOrderService.webMainOrderList(mainOrdersVO);
        return getDataTable(list);
    }

    /**
     * 查询订单主列表
     */
    @ApiOperation("用户查询订单列表")
    @GetMapping("/mainOrderList")
    public R<List<MainOrdersVO>> selectOrderList(Integer orderStatus) {
        startPage();
        Long userId = SecurityUtils.getUserId();
        List<MainOrdersVO> list = mainOrderService.selectByUserId(orderStatus, userId);
        return R.ok(list);
    }

/*    @ApiOperation("用户查询小订单列表")
    @GetMapping("/secondOrderList")
    public R<List<SecondOrder>> selectSecondOrderList(String orderId) {
        startPage();
        Long userId = SecurityUtils.getUserId();
        QueryWrapper<SecondOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id", orderId).eq("user_id", userId);
        List<SecondOrder> list = secondOrderService.list(wrapper);
        return R.ok(list);
    }*/


    /**
     * 获取订单主详细信息
     */
    @ApiOperation("用户获取订单详细信息以及大订单所属小订单")
    @GetMapping("orderInfo/{mainOrderId}")
    public R<OrdersInfoVO> getMainOrderInfo(@PathVariable("mainOrderId") String mainOrderId) {
        QueryWrapper<MainOrders> wrapper = new QueryWrapper<>();
        Long userId = SecurityUtils.getUserId();
        wrapper.eq("main_order_id", mainOrderId);
        MainOrders mainOrders = mainOrderService.list(wrapper).get(0);
        QueryWrapper<SecondOrder> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("main_order_id", mainOrderId);
        List<SecondOrder> list = secondOrderService.list(wrapper1);
        QueryWrapper<Address> addressQueryWrapper = new QueryWrapper<Address>().eq("address_id", mainOrders.getAddressId());
        Address address = addressService.getOne(addressQueryWrapper);
        QueryWrapper<VolunteerInfo> volunteerInfoQueryWrapper = new QueryWrapper<VolunteerInfo>().eq("volunteer_id", mainOrders.getVolunteerId()).eq("service_category",mainOrders.getServiceCategory());
        VolunteerInfo volunteerInfo = volunteerInfoService.list(volunteerInfoQueryWrapper).get(0);
        OrdersInfoVO ordersInfoVO = new OrdersInfoVO();
        ordersInfoVO.setMainOrders(mainOrders);
        ordersInfoVO.setSecondOrderList(list);
        ordersInfoVO.setAddress(address);
        ordersInfoVO.setVolunteerInfo(volunteerInfo);
        return R.ok(ordersInfoVO);
    }

    /**
     * 新增订单主
     */
    @ApiOperation("用户创建订单")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping("/createOrder")
    public AjaxResult createOrder(@RequestBody OrderRequestDTO orderRequest) {
        mainOrderService.createOrders(orderRequest);
        return AjaxResult.success();
    }

    /**
     * 用户点击完成订单 , 根据评价算钱。
     */
    @ApiOperation(value = "用户评价小订单",notes = "传入对象参数 包含： seconderId, score, userReview, userPicture")
    @PostMapping("/userFinishOrder")
    public AjaxResult userFinish (@RequestBody UserOrderReviewDTO reviewDTO) {

        secondOrderService.userFinish(reviewDTO);
        return AjaxResult.success();
    }


    /**
     * 用户点击完成订单
     */
/*    @ApiOperation("用户点击完成主订单")
    @PostMapping("/userFinishOrder")
    public AjaxResult userFinish (String mainOrderID) {
        UpdateWrapper<MainOrders> updateWrapper = new UpdateWrapper<MainOrders>().eq("main_order_id", mainOrderID).set("order_status", 8);
        mainOrderService.update(updateWrapper);
        return AjaxResult.success();
    }*/



    /**
     * 修改订单主
     */
/*    @ApiOperation("修改订单主")
    @PreAuthorize("@ss.hasPermi('system:orders:edit')")
    @Log(title = "订单主", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MainOrders orders)
    {
        return toAjax(lOrdersService.updateLOrders(orders));
    }*/

    /**
     * 删除订单主
     */
/*    @ApiOperation("删除订单主")
    @PreAuthorize("@ss.hasPermi('system:orders:remove')")
    @Log(title = "订单主", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public R<Integer> remove(@PathVariable String[] orderIds)
    {
        return R.ok(lOrdersService.deleteLOrdersByOrderIds(orderIds));
    }*/

}
