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

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.module.system.dal.dataobject.workovertime.WorkOvertimeRuleAttendanceGroupDO;
import cn.iocoder.yudao.module.system.dal.dataobject.workovertime.WorkOvertimeRuleDO;
import cn.iocoder.yudao.module.system.dal.mysql.workovertime.WorkOvertimeRuleAttendanceGroupMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 加班规则考勤组关联 Service 实现类
 *
 * @author 艾楷
 */
@Service
@Validated
public class WorkOvertimeRuleAttendanceGroupServiceImpl implements WorkOvertimeRuleAttendanceGroupService {

    @Resource
    private WorkOvertimeRuleAttendanceGroupMapper workOvertimeRuleAttendanceGroupMapper;
    @Resource
    @Lazy
    private WorkOvertimeRuleService workOvertimeRuleService;

    @Override
    public List<WorkOvertimeRuleAttendanceGroupDO> getListByRuleId(Long ruleId) {
        return workOvertimeRuleAttendanceGroupMapper.selectList(new LambdaQueryWrapper<WorkOvertimeRuleAttendanceGroupDO>()
                .eq(WorkOvertimeRuleAttendanceGroupDO::getRuleId, ruleId));
    }


    @Override
    public WorkOvertimeRuleDO getRuleByAttendanceGroupId(Long attendanceGroupId) {
        List<WorkOvertimeRuleAttendanceGroupDO> workOvertimeRuleAttendanceGroupDOS = workOvertimeRuleAttendanceGroupMapper.selectList(new LambdaQueryWrapper<WorkOvertimeRuleAttendanceGroupDO>()
                .eq(WorkOvertimeRuleAttendanceGroupDO::getAttendanceGroupId, attendanceGroupId));
        if (CollUtil.isEmpty(workOvertimeRuleAttendanceGroupDOS)) {
            return null;
        }
        Long ruleId = workOvertimeRuleAttendanceGroupDOS.get(0).getRuleId();
        return workOvertimeRuleService.getWorkOvertimeRule(ruleId);
    }

    @Override
    public void insertDeleteBatch(Long ruleId, List<List<Long>> attendanceGroupIdLists) {
        if (CollUtil.isNotEmpty(attendanceGroupIdLists.get(0))) {
            List<WorkOvertimeRuleAttendanceGroupDO> workOvertimeRuleAttendanceGroupDOS = new ArrayList<>();
            attendanceGroupIdLists.get(0).forEach(a -> {
                        workOvertimeRuleAttendanceGroupDOS.add(new WorkOvertimeRuleAttendanceGroupDO().setRuleId(ruleId)
                                .setAttendanceGroupId(a));
                    }
            );
            workOvertimeRuleAttendanceGroupMapper.insertBatch(workOvertimeRuleAttendanceGroupDOS);
        }
        if (CollUtil.isNotEmpty(attendanceGroupIdLists.get(2))) {
            workOvertimeRuleAttendanceGroupMapper.delete(new LambdaQueryWrapper<WorkOvertimeRuleAttendanceGroupDO>()
                    .eq(WorkOvertimeRuleAttendanceGroupDO::getRuleId, ruleId)
                    .in(WorkOvertimeRuleAttendanceGroupDO::getAttendanceGroupId, attendanceGroupIdLists.get(2)));
        }
    }
}
