package com.ruoyi.web.work.controller;

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.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.work.domain.Feedback;
import com.ruoyi.web.work.service.IFeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.rmi.ServerException;
import java.util.Arrays;
import java.util.List;

/**
 * 问题反馈
 *
 * @author lsw
 * @date 2024-04-24
 */
@RestController
@RequestMapping("/work/feedback")
public class FeedbackController extends BaseController {
    @Autowired
    private IFeedbackService feedbackService;

    @GetMapping("/list")
    public TableDataInfo list(Feedback feedback) {
        feedback.setCompanyId(getLoginUser().getUser().getCompanyId());
        if (getLoginUser().getUser().getType() == 1) {
            feedback.setUserId(getUserId());
        }
        startPage();
        List<Feedback> list = feedbackService.selectList(feedback);
        return getDataTable(list);
    }

    @GetMapping(value = "/detail/{id}")
    public AjaxResult detail(@PathVariable("id") Long id) {
        return AjaxResult.success(feedbackService.getById(id));
    }

    @Log(title = "提交问题反馈", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody Feedback feedback) throws ServerException {
        return feedbackService.add(feedback);
    }

    @PreAuthorize("@ss.hasPermi('work:feedback:edit')")
    @Log(title = "问题反馈", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody Feedback feedback) {
        feedback.setState(1);
        return toAjax(feedbackService.updateById(feedback));
    }

    @PreAuthorize("@ss.hasPermi('work:feedback:remove')")
    @Log(title = "删除问题反馈", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(feedbackService.removeByIds(Arrays.asList(ids)));
    }

    @Log(title = "删除问题反馈", businessType = BusinessType.DELETE)
    @GetMapping("/delByUser/{id}")
    public AjaxResult delByUser(@PathVariable Long id) {
        return toAjax(feedbackService.delByUser(id));
    }
}
