package com.pusong.workflow.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import com.pusong.common.core.domain.R;
import com.pusong.common.core.validate.AddGroup;
import com.pusong.common.core.validate.EditGroup;
import com.pusong.common.excel.utils.ExcelUtil;
import com.pusong.common.idempotent.annotation.RepeatSubmit;
import com.pusong.common.log.annotation.Log;
import com.pusong.common.log.enums.BusinessType;
import com.pusong.common.mybatis.core.page.PageQuery;
import com.pusong.common.mybatis.core.page.TableDataInfo;
import com.pusong.common.web.core.BaseController;
import com.pusong.workflow.domain.bo.SysUserLeaveBo;
import com.pusong.workflow.domain.vo.SysUserLeaveVo;
import com.pusong.workflow.service.ISysUserLeaveService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 请假
 *
 * @author may
 * @date 2023-07-21
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/workflow/leave")
public class SysUserLeaveController extends BaseController {

    private final ISysUserLeaveService sysUserLeaveService;

    /**
     * 查询请假列表
     */
    @SaCheckPermission("workflow:leave:list")
    @GetMapping("/list")
    public TableDataInfo<SysUserLeaveVo> list(SysUserLeaveBo bo, PageQuery pageQuery) {
        return sysUserLeaveService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出请假列表
     */
    @SaCheckPermission("workflow:leave:export")
    @Log(title = "请假", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysUserLeaveBo bo, HttpServletResponse response) {
        List<SysUserLeaveVo> list = sysUserLeaveService.queryList(bo);
        ExcelUtil.exportExcel(list, "请假", SysUserLeaveVo.class, response);
    }

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

    /**
     * 新增请假
     */
    @SaCheckPermission("workflow:leave:add")
    @Log(title = "请假", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<SysUserLeaveVo> add(@Validated(AddGroup.class) @RequestBody SysUserLeaveBo bo) {
        return R.ok(sysUserLeaveService.insertByBo(bo));
    }

    /**
     * 修改请假
     */
    @SaCheckPermission("workflow:leave:edit")
    @Log(title = "请假", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<SysUserLeaveVo> edit(@Validated(EditGroup.class) @RequestBody SysUserLeaveBo bo) {
        return R.ok(sysUserLeaveService.updateByBo(bo));
    }

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