package com.fs.company.controller.live;

import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.company.domain.CompanyUser;
import com.fs.framework.security.SecurityUtils;
import com.fs.live.domain.LiveGoodsOrder;
import com.fs.live.service.ILiveGoodsOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单Controller
 * 
 * @author fs
 * @date 2025-01-17
 */
@RestController
@RequestMapping("/live/liveOrder")
public class LiveGoodsOrderController extends BaseController
{
    @Autowired
    private ILiveGoodsOrderService liveGoodsOrderService;

    /**
     * 查询订单列表
     */
    @PreAuthorize("@ss.hasPermi('live:liveOrder:list')")
    @GetMapping("/list")
    public TableDataInfo list(LiveGoodsOrder liveGoodsOrder)
    {
        // 设置企业ID和企业用户ID
        setCompanyId(liveGoodsOrder);

        startPage();
        List<LiveGoodsOrder> list = liveGoodsOrderService.selectLiveGoodsOrderList(liveGoodsOrder);
        return getDataTable(list);
    }

    /**
     * 导出订单列表
     */
    @PreAuthorize("@ss.hasPermi('live:liveOrder:export')")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(LiveGoodsOrder liveGoodsOrder)
    {
        // 设置企业ID和企业用户ID
        setCompanyId(liveGoodsOrder);

        List<LiveGoodsOrder> list = liveGoodsOrderService.selectLiveGoodsOrderList(liveGoodsOrder);
        ExcelUtil<LiveGoodsOrder> util = new ExcelUtil<LiveGoodsOrder>(LiveGoodsOrder.class);
        return util.exportExcel(list, "订单数据");
    }

    /**
     * 获取订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('live:liveOrder:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId)
    {
        CompanyUser user = SecurityUtils.getLoginUser().getUser();
        return AjaxResult.success(liveGoodsOrderService.selectLiveGoodsOrderByOrderIdAndCompanyIdAndCompanyUserId(orderId, user.getCompanyId(), user.getUserId()));
    }

    /**
     * 新增订单
     */
    @PreAuthorize("@ss.hasPermi('live:liveOrder:add')")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LiveGoodsOrder liveGoodsOrder)
    {
        // 设置企业ID和企业用户ID
        setCompanyId(liveGoodsOrder);
        return toAjax(liveGoodsOrderService.insertLiveGoodsOrder(liveGoodsOrder));
    }

    /**
     * 修改订单
     */
    @PreAuthorize("@ss.hasPermi('live:liveOrder:edit')")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LiveGoodsOrder liveGoodsOrder)
    {
        return toAjax(liveGoodsOrderService.updateLiveGoodsOrder(liveGoodsOrder));
    }

    /**
     * 删除订单
     */
    @PreAuthorize("@ss.hasPermi('live:liveOrder:remove')")
    @Log(title = "订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds)
    {
        return toAjax(liveGoodsOrderService.deleteLiveGoodsOrderByOrderIds(orderIds));
    }

    /**
     * 设置企业ID和企业用户ID
     * @param liveGoodsOrder 直播商品订单
     */
    private void setCompanyId(LiveGoodsOrder liveGoodsOrder) {
        CompanyUser user = SecurityUtils.getLoginUser().getUser();
        liveGoodsOrder.setCompanyId(user.getCompanyId());
        liveGoodsOrder.setCompanyUserId(user.getUserId());
    }
}
