package com.xinelu.manage.controller.goodsOrder;

import com.xinelu.common.annotation.Log;
import com.xinelu.common.core.controller.BaseController;
import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.common.core.page.TableDataInfo;
import com.xinelu.common.enums.BusinessType;
import com.xinelu.common.utils.poi.ExcelUtil;
import com.xinelu.manage.domain.goodsOrder.GoodsOrder;
import com.xinelu.manage.service.goodsOrder.IGoodsOrderService;
import com.xinelu.manage.vo.goodsorder.GoodsInfoOrderVO;
import com.xinelu.manage.vo.goodsorder.GoodsOrderVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

/**
 * 商品订单Controller
 *
 * @author xinyilu
 * @date 2022-10-18
 */
@RestController
@RequestMapping("/system/goodsOrder")
public class GoodsOrderController extends BaseController {
    @Resource
    private IGoodsOrderService goodsOrderService;

    /**
     * 查询商品订单列表
     */
    @PreAuthorize("@ss.hasPermi('system:goodsOrder:list')")
    @GetMapping("/list")
    public TableDataInfo list(GoodsInfoOrderVO goodsOrder) {
        startPage();
        List<GoodsInfoOrderVO> list = goodsOrderService.selectGoodsOrderList(goodsOrder);
        return getDataTable(list);
    }

    /**
     * 导出商品订单列表
     */
    @PreAuthorize("@ss.hasPermi('system:goodsOrder:export')")
    @Log(title = "商品订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GoodsInfoOrderVO goodsOrder) {
        List<GoodsInfoOrderVO> list = goodsOrderService.selectGoodsOrderList(goodsOrder);
        ExcelUtil<GoodsInfoOrderVO> util = new ExcelUtil<>(GoodsInfoOrderVO.class);
        util.exportExcel(response, list, "商品订单数据");
    }

    /**
     * 获取商品订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:goodsOrder:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(goodsOrderService.selectGoodsOrderById(id));
    }

    /**
     * 新增商品订单
     */
    @PreAuthorize("@ss.hasPermi('system:goodsOrder:add')")
    @Log(title = "商品订单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody GoodsOrder goodsOrder) {
        return toAjax(goodsOrderService.insertGoodsOrder(goodsOrder));
    }

    /**
     * 修改商品订单 退款
     */
    @PreAuthorize("@ss.hasPermi('system:goodsOrder:edit')")
    @Log(title = "商品订单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody GoodsOrder goodsOrder) {
        return toAjax(goodsOrderService.updateGoodsOrder(goodsOrder));
    }

    /**
     * 删除商品订单
     */
    @PreAuthorize("@ss.hasPermi('system:goodsOrder:remove')")
    @Log(title = "商品订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(goodsOrderService.deleteGoodsOrderByIds(ids));
    }

    /**
     * 获取商品订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:goodsOrder:goodsOrderByOrder')")
    @GetMapping(value = "/goodsOrderByOrder")
    public AjaxResult goodsOrderByOrder(String orderNo) {
        if (StringUtils.isBlank(orderNo)) {
            return AjaxResult.error("订单编号不能为空！");
        }
        GoodsOrderVO goodsOrderByOrder = goodsOrderService.getGoodsOrderByOrder(orderNo);
        return AjaxResult.success(goodsOrderByOrder);
    }

    /**
     * 获取预约订单明细详细信息
     */
    @GetMapping(value = "/goodsDetailed")
    public AjaxResult getAppointmentOrderDetailsByDetailed(String orderNo) {
        if (StringUtils.isEmpty(orderNo)) {
            return AjaxResult.error("订单编号不能为空");
        }
        return AjaxResult.success(goodsOrderService.getGoodsOrderDetailsByOrderNo(orderNo));
    }

    /**
     * 修改商品订单 添加物流单号
     */
    @PreAuthorize("@ss.hasPermi('system:goodsOrder:editExpressNo')")
    @PostMapping("/editExpressNo")
    public AjaxResult editExpressNo(@RequestBody GoodsOrder goodsOrder) {
        return toAjax(goodsOrderService.updateGoodsOrderExpressNo(goodsOrder));
    }

    /**
     * 根据订单id查询商品退款原因
     *
     * @return 记过信息
     */
    @PreAuthorize("@ss.hasPermi('system:goodsOrder:goodsOrderRefund')")
    @GetMapping(value = "/goodsOrderRefund")
    public AjaxResult goodsOrderRefund(Long goodsOrderId) {
        if (Objects.isNull(goodsOrderId)) {
            return AjaxResult.error("商品id不能为空");
        }
        return AjaxResult.success(goodsOrderService.getGoodsOrderRefund(goodsOrderId));
    }

}
