package com.ruoyi.web.controller.system;

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.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Dangan;
import com.ruoyi.system.domain.Feedback;
import com.ruoyi.system.domain.FeedbackFile;
import com.ruoyi.system.service.IDanganService;
import com.ruoyi.system.service.IFeedbackFileService;
import com.ruoyi.system.service.IFeedbackService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 问题反馈Controller
 * 
 * @author ruoyi
 * @date 2022-10-21
 */
@Controller
@RequestMapping("/system/feedback")
public class FeedbackController extends BaseController
{
    private String prefix = "system/feedback";

    @Autowired
    private IFeedbackService feedbackService;
    @Autowired
    private IDanganService danganService;
    @Autowired
    private IFeedbackFileService feedbackFileService;

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

    /**
     * 查询问题反馈列表
     */
    @RequiresPermissions("system:feedback:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Feedback feedback)
    {
        startPage();
        //只查询自己提交的
        SysUser user = ShiroUtils.getSysUser();
        if(!user.isAdmin()){
            feedback.setUserId(user.getUserId());
        }
        List<Feedback> list = feedbackService.selectFeedbackList(feedback);
        return getDataTable(list);
    }

    /**
     * 导出问题反馈列表
     */
    @RequiresPermissions("system:feedback:export")
    @Log(title = "问题反馈", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Feedback feedback)
    {
        List<Feedback> list = feedbackService.selectFeedbackList(feedback);
        ExcelUtil<Feedback> util = new ExcelUtil<Feedback>(Feedback.class);
        return util.exportExcel(list, "feedback");
    }

    /**
     * 新增问题反馈
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        //系统档案列表
       List<Dangan> danganList= danganService.selectDanganList(new Dangan());
       mmap.put("danganList",danganList);
        return prefix + "/add";
    }

    /**
     * 新增保存问题反馈
     */
    @RequiresPermissions("system:feedback:add")
    @Log(title = "问题反馈", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Feedback feedback)
    {
        //反馈人姓名
        feedback.setUserId(ShiroUtils.getUserId());
        feedback.setUserName(ShiroUtils.getSysUser().getUserName());
        feedback.setFeedbackTime(DateUtils.getNowDate());
        return toAjax(feedbackService.insertFeedback(feedback));
    }

    /**
     * 修改问题反馈
     */
    @GetMapping("/edit/{feedbackId}")
    public String edit(@PathVariable("feedbackId") Long feedbackId, ModelMap mmap)
    {
        Feedback feedback = feedbackService.selectFeedbackById(feedbackId);
        mmap.put("feedback", feedback);
        List<Dangan> danganList= danganService.selectDanganList(new Dangan());
        mmap.put("danganList",danganList);

        //查询反馈附件
        FeedbackFile feedbackFile  = new FeedbackFile();
        feedbackFile.setFeedbackId(feedback.getFeedbackId());
        List<FeedbackFile> files = feedbackFileService.selectFeedbackFileList(feedbackFile);
        mmap.put("fileList", files);
        return prefix + "/edit";
    }

    /**
     * 修改保存问题反馈
     */
    @RequiresPermissions("system:feedback:edit")
    @Log(title = "问题反馈", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Feedback feedback)
    {
        return toAjax(feedbackService.updateFeedback(feedback));
    }

    /**
     * 删除问题反馈
     */
    @RequiresPermissions("system:feedback:remove")
    @Log(title = "问题反馈", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(feedbackService.deleteFeedbackByIds(ids));
    }

    /**
     * 查看
     */
    @GetMapping("/look/{feedbackId}")
    public String look(@PathVariable("feedbackId") Long feedbackId, ModelMap mmap)
    {
        Feedback feedback = feedbackService.selectFeedbackById(feedbackId);
        mmap.put("feedback", feedback);

        List<Dangan> danganList= danganService.selectDanganList(new Dangan());
        mmap.put("danganList",danganList);

        //查询反馈附件
        FeedbackFile feedbackFile  = new FeedbackFile();
        feedbackFile.setFeedbackId(feedback.getFeedbackId());
        List<FeedbackFile> files = feedbackFileService.selectFeedbackFileList(feedbackFile);
        mmap.put("fileList", files);
        return prefix + "/look";
    }

}
