package com.ruoyi.web.modules.order.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
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.exception.ServiceException;
import com.ruoyi.web.modules.category.support.SellerShopContext;
import com.ruoyi.web.modules.delivery.service.IShopDeliveryService;
import com.ruoyi.web.modules.order.dto.OrderDeliveredDTO;
import com.ruoyi.web.modules.order.dto.OrderLogisticsDTO;
import com.ruoyi.web.modules.order.dto.OrderShipDTO;
import com.ruoyi.web.modules.order.dto.SellerOrderQuery;
import com.ruoyi.web.modules.order.service.IOrderService;
import com.ruoyi.web.modules.order.support.OrderSupport;
import com.ruoyi.web.modules.order.vo.OrderDetailVO;
import com.ruoyi.web.modules.store.constant.ShopConstants;

/**
 * 商家端发货管理
 */
@RestController
@RequestMapping("/agri/seller/ship")
public class SellerShipController extends BaseController
{
    @Autowired
    private IOrderService orderService;

    @Autowired
    private IShopDeliveryService shopDeliveryService;

    @PreAuthorize("@ss.hasPermi('agri:seller:ship:list')")
    @GetMapping("/badges")
    public AjaxResult badges()
    {
        return success(orderService.countSellerShipBadges(requireShopId()));
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:ship:list')")
    @GetMapping("/list")
    public TableDataInfo list(SellerOrderQuery query)
    {
        if (query == null)
        {
            query = new SellerOrderQuery();
        }
        OrderSupport.assertShipTab(query.getOrderStatus());
        startPage();
        return getDataTable(orderService.selectSellerList(requireShopId(), query));
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:ship:query')")
    @GetMapping("/{orderId}")
    public AjaxResult detail(@PathVariable("orderId") Long orderId)
    {
        Long shopId = requireShopId();
        OrderDetailVO detail = orderService.selectSellerShipDetail(shopId, orderId);
        detail.setShopDelivery(shopDeliveryService.getByShopId(shopId));
        return success(detail);
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:ship:ship')")
    @Log(title = "商家发货管理", businessType = BusinessType.UPDATE)
    @PostMapping("/{orderId}/ship")
    public AjaxResult ship(@PathVariable("orderId") Long orderId, @RequestBody OrderShipDTO dto)
    {
        orderService.shipOrder(requireShopId(), orderId, dto, getUsername());
        return success();
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:ship:ship')")
    @Log(title = "商家发货管理物流", businessType = BusinessType.UPDATE)
    @PostMapping("/{orderId}/logistics")
    public AjaxResult logistics(@PathVariable("orderId") Long orderId, @RequestBody OrderLogisticsDTO dto)
    {
        orderService.appendLogistics(requireShopId(), orderId, dto, getUsername());
        return success();
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:ship:ship')")
    @Log(title = "商家发货管理送达", businessType = BusinessType.UPDATE)
    @PostMapping("/{orderId}/delivered")
    public AjaxResult delivered(@PathVariable("orderId") Long orderId, @RequestBody OrderDeliveredDTO dto)
    {
        orderService.registerDelivered(requireShopId(), orderId, dto, getUsername());
        return success();
    }

    private Long requireShopId()
    {
        Long shopId = SellerShopContext.getShopId();
        if (shopId == null || shopId <= 0)
        {
            throw new ServiceException(ShopConstants.MSG_SHOP_CONTEXT_REQUIRED);
        }
        return shopId;
    }
}
