package com.fs.live.controller;

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

import com.fs.common.core.domain.R;
import com.fs.live.domain.LiveCouponIssue;
import com.fs.live.param.LiveCouponPublishParam;
import com.fs.live.service.ILiveCouponIssueService;
import com.fs.live.vo.LiveCouponListVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.enums.BusinessType;
import com.fs.live.domain.LiveCoupon;
import com.fs.live.service.ILiveCouponService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 优惠券Controller
 *
 * @author fs
 * @date 2025-09-30
 */
@RestController
@RequestMapping("/live/coupon")
public class LiveCouponController extends BaseController
{
    @Autowired
    private ILiveCouponService liveCouponService;
    @Autowired
    private ILiveCouponIssueService liveCouponIssueService;


    /**
     * 查询优惠券列表
     */
//    @PreAuthorize("@ss.hasPermi('live:liveCoupon:list')")
    @GetMapping("/list")
    public TableDataInfo list(LiveCoupon liveCoupon)
    {
        startPage();
        liveCoupon.setIsDel(0);
        List<LiveCoupon> list = liveCouponService.selectLiveCouponList(liveCoupon);
        return getDataTable(list);
    }

    /**
     * 导出优惠券列表
     */
//    @PreAuthorize("@ss.hasPermi('live:liveCoupon:export')")
    @Log(title = "优惠券", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(LiveCoupon liveCoupon)
    {
        List<LiveCoupon> list = liveCouponService.selectLiveCouponList(liveCoupon);
        ExcelUtil<LiveCoupon> util = new ExcelUtil<LiveCoupon>(LiveCoupon.class);
        return util.exportExcel(list, "coupon");
    }

    /**
     * 获取优惠券详细信息
     */
//    @PreAuthorize("@ss.hasPermi('live:liveCoupon:query')")
    @GetMapping(value = "/{couponId}")
    public AjaxResult getInfo(@PathVariable("couponId") Long couponId)
    {
        return AjaxResult.success(liveCouponService.selectLiveCouponById(couponId));
    }

    /**
     * 新增优惠券
     */
//    @PreAuthorize("@ss.hasPermi('live:liveCoupon:add')")
    @Log(title = "优惠券", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LiveCoupon liveCoupon)
    {
        return toAjax(liveCouponService.insertLiveCoupon(liveCoupon));
    }

    /**
     * 修改优惠券
     */
//    @PreAuthorize("@ss.hasPermi('live:liveCoupon:edit')")
    @Log(title = "优惠券", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LiveCoupon liveCoupon)
    {
        return toAjax(liveCouponService.updateLiveCoupon(liveCoupon));
    }

    /**
     * 删除优惠券
     */
//    @PreAuthorize("@ss.hasPermi('live:liveCoupon:remove')")
    @Log(title = "优惠券", businessType = BusinessType.DELETE)
	@DeleteMapping("/{couponIds}")
    public AjaxResult remove(@PathVariable Long[] couponIds)
    {
        return toAjax(liveCouponService.deleteLiveCouponByIds(couponIds));
    }

//    @PreAuthorize("@ss.hasPermi('live:liveCoupon:publish')")
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody LiveCouponPublishParam publishParam)
    {
        LiveCoupon coupon=liveCouponService.selectLiveCouponById(publishParam.getCouponId());
        LiveCouponIssue issue=new LiveCouponIssue();
        issue.setCouponId(publishParam.getCouponId());
        issue.setCouponName(coupon.getTitle());
        issue.setCouponType(Math.toIntExact(coupon.getType()));
        issue.setStartTime(publishParam.getStartTime());
        issue.setLimitTime(publishParam.getLimitTime());
        issue.setTotalCount(Long.valueOf(publishParam.getTotalCount()));
        issue.setRemainCount(Long.valueOf(publishParam.getTotalCount()));
        issue.setIsPermanent(0);
        issue.setStatus(1);
        issue.setCreateTime(new Date());
        // 继承优惠券的领取上限字段
        issue.setLimitReceiveCount(coupon.getLimitReceiveCount());
        return toAjax( liveCouponIssueService.insertLiveCouponIssue(issue));
    }

//    @PreAuthorize("@ss.hasPermi('live:liveCoupon:batchPublish')")
    @PostMapping("/batchPublish")
    public R batchPublish(@RequestBody LiveCouponPublishParam publishParam)
    {

        List<LiveCoupon> list=liveCouponService.selectLiveCouponByIds(publishParam.getIds());
        for (LiveCoupon coupon :list ){
            LiveCouponIssue issue=new LiveCouponIssue();
            issue.setCouponId(coupon.getCouponId());
            issue.setCouponName(coupon.getTitle());
            issue.setCouponType(Math.toIntExact(coupon.getType()));
            issue.setStartTime(publishParam.getStartTime());
            issue.setLimitTime(publishParam.getLimitTime());
            issue.setTotalCount(Long.valueOf(publishParam.getTotalCount()));
            issue.setRemainCount(Long.valueOf(publishParam.getTotalCount()));
            issue.setIsPermanent(0);
            issue.setStatus(1);
            issue.setCreateTime(new Date());
            // 继承优惠券的领取上限字段
            issue.setLimitReceiveCount(coupon.getLimitReceiveCount());
            liveCouponIssueService.insertLiveCouponIssue(issue);
        }
        return R.ok();
    }

//    @PreAuthorize("@ss.hasPermi('live:liveCoupon:query')")
    @GetMapping(value = "/listLiveCoupon")
    public TableDataInfo listLiveCoupon(@RequestParam("liveId") Long liveId)
    {
        startPage();
        List<LiveCoupon> list = liveCouponService.selectLiveCouponByLiveId(liveId);
        return getDataTable(list);
    }
//    @PreAuthorize("@ss.hasPermi('live:liveCoupon:query')")
    @GetMapping(value = "/listCoupon")
    public TableDataInfo listCoupon(@RequestParam("liveId") Long liveId,@RequestParam("couponName") String couponName)
    {
        startPage();
        List<LiveCoupon> list = liveCouponService.listCoupon(liveId,couponName);
        return getDataTable(list);
    }

    /**
     * 查询优惠券列表
     */
//    @PreAuthorize("@ss.hasPermi('live:liveCoupon:list')")
    @GetMapping("/listOn")
    public TableDataInfo listOn(@RequestParam("liveId") Long liveId)
    {
        startPage();
        List<LiveCoupon> list = liveCouponService.listOn(liveId);
        return getDataTable(list);
    }


//    @PreAuthorize("@ss.hasPermi('live:liveCoupon:add')")
    @Log(title = "直播优惠券", businessType = BusinessType.INSERT)
    @PostMapping("/delLiveCoupon")
    public AjaxResult delLiveCoupon(@RequestBody LiveCouponListVo vo)
    {
        return toAjax(liveCouponService.delLiveCoupon(vo));
    }

    /**
     * 新增直播商品
     */
//    @PreAuthorize("@ss.hasPermi('live:liveCoupon:add')")
    @Log(title = "直播优惠券", businessType = BusinessType.INSERT)
    @PostMapping("/addLiveCoupon")
    public AjaxResult addLiveCoupon(@RequestBody Map<String, Object> payload)
    {

        return toAjax(liveCouponService.insertLiveCouponEntity(payload));
    }

//    @PreAuthorize("@ss.hasPermi('live:liveCoupon:edit')")
    @PostMapping("/handleIsShowChange")
    public R handleIsShowChange(@RequestBody Map<String, Object> payload) {
        return liveCouponService.handleIsShowChange(payload);
    }

    /**
     * 批量删除视频
     */
//    @PreAuthorize("@ss.hasPermi('live:liveCoupon:edit')")
    @PostMapping("/handleDeleteSelected")
    public R handleDeleteSelected(@RequestBody LiveCouponListVo listVo) {
        return liveCouponService.handleDeleteSelectedAdmin(listVo);
    }
    /**
     * 批量删除视频
     */
//    @PreAuthorize("@ss.hasPermi('live:liveCoupon:edit')")
    @PostMapping("/updateLiveCouponBind")
    public R updateLiveCouponBind(@RequestBody LiveCouponListVo listVo) {
        return liveCouponService.updateLiveCouponBind(listVo);
    }
}
