package com.ruoyi.biemo.business.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.biemo.business.domain.Report;
import com.ruoyi.biemo.business.service.ReportService;
import com.ruoyi.biemo.core.page.Page;
import com.ruoyi.biemo.core.page.PageFactory;
import com.ruoyi.biemo.nlp.SentimentAnalysisUtils;
import com.ruoyi.biemo.nlp.WordVectorModelUtils;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
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 javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 报告提交Controller
 *
 * @author makesoft
 * @date 2022-09-18
 */
@RestController
@RequestMapping("/makesoft/report")
public class ReportController
{

    @Autowired
    private ReportService reportService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysDeptService deptService;

    /**
     * 查询分类管理列表
     */
    //@PreAuthorize("@ss.hasPermi('biemo:report:list')")
    @GetMapping("/list")
    public Page<Report> list(Report report)
    {
        Page<Report> page =  reportService.selectReportPage(report, PageFactory.defaultPage());
        //Page<Report> page =  reportService.getReportList(SecurityUtils.getUsername());
        return page;
    }

    @GetMapping("/indexList")
    public Page<Report> indexList(Report report)
    {
        Page<Report> page =  reportService.selectReportPage(report, PageFactory.defaultPage());
        //Page<Report> page =  reportService.getReportList(SecurityUtils.getUsername());
//        page.getRows().forEach(report1 -> {
//            SysUser user =  userService.selectUserById(report1.getCreatedBy());
//            report1.setUser(user);
//            SysDept dept = deptService.selectDeptById(user.getDeptId());
//            report1.setDept(dept);
//        });
        return page;
    }

    /**
     * 导出分类管理列表
     */
    //@PreAuthorize("@ss.hasPermi('biemo:report:export')")
    @Log(title = "分类管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Report report)
    {
        List<Report> list = reportService.selectReportList(report);
        ExcelUtil<Report> util = new ExcelUtil<>(Report.class);
        util.exportExcel(response, list, "分类管理数据");
    }

    /**
     * 获取分类管理详细信息
     */
    //@PreAuthorize("@ss.hasPermi('biemo:report:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(reportService.selectReportById(id));
    }

    /**
     * 新增分类管理或修改文章
     */
    //@PreAuthorize("@ss.hasPermi('biemo:report:add')")
    @Log(title = "分类管理", businessType = BusinessType.INSERT)
    @RequestMapping
    public AjaxResult add(@RequestBody Report report)
    {
        //reportService.insertOrUpdateReport(report);
        return  reportService.uploadReport(report, SecurityUtils.getUsername());
    }


    /**
     * 删除分类管理
     */
    //@PreAuthorize("@ss.hasPermi('biemo:report:remove')")
    @Log(title = "分类管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        reportService.deleteReportByIds(ids);
        return AjaxResult.success();
    }


    @RequestMapping("/ticket2token")
    public AjaxResult ticket2token(String ticket){
        return AjaxResult.success("查询成功",reportService.ticket2token(ticket));
    }


    @PostMapping("/similar")
    public AjaxResult similar(@RequestBody JSONObject text1, @RequestBody String text2){
        String text11 = text1.get("text1").toString();
        String text22 = text1.get("text2").toString();
        return AjaxResult.success(WordVectorModelUtils.similarCount(text11,text22));
    }

    @PostMapping("/sentiment")
    public AjaxResult sentiment(@RequestBody String text){
        return AjaxResult.success("查询成功",SentimentAnalysisUtils.analysis(text));
    }

    @PostMapping("/sentiment/import")
    public AjaxResult sentimentImport(MultipartFile[] files,String sentimentName){
        return AjaxResult.success("模型训练成功",SentimentAnalysisUtils.analysisImport(files,sentimentName));
    }

    /**
     * 获取当前的情感词
     * @return
     */
    @GetMapping("/sentiment/getCurrSentiments")
    public AjaxResult getCurrSentiments(){
        return AjaxResult.success("查询成功",SentimentAnalysisUtils.getCurrSentiments());
    }

    /**
     * 删除情感词目录
     */
    @DeleteMapping("/sentiment/removeSentiments/{sentiments}")
    public AjaxResult removeSentiments(@PathVariable("sentiments") String[] sentimentsArray){
        SentimentAnalysisUtils.removeSentiments(sentimentsArray);
        return AjaxResult.success();
    }

}
