package com.gxwebsoft.shop.controller;

import cn.hutool.core.date.DateUtil;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.shop.entity.Order;
import com.gxwebsoft.shop.service.OrderDeliveryService;
import com.gxwebsoft.shop.entity.OrderDelivery;
import com.gxwebsoft.shop.param.OrderDeliveryParam;
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 com.gxwebsoft.shop.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
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")
public class OrderDeliveryController extends BaseController {
    @Resource
    private OrderDeliveryService orderDeliveryService;
    @Resource
    private OrderService orderService;

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

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

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

    @Transactional(rollbackFor = {Exception.class})
    @PreAuthorize("hasAuthority('shop:orderDelivery:list')")
    @ApiOperation("添加发货单")
    @PostMapping()
    public ApiResult<?> save(@RequestBody OrderDelivery orderDelivery) {
        if (orderDeliveryService.save(orderDelivery)) {
          final Order order = orderService.getById(orderDelivery.getOrderId());
          order.setDeliveryStatus(20);
          order.setDeliveryTime(DateUtil.date());
          orderService.updateById(order);
          return success("操作成功");
        }
        return fail("操作失败");
    }

    @PreAuthorize("hasAuthority('shop:orderDelivery:list')")
    @ApiOperation("修改发货单")
    @PutMapping()
    public ApiResult<?> update(@RequestBody OrderDelivery orderDelivery) {
        if (orderDeliveryService.updateById(orderDelivery)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }
    @PreAuthorize("hasAuthority('shop:orderDelivery:list')")
    @ApiOperation("删除发货单")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (orderDeliveryService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('shop:orderDelivery:list')")
    @ApiOperation("批量添加发货单")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<OrderDelivery> list) {
        if (orderDeliveryService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('shop:orderDelivery:list')")
    @ApiOperation("批量修改发货单")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<OrderDelivery> batchParam) {
        if (batchParam.update(orderDeliveryService, "delivery_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }
    @PreAuthorize("hasAuthority('shop:orderDelivery:list')")
    @ApiOperation("批量删除发货单")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (orderDeliveryService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
