package com.gxwebsoft.shop.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.shop.service.OrderDeliveryGoodsService;
import com.gxwebsoft.shop.entity.OrderDeliveryGoods;
import com.gxwebsoft.shop.param.OrderDeliveryGoodsParam;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.system.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 发货单商品控制器
 *
 * @author 科技小王子
 * @since 2024-09-24 01:39:23
 */
@Api(tags = "发货单商品管理")
@RestController
@RequestMapping("/api/shop/order-delivery-goods")
public class OrderDeliveryGoodsController extends BaseController {
    @Resource
    private OrderDeliveryGoodsService orderDeliveryGoodsService;

    @ApiOperation("分页查询发货单商品")
    @GetMapping("/page")
    public ApiResult<PageResult<OrderDeliveryGoods>> page(OrderDeliveryGoodsParam param) {
        // 使用关联查询
        return success(orderDeliveryGoodsService.pageRel(param));
    }

    @ApiOperation("查询全部发货单商品")
    @GetMapping()
    public ApiResult<List<OrderDeliveryGoods>> list(OrderDeliveryGoodsParam param) {
        // 使用关联查询
        return success(orderDeliveryGoodsService.listRel(param));
    }

    @PreAuthorize("hasAuthority('shop:orderDeliveryGoods:list')")
    @ApiOperation("根据id查询发货单商品")
    @GetMapping("/{id}")
    public ApiResult<OrderDeliveryGoods> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(orderDeliveryGoodsService.getByIdRel(id));
    }

    @ApiOperation("添加发货单商品")
    @PostMapping()
    public ApiResult<?> save(@RequestBody OrderDeliveryGoods orderDeliveryGoods) {
        if (orderDeliveryGoodsService.save(orderDeliveryGoods)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("修改发货单商品")
    @PutMapping()
    public ApiResult<?> update(@RequestBody OrderDeliveryGoods orderDeliveryGoods) {
        if (orderDeliveryGoodsService.updateById(orderDeliveryGoods)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @ApiOperation("删除发货单商品")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (orderDeliveryGoodsService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @ApiOperation("批量添加发货单商品")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<OrderDeliveryGoods> list) {
        if (orderDeliveryGoodsService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("批量修改发货单商品")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<OrderDeliveryGoods> batchParam) {
        if (batchParam.update(orderDeliveryGoodsService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @ApiOperation("批量删除发货单商品")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (orderDeliveryGoodsService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
