package org.dromara.zhishu.controller;

import java.util.List;

import io.github.linpeilie.BaseMapper;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
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.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.zhishu.domain.vo.TAuditVo;
import org.dromara.zhishu.domain.bo.TAuditBo;
import org.dromara.zhishu.service.ITAuditService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 审核
 *
 * @author Lion Li
 * @date 2025-04-01
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/zhishu/audit")
public class TAuditController extends BaseController {

    @Autowired
    private final ITAuditService tAuditService;


    /**
     * 查询审核列表
     */
    @SaCheckPermission("zhishu:audit:list")
    @GetMapping("/list")
    public TableDataInfo<TAuditVo> list(TAuditBo bo, PageQuery pageQuery) {
        return tAuditService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出审核列表
     */
    @SaCheckPermission("zhishu:audit:export")
    @Log(title = "审核", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TAuditBo bo, HttpServletResponse response) {
        List<TAuditVo> list = tAuditService.queryList(bo);
        ExcelUtil.exportExcel(list, "审核", TAuditVo.class, response);
    }

    /**
     * 获取审核详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("zhishu:audit:query")
    @GetMapping("/{id}")
    public R<TAuditVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(tAuditService.queryById(id));
    }

    /**
     * 新增审核
     */
    @SaCheckPermission("zhishu:audit:add")
    @Log(title = "审核", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TAuditBo bo) {
        return toAjax(tAuditService.insertByBo(bo));
    }

    /**
     * 修改审核
     */
//    @SaCheckPermission("zhishu:audit:edit")
    @Log(title = "审核", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TAuditBo bo) {
        return toAjax(tAuditService.updateByBo(bo));
    }

    /**
     * 删除审核
     *
     * @param ids 主键串
     */
    @SaCheckPermission("zhishu:audit:remove")
    @Log(title = "审核", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(tAuditService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 修改审核通过状态
     *
     */
//    @SaCheckPermission("zhishu:audit:updateStatus")
    @Log(title = "审核状态", businessType = BusinessType.UPDATE)
    @PutMapping("/updateStatus")
    public R<Void> updateStatus(@RequestBody TAuditBo bo) {
     return toAjax(tAuditService.updateByStatus(bo));
    }


    /**
     * 修改审核未通过状态
     *
     */
//    @SaCheckPermission("zhishu:audit:updateStatus")
//    @Log(title = "审核状态", businessType = BusinessType.INSERT)
    @PostMapping("/failSend")
    public R<Void> failSend(@RequestBody TAuditBo bo) {
        //修改审核状态，为未通过（1）
        tAuditService.updateByStatus(bo);
        //将修改内容写入t_audit_log的日志表里
       Integer message = tAuditService.InsertLog(bo);
        return toAjax(message);
    }



}
