package cn.iocoder.yudao.module.member.controller.app.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.common.util.object.BeanUtils;
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.app.order.vo.AppOrderPageReqVO;
import cn.iocoder.yudao.module.member.controller.app.order.vo.AppOrderRespVO;
import cn.iocoder.yudao.module.member.controller.app.order.vo.AppOrderSaveReqVO;
import cn.iocoder.yudao.module.member.controller.app.order.vo.AppPageVo;
import cn.iocoder.yudao.module.member.dal.dataobject.order.DishOrderDO;
import cn.iocoder.yudao.module.member.service.order.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
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.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
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.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "用户 App - 会员订单")
@RestController
@RequestMapping("/member/order")
@Validated
public class AppOrderController {

    @Resource
    private OrderService orderService;

    @PostMapping("/create")
    @Operation(summary = "创建会员订单")
    public CommonResult<Long> createOrder(@Valid @RequestBody AppOrderSaveReqVO createReqVO) {
        return success(orderService.createOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新会员订单")
    public CommonResult<Boolean> updateOrder(@Valid @RequestBody AppOrderSaveReqVO updateReqVO) {
        orderService.updateOrder(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除会员订单")
    @Parameter(name = "id", description = "编号", required = true)
    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")
    public CommonResult<AppOrderRespVO> getOrder(@RequestParam("id") Long id) {
        return success(orderService.getOrder(id));
    }

    //@GetMapping("/page")
    //@Operation(summary = "获得会员订单分页")
    //public CommonResult<PageResult<AppOrderRespVO>> getOrderPage(@Valid AppOrderPageReqVO pageReqVO) {
    //    PageResult<DishOrderDO> pageResult = orderService.getOrderPage(pageReqVO);
    //    return success(BeanUtils.toBean(pageResult, AppOrderRespVO.class));
    //}

    @GetMapping("/export-excel")
    @Operation(summary = "导出会员订单 Excel")
    @PreAuthorize("@ss.hasPermission('member:order:export')")
    @OperateLog(type = EXPORT)
    public void exportOrderExcel(@Valid AppOrderPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DishOrderDO> list = orderService.getOrderPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "会员订单.xls", "数据", AppOrderRespVO.class,
                        BeanUtils.toBean(list, AppOrderRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得会员订单")
    public CommonResult<PageResult<AppOrderRespVO>> getOrderPage(AppPageVo vo) {
        PageResult<AppOrderRespVO> orderList = orderService.getOrderList(vo);
        return success(orderList);
    }


}