package cn.iocoder.yudao.module.system.service.attendance.group;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils;
import cn.iocoder.yudao.module.system.controller.admin.group.vo.AttendanceGroupPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.group.vo.AttendanceGroupSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.attendance.group.AttendanceGroupDO;
import cn.iocoder.yudao.module.system.dal.dataobject.attendance.groupuser.AttendanceGroupUserDO;
import cn.iocoder.yudao.module.system.dal.mysql.attendance.group.AttendanceGroupMapper;
import cn.iocoder.yudao.module.system.dal.mysql.attendance.groupuser.AttendanceGroupUserMapper;
import cn.iocoder.yudao.module.system.service.attendance.fixed.AttendanceFixedService;
import cn.iocoder.yudao.module.system.service.attendance.groupsystem.AttendanceGroupSystemService;
import cn.iocoder.yudao.module.system.service.attendance.scheduling.AttendanceSchedulingService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

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

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.GROUP_NOT_EXISTS;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.THERE_ARE_STILL_PEOPLE_IN_THE_ATTENDANCE_GROUP_WHO_ARE_NOT_ALLOWED_TO_BE_DELETED;

/**
 * 考勤组 Service 实现类
 *
 * @author 艾楷
 */
@Service
@Validated
public class AttendanceGroupServiceImpl implements AttendanceGroupService {

    @Resource
    private AttendanceGroupMapper groupMapper;
    @Resource
    private AttendanceGroupMapper attendanceGroupMapper;
    @Resource
    private AttendanceGroupUserMapper attendanceGroupUserMapper;
    @Resource
    private AttendanceFixedService attendanceFixedService;
    @Resource
    private AttendanceSchedulingService attendanceSchedulingService;
    @Resource
    private AttendanceGroupSystemService attendanceGroupSystemService;


    @Override
    public Long createGroup(AttendanceGroupSaveReqVO createReqVO) {
        AttendanceGroupDO group = BeanUtils.toBean(createReqVO, AttendanceGroupDO.class);
        groupMapper.insert(group);
        //把考勤组用户ids拿出来
        // 插入
        attendanceGroupSystemService.batchCreateOrUpdate(createReqVO.getUserIds(), group.getId());
        // 返回
        return group.getId();
    }

    @Override
    public void updateGroup(AttendanceGroupSaveReqVO updateReqVO) {
        // 校验存在
        validateGroupExists(updateReqVO.getId());
        // 更新
        AttendanceGroupDO updateObj = BeanUtils.toBean(updateReqVO, AttendanceGroupDO.class);
        groupMapper.updateById(updateObj);
        // 插入
        attendanceGroupSystemService.batchCreateOrUpdate(updateReqVO.getUserIds(), updateReqVO.getId());
    }

    @Override
    public void deleteGroup(Long id) {
        // 校验存在
        validateGroupExists(id);
        // - 判断考勤组内是否存在人员 - 如果有的话提示不能删除
        int num = Math.toIntExact(attendanceGroupUserMapper.selectCount(new LambdaQueryWrapper<AttendanceGroupUserDO>().eq(AttendanceGroupUserDO::getAttendanceGroupId, id)));
        if (num > 0) {
            throw exception(THERE_ARE_STILL_PEOPLE_IN_THE_ATTENDANCE_GROUP_WHO_ARE_NOT_ALLOWED_TO_BE_DELETED);
        }
        // 删除
        groupMapper.deleteById(id);
        attendanceGroupSystemService.delByGroupId(id);
    }

    private void validateGroupExists(Long id) {
        if (groupMapper.selectById(id) == null) {
            throw exception(GROUP_NOT_EXISTS);
        }
    }

    @Override
    public AttendanceGroupDO getGroup(Long id) {
        return groupMapper.selectById(id);
    }

    @Override
    public PageResult<AttendanceGroupDO> getGroupPage(AttendanceGroupPageReqVO pageReqVO) {
        IPage<AttendanceGroupDO> page = groupMapper.selectPageList(pageReqVO, MyBatisUtils.buildPage(pageReqVO));
        return new PageResult<>(page.getRecords(), page.getTotal());
    }

    @Override
    public AttendanceGroupDO getByUserId(Long userId) {
        List<AttendanceGroupDO> dos = groupMapper.getByUserId(userId);
        if (!dos.isEmpty()) {
            return dos.get(0);
        }
        return null;
    }

    @Override
    public List<AttendanceGroupDO> getAll() {
        return attendanceGroupMapper.selectList();
    }

    @Override
    public List<AttendanceGroupDO> getAllFilterWorkOvertimeRule(Long workOvertimeRuleId) {
        return attendanceGroupMapper.getAllFilterWorkOvertimeRule(workOvertimeRuleId);
    }

    @Override
    public List<Long> getUseGroupIds(Long shiftId) {
        List<Long> fixedGroupIds = attendanceFixedService.getUseGroupIds(shiftId);
        List<Long> ids = new ArrayList<>(fixedGroupIds);
        List<Long> schedulingGroupIds = attendanceSchedulingService.getUseGroupIds(shiftId);
        ids.addAll(schedulingGroupIds);
        return ids;
    }

}
