package com.ruoyi.web.controller.client;

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.MusicCollect;
import com.ruoyi.common.core.domain.entity.UserCollect;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.IMusicCollectService;
import com.ruoyi.system.service.IUserCollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import oshi.util.platform.mac.SysctlUtil;

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

/**
 * 用户收藏音乐Controller
 * 
 * @author ruoyi
 * @date 2025-07-15
 */
@RestController
@RequestMapping("/client/collect")
public class UserCollectController extends BaseController
{
    @Autowired
    private IUserCollectService userCollectService;
    @Autowired
    private IMusicCollectService musicCollectService;

    /**
     * 查询用户收藏音乐列表
     */
    @GetMapping("/list")
    public TableDataInfo list(UserCollect userCollect)
    {
        startPage();
        Long userId = SecurityUtils.getUserId();
        userCollect.setUserId(userId);
        List<UserCollect> list = userCollectService.selectUserCollectList(userCollect);
        return getDataTableData(list);
    }

    /**
     * 导出用户收藏音乐列表
     */
    @PreAuthorize("@ss.hasPermi('system:collect:export')")
    @Log(title = "用户收藏音乐", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserCollect userCollect)
    {
        List<UserCollect> list = userCollectService.selectUserCollectList(userCollect);
        ExcelUtil<UserCollect> util = new ExcelUtil<UserCollect>(UserCollect.class);
        util.exportExcel(response, list, "用户收藏音乐数据");
    }

    /**
     * 获取用户收藏音乐详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:collect:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(userCollectService.selectUserCollectById(id));
    }

    /**
     * 新增用户收藏音乐
     */
    @PostMapping("/create")
    public AjaxResult add(@RequestBody UserCollect userCollect)
    {
        userCollectService.insertUserCollect(userCollect);
        return AjaxResult.success();
    }


    /**
     * 新增用户收藏音乐
     */
    @PostMapping("/bind")
    public AjaxResult bind(@RequestBody MusicCollect musicCollect)
    {
        musicCollectService.insertMusicCollect(musicCollect);
        return AjaxResult.success();
    }


    /**
     * 删除歌单
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        int del = userCollectService.deleteUserCollectByIds(ids);
        return AjaxResult.success();
    }


    /**
     * 取消收藏歌单
     */
    @PostMapping("/cancel/bind")
    public AjaxResult cancel(@RequestBody MusicCollect musicCollect)
    {
        userCollectService.cancelBindMusic(musicCollect);
        return AjaxResult.success();
    }




    /**
     * 修改用户收藏音乐
     */
    @PreAuthorize("@ss.hasPermi('system:collect:edit')")
    @Log(title = "用户收藏音乐", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserCollect userCollect)
    {
        return toAjax(userCollectService.updateUserCollect(userCollect));
    }


}
