package com.cmvr.web.controller.tts;

import com.cmvr.common.annotation.Log;
import com.cmvr.common.core.controller.BaseController;
import com.cmvr.common.core.domain.AjaxResult;
import com.cmvr.common.core.page.TableDataInfo;
import com.cmvr.common.enums.BusinessType;
import com.cmvr.tts.domain.CorpusCategory;
import com.cmvr.tts.domain.CorpusInfo;
import com.cmvr.tts.service.ICorpusInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import jakarta.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 车载语料数据Controller
 *
 * @author cmvr
 */
@RestController
@RequestMapping("/tts/corpus")
@Api(tags = "智能座舱-车载语料管理")
public class CorpusInfoController extends BaseController {

    @Autowired
    private ICorpusInfoService corpusInfoService;

    /**
     * 查询语料列表
     */
    @ApiOperation("查询语料列表")
    @PreAuthorize("@ss.hasPermi('tts:corpus:list')")
    @GetMapping("/list")
    public TableDataInfo list(CorpusInfo corpus) {
        startPage();
        return corpusInfoService.selectCorpusList(corpus);
    }

    /**
     * 获取语料详细信息
     */
    @ApiOperation("获取语料详细信息")
    @PreAuthorize("@ss.hasPermi('tts:corpus:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(corpusInfoService.selectCorpusById(id));
    }

    /**
     * 新增语料
     */
    @ApiOperation("新增语料")
    @PreAuthorize("@ss.hasPermi('tts:corpus:add')")
    @Log(title = "车载语料", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CorpusInfo corpus) {
        return toAjax(corpusInfoService.insertCorpus(corpus));
    }

    /**
     * 修改语料
     */
    @ApiOperation("修改语料")
    @PreAuthorize("@ss.hasPermi('tts:corpus:edit')")
    @Log(title = "车载语料", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CorpusInfo corpus) {
        return toAjax(corpusInfoService.updateCorpus(corpus));
    }

    /**
     * 删除语料
     */
    @ApiOperation("删除语料")
    @PreAuthorize("@ss.hasPermi('tts:corpus:remove')")
    @Log(title = "车载语料", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(corpusInfoService.deleteCorpusByIds(ids));
    }

    /**
     * 获取音频流
     */
    @ApiOperation("获取音频流")
    @PreAuthorize("@ss.hasPermi('tts:corpus:query')")
    @GetMapping("/audio/{id}")
    public void getAudio(@PathVariable String id, HttpServletResponse response) {
        corpusInfoService.getAudioStream(id, response);
    }

    /**
     * 上传语料音频
     */
    @ApiOperation("上传语料音频")
    @PreAuthorize("@ss.hasPermi('tts:corpus:edit')")
    @Log(title = "车载语料", businessType = BusinessType.UPDATE)
    @PostMapping("/audio/upload/{id}")
    public AjaxResult uploadAudio(@PathVariable String id, @RequestParam("file") MultipartFile file) {
        String path = corpusInfoService.uploadCorpusAudio(id, file);
        return success(path);
    }

    /**
     * 批量导入语料
     */
    @ApiOperation("批量导入语料")
    @PreAuthorize("@ss.hasPermi('tts:corpus:import')")
    @Log(title = "车载语料", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult importData(@RequestParam("file") MultipartFile file) {
        Map<String, Object> result = corpusInfoService.importCorpus(file);
        return success(result);
    }

    /**
     * 查询看板统计数据
     */
    @ApiOperation("查询看板统计数据")
    @PreAuthorize("@ss.hasPermi('tts:corpus:query')")
    @GetMapping("/dashboard/stats")
    public AjaxResult getDashboardStats() {
        return success(corpusInfoService.getDashboardStats());
    }

    /**
     * 按语种统计
     */
    @ApiOperation("按语种统计")
    @PreAuthorize("@ss.hasPermi('tts:corpus:query')")
    @GetMapping("/stats/language")
    public AjaxResult getStatsByLanguage() {
        return success(corpusInfoService.getStatsByLanguage());
    }

    /**
     * 按方言统计
     */
    @ApiOperation("按方言统计")
    @PreAuthorize("@ss.hasPermi('tts:corpus:query')")
    @GetMapping("/stats/dialect")
    public AjaxResult getStatsByDialect() {
        return success(corpusInfoService.getStatsByDialect());
    }

    /**
     * 按情绪统计
     */
    @ApiOperation("按情绪统计")
    @PreAuthorize("@ss.hasPermi('tts:corpus:query')")
    @GetMapping("/stats/emotion")
    public AjaxResult getStatsByEmotion() {
        return success(corpusInfoService.getStatsByEmotion());
    }

    /**
     * 按分类统计
     */
    @ApiOperation("按分类统计")
    @PreAuthorize("@ss.hasPermi('tts:corpus:query')")
    @GetMapping("/stats/category")
    public AjaxResult getStatsByCategory() {
        return success(corpusInfoService.getStatsByCategory());
    }

    /**
     * 查询分类树
     */
    @ApiOperation("查询分类树")
    @PreAuthorize("@ss.hasPermi('tts:corpus:query')")
    @GetMapping("/category/tree")
    public AjaxResult getCategoryTree() {
        return success(corpusInfoService.getCategoryTree());
    }

    /**
     * 新增分类
     */
    @ApiOperation("新增分类")
    @PreAuthorize("@ss.hasPermi('tts:corpus:add')")
    @Log(title = "语料分类", businessType = BusinessType.INSERT)
    @PostMapping("/category")
    public AjaxResult addCategory(@RequestBody CorpusCategory category) {
        return toAjax(corpusInfoService.insertCategory(category));
    }

    /**
     * 修改分类
     */
    @ApiOperation("修改分类")
    @PreAuthorize("@ss.hasPermi('tts:corpus:edit')")
    @Log(title = "语料分类", businessType = BusinessType.UPDATE)
    @PutMapping("/category")
    public AjaxResult editCategory(@RequestBody CorpusCategory category) {
        return toAjax(corpusInfoService.updateCategory(category));
    }

    /**
     * 删除分类
     */
    @ApiOperation("删除分类")
    @PreAuthorize("@ss.hasPermi('tts:corpus:remove')")
    @Log(title = "语料分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/category/{id}")
    public AjaxResult removeCategory(@PathVariable String id) {
        return toAjax(corpusInfoService.deleteCategoryById(id));
    }
}
