package com.ruoyi.oa.controller;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.oa.domain.LaborCostData;
import com.ruoyi.oa.domain.vo.AttendanceMonthlyCount;
import com.ruoyi.oa.domain.vo.SysUserVo;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
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.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.oa.domain.vo.SysOaAttendanceVo;
import com.ruoyi.oa.domain.bo.SysOaAttendanceBo;
import com.ruoyi.oa.service.ISysOaAttendanceService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 人员考勤
 *
 * @author hdka
 * @date 2024-11-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/oa/oaAttendance")
public class SysOaAttendanceController extends BaseController {

    private final ISysOaAttendanceService iSysOaAttendanceService;


    /**
     * 获取当前月的天数
     * @return
     */
    @GetMapping("/date")
    public R<Long> date() {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(DateUtils.getNowDate());
        Long dateLength = (long) calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

        return R.ok(dateLength);
    }


    /**
     * 获取当前月的天数
     * @return
     */
    @GetMapping("/calc/{time}")
    public R<List<LaborCostData>> calc(@PathVariable("time") Date time) {
        return R.ok(iSysOaAttendanceService.workerCalc(time));
    }


    /**
     * 查询人员考勤列表
     */
    @GetMapping("/list")
    public TableDataInfo<SysOaAttendanceVo> list(SysOaAttendanceBo bo, PageQuery pageQuery) {
        return iSysOaAttendanceService.queryPageList(bo, pageQuery);
    }



    /**
     * 查询人员考勤列表
     */
    @GetMapping("/worker/list")
    public TableDataInfo<SysUserVo> workerList(SysOaAttendanceBo bo, PageQuery pageQuery) {

        return iSysOaAttendanceService.workerList(bo);
    }


    /**
     * 导出人员考勤列表
     */
    @Log(title = "人员考勤", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysOaAttendanceBo bo, HttpServletResponse response) {
        List<SysOaAttendanceVo> list = iSysOaAttendanceService.queryList(bo);
        ExcelUtil.exportExcel(list, "人员考勤", SysOaAttendanceVo.class, response);
    }

    /**
     * 获取人员考勤详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<SysOaAttendanceVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iSysOaAttendanceService.queryById(id));
    }

    /**
     * 新增人员考勤
     */
    @Log(title = "人员考勤", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysOaAttendanceBo bo) {
        return toAjax(iSysOaAttendanceService.insertByBo(bo));
    }

    /**
     * 修改人员考勤
     */
    @Log(title = "人员考勤", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysOaAttendanceBo bo) {
        return toAjax(iSysOaAttendanceService.updateByBo(bo));
    }

    /**
     * 删除人员考勤
     *
     * @param ids 主键串
     */
    @Log(title = "人员考勤", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iSysOaAttendanceService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 删除人员考勤
     *
     * @param day 主键串
     */
    @Log(title = "人员考勤", businessType = BusinessType.DELETE)
    @DeleteMapping("/all/{day}")
    public R<Void> delOaAttendanceAll(@PathVariable Long day) {
        return toAjax(iSysOaAttendanceService.delOaAttendanceAll(day));
    }


    /**
     * 新增人员考勤
     */
    @Log(title = "人员考勤", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/insertBatch")
    public R<Void> insertBatch(@Validated(AddGroup.class) @RequestBody SysOaAttendanceBo bo) {
        return toAjax( iSysOaAttendanceService.insertBatch(bo));
    }

    @GetMapping("/data/{refDate}")
    public R<List<AttendanceMonthlyCount>> selectLastSixMonthsByMonth(@PathVariable String refDate) {
        return R.ok(iSysOaAttendanceService.selectLastSixMonthsByMonth(refDate));
    }


    @GetMapping("/data/pie/{refDate}")
    public R<AttendanceMonthlyCount> selectAttendanceMonthlyCountByMonth(@PathVariable String refDate) {
        return R.ok(iSysOaAttendanceService.selectAttendanceMonthlyCountByMonth(refDate));
    }


    @GetMapping("/data/card/{refDate}")
    public R<List<AttendanceMonthlyCount>> selectMonthlyCounts(@PathVariable String refDate) {
        return R.ok(iSysOaAttendanceService.selectMonthlyCounts(refDate));
    }
}
