package org.dromara.system.controller.record;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.system.domain.record.bo.RecordLeaveBo;
import org.dromara.system.domain.record.vo.RecordLeaveVo;
import org.dromara.system.service.record.IRecordLeaveService;
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;


/**
 * 请假记录信息
 *
 * @author boman
 * @date 2023-08-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/record/leave")
public class RecordLeaveController extends BaseController {

    private final IRecordLeaveService recordLeaveService;

    /**
     * 查询请假记录信息列表
     */
    @SaCheckPermission("system:leave:list")
    @GetMapping("/list")
    public TableDataInfo<RecordLeaveVo> list(RecordLeaveBo bo, PageQuery pageQuery) {
        return recordLeaveService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出请假记录信息列表
     */
    @SaCheckPermission("record:record:export")
    @Log(title = "请假记录信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(RecordLeaveBo bo, HttpServletResponse response) {
        List<RecordLeaveVo> list = recordLeaveService.queryList(bo);
        ExcelUtil.exportExcel(list, "请假记录信息", RecordLeaveVo.class, response);
    }

    /**
     * 获取请假记录信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("record:record:query")
    @GetMapping("/{id}")
    public R<RecordLeaveVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(recordLeaveService.queryById(id));
    }

    /**
     * 新增请假记录信息
     */
    @SaCheckPermission("system:leave:add")
    @Log(title = "请假记录信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody RecordLeaveBo bo) {
        return toAjax(recordLeaveService.insertByBo(bo));
    }

    /**
     * 修改请假记录信息
     */
    @SaCheckPermission("record:record:edit")
    @Log(title = "请假记录信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/put")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody RecordLeaveBo bo) {
        return toAjax(recordLeaveService.updateByBo(bo));
    }

    /**
     * 删除请假记录信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:leave:remove")
    @Log(title = "请假记录信息", businessType = BusinessType.DELETE)
    @GetMapping("/delete/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(recordLeaveService.deleteWithValidByIds(List.of(ids), true));
    }
}
