package com.yiqi.admin.controller.order;

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

import com.yiqi.common.exception.ServiceException;
import com.yiqi.common.constant.UrlConstants;
import com.yiqi.common.core.domain.R;
import com.yiqi.common.enums.DeliveryWayType;
import com.yiqi.common.utils.bean.BeanUtils;
import com.yiqi.order.domain.dto.OrderGoodsOutGoodsDTO;
import com.yiqi.order.domain.dto.OrderPurchaseGoodsAddDTO;
import com.yiqi.order.domain.dto.OrderPurchaseGoodsQueryDTO;
import com.yiqi.order.domain.vo.OrderDeliveryPathVO;
import com.yiqi.order.domain.vo.OrderPurchaseGoodsInfoVO;
import com.yiqi.order.domain.vo.OrderSellGoodsListVO;
import com.yiqi.system.domain.SysOrg;
import com.yiqi.system.service.ISysOrgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.yiqi.common.annotation.Log;
import com.yiqi.common.core.controller.BaseController;
import com.yiqi.common.core.domain.AjaxResult;
import com.yiqi.common.enums.BusinessType;
import com.yiqi.order.domain.OrderPurchaseGoods;
import com.yiqi.order.service.IOrderPurchaseGoodsService;
import com.yiqi.common.utils.poi.ExcelUtil;
import com.yiqi.common.core.page.TableDataInfo;

/**
 * 商品订单Controller
 *
 * @author ruoyi
 * @date 2023-10-17
 */
@Api(tags = "管理后台 - 进出货商品订单")
@RestController
@RequestMapping(UrlConstants.managerApi + "/order/purchaseGoods")
public class OrderPurchaseGoodsController extends BaseController {
    @Autowired
    private IOrderPurchaseGoodsService orderPurchaseGoodsService;
    @Autowired
    private ISysOrgService sysOrgService;

    /**
     * 查询商品订单列表
     */
    @PreAuthorize("@ss.hasPermi('order:purchaseGoods:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询商品订单列表")
    public TableDataInfo list(OrderPurchaseGoods orderPurchaseGoods) {
        startPage();
        List<OrderPurchaseGoods> list = orderPurchaseGoodsService.selectOrderPurchaseGoodsList(orderPurchaseGoods);
        return getDataTable(list);
    }

    /**
     * 导出商品订单列表
     */
    @PreAuthorize("@ss.hasPermi('order:purchaseGoods:export')")
    @Log(title = "商品订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出商品订单列表")
    public void export(HttpServletResponse response, OrderPurchaseGoods orderPurchaseGoods) {
        List<OrderPurchaseGoods> list = orderPurchaseGoodsService.selectOrderPurchaseGoodsList(orderPurchaseGoods);
        ExcelUtil<OrderPurchaseGoods> util = new ExcelUtil<OrderPurchaseGoods>(OrderPurchaseGoods.class);
        util.exportExcel(response, list, "商品订单数据");
    }

    /**
     * 获取商品订单详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取商品订单详细信息")
    public R<OrderPurchaseGoodsInfoVO> getInfo(@PathVariable("id") Long id) {
        OrderPurchaseGoodsInfoVO orderPurchaseGoodsInfoVO = new OrderPurchaseGoodsInfoVO();
        OrderPurchaseGoods purchaseGoods = orderPurchaseGoodsService.getById(id);
        if (purchaseGoods == null) {
            throw new ServiceException("订单不存在");
        }
        BeanUtils.copyProperties(purchaseGoods, orderPurchaseGoodsInfoVO);
        SysOrg sysOrg = sysOrgService.findById(purchaseGoods.getOrgId(), purchaseGoods.getSourceType());
        orderPurchaseGoodsInfoVO.setOrgName(sysOrg.getName());
        orderPurchaseGoodsInfoVO.setOrderPurchaseGoodsVOList(orderPurchaseGoodsService.selectOrderPurchaseGoodsById(id));
        orderPurchaseGoodsService.handleAddressInfo(orderPurchaseGoodsInfoVO, purchaseGoods);
        return R.ok(orderPurchaseGoodsInfoVO);
    }

    /**
     * 新增商品进货订单
     */
    @PreAuthorize("@ss.hasPermi('order:purchaseGoods:add')")
    @Log(title = "新增商品进货订单", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增商品进货订单")
    public AjaxResult add(@RequestBody OrderPurchaseGoodsAddDTO orderPurchaseGoodsAddDTO) {
        return toAjax(orderPurchaseGoodsService.insertOrderPurchaseGoods(orderPurchaseGoodsAddDTO));
    }

    /**
     * 修改商品订单
     */
    @PreAuthorize("@ss.hasPermi('order:purchaseGoods:edit')")
    @Log(title = "商品订单", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改商品订单")
    public AjaxResult edit(@RequestBody OrderPurchaseGoods orderPurchaseGoods) {
        orderPurchaseGoods.setUpdateById(getUserId());
        orderPurchaseGoods.setUpdateBy(getUsername());
        return toAjax(orderPurchaseGoodsService.updateOrderPurchaseGoods(orderPurchaseGoods));
    }

    /**
     * 删除商品订单
     */
    @PreAuthorize("@ss.hasPermi('order:purchaseGoods:remove')")
    @Log(title = "商品订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除商品订单")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(orderPurchaseGoodsService.deleteOrderPurchaseGoodsByIds(ids));
    }


    /**
     * 商品出货订单列表
     */
    @PreAuthorize("@ss.hasPermi('order:purchaseGoods:list')")
    @GetMapping("/sellGoodsList")
    @ApiOperation(value = "商品出货订单列表")
    public TableDataInfo sellGoodsList(OrderPurchaseGoodsQueryDTO orderPurchaseGoodsQueryDTO) {
        startPage();
        List<OrderSellGoodsListVO> list = orderPurchaseGoodsService.sellGoodsList(orderPurchaseGoodsQueryDTO);
        return getDataTable(list);
    }

    /**
     * 商品出货订单列表--总部工厂的门店订单列表
     */
    @PreAuthorize("@ss.hasPermi('order:purchaseGoods:list')")
    @GetMapping("/sellGoodsListByHqFactory")
    @ApiOperation(value = "商品出货订单列表--总部工厂的门店订单列表")
    public TableDataInfo sellGoodsListByHqFactory(OrderPurchaseGoodsQueryDTO orderPurchaseGoodsQueryDTO) {
        startPage();
        List<OrderSellGoodsListVO> list = orderPurchaseGoodsService.sellGoodsListByHqFactory(orderPurchaseGoodsQueryDTO);
        return getDataTable(list);
    }

    /**
     * 商品出货-接单(确认订单)
     */
    @GetMapping("/confirmOrder")
    @ApiOperation(value = "商品出货-接单(确认订单)")
    public AjaxResult confirmOrder(@RequestParam("orderId") Long orderId) {
        return toAjax(orderPurchaseGoodsService.confirmOrder(orderId));
    }

    /**
     * 商品出货-拒单
     */
    @Transactional(rollbackFor = Exception.class)
    @GetMapping("/refuseOrder")
    @ApiOperation(value = "商品出货-拒单")
    public AjaxResult refuseOrder(@RequestParam("orderId") Long orderId) {
        return toAjax(orderPurchaseGoodsService.refuseOrder(orderId));
    }

    /**
     * 商品出货-出货 -快递配送
     */
    @PostMapping("/outOrderGoods")
    @ApiOperation(value = "商品出货-出货-快递配送")
    public AjaxResult outOrderGoods(@RequestBody @Valid OrderGoodsOutGoodsDTO orderGoodsOutGoodsDTO) throws Exception {
        return toAjax(orderPurchaseGoodsService.outOrderGoods(orderGoodsOutGoodsDTO, DeliveryWayType.DELIVERY.getCode()));
    }

    /**
     * 商品出货-出货-自送
     */
    @GetMapping("/outOrderGoodsBySelf")
    @ApiOperation(value = "商品出货-出货-自送")
    public AjaxResult outOrderGoodsBySelf(@RequestParam("orderId") Long orderId) throws Exception {
        OrderGoodsOutGoodsDTO orderGoodsOutGoodsDTO = new OrderGoodsOutGoodsDTO();
        orderGoodsOutGoodsDTO.setOrderId(orderId);
        return toAjax(orderPurchaseGoodsService.outOrderGoods(orderGoodsOutGoodsDTO, DeliveryWayType.SELF_SEND.getCode()));
    }

    /**
     * 商品出货-取消订单
     */
    @Transactional(rollbackFor = Exception.class)
    @GetMapping("/cancelOrderGoods")
    @ApiOperation(value = "商品出货-取消订单")
    public AjaxResult cancelOrderGoods(@RequestParam("orderId") Long orderId) {
        return toAjax(orderPurchaseGoodsService.cancelOrderGoods(orderId));
    }


    /**
     * 商品确认收货
     */
    @GetMapping("/confirmReceiveGoodsByOrderGoods")
    @ApiOperation(value = "商品确认收货")
    public AjaxResult confirmReceiveGoodsByOrderGoods(@RequestParam("orderId") Long orderId) {
        return toAjax(orderPurchaseGoodsService.confirmReceiveGoodsByOrderGoods(orderId));
    }

    /**
     * 查询订单物流轨迹
     */
    @GetMapping("/getDeliveryPath")
    @ApiOperation(value = "查询订单物流轨迹")
    public R<OrderDeliveryPathVO> getDeliveryPath(@RequestParam("orderId") Long orderId) throws Exception {
        return R.ok(orderPurchaseGoodsService.getDeliveryPath(orderId));
    }

}
