package org.dromara.system.controller;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.FeedbackSubmitBo;
import org.dromara.system.domain.bo.TblDzbcFeedbackBo;
import org.dromara.system.domain.vo.TblDzbcFeedbackVo;
import org.dromara.system.service.ITblDzbcFeedbackService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/dzbc/feedback")
public class TblDzbcFeedbackController extends BaseController {

    private final ITblDzbcFeedbackService tblDzbcFeedbackService;

    @GetMapping("/list")
    public TableDataInfo<TblDzbcFeedbackVo> list(TblDzbcFeedbackBo bo, PageQuery pageQuery) {
        return tblDzbcFeedbackService.queryPageList(bo, pageQuery);
    }

    @GetMapping("/my/list")
    public R<List<TblDzbcFeedbackVo>> myList() {
        Long userId = LoginHelper.getUserId();
        return R.ok(tblDzbcFeedbackService.queryByUserId(userId));
    }

    @Log(title = "问题建议", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TblDzbcFeedbackBo bo, HttpServletResponse response) {
        List<TblDzbcFeedbackVo> list = tblDzbcFeedbackService.queryList(bo);
        ExcelUtil.exportExcel(list, "问题建议", TblDzbcFeedbackVo.class, response);
    }

    @GetMapping("/{id}")
    public R<TblDzbcFeedbackVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(tblDzbcFeedbackService.queryById(id));
    }

    @Log(title = "提交问题建议", businessType = BusinessType.INSERT)
    @PostMapping(value = "/submit", consumes = "multipart/form-data")
    public R<TblDzbcFeedbackVo> submit(@Validated @ModelAttribute FeedbackSubmitBo bo) {
        return R.ok(tblDzbcFeedbackService.submit(bo));
    }

    @Log(title = "回复问题建议", businessType = BusinessType.UPDATE)
    @PostMapping("/reply/{id}")
    public R<Void> reply(@NotNull(message = "主键不能为空") @PathVariable Long id,
                         @NotBlank(message = "回复内容不能为空") @RequestParam String replyContent) {
        return toAjax(tblDzbcFeedbackService.reply(id, replyContent));
    }

    @Log(title = "问题建议", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TblDzbcFeedbackBo bo) {
        return toAjax(tblDzbcFeedbackService.updateByBo(bo));
    }

    @Log(title = "问题建议", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(tblDzbcFeedbackService.deleteWithValidByIds(List.of(ids), true));
    }
}
