package org.dromara.system.controller.system;

import cn.dev33.satoken.stp.StpUtil;
import jakarta.validation.constraints.NotEmpty;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
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.SysComplaintBo;
import org.dromara.system.domain.vo.SysComplaintVo;
import org.dromara.system.service.ISysComplaintService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 投诉建议管理
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/complaint")
public class SysComplaintController extends BaseController {

    private final ISysComplaintService sysComplaintService;

    /**
     * 查询投诉建议列表（管理端）
     */
//     @SaCheckPermission("system:complaint:list")
    @GetMapping("/list")
    public TableDataInfo<SysComplaintVo> list(SysComplaintBo bo, PageQuery pageQuery) {
        return sysComplaintService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询当前员工的投诉建议列表（小程序端）
     * @Author: Trae
     */
    @GetMapping("/myList")
    public TableDataInfo<SysComplaintVo> myList(PageQuery pageQuery) {
        return sysComplaintService.queryMyPageList(LoginHelper.getUserId(), pageQuery);
    }

    /**
     * 提交投诉建议（小程序端，需登录）
     */
    @Log(title = "投诉建议", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public R<Void> submit(@Validated @RequestBody SysComplaintBo bo) {
        bo.setEmployeeId(LoginHelper.getUserId());
        return toAjax(sysComplaintService.insertByBo(bo));
    }

    /**
     * 查询投诉建议详情
     * @Author: Antigravity
     */
//     @SaCheckPermission("complaint:complaint:query")
    @GetMapping("/detail/{id}")
    public R<SysComplaintVo> detail(@PathVariable Long id) {
        return R.ok(sysComplaintService.queryById(id));
    }

    /**
     * 处理投诉建议
     * @Author: Antigravity
     */
//     @SaCheckPermission("complaint:complaint:deal")
    @Log(title = "投诉建议处理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/deal")
    public R<Void> deal(@RequestBody SysComplaintBo bo) {
        bo.setStatus("1");
        return toAjax(sysComplaintService.updateByBo(bo));
    }

    /**
     * 删除投诉建议（管理端）
     *
     * @param ids 主键串
     */
//     @SaCheckPermission("system:complaint:remove")
    @Log(title = "投诉建议", businessType = BusinessType.DELETE)
    @RepeatSubmit()
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(sysComplaintService.deleteWithValidByIds(List.of(ids)));
    }

}
