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.domain.R;
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.LoginUser;
import com.fs.framework.security.SecurityUtils;
import com.fs.his.service.IFsStoreProductService;
import com.fs.hisStore.service.IFsStoreProductScrmService;
import com.fs.hisStore.vo.FsStoreProductListVO;
import com.fs.live.domain.LiveGoods;
import com.fs.live.service.ILiveGoodsService;
import com.fs.live.vo.LiveGoodsListVo;
import com.fs.live.vo.LiveGoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 直播商品Controller
 *
 * @author fs
 * @date 2025-01-17
 */
@RestController
@RequestMapping("/live/liveGoods")
public class LiveGoodsController extends BaseController
{
    @Autowired
    private ILiveGoodsService liveGoodsService;

    @Autowired
    private IFsStoreProductScrmService fsStoreProductService;

    /**
     * 查询直播商品列表
     */
////    @PreAuthorize("@ss.hasPermi('live:liveGoods:list')")
    @GetMapping("/list")
    public TableDataInfo list(LiveGoods liveGoods, @RequestParam(value = "liveId", required = true) Long liveId)
    {
        // 设置企业ID和企业用户ID
        setCompanyId(liveGoods);
        liveGoods.setLiveId(liveId);

        startPage();
        List<LiveGoodsVo> list = liveGoodsService.selectProductListByLiveId(liveGoods);
        return getDataTable(list);
    }

    /**
     * 查询商品列表
     */
    @PreAuthorize("@ss.hasPermi('store:storeProduct:list')")
    @GetMapping("/liveList")
    public TableDataInfo liveList(LiveGoods liveGoods)
    {
        startPage();
        List<FsStoreProductListVO> list = fsStoreProductService.liveList(liveGoods);
        return getDataTable(list);
    }

    /**
     * 导出直播商品列表
     */
////    @PreAuthorize("@ss.hasPermi('live:liveGoods:export')")
    @Log(title = "直播商品", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(LiveGoods liveGoods)
    {
        // 设置企业ID和企业用户ID
        setCompanyId(liveGoods);

        List<LiveGoods> list = liveGoodsService.selectLiveGoodsList(liveGoods);
        ExcelUtil<LiveGoods> util = new ExcelUtil<LiveGoods>(LiveGoods.class);
        return util.exportExcel(list, "直播商品数据");
    }

    /**
     * 获取直播商品详细信息
     */
//    @PreAuthorize("@ss.hasPermi('live:liveGoods:query')")
    @GetMapping(value = "/{goodsId}")
    public AjaxResult getInfo(@PathVariable("goodsId") Long goodsId)
    {
        CompanyUser user = SecurityUtils.getLoginUser().getUser();
        return AjaxResult.success();
    }

    /**
     * 新增直播商品
     */
//    @PreAuthorize("@ss.hasPermi('live:liveGoods:add')")
    @Log(title = "直播商品", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody Map<String, Object> payload)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        CompanyUser user = loginUser.getUser();
        return liveGoodsService.insertLiveGoods(payload,user);
    }

    /**
     * 修改直播商品
     */
//    @PreAuthorize("@ss.hasPermi('live:liveGoods:edit')")
    @Log(title = "直播商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody LiveGoods liveGoods)
    {
        return liveGoodsService.updateLiveGoods(liveGoods);
    }

    /**
     * 删除直播商品
     */
//    @PreAuthorize("@ss.hasPermi('live:liveGoods:remove')")
    @Log(title = "直播商品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{goodsIds}")
    public AjaxResult remove(@PathVariable Long[] goodsIds)
    {
        return toAjax(liveGoodsService.deleteLiveGoodsByGoodsIds(goodsIds));
    }

    /**
     * 设置企业ID 企业用户ID
     * @param liveGoods 直播商品
     */
    private void setCompanyId(LiveGoods liveGoods) {
        // 设置企业ID 企业用户ID
        LoginUser loginUser = SecurityUtils.getLoginUser();
        CompanyUser user = loginUser.getUser();
        liveGoods.setCompanyId(user.getCompanyId());
        liveGoods.setCompanyUserId(user.getUserId());
    }

    /**
     * 批量上下架视频
     */
//    @PreAuthorize("@ss.hasPermi('live:liveGoods:edit')")
    @PostMapping("/handleShelfOrUn")
    public R handleShelfOrUn(@RequestBody LiveGoodsListVo listVo) {
        setListCompanyId(listVo);
        return liveGoodsService.handleShelfOrUn(listVo);
    }

    /**
     * 批量删除视频
     */
//    @PreAuthorize("@ss.hasPermi('live:liveGoods:remove')")
    @PostMapping("/handleDeleteSelected")
    public R handleDeleteSelected(@RequestBody LiveGoodsListVo listVo) {
        setListCompanyId(listVo);
        return liveGoodsService.handleDeleteSelected(listVo);
    }

    /**
     * 更新展示状态
     */
//    @PreAuthorize("@ss.hasPermi('live:liveGoods:edit')")
    @PostMapping("/handleIsShowChange")
    public R handleIsShowChange(@RequestBody LiveGoodsListVo listVo) {
        setListCompanyId(listVo);
        return liveGoodsService.handleIsShowChange(listVo);
    }

    /**
     * 设置企业ID和企业用户ID
     */
    private void setListCompanyId(LiveGoodsListVo live) {
        CompanyUser user = SecurityUtils.getLoginUser().getUser();
        live.setCompanyId(user.getCompanyId());
        live.setCompanyUserId(user.getUserId());
    }
}
