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.MusicMaxReq;
import com.ruoyi.common.core.domain.entity.MusicScene;
import com.ruoyi.common.core.domain.entity.MusicSceneRelate;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.MusicType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.mapper.MusicSceneMapper;
import com.ruoyi.system.mapper.MusicSceneRelateMapper;
import com.ruoyi.system.service.IMusicInfoService;
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;
import java.util.Map;

/**
 * 音乐信息Controller
 *
 * @author ruoyi
 * @date 2025-07-15
 */
@RestController
@RequestMapping("/back/music")
public class MusicInfoController extends BaseController
{
    @Autowired
    private IMusicInfoService musicInfoService;
    @Autowired
    private MusicSceneRelateMapper relateMapper;
    @Autowired
    private MusicSceneMapper musicSceneMapper;

    /**
     * 查询音乐信息列表
     */
//    @PreAuthorize("@ss.hasPermi('system:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(MusicInfo musicInfo)
    {
        startPage();
        List<MusicInfo> list = musicInfoService.selectMusicInfoList(musicInfo);
        // 如果是混音查询混音的信息
        if (musicInfo.getMusicType() != null && musicInfo.getMusicType().equals(MusicType.MIXING.getMusicType())){
            for (MusicInfo info : list) {
                // 查询混音相关信息
                MusicSceneRelate relateInfo = relateMapper.selectByMusicId(String.valueOf(info.getMusicId()));
                if (relateInfo != null) {
                    String sceneIds = relateInfo.getSceneIds();
                    String[] sceneArray = sceneIds.split(",");
                    List<MusicScene> musicScenes = musicSceneMapper.selectMusicSceneBySceneIds(sceneArray);
                    relateInfo.setMusicScenes(musicScenes);
                    info.setMusicScene(relateInfo);
                }
            }
            //list = musicInfoService.findMusicByCate(musicInfo);
        }

        return getDataTable(list);
    }

    /**
     * 导出音乐信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:info:export')")
    @Log(title = "音乐信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MusicInfo musicInfo)
    {
        List<MusicInfo> list = musicInfoService.selectMusicInfoList(musicInfo);
        ExcelUtil<MusicInfo> util = new ExcelUtil<MusicInfo>(MusicInfo.class);
        util.exportExcel(response, list, "音乐信息数据");
    }

    /**
     * 获取音乐信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(musicInfoService.selectMusicInfoById(id));
    }

//    /**
//     * 新增音乐信息
//     */
//    @PreAuthorize("@ss.hasPermi('system:info:add')")
//    @Log(title = "音乐信息", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add(@RequestBody MusicInfo musicInfo)
//    {
//        return toAjax(musicInfoService.insertMusicInfo(musicInfo));
//    }

    /**
     * 修改音乐信息
     */
//    @PreAuthorize("@ss.hasPermi('system:info:edit')")
//    @Log(title = "音乐信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MusicInfo musicInfo)
    {
        return toAjax(musicInfoService.updateMusicInfo(musicInfo));
    }

    /**
     * 删除音乐信息
     */
    @PreAuthorize("@ss.hasPermi('system:info:remove')")
    @Log(title = "音乐信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(musicInfoService.deleteMusicInfoByIds(ids));
    }


    /**
     * CREATE TABLE `music_info` (
     *   `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT 'id',
     *   `music_id` bigint DEFAULT NULL COMMENT '音乐id',
     *   `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '名字',
     *   `author` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '作者',
     *   `vip` int DEFAULT NULL COMMENT '1 vip 2 不',
     *   `shelf` int DEFAULT NULL COMMENT '1 商家 2 下架',
     *   `img_addr` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '封面',
     *   `music_addr` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '音乐地址',
     *   `music_type` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '音乐类型 ordinary 混音mixing',
     *   `creator` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '创建人',
     *   `modify` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '修改人',
     *   `create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
     *   `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
     *   `is_del` int DEFAULT '0' COMMENT '1 删除',
     *   `label` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '标签',
     *   PRIMARY KEY (`id`) USING BTREE
     * ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci ROW_FORMAT=DYNAMIC COMMENT='音乐信息表';
     */
    //@PreAuthorize("@ss.hasPermi('system:info:add')")
    //@Log(title = "音乐信息", businessType = BusinessType.INSERT)
    @PostMapping("/addMusic")
    public AjaxResult add(@RequestBody MusicInfo musicInfo) {
        return toAjax(musicInfoService.insertMusicInfo(musicInfo));
    }

    @RequestMapping("/add/mixMusic")
    public AjaxResult addMixMusicInfo(@RequestBody MusicMaxReq param){
        MusicInfo newMusic = musicInfoService.addMixMusicInfo(param);
        if (newMusic != null){
            return AjaxResult.success(newMusic);
        }
        return AjaxResult.error("混音错误");
    }

    // 编辑混音歌曲
    @RequestMapping("/edit/mixMusic")
    public AjaxResult editMixMusicInfo(@RequestBody MusicMaxReq param){
        MusicInfo newMusic = musicInfoService.editMixMusicInfo(param);
        if (newMusic != null){
            return AjaxResult.success(newMusic);
        }
        return AjaxResult.error("混音错误");
    }
}
