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.OroqenOrderItem;
import org.jeecg.modules.oroqen.service.IOroqenOrderItemService;
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/orderItem")
@Slf4j
public class OroqenOrderItemController extends JeecgController<OroqenOrderItem, IOroqenOrderItemService> {

    @Autowired
    private IOroqenOrderItemService oroqenOrderItemService;

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

    /**
     * 根据订单ID查询订单项
     */
    @AutoLog(value = "订单项-根据订单ID查询")
    @Operation(summary = "订单项-根据订单ID查询", description = "订单项-根据订单ID查询")
    @GetMapping(value = "/order/{orderId}")
    public Result<List<OroqenOrderItem>> getByOrderId(@PathVariable String orderId) {
        List<OroqenOrderItem> list = oroqenOrderItemService.getByOrderId(orderId);
        return Result.OK(list);
    }

    /**
     * 添加
     */
    @AutoLog(value = "订单项-添加")
    @Operation(summary = "订单项-添加", description = "订单项-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody OroqenOrderItem oroqenOrderItem) {
        oroqenOrderItemService.save(oroqenOrderItem);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     */
    @AutoLog(value = "订单项-编辑")
    @Operation(summary = "订单项-编辑", description = "订单项-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody OroqenOrderItem oroqenOrderItem) {
        oroqenOrderItemService.updateById(oroqenOrderItem);
        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) {
        oroqenOrderItemService.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.oroqenOrderItemService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

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

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

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