package jnpf.attendance.service.impl;

import cn.hutool.core.collection.CollUtil;
import jnpf.attendance.mapper.DailyRuleChangeMapper;
import jnpf.attendance.service.AttendanceClockInService;
import jnpf.attendance.service.DailyRuleChangeService;
import jnpf.base.UserInfo;
import jnpf.base.service.SuperServiceImpl;
import jnpf.entity.attendance.DailyRuleChange;
import jnpf.model.attendance.vo.UserDayVo;
import jnpf.util.DateDetail;
import jnpf.util.UserProvider;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 出勤规则变更服务实现
 *
 * @author yanwenfu
 * @create 2026-05-20
 */
@Service
public class DailyRuleChangeServiceImpl extends SuperServiceImpl<DailyRuleChangeMapper, DailyRuleChange> implements DailyRuleChangeService {

    @Resource
    private AttendanceClockInService attendanceClockInService;

    @Override
    public boolean dailyRuleChangeExecute() {
        // 查询是否有出勤变更未执行的记录
        List<DailyRuleChange> list = this.list();
        if (!list.isEmpty()) {
            UserInfo userInfo = UserProvider.getUser();
            attendanceClockInService.changeAttendanceRuleBatch(change(list), userInfo);
        }
        return true;
    }

    public static List<UserDayVo> change(List<DailyRuleChange> list) {

        if (CollUtil.isEmpty(list)) {
            return List.of();
        }
        return list.stream().map(item -> {
            UserDayVo vo = new UserDayVo();
            vo.setTenantId(item.getTenantId());
            vo.setGroupId(item.getGroupId());
            vo.setUserId(item.getUserId());
            vo.setDay(item.getDay());
            if (item.getDay() != null) {
                vo.setDayStr(DateDetail.getDate2Str(item.getDay(), DateDetail.DF));
            }
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public void saveRecordBatch(List<DailyRuleChange> list) {

        baseMapper.saveRecordBatch(list);
    }
}
