package com.fs.live.controller;

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.SecurityUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.live.domain.LiveLotteryConf;
import com.fs.live.param.LiveLotteryProductSaveParam;
import com.fs.live.service.ILiveLotteryConfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 直播抽奖配置Controller
 *
 * @author fs
 * @date 2025-07-17
 */
@RestController
@RequestMapping("/live/liveLotteryConf")
public class LiveLotteryConfController extends BaseController
{
    @Autowired
    private ILiveLotteryConfService liveLotteryConfService;

    /**
     * 查询直播抽奖配置列表
     */
//    @PreAuthorize("@ss.hasPermi('live:liveLotteryConf:list')")
    @GetMapping("/list")
    public TableDataInfo list(LiveLotteryConf liveLotteryConf)
    {
        startPage();
        List<LiveLotteryConf> list = liveLotteryConfService.selectLiveLotteryConfList(liveLotteryConf);
        return getDataTable(list);
    }

    @GetMapping("/listOn")
    public TableDataInfo listOn(LiveLotteryConf liveLotteryConf)
    {
        startPage();
        List<LiveLotteryConf> list = liveLotteryConfService.selectLiveLotteryConfListOn(liveLotteryConf);
        return getDataTable(list);
    }
    /**
     * 导出直播抽奖配置列表
     */
//    @PreAuthorize("@ss.hasPermi('live:liveLotteryConf:export')")
    @Log(title = "直播抽奖配置", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(LiveLotteryConf liveLotteryConf)
    {
        List<LiveLotteryConf> list = liveLotteryConfService.selectLiveLotteryConfList(liveLotteryConf);
        ExcelUtil<LiveLotteryConf> util = new ExcelUtil<LiveLotteryConf>(LiveLotteryConf.class);
        return util.exportExcel(list, "直播抽奖配置数据");
    }

    /**
     * 获取直播抽奖配置详细信息
     */
//    @PreAuthorize("@ss.hasPermi('live:liveLotteryConf:query')")
    @GetMapping(value = "/{lotteryId}")
    public AjaxResult getInfo(@PathVariable("lotteryId") Long lotteryId)
    {
        return AjaxResult.success(liveLotteryConfService.selectLiveLotteryConfByLotteryId(lotteryId));
    }

    /**
     * 新增直播抽奖配置
     */
//    @PreAuthorize("@ss.hasPermi('live:liveLotteryConf:add')")
    @Log(title = "直播抽奖配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LiveLotteryConf liveLotteryConf)
    {
        return toAjax(liveLotteryConfService.insertLiveLotteryConf(liveLotteryConf));
    }

    /**
     * 修改直播抽奖配置
     */
//    @PreAuthorize("@ss.hasPermi('live:liveLotteryConf:edit')")
    @Log(title = "直播抽奖配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody LiveLotteryConf liveLotteryConf)
    {
        return liveLotteryConfService.updateLiveLotteryConf(liveLotteryConf);
    }

    /**
     * 删除直播抽奖配置
     */
//    @PreAuthorize("@ss.hasPermi('live:liveLotteryConf:remove')")
//    @Log(title = "直播抽奖配置", businessType = BusinessType.DELETE)
//	@DeleteMapping("/{lotteryIds}")
//    public AjaxResult remove(@PathVariable Long[] lotteryIds)
//    {
//        return toAjax(liveLotteryConfService.deleteLiveLotteryConfByLotteryIds(lotteryIds));
//    }

//    @PreAuthorize("@ss.hasPermi('live:liveLotteryConf:query')")
    @GetMapping("/live/{liveId}")
    public List<LiveLotteryConf> getByLiveId(@PathVariable Long liveId) {
        return liveLotteryConfService.getByLiveId(liveId);
    }

//    @PreAuthorize("@ss.hasPermi('live:liveLotteryConf:remove')")
    @DeleteMapping("/{id}")
    public void delete(@PathVariable Long id) {
        liveLotteryConfService.delete(id);
    }

    /**
     * 发起抽奖
     * */
//    @PreAuthorize("@ss.hasPermi('live:liveLotteryConf:edit')")
    @GetMapping("/start/{lotteryId}")
    public String  start(@PathVariable Long lotteryId) {
       return liveLotteryConfService.startLottery(lotteryId, SecurityUtils.getLoginUser().getUser().getUserId());
    }

    /** 查询抽奖配置的所有商品信息*/
//    @PreAuthorize("@ss.hasPermi('live:liveLotteryConf:query')")
    @GetMapping("/getGoods/{lotteryId}")
    public R getGoods(@PathVariable Long lotteryId) {
        return R.ok().put("data",liveLotteryConfService.getGoods(lotteryId));
    }

    /** 抽奖商品配置保存*/
//    @PreAuthorize("@ss.hasPermi('live:liveLotteryConf:edit')")
    @PostMapping("/product")
    public void saveGoods(@RequestBody LiveLotteryProductSaveParam liveLotteryProducts) {
        //liveLotteryProducts.setCreateBy(getUsername());
        liveLotteryConfService.saveProducts(liveLotteryProducts);
    }

    /**
     * 设置自动化抽奖
     */
    @PostMapping("/setAutoLottery")
    public R setAutoLottery(@RequestParam Long lotteryId,
                            @RequestParam String autoStartTime,
                            @RequestParam String autoSettlementTime) {
        return liveLotteryConfService.setAutoLottery(lotteryId, autoStartTime, autoSettlementTime);
    }

}
