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.UserHistory;
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.IUserHistoryService;
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("/client/history")
public class UserHistoryController extends BaseController
{
    @Autowired
    private IUserHistoryService userHistoryService;

    /**
     * 查询用户播放历史列表
     */
    @GetMapping("/list")
    public TableDataInfo list(UserHistory userHistory)
    {
        startPage();
        List<UserHistory> list = userHistoryService.selectUserHistoryList(userHistory);
        return getDataTableData(list);
    }

    /**
     * 导出用户播放历史列表
     */
    @PreAuthorize("@ss.hasPermi('system:history:export')")
    @Log(title = "用户播放历史", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserHistory userHistory)
    {
        List<UserHistory> list = userHistoryService.selectUserHistoryList(userHistory);
        ExcelUtil<UserHistory> util = new ExcelUtil<UserHistory>(UserHistory.class);
        util.exportExcel(response, list, "用户播放历史数据");
    }

    /**
     * 获取用户播放历史详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:history:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(userHistoryService.selectUserHistoryById(id));
    }

    /**
     * 修改用户播放历史
     */
    @PreAuthorize("@ss.hasPermi('system:history:edit')")
    @Log(title = "用户播放历史", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserHistory userHistory)
    {
        return toAjax(userHistoryService.updateUserHistory(userHistory));
    }

    /**
     * 新增用户播放历史
     */
    @GetMapping("/add/{musicId}")
    public AjaxResult add(@PathVariable String musicId)
    {
        Long userId = SecurityUtils.getUserId();
        UserHistory userHistory = new UserHistory();
        userHistory.setMusicId(Long.parseLong(musicId));
        userHistory.setUserId(userId);
        return toAjax(userHistoryService.insertUserHistory(userHistory));
    }

    /**
     * 删除用户播放历史
     */
    @DeleteMapping("/del/history/{musicId}")
    public AjaxResult removeHistory(@PathVariable String[] musicId)
    {
        userHistoryService.deleteUserHistoryByMusicId(musicId);
        return AjaxResult.success();
    }

    /**
     * 删除用户播放历史
     */
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(userHistoryService.deleteUserHistoryByIds(ids));
    }
}
