
package com.wzj.soopin.order.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzj.soopin.order.convert.OrderItemConvert;
import com.wzj.soopin.order.domain.bo.OrderItemBo;
import com.wzj.soopin.order.domain.entity.OrderItem;
import com.wzj.soopin.order.domain.vo.OrderItemVO;
import com.wzj.soopin.order.service.OrderItemService;
import com.wzj.soopin.order.service.impl.OrderItemServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.web.core.BaseController;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单中所包含的商品Controller
 *
 * @author zcc
 * @date 2022-12-01
 */
@Tag(name = "订单中所包含的商品")
@RestController
@RequestMapping("/oms/orderItem")
@RequiredArgsConstructor
public class OrderItemController extends BaseController {


    private final OrderItemServiceImpl service;
    private final OrderItemConvert convert;
    private final OrderItemService orderItemService;

    @Operation(summary = "查询订单中所包含的商品列表")
    @PostMapping("/list")
    public R<IPage<OrderItemVO>> list(@RequestBody OrderItemBo query, Page<OrderItem> page) {
        Page<OrderItem> list = orderItemService.page(page,query.toWrapper() );
        return R.ok(convert.toVO(list));
    }

    @Operation(summary = "导出订单商品列表")
    @Log(title = "订单商品", businessType = BusinessType.EXPORT)
    @PostMapping("export")
    public R<String> export(OrderItemBo query) {
        List<OrderItem> list = service.list(query.toWrapper());
        ExcelUtil<OrderItemVO> util = new ExcelUtil<>(OrderItemVO.class);
        return R.ok(util.writeExcel(convert.toVO(list), "订单中所包含的商品数据"));
    }

    @Operation(summary = "获取订单中所包含的商品详细信息")
    @GetMapping(value = "/{id}")
    public R getInfo(@PathVariable("id") Long id) {
        return R.ok(service.getById(id));
    }

    @Operation(summary = "新增订单中所包含的商品")
    @Log(title = "订单中所包含的商品", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public R add(@RequestBody OrderItem orderItem) {
        return R.ok(service.save(orderItem));
    }

    @Operation(summary = "修改订单中所包含的商品")
    @Log(title = "订单中所包含的商品", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public R edit(@RequestBody OrderItem orderItem) {
        return R.ok(service.updateById(orderItem));
    }

    @Operation(summary = "删除订单中所包含的商品")
    @Log(title = "订单中所包含的商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R remove(@PathVariable Long id) {
        return R.ok(service.removeById(id));
    }
}
