package com.ruoyi.brewery.controller;

import com.ruoyi.brewery.domain.OrderShippingVO;
import com.ruoyi.brewery.domain.WyGoods;
import com.ruoyi.brewery.domain.WyRedeemOrder;
import com.ruoyi.brewery.service.IWyRedeemOrderService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 兑换订单Controller
 * 
 * @author ruoyi
 * @date 2024-12-21
 */
@Api(tags = "兑换订单")
@RestController
@RequestMapping("/brewery/redeemOrder")
public class BreweryRedeemOrderController extends BaseController
{
    @Autowired
    private IWyRedeemOrderService wyRedeemOrderService;

    /**
     * 查询兑换订单列表
     */
    @ApiOperation("查询兑换订单列表")
    @PreAuthorize("@bss.hasPermi('brewery:redeemOrder:list')")
    @GetMapping("/list")
    public TableDataInfo list(WyRedeemOrder wyRedeemOrder)
    {
        startPage();
        wyRedeemOrder.setBreweryId(SecurityUtils.getBreweryLoginUser().getBreweryId());
        List<WyRedeemOrder> list = wyRedeemOrderService.selectWyRedeemOrderList(wyRedeemOrder);
        return getDataTable(list);
    }

    /**
     * 导出兑换订单列表
     */
    @ApiOperation("导出兑换订单列表")
    @PreAuthorize("@bss.hasPermi('brewery:redeemOrder:export')")
    @Log(title = "兑换订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WyRedeemOrder wyRedeemOrder)
    {
        wyRedeemOrder.setBreweryId(SecurityUtils.getBreweryLoginUser().getBreweryId());
        List<WyRedeemOrder> list = wyRedeemOrderService.selectWyRedeemOrderList(wyRedeemOrder);
        ExcelUtil<WyRedeemOrder> util = new ExcelUtil<WyRedeemOrder>(WyRedeemOrder.class);
        util.exportExcel(response, list, "兑换订单数据");
    }

    /**
     * 获取兑换订单详细信息
     */
    @ApiOperation("获取兑换订单详细信息")
    @PreAuthorize("@bss.hasPermi('brewery:redeemOrder:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        validate(id);
        return success(wyRedeemOrderService.selectWyRedeemOrderById(id));
    }

    /**
     * 新增兑换订单
     */
    @ApiOperation("新增兑换订单")
    @PreAuthorize("@bss.hasPermi('brewery:redeemOrder:add')")
    @Log(title = "兑换订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WyRedeemOrder wyRedeemOrder)
    {
        wyRedeemOrder.setBreweryId(SecurityUtils.getBreweryLoginUser().getBreweryId());
        return toAjax(wyRedeemOrderService.insertWyRedeemOrder(wyRedeemOrder));
    }

    /**
     * 修改兑换订单
     */
    @ApiOperation("修改兑换订单")
    @PreAuthorize("@bss.hasPermi('brewery:redeemOrder:edit')")
    @Log(title = "兑换订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WyRedeemOrder wyRedeemOrder)
    {
        validate(wyRedeemOrder.getId());
        return toAjax(wyRedeemOrderService.updateWyRedeemOrder(wyRedeemOrder));
    }

    /**
     * 订单发货
     */
    @ApiOperation("订单发货")
    @PreAuthorize("@bss.hasPermi('brewery:redeemOrder:edit')")
    @Log(title = "订单发货", businessType = BusinessType.UPDATE)
    @PostMapping("/delivery")
    public AjaxResult delivery(@Validated @RequestBody OrderShippingVO shippingVO)
    {
        final WyRedeemOrder wyRedeemOrder = wyRedeemOrderService.selectWyRedeemOrderById(shippingVO.getOrderId());
        if (wyRedeemOrder.getId() == null) {
            throw new IllegalArgumentException("订单不存在");
        }
        validate(wyRedeemOrder.getId());
        wyRedeemOrder.setOrderStatus(2);
        wyRedeemOrder.setShippingTime(DateUtils.getNowDate());
        wyRedeemOrder.setShippingCompany(shippingVO.getShippingCompany());
        wyRedeemOrder.setDispatchMethod(shippingVO.getDispatchMethod());
        wyRedeemOrder.setTrackingNumber(shippingVO.getTrackingNumber());
        wyRedeemOrder.setBreweryId(SecurityUtils.getBreweryLoginUser().getBreweryId());
        return toAjax(wyRedeemOrderService.updateOrderStatus(wyRedeemOrder));
    }

    /**
     * 取消订单
     */
    @ApiOperation("取消订单")
    @PreAuthorize("@bss.hasPermi('brewery:redeemOrder:edit')")
    @Log(title = "取消订单", businessType = BusinessType.UPDATE)
    @PostMapping("/cancel")
    public AjaxResult cancel(@RequestBody WyRedeemOrder wyRedeemOrder)
    {
        if (wyRedeemOrder.getId() == null) {
            throw new IllegalArgumentException("订单不存在");
        }
        validate(wyRedeemOrder.getId());
        wyRedeemOrder.setOrderStatus(-1);
        wyRedeemOrder.setBreweryId(SecurityUtils.getBreweryLoginUser().getBreweryId());
        return toAjax(wyRedeemOrderService.updateOrderStatus(wyRedeemOrder));
    }

    /**
     * 删除兑换订单
     */
    @ApiOperation("删除兑换订单")
    @PreAuthorize("@bss.hasPermi('brewery:redeemOrder:remove')")
    @Log(title = "兑换订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        validate(id);
        return toAjax(wyRedeemOrderService.deleteWyRedeemOrderById(id));
    }

    private void validate(Long id) {
        final WyRedeemOrder wyRedeemOrder = wyRedeemOrderService.selectWyRedeemOrderById(id);
        if (wyRedeemOrder == null) {
            throw new IllegalArgumentException("订单不存在");
        }
        if (!SecurityUtils.getBreweryLoginUser().getBreweryId().equals(wyRedeemOrder.getBreweryId())) {
            throw new IllegalArgumentException("只能操作自己的品牌方的订单");
        }
    }
}
