package org.dromara.project.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.excel.utils.ExcelUtil;
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.web.core.BaseController;
import org.dromara.project.domain.dto.attendance.BusAttendanceMonthByUserIdReq;
import org.dromara.project.domain.dto.attendance.BusAttendanceQueryReq;
import org.dromara.project.domain.dto.attendance.BusAttendanceQueryTwoWeekReq;
import org.dromara.project.domain.vo.attendance.BusAttendanceClockDateForTwoWeekVo;
import org.dromara.project.domain.vo.attendance.BusAttendanceMonthByUserIdVo;
import org.dromara.project.domain.vo.attendance.BusAttendanceVo;
import org.dromara.project.service.IBusAttendanceService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 考勤
 *
 * @author lcj
 * @date 2025-04-07
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/project/attendance")
public class BusAttendanceController extends BaseController {

    private final IBusAttendanceService busAttendanceService;

    /**
     * 查询考勤列表
     */
    @SaCheckPermission("project:attendance:list")
    @GetMapping("/list")
    public TableDataInfo<BusAttendanceVo> list(BusAttendanceQueryReq req, PageQuery pageQuery) {
        return busAttendanceService.queryPageList(req, pageQuery);
    }

    /**
     * 查询近两周考勤列表
     */
    @SaCheckPermission("project:attendance:list")
    @GetMapping("/list/clockDate/twoWeek")
    public R<List<BusAttendanceClockDateForTwoWeekVo>> listClockDateForTwoWeek(BusAttendanceQueryTwoWeekReq req) {
        return R.ok(busAttendanceService.listClockDateForTwoWeek(req));
    }

    /**
     * 查询施工人员月份考勤列表
     */
    @SaCheckPermission("project:attendance:list")
    @GetMapping("/list/month/byUserId")
    public R<List<BusAttendanceMonthByUserIdVo>> listAttendanceMonthListByUserId(BusAttendanceMonthByUserIdReq req) {
        return R.ok(busAttendanceService.listAttendanceMonthListByUserId(req));
    }

    /**
     * 导出考勤列表
     */
    @SaCheckPermission("project:attendance:export")
    @Log(title = "考勤", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BusAttendanceQueryReq req, HttpServletResponse response) {
        List<BusAttendanceVo> list = busAttendanceService.queryList(req);
        ExcelUtil.exportExcel(list, "考勤", BusAttendanceVo.class, response);
    }

    /**
     * 获取考勤详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("project:attendance:query")
    @GetMapping("/{id}")
    public R<BusAttendanceVo> getInfo(@NotNull(message = "主键不能为空")
                                      @PathVariable Long id) {
        return R.ok(busAttendanceService.queryById(id));
    }

}
