package com.yami.shop.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.RefundDelivery;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.api.util.SecurityUtils;
import com.yami.shop.service.RefundDeliveryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * 退货物流信息接口
 */
@RestController
@RequestMapping("/refund/delivery")
@Api(tags = "退货物流信息接口")
@AllArgsConstructor
public class RefundDeliveryController {

    private final RefundDeliveryService refundDeliveryService;

    /**
     * 分页获取退货物流信息列表
     */
    @GetMapping("/page")
    @ApiOperation("分页获取退货物流信息列表")
    public ResponseEntity<IPage<RefundDelivery>> page(PageParam<RefundDelivery> pageParam, RefundDelivery refundDelivery) {
        IPage<RefundDelivery> refundDeliveryPage = refundDeliveryService.page(pageParam, new LambdaQueryWrapper<RefundDelivery>());
        return ResponseEntity.ok(refundDeliveryPage);
    }

    /**
     * 根据退款编号获取退货物流信息
     */
    @GetMapping("/getByRefundSn/{refundSn}")
    @ApiOperation("根据退款编号获取退货物流信息")
    public ResponseEntity<RefundDelivery> getByRefundSn(@PathVariable("refundSn") String refundSn) {
        RefundDelivery refundDelivery = refundDeliveryService.getOne(new LambdaQueryWrapper<RefundDelivery>().eq(RefundDelivery::getRefundSn, refundSn));
        return ResponseEntity.ok(refundDelivery);
    }

    /**
     * 根据ID获取退货物流信息
     */
    @GetMapping("/getById/{id}")
    @ApiOperation("根据ID获取退货物流信息")
    public ResponseEntity<RefundDelivery> getById(@PathVariable Long id) {
        RefundDelivery refundDelivery = refundDeliveryService.getById(id);
        return ResponseEntity.ok(refundDelivery);
    }

    /**
     * 保存退货物流信息
     */
    @PostMapping("/save")
    @ApiOperation("保存退货物流信息")
    public ResponseEntity<Void> save(@Valid @RequestBody RefundDelivery refundDelivery) {
        refundDeliveryService.save(refundDelivery);
        return ResponseEntity.ok().build();
    }

    /**
     * 更新退货物流信息
     */
    @PutMapping("/update")
    @ApiOperation("更新退货物流信息")
    public ResponseEntity<String> update(@Valid @RequestBody RefundDelivery refundDelivery) {
        RefundDelivery dbRefundDelivery = refundDeliveryService.getById(refundDelivery.getRefundDeliveryId());
        if (dbRefundDelivery == null) {
            return ResponseEntity.badRequest().body("退货物流信息不存在");
        }
        // 确保用户只能更新自己的退货物流信息
        if (!Objects.equals(dbRefundDelivery.getUserId(), SecurityUtils.getUser().getUserId())) {
            return ResponseEntity.badRequest().body("无权限操作");
        }
        refundDeliveryService.updateById(refundDelivery);
        return ResponseEntity.ok("更新成功");
    }


    /**
     * 删除退货物流信息
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除退货物流信息")
    public ResponseEntity<String> delete(@PathVariable Long id) {
        RefundDelivery refundDelivery = refundDeliveryService.getById(id);
        if (refundDelivery == null) {
            return ResponseEntity.badRequest().body("退货物流信息不存在");
        }
        // 确保用户只能删除自己的退货物流信息
        if (!Objects.equals(refundDelivery.getUserId(), SecurityUtils.getUser().getUserId())) {
            return ResponseEntity.badRequest().body("无权限操作");
        }
        refundDeliveryService.removeById(id);
        return ResponseEntity.ok("删除成功");
    }
}
