package com.ruoyi.app.user;

import java.text.SimpleDateFormat;
import java.util.List;
import jakarta.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.system.domain.VipUser;
import com.ruoyi.system.mapper.SysFeedbackMapper;
import com.ruoyi.system.service.IInfoUserService;
import com.ruoyi.system.utils.Auth;
import com.ruoyi.system.utils.JwtUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysFeedback;
import com.ruoyi.system.service.ISysFeedbackService;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * feedbackController
 * 
 * @author ruoyi
 * @date 2023-06-11
 */
@RestController
@RequestMapping("/system/feedback")
public class SysFeedbackController extends BaseController
{
    @Autowired
    private ISysFeedbackService sysFeedbackService;
    @Autowired
    private IInfoUserService infoUserService;
    @Autowired
    private SysFeedbackMapper sysFeedbackMapper;


    /**
     * 添加反馈问题
     */
    @Anonymous
    @Auth
    @PostMapping("/addfeedbxp")
    public AjaxResult addfeedbxp(@RequestHeader String token,@RequestBody SysFeedback sysFeedback)
    {
        JwtUtil jwtUtil = new JwtUtil();
        String id = jwtUtil.getusid(token);
        sysFeedback.setUserId(Long.valueOf(id));
        return toAjax(sysFeedbackService.insertSysFeedback(sysFeedback));


    }

    /**
     * 获取问题详情
     */
    @Anonymous
    @GetMapping("/getfeedbxp")
    public AjaxResult getfeedbxp(@RequestParam Long id)
    {
        SysFeedback sysFeedback = sysFeedbackService.selectSysFeedbackById(id);
        return success(sysFeedback);
    }
    /**
     * 获取问题列表
     */
    @Anonymous
    @Auth
    @GetMapping("/getfeedblist")
    public AjaxResult getfeedblist(@RequestHeader String token)
    {
        JwtUtil jwtUtil = new JwtUtil();
        String id = jwtUtil.getusid(token);
        List<SysFeedback> list = sysFeedbackMapper.getfeedblist(Long.valueOf(id));
        JSONArray arr = new JSONArray();
        for (int i=0;i<list.size();i++){
            JSONObject org = new JSONObject();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            org.put("id",list.get(i).getId());
            org.put("cretim",sdf.format(list.get(i).getCretim()));
            org.put("userId",list.get(i).getUserId());
            org.put("user",infoUserService.selectInfoUserByUserId(list.get(i).getUserId()));
            org.put("content",list.get(i).getContent());
            arr.add(org);
        }
        return success(arr);
    }
    /**
     * 查询feedback列表
     */
    @PreAuthorize("@ss.hasPermi('system:feedback:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysFeedback sysFeedback)
    {
        startPage();
        List<SysFeedback> list = sysFeedbackService.selectSysFeedbackList(sysFeedback);
        JSONArray arr = new JSONArray();
        for (int i=0;i<list.size();i++){
            JSONObject org = new JSONObject();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            org.put("id",list.get(i).getId());
            org.put("cretim",sdf.format(list.get(i).getCretim()));
            org.put("userId",list.get(i).getUserId());
            org.put("user",infoUserService.selectInfoUserByUserId(list.get(i).getUserId()));
            org.put("content",list.get(i).getContent());
            arr.add(org);
        }
        return getDataTable(arr);
    }

    /**
     * 导出feedback列表
     */
    @PreAuthorize("@ss.hasPermi('system:feedback:export')")
    @Log(title = "feedback", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysFeedback sysFeedback)
    {
        List<SysFeedback> list = sysFeedbackService.selectSysFeedbackList(sysFeedback);
        ExcelUtil<SysFeedback> util = new ExcelUtil<SysFeedback>(SysFeedback.class);
        util.exportExcel(response, list, MessageUtils.message("no.export.excel.feedback"));
    }

    /**
     * 获取feedback详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:feedback:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysFeedbackService.selectSysFeedbackById(id));
    }

    /**
     * 新增feedback
     */
    @PreAuthorize("@ss.hasPermi('system:feedback:add')")
    @Log(title = "feedback", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysFeedback sysFeedback)
    {
        return toAjax(sysFeedbackService.insertSysFeedback(sysFeedback));
    }

    /**
     * 修改feedback
     */
    @PreAuthorize("@ss.hasPermi('system:feedback:edit')")
    @Log(title = "feedback", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysFeedback sysFeedback)
    {
        return toAjax(sysFeedbackService.updateSysFeedback(sysFeedback));
    }

    /**
     * 删除feedback
     */
    @PreAuthorize("@ss.hasPermi('system:feedback:remove')")
    @Log(title = "feedback", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysFeedbackService.deleteSysFeedbackByIds(ids));
    }
}
