package cn.iocoder.yudao.module.system.service.holiday.holidaysettingrange;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.controller.admin.holiday.holidaysettingrange.vo.HolidaySettingRangePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.holiday.holidaysettingrange.vo.HolidaySettingRangeSaveReqVO;
import cn.iocoder.yudao.module.system.controller.admin.user.dto.UserDTO;
import cn.iocoder.yudao.module.system.dal.dataobject.holiday.holidaysettingrange.HolidaySettingRangeDO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.dal.mysql.holiday.holidaysettingrange.HolidaySettingRangeMapper;
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

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

/**
 * 假期规则人员范围 Service 实现类
 *
 * @author 艾楷
 */
@Service
@Validated
public class HolidaySettingRangeServiceImpl implements HolidaySettingRangeService {

    @Resource
    private HolidaySettingRangeMapper holidaySettingRangeMapper;
    @Resource
    private AdminUserService adminUserService;

    @Override
    public Long createHolidaySettingRange(HolidaySettingRangeSaveReqVO createReqVO) {
        // 插入
        HolidaySettingRangeDO holidaySettingRange = BeanUtils.toBean(createReqVO, HolidaySettingRangeDO.class);
        holidaySettingRangeMapper.insert(holidaySettingRange);
        // 返回
        return holidaySettingRange.getId();
    }

    @Override
    public void updateHolidaySettingRange(HolidaySettingRangeSaveReqVO updateReqVO) {
        // 更新
        HolidaySettingRangeDO updateObj = BeanUtils.toBean(updateReqVO, HolidaySettingRangeDO.class);
        holidaySettingRangeMapper.updateById(updateObj);
    }

    @Override
    public void deleteHolidaySettingRange(Long id) {
        // 删除
        holidaySettingRangeMapper.deleteById(id);
    }

    @Override
    public HolidaySettingRangeDO getHolidaySettingRange(Long id) {
        return holidaySettingRangeMapper.selectById(id);
    }

    @Override
    public PageResult<HolidaySettingRangeDO> getHolidaySettingRangePage(HolidaySettingRangePageReqVO pageReqVO) {
        return holidaySettingRangeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<HolidaySettingRangeDO> selectBySettingId(Long settingId) {
        return holidaySettingRangeMapper.selectList(new LambdaQueryWrapper<HolidaySettingRangeDO>()
                .eq(HolidaySettingRangeDO::getHolidaySettingId, settingId));
    }

    @Override
    public List<AdminUserDO> getUsersByRange(List<HolidaySettingRangeDO> holidaySettingRangeDOS) {
        if (CollUtil.isEmpty(holidaySettingRangeDOS)) {
            return Collections.emptyList();
        }
        List<AdminUserDO> list = new ArrayList<>();
        List<Long> groupIds = holidaySettingRangeDOS.stream().filter(a -> a.getType() == 1).map(HolidaySettingRangeDO::getObjectId).collect(Collectors.toList());
        List<Long> deptIds = holidaySettingRangeDOS.stream().filter(a -> a.getType() == 2).map(HolidaySettingRangeDO::getObjectId).collect(Collectors.toList());
        List<Long> userIds = holidaySettingRangeDOS.stream().filter(a -> a.getType() == 3).map(HolidaySettingRangeDO::getObjectId).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(groupIds)) {
            list.addAll(adminUserService.getAllList(new UserDTO().setGroupIds(groupIds)));
        }
        if (CollUtil.isNotEmpty(deptIds)) {
            list.addAll(adminUserService.getAllList(new UserDTO().setDeptIds(deptIds)));
        }
        if (CollUtil.isNotEmpty(userIds)) {
            list.addAll(adminUserService.getAllList(new UserDTO().setUserIds(userIds)));
        }
        return list.stream().distinct().collect(Collectors.toList());
    }

    @Override
    public List<HolidaySettingRangeDO> getListByHolidaySettingId(Long holidaySettingId) {
        return holidaySettingRangeMapper.selectList(new LambdaQueryWrapper<HolidaySettingRangeDO>()
                .eq(HolidaySettingRangeDO::getHolidaySettingId, holidaySettingId));
    }

    @Override
    public List<HolidaySettingRangeDO> getListByHolidaySettingIds(List<Long> holidaySettingIds) {
        if (CollUtil.isEmpty(holidaySettingIds)) {
            return Collections.emptyList();
        }
        return holidaySettingRangeMapper.selectList(new LambdaQueryWrapper<HolidaySettingRangeDO>()
                .in(HolidaySettingRangeDO::getHolidaySettingId, holidaySettingIds));
    }

    @Override
    public List<HolidaySettingRangeDO> getByGroupId(Long groupId) {
        return holidaySettingRangeMapper.selectList(new LambdaQueryWrapper<HolidaySettingRangeDO>()
                .eq(HolidaySettingRangeDO::getType, 1)
                .eq(HolidaySettingRangeDO::getObjectId, groupId));
    }

    @Override
    public List<Long> getHolidaySettingIdsByGroupId(Long groupId) {
        return this.getByGroupId(groupId).stream().map(HolidaySettingRangeDO::getHolidaySettingId).collect(Collectors.toList());
    }

}
