package cn.iocoder.yudao.module.member.controller.admin.order;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.member.controller.admin.order.vo.OrderDetailsReqVO;
import cn.iocoder.yudao.module.member.controller.admin.order.vo.OrderDetailsRespVO;
import cn.iocoder.yudao.module.member.controller.admin.order.vo.OrderMoneyRespVO;
import cn.iocoder.yudao.module.member.controller.app.orderdetail.vo.AppOrderDetailRespVO;
import cn.iocoder.yudao.module.member.service.order.OrderService;
import cn.iocoder.yudao.module.member.service.orderdetail.OrderDetailService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 会员订单")
@RestController
@RequestMapping("/member/order")
@Validated
public class OrderController {

    @Resource
    private OrderService orderService;
    @Resource
    private OrderDetailService detailService;
    //
    //@PostMapping("/create")
    //@Operation(summary = "创建会员订单")
    //@PreAuthorize("@ss.hasPermission('member:order:create')")
    //public CommonResult<Long> createOrder(@Valid @RequestBody OrderSaveReqVO createReqVO) {
    //    return success(orderService.createOrder(createReqVO));
    //}
    //
    //@PutMapping("/update")
    //@Operation(summary = "更新会员订单")
    //@PreAuthorize("@ss.hasPermission('member:order:update')")
    //public CommonResult<Boolean> updateOrder(@Valid @RequestBody OrderSaveReqVO updateReqVO) {
    //    orderService.updateOrder(updateReqVO);
    //    return success(true);
    //}
    //
    //@DeleteMapping("/delete")
    //@Operation(summary = "删除会员订单")
    //@Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('member:order:delete')")
    //public CommonResult<Boolean> deleteOrder(@RequestParam("id") Long id) {
    //    orderService.deleteOrder(id);
    //    return success(true);
    //}
    //
    //@GetMapping("/get")
    //@Operation(summary = "获得会员订单")
    //@Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('member:order:query')")
    //public CommonResult<OrderRespVO> getOrder(@RequestParam("id") Long id) {
    //    OrderDO order = orderService.getOrder(id);
    //    return success(BeanUtils.toBean(order, OrderRespVO.class));
    //}
    //
    //@GetMapping("/page")
    //@Operation(summary = "获得会员订单分页")
    //@PreAuthorize("@ss.hasPermission('member:order:query')")
    //public CommonResult<PageResult<OrderRespVO>> getOrderPage(@Valid OrderPageReqVO pageReqVO) {
    //    PageResult<OrderDO> pageResult = orderService.getOrderPage(pageReqVO);
    //    return success(BeanUtils.toBean(pageResult, OrderRespVO.class));
    //}
    //
    @GetMapping("/export-excel")
    @Operation(summary = "导出会员订单 Excel")
    @PreAuthorize("@ss.hasPermission('member:order:export')")
    @OperateLog(type = EXPORT)
    public void exportOrderExcel(@Valid OrderDetailsReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OrderDetailsRespVO> list = orderService.getPageOrderDetails(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "会员订单.xls", "数据", OrderDetailsRespVO.class,
                list);
    }
    /*================后台管理-订单管理========================*/
    /**
     * @Description:
     * @Author: qjq
     * @Date: 2024/4/1 9:13 
     * @return 
     */
    @GetMapping("/page")
    @Operation(summary = "获得会员订单分页")
    @PreAuthorize("@ss.hasPermission('member:order:query')")
    public CommonResult<PageResult<OrderDetailsRespVO>> getPageResult(@Valid    OrderDetailsReqVO reqVO){
        return CommonResult.success(orderService.getPageOrderDetails(reqVO));
    }
    /*================后台管理-订单管理-订单详情========================*/
    @GetMapping("/get")
    @Operation(summary = "获得会员订单明细")
    @PreAuthorize("@ss.hasPermission('member:order:query')")
    public CommonResult<List<AppOrderDetailRespVO>> getOrderDetails(@RequestParam("orderId")Long orderId){
        return CommonResult.success(detailService.selectListByOrderId(orderId));
    }
    /*================后台管理-订单管理-订单明细与用户信息========================*/
    @GetMapping("/get-details")
    @Operation(summary = "获得会员订单明细与用户信息")
    @PreAuthorize("@ss.hasPermission('member:order:query')")
    public CommonResult<OrderDetailsRespVO> getOrderDetailsAndUser(@RequestParam("orderId")Long orderId){
        return CommonResult.success(orderService.getOrderDetailsAndUser(orderId));
    }
    @GetMapping("/user/money")
    @Operation(summary = "根据会员编号，获得会员余额,充值金额,消费金额")
    @PreAuthorize("@ss.hasPermission('member:order:query')")
    public CommonResult<OrderMoneyRespVO> getUserMeney(@RequestParam("userId")Long userId){
        return CommonResult.success(orderService.getUserMeney(userId));
    }
}
