package com.ruoyi.project.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.project.domain.*;
import com.ruoyi.project.service.ITrsWordResultsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.project.service.ITrsResultsLogService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 审校日志Controller
 * 
 * @author ruoyi
 * @date 2025-03-28
 */
@Controller
@RequestMapping("/project/resultlog")
public class TrsResultsLogController extends BaseController
{
    private String prefix = "project/resultlog";

    @Autowired
    private ITrsResultsLogService trsResultsLogService;

    @Autowired
    private ITrsWordResultsService trsWordResultsService;

    @RequiresPermissions("project:resultlog:view")
    @GetMapping()
    public String resultlog()
    {
        return prefix + "/resultlog";
    }

    /**
     * 查询审校日志列表
     */
    @RequiresPermissions("project:resultlog:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TrsResultsLog trsResultsLog)
    {
        startPage();
        List<TrsResultsLog> list = trsResultsLogService.selectTrsResultsLogList(trsResultsLog);
        return getDataTable(list);
    }

    /**
     * 导出审校日志列表
     */
    @RequiresPermissions("project:resultlog:export")
    @Log(title = "审校日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TrsResultsLog trsResultsLog)
    {
        List<TrsResultsLog> list = trsResultsLogService.selectTrsResultsLogList(trsResultsLog);
        //增加列
        List<TrsResultsLogResVo> list2 = new ArrayList<>();
        for (TrsResultsLog trsResultsLog1 : list) {
            TrsResultsLogResVo trsResultsLogResVo = new TrsResultsLogResVo();
            BeanUtils.copyBeanProp(trsResultsLogResVo, trsResultsLog1);

            TrsWordResults trsWordResults = new TrsWordResults();
            trsWordResults.setLogId(trsResultsLog1.getId());
            List<TrsWordResults> resultsList = trsWordResultsService.selectTrsWordResultsList(trsWordResults);

            TrsWordResults trsWordResults2 = new TrsWordResults();
            trsWordResults2.setLogId(trsResultsLog1.getId());
            trsWordResults2.setRes(1);
            List<TrsWordResults> resultsListWb = trsWordResultsService.selectTrsWordResultsList(trsWordResults2);


            trsResultsLogResVo.setErrnum(resultsList.size()+"");
            trsResultsLogResVo.setWbnum(resultsListWb.size()+"");
            list2.add(trsResultsLogResVo);
        }
        ExcelUtil<TrsResultsLogResVo> util = new ExcelUtil<TrsResultsLogResVo>(TrsResultsLogResVo.class);
        return util.exportExcel(list2, "审校日志数据");
    }

    /**
     * 导出审校日志列表
     */
    @RequiresPermissions("project:resultlog:exportadmin")
    @Log(title = "审校日志", businessType = BusinessType.EXPORT)
    @RequestMapping("/exportadmin")
    @ResponseBody
    public AjaxResult exportadmin(TrsResultsLog trsResultsLog)
    {
        List<TrsResultsLogAdmin> list = trsResultsLogService.selectTrsResultsLogExportList(trsResultsLog);
        List list2 = new ArrayList();
        for (TrsResultsLogAdmin trsResultsLog1 : list) {
            TrsWordResults trsWordResults = new TrsWordResults();
            trsWordResults.setLogId(trsResultsLog1.getId());
            List<TrsWordResults> resultsList = trsWordResultsService.selectTrsWordResultsList(trsWordResults);

            TrsWordResults trsWordResults2 = new TrsWordResults();
            trsWordResults2.setLogId(trsResultsLog1.getId());
            trsWordResults2.setRes(1);
            List<TrsWordResults> resultsListWb = trsWordResultsService.selectTrsWordResultsList(trsWordResults2);

            trsResultsLog1.setErrnum(resultsList.size()+"");
            trsResultsLog1.setWbnum(resultsListWb.size()+"");
            list2.add(trsResultsLog1);
        }

        ExcelUtil<TrsResultsLogAdmin> util = new ExcelUtil<TrsResultsLogAdmin>(TrsResultsLogAdmin.class);
        return util.exportExcel(list2, "审校日志数据");
    }

    /**
     * 新增审校日志
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存审校日志
     */
    @RequiresPermissions("project:resultlog:add")
    @Log(title = "审校日志", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TrsResultsLog trsResultsLog)
    {
        trsResultsLog.setDeptId(getSysUser().getDeptId());
        trsResultsLog.setUserId(getSysUser().getUserId());
        return toAjax(trsResultsLogService.insertTrsResultsLog(trsResultsLog));
    }

    /**
     * 修改审校日志
     */
    @RequiresPermissions("project:resultlog:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TrsResultsLog trsResultsLog = trsResultsLogService.selectTrsResultsLogById(id);
        mmap.put("trsResultsLog", trsResultsLog);
        return prefix + "/edit";
    }

    /**
     * 修改保存审校日志
     */
    @RequiresPermissions("project:resultlog:edit")
    @Log(title = "审校日志", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TrsResultsLog trsResultsLog)
    {
        return toAjax(trsResultsLogService.updateTrsResultsLog(trsResultsLog));
    }

    /**
     * 删除审校日志
     */
    @RequiresPermissions("project:resultlog:remove")
    @Log(title = "审校日志", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(trsResultsLogService.deleteTrsResultsLogByIds(ids));
    }

    /**
     * 查看审校日志
     */
    /*@RequiresPermissions("project:resultlog:edit")*/
    @GetMapping("/view/{id}")
    public String view(@PathVariable("id") Long id, ModelMap mmap)
    {
        TrsResultsLog trsResultsLog = trsResultsLogService.selectTrsResultsLogById(id);
        mmap.put("trsResultsLog", trsResultsLog);
        return prefix + "/view";
    }

}
