package org.jeecg.modules.oroqen.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.oroqen.entity.OroqenOrder;
import org.jeecg.modules.oroqen.service.IOroqenOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

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

/**
 * @Description: 订单
 * @Author: jeecg-boot
 * @Date: 2025-01-08
 * @Version: V1.0
 */
@Tag(name = "订单", description = "订单")
@RestController
@RequestMapping("/oroqen/order")
@Slf4j
public class OroqenOrderController extends JeecgController<OroqenOrder, IOroqenOrderService> {

    @Autowired
    private IOroqenOrderService oroqenOrderService;

    /**
     * 分页列表查询
     */
    @AutoLog(value = "订单-分页列表查询")
    @Operation(summary = "订单-分页列表查询", description = "订单-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<OroqenOrder>> queryPageList(OroqenOrder oroqenOrder,
                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                    HttpServletRequest req) {
        QueryWrapper<OroqenOrder> queryWrapper = QueryGenerator.initQueryWrapper(oroqenOrder, req.getParameterMap());
        Page<OroqenOrder> page = new Page<OroqenOrder>(pageNo, pageSize);
        IPage<OroqenOrder> pageList = oroqenOrderService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 根据用户ID查询订单
     */
    @AutoLog(value = "订单-根据用户ID查询")
    @Operation(summary = "订单-根据用户ID查询", description = "订单-根据用户ID查询")
    @GetMapping(value = "/user/{userId}")
    public Result<List<OroqenOrder>> getByUserId(@PathVariable String userId) {
        List<OroqenOrder> list = oroqenOrderService.getByUserId(userId);
        return Result.OK(list);
    }

    /**
     * 根据订单状态查询订单
     */
    @AutoLog(value = "订单-根据状态查询")
    @Operation(summary = "订单-根据状态查询", description = "订单-根据状态查询")
    @GetMapping(value = "/status/{status}")
    public Result<List<OroqenOrder>> getByStatus(@PathVariable Integer status) {
        List<OroqenOrder> list = oroqenOrderService.getByStatus(status);
        return Result.OK(list);
    }

    /**
     * 创建订单
     */
    @AutoLog(value = "订单-创建订单")
    @Operation(summary = "订单-创建订单", description = "订单-创建订单")
    @PostMapping(value = "/create")
    public Result<String> createOrder(@RequestBody OroqenOrder oroqenOrder) {
        String orderId = oroqenOrderService.createOrder(oroqenOrder);
        return Result.OK(orderId, "订单创建成功！");
    }

    /**
     * 编辑
     */
    @AutoLog(value = "订单-编辑")
    @Operation(summary = "订单-编辑", description = "订单-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody OroqenOrder oroqenOrder) {
        oroqenOrderService.updateById(oroqenOrder);
        return Result.OK("编辑成功!");
    }

    /**
     * 取消订单
     */
    @AutoLog(value = "订单-取消订单")
    @Operation(summary = "订单-取消订单", description = "订单-取消订单")
    @PostMapping(value = "/cancel/{id}")
    public Result<String> cancelOrder(@PathVariable String id) {
        oroqenOrderService.cancelOrder(id);
        return Result.OK("订单取消成功!");
    }

    /**
     * 发货
     */
    @AutoLog(value = "订单-发货")
    @Operation(summary = "订单-发货", description = "订单-发货")
    @PostMapping(value = "/ship")
    public Result<String> shipOrder(@RequestParam String orderId,
                                    @RequestParam String shippingCompany,
                                    @RequestParam String trackingNumber) {
        oroqenOrderService.shipOrder(orderId, shippingCompany, trackingNumber);
        return Result.OK("发货成功!");
    }

    /**
     * 确认收货
     */
    @AutoLog(value = "订单-确认收货")
    @Operation(summary = "订单-确认收货", description = "订单-确认收货")
    @PostMapping(value = "/confirm/{id}")
    public Result<String> confirmReceived(@PathVariable String id) {
        oroqenOrderService.confirmReceived(id);
        return Result.OK("确认收货成功!");
    }

    /**
     * 通过id删除
     */
    @AutoLog(value = "订单-通过id删除")
    @Operation(summary = "订单-通过id删除", description = "订单-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        oroqenOrderService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     */
    @AutoLog(value = "订单-批量删除")
    @Operation(summary = "订单-批量删除", description = "订单-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.oroqenOrderService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     */
    @AutoLog(value = "订单-通过id查询")
    @Operation(summary = "订单-通过id查询", description = "订单-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<OroqenOrder> queryById(@RequestParam(name = "id", required = true) String id) {
        OroqenOrder oroqenOrder = oroqenOrderService.getById(id);
        if (oroqenOrder == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(oroqenOrder);
    }

    /**
     * 导出excel
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, OroqenOrder oroqenOrder) {
        return super.exportXls(request, oroqenOrder, OroqenOrder.class, "订单");
    }

    /**
     * 通过excel导入数据
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, OroqenOrder.class);
    }
}