package com.ruoyi.oa.controller;

import java.util.List;
import java.util.Arrays;
import java.util.stream.Collectors;

import com.ruoyi.oa.domain.vo.ProjectAttendanceVo;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.oa.domain.vo.OaAttendanceRecordVo;
import com.ruoyi.oa.domain.bo.OaAttendanceRecordBo;
import com.ruoyi.oa.service.IOaAttendanceRecordService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 计算记录
 *
 * @author ruoyi
 * @date 2025-05-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/oa/attendanceRecord")
public class OaAttendanceRecordController extends BaseController {

    private final IOaAttendanceRecordService iOaAttendanceRecordService;

    /**
     * 查询计算记录列表
     */
    @GetMapping("/list")
    public TableDataInfo<OaAttendanceRecordVo> list(OaAttendanceRecordBo bo, PageQuery pageQuery) {
        return iOaAttendanceRecordService.queryPageList(bo, pageQuery);
    }

    @GetMapping("/gen-list/{date}")
    public R<List<String>> list(@PathVariable String date) {
        return R.ok(iOaAttendanceRecordService.listGenDate(date));
    }

    /**
     * 导出计算记录列表
     */
    @PostMapping("/export")
    public void export(OaAttendanceRecordBo bo, HttpServletResponse response) {
        List<OaAttendanceRecordVo> list = iOaAttendanceRecordService.queryList(bo);
        ExcelUtil.exportExcel(list, "计算记录", OaAttendanceRecordVo.class, response);
    }

    /**
     * 获取计算记录详细信息
     *
     * @param recordId 主键
     */
    @GetMapping("/{recordId}")
    public R<OaAttendanceRecordVo> getInfo(@NotNull(message = "主键不能为空")
                                           @PathVariable Long recordId) {
        return R.ok(iOaAttendanceRecordService.queryById(recordId));
    }

    /**
     * 新增计算记录
     */
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody OaAttendanceRecordBo bo) {
        return toAjax(iOaAttendanceRecordService.insertByBo(bo));
    }

    /**
     * 修改计算记录
     */
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody OaAttendanceRecordBo bo) {
        return toAjax(iOaAttendanceRecordService.updateByBo(bo));
    }

    /**
     * 删除计算记录
     *
     * @param recordIds 主键串
     */
    @DeleteMapping("/{recordIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] recordIds) {
        return toAjax(iOaAttendanceRecordService.deleteWithValidByIds(Arrays.asList(recordIds), true));
    }



    @GetMapping("/attendance")
    public R<List<ProjectAttendanceVo>>getAttendance(
            @RequestParam List<String> userIds,
            @RequestParam(required = false) String calcTime,
            @RequestParam String relationMonth) {
        // 使用 Stream 将 List<String> 转换为 List<Long>
        List<Long> longList = userIds.stream()
                .map(Long::parseLong)  // 将每个 String 转换为 Long
                .collect(Collectors.toList());
        return R.ok(iOaAttendanceRecordService.getAttendance(longList,calcTime, relationMonth));
    }
}
