package com.sf.order.controller;

import com.sf.common.annotation.Log;
import com.sf.common.core.controller.BaseController;
import com.sf.common.core.domain.AjaxResult;
import com.sf.common.core.page.TableDataInfo;
import com.sf.common.enums.BusinessType;
import com.sf.common.utils.poi.ExcelUtil;
import com.sf.order.domain.OrderInfo;
import com.sf.order.domain.dto.OrderCreateDto;
import com.sf.order.domain.req.OrderListReqVo;
import com.sf.order.domain.res.OrderListResVo;
import com.sf.order.service.IOrderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 订单基础信息Controller
 *
 * @author ztzh
 * @date 2024-04-09
 */
@RestController
@RequestMapping("/order")
public class OrderInfoController extends BaseController {

    @Autowired
    private IOrderInfoService orderInfoService;

    /**
     * 查询订单基础信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(OrderListReqVo vo) {
        startPage();
        List<OrderListResVo> list = orderInfoService.queryList(vo);
        return getDataTable(list);
    }


    /**
     * 导出订单基础信息列表
     */
    @Log(title = "订单基础信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderInfo orderInfo) {
        List<OrderInfo> list = orderInfoService.selectOrderInfoList(orderInfo);
        ExcelUtil<OrderInfo> util = new ExcelUtil<OrderInfo>(OrderInfo.class);
        util.exportExcel(response, list, "订单基础信息数据");
    }

    /**
     * 获取订单基础信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(orderInfoService.selectOrderInfoById(id));
    }

    /**
     * 新增订单基础信息
     */
    @Log(title = "创建订单基础信息", businessType = BusinessType.INSERT)
    @PostMapping(value = "/createOrder")
    public AjaxResult createOrder(@RequestBody OrderCreateDto orderCreateDto) {
        return AjaxResult.success(orderInfoService.createOrder(orderCreateDto));
    }

    /**
     * 修改订单基础信息
     */
    @Log(title = "订单基础信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderInfo orderInfo) {
        return toAjax(orderInfoService.updateOrderInfo(orderInfo));
    }

    /**
     * 支付订单
     */
    @PostMapping(value = "/pay/{orderId}")
    private String orderPay(@PathVariable(value = "orderId") Long orderId) {
        orderInfoService.orderPay(orderId);
        return null;
    }

    /**
     * 删除订单基础信息
     */
    @Log(title = "订单基础信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(orderInfoService.deleteOrderInfoByIds(ids));
    }
}
