package cn.iocoder.yudao.module.system.api.workovertime;

import cn.hutool.core.bean.BeanUtil;
import cn.iocoder.yudao.framework.common.Constants;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.system.api.workovertime.vo.WorkOvertimeRuleApiVO;
import cn.iocoder.yudao.module.system.dal.dataobject.attendance.group.AttendanceGroupDO;
import cn.iocoder.yudao.module.system.dal.dataobject.workovertime.WorkOvertimeRuleDO;
import cn.iocoder.yudao.module.system.service.attendance.group.AttendanceGroupService;
import cn.iocoder.yudao.module.system.service.workovertime.WorkOvertimeRuleAttendanceGroupService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@Validated
public class WorkOvertimeApiImpl implements WorkOvertimeApi {

    @Resource
    private AttendanceGroupService attendanceGroupService;
    @Resource
    private WorkOvertimeRuleAttendanceGroupService workOvertimeRuleAttendanceGroupService;

    @Override
    public CommonResult<WorkOvertimeRuleApiVO> getOvertimeRulesByUserId(Long userId) {
        WorkOvertimeRuleApiVO vo = new WorkOvertimeRuleApiVO();
        //先获取当前用户所在考勤组 - 再根据考勤组 查询对应的加班规则
        AttendanceGroupDO attendanceGroupDO = attendanceGroupService.getByUserId(userId);
        if (attendanceGroupDO == null) {
            return CommonResult.success(vo.setIsGroup(Constants.ZERO));
        }
        WorkOvertimeRuleDO workOvertimeRuleDO = workOvertimeRuleAttendanceGroupService.getRuleByAttendanceGroupId(attendanceGroupDO.getId());
        if (workOvertimeRuleDO == null) {
            return CommonResult.success(vo.setIsOvertime(Constants.ZERO));
        }
        BeanUtil.copyProperties(workOvertimeRuleDO, vo);
        return CommonResult.success(vo);
    }
}
