package com.ruoyi.web.controller.back;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.MusicInfo;
import com.ruoyi.common.core.domain.entity.RecommendInfo;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.IMusicInfoService;
import com.ruoyi.system.service.IRecommendInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 推荐Controller
 * 
 * @author ruoyi
 * @date 2025-07-15
 */
@RestController
@RequestMapping("/back/re")
public class RecommendInfoController extends BaseController
{
    @Autowired
    private IRecommendInfoService recommendInfoService;
    @Autowired
    private IMusicInfoService musicInfoService;

    /**
     * 查询推荐列表
     */
    //@PreAuthorize("@ss.hasPermi('system:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(RecommendInfo recommendInfo)
    {
        startPage();
        List<RecommendInfo> list = recommendInfoService.selectRecommendInfoList(recommendInfo);
        return getDataTable(list);
    }

    /**
     * 导出推荐列表
     */
    @PreAuthorize("@ss.hasPermi('system:info:export')")
    @Log(title = "推荐", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RecommendInfo recommendInfo)
    {
        List<RecommendInfo> list = recommendInfoService.selectRecommendInfoList(recommendInfo);
        ExcelUtil<RecommendInfo> util = new ExcelUtil<RecommendInfo>(RecommendInfo.class);
        util.exportExcel(response, list, "推荐数据");
    }

    /**
     * 获取推荐详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(recommendInfoService.selectRecommendInfoById(id));
    }

    /**
     * 新增推荐
     */
//    @PreAuthorize("@ss.hasPermi('system:info:add')")
//    @Log(title = "推荐", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RecommendInfo recommendInfo)
    {
        return toAjax(recommendInfoService.insertRecommendInfo(recommendInfo));
    }

    /**
     * 修改推荐
     */
    @PreAuthorize("@ss.hasPermi('system:info:edit')")
    @Log(title = "推荐", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RecommendInfo recommendInfo)
    {
        return toAjax(recommendInfoService.updateRecommendInfo(recommendInfo));
    }

    /**
     * 删除推荐
     */
    @PreAuthorize("@ss.hasPermi('system:info:remove')")
    @Log(title = "推荐", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(recommendInfoService.deleteRecommendInfoByIds(ids));
    }

    // 绑定推荐音乐
    @PutMapping("/bind/{reId}/{musicIds}")
    public AjaxResult bindMusic(@PathVariable String reId,@PathVariable String[] musicIds){
        int insertCount = recommendInfoService.bindMusic(reId,musicIds);
        if (insertCount > 0){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    // 根据推荐获取分类音乐
    @GetMapping("/music/{reId}")
    public TableDataInfo geReIdMusic(@PathVariable String reId){
        startPage();
        List<MusicInfo> list = musicInfoService.selectMusicInfoByReId(reId);
        return getDataTable(list);
    }

}
