package com.xinelu.manage.controller.scriptInfo;

import com.xinelu.common.annotation.Log;
import com.xinelu.common.constant.Constants;
import com.xinelu.common.core.controller.BaseController;
import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.common.core.page.TableDataInfo;
import com.xinelu.common.enums.BusinessType;
import com.xinelu.common.utils.poi.ExcelUtil;
import com.xinelu.manage.domain.scriptInfo.ScriptInfo;
import com.xinelu.manage.dto.script.ScriptInfoDto;
import com.xinelu.manage.service.scriptInfo.IScriptInfoService;
import com.xinelu.manage.vo.scriptInfo.ScriptVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

/**
 * 话术信息Controller
 *
 * @author xinelu
 * @date 2024-02-26
 */
@Api(tags = "话术库控制器")
@RestController
@RequestMapping("/manage/script")
public class ScriptInfoController extends BaseController {
    @Resource
    private IScriptInfoService scriptInfoService;

    /**
     * 查询话术信息列表
     */
    @ApiOperation("查询话术信息列表")
    @GetMapping("/list")
    public TableDataInfo list(ScriptInfoDto scriptInfo) {
        startPage();
        List<ScriptInfo> list = scriptInfoService.selectScriptInfoList(scriptInfo);
        return getDataTable(list);
    }

    /**
     * 导出话术信息列表
     */
    @ApiOperation("导出话术信息列表")
    @PreAuthorize("@ss.hasPermi('manage:script:export')")
    @Log(title = "话术信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScriptInfoDto scriptInfo) {
        List<ScriptInfo> list = scriptInfoService.selectScriptInfoList(scriptInfo);
        ExcelUtil<ScriptInfo> util = new ExcelUtil<ScriptInfo>(ScriptInfo.class);
        util.exportExcel(response, list, "话术信息数据");
    }

    /**
     * 获取话术信息详细信息
     */
    @ApiOperation("获取话术信息详细信息")
    @PreAuthorize("@ss.hasPermi('manage:script:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(scriptInfoService.selectScriptInfoById(id));
    }

    /**
     * 新增话术列表信息
     */
    @ApiOperation("新增话术信息")
    @PreAuthorize("@ss.hasPermi('manage:script:add')")
    @Log(title = "话术信息", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResult add(@RequestBody ScriptInfo scriptInfo) {
        return toAjax(scriptInfoService.insertScriptInfo(scriptInfo));
    }

    /**
     * 修改话术信息
     */
    @ApiOperation("修改话术信息")
    @PreAuthorize("@ss.hasPermi('manage:script:edit')")
    @Log(title = "话术信息", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/edit")
    public AjaxResult edit(@RequestBody ScriptInfo scriptInfo) {
        return toAjax(scriptInfoService.updateScriptInfo(scriptInfo));
    }

    /**
     * 删除话术信息
     */
    @ApiOperation("删除话术信息")
    @PreAuthorize("@ss.hasPermi('manage:script:remove')")
    @Log(title = "话术信息", businessType = BusinessType.DELETE)
    @DeleteMapping("remove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(scriptInfoService.deleteScriptInfoByIds(ids));
    }

    /**
     * 话术图片上传
     */
    @PostMapping("/uploadScriptInfo")
    public AjaxResult uploadScriptInfo(@RequestParam("file") MultipartFile multipartFile) throws Exception {
        if (Objects.isNull(multipartFile) || StringUtils.isBlank(multipartFile.getOriginalFilename())) {
            return AjaxResult.error("当前文件不存在，无法上传！");
        }
        if (multipartFile.getOriginalFilename().contains(Constants.EMPTY)) {
            return AjaxResult.error("当前文件名含有空格，请先去除空格在上传！");
        }
        return scriptInfoService.uploadScriptInfo(multipartFile);
    }

    /**
     * 查询话术节点
     */
    @GetMapping("/selectScriptEdgeNode")
    public AjaxResult selectScriptEdgeNode(Long id) {
        return scriptInfoService.selectScriptEdgeNode(id);
    }

    /**
     * 新增话术节点(暂时不用)
     */
    @PostMapping("/insertScriptEdgeNode")
    public AjaxResult insertScriptEdgeNode(@RequestBody ScriptVO scriptVO) {
        return scriptInfoService.insertScriptEdgeNode(scriptVO);
    }

    /**
     * 修改话术节点
     */
    @PostMapping("/updateScriptEdgeNode")
    public AjaxResult updateScriptEdgeNode(@RequestBody ScriptVO scriptVO) {
        return scriptInfoService.updateScriptEdgeNode(scriptVO);
    }

    /**
     * 查询话术变量
     */
    @GetMapping("/selectScriptVariable")
    public AjaxResult selectScriptVariable(Long id) {
        return scriptInfoService.selectScriptVariable(id);
    }
}
