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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.map.MapUtil;
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.holidaybalancesetting.vo.HolidayBalanceSettingPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.holiday.holidaybalancesetting.vo.HolidayBalanceSettingSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.holiday.holidaybalancesetting.HolidayBalanceSettingDO;
import cn.iocoder.yudao.module.system.dal.mysql.holiday.holidaybalancesetting.HolidayBalanceSettingMapper;
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.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 假期余额设置 Service 实现类
 *
 * @author 艾楷
 */
@Service
@Validated
public class HolidayBalanceSettingServiceImpl implements HolidayBalanceSettingService {

    @Resource
    private HolidayBalanceSettingMapper holidayBalanceSettingMapper;

    @Override
    public Long createHolidayBalanceSetting(HolidayBalanceSettingSaveReqVO createReqVO) {
        // 插入
        HolidayBalanceSettingDO holidayBalanceSetting = BeanUtils.toBean(createReqVO, HolidayBalanceSettingDO.class);
        holidayBalanceSettingMapper.insert(holidayBalanceSetting);
        // 返回
        return holidayBalanceSetting.getId();
    }

    @Override
    public void updateHolidayBalanceSetting(HolidayBalanceSettingSaveReqVO updateReqVO) {
        // 更新
        HolidayBalanceSettingDO updateObj = BeanUtils.toBean(updateReqVO, HolidayBalanceSettingDO.class);
        holidayBalanceSettingMapper.updateById(updateObj);
    }

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


    @Override
    public HolidayBalanceSettingDO getHolidayBalanceSetting(Long id) {
        return holidayBalanceSettingMapper.selectById(id);
    }

    @Override
    public PageResult<HolidayBalanceSettingDO> getHolidayBalanceSettingPage(HolidayBalanceSettingPageReqVO pageReqVO) {
        return holidayBalanceSettingMapper.selectPage(pageReqVO);
    }

    @Override
    public HolidayBalanceSettingDO selectBySettingId(Long settingId) {
        List<HolidayBalanceSettingDO> list = holidayBalanceSettingMapper.selectList(new LambdaQueryWrapper<HolidayBalanceSettingDO>()
                .eq(HolidayBalanceSettingDO::getHolidaySettingId, settingId));
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public Map<Long, HolidayBalanceSettingDO> selectBySettingIds(List<Long> settingIds) {
        if (CollUtil.isEmpty(settingIds)) {
            return MapUtil.empty();
        }
        List<HolidayBalanceSettingDO> list = holidayBalanceSettingMapper.selectList(new LambdaQueryWrapper<HolidayBalanceSettingDO>()
                .in(HolidayBalanceSettingDO::getHolidaySettingId, settingIds));
        return list.stream().collect(Collectors.toMap(HolidayBalanceSettingDO::getHolidaySettingId, holidayBalanceSettingDO -> holidayBalanceSettingDO));
    }

    @Override
    public List<HolidayBalanceSettingDO> getHolidayBalanceSettingByHolidaySettingIds(List<Long> holidaySettingIds) {
        if (CollUtil.isEmpty(holidaySettingIds)) {
            return ListUtil.empty();
        }
        return holidayBalanceSettingMapper.selectList(new LambdaQueryWrapper<HolidayBalanceSettingDO>()
                .in(HolidayBalanceSettingDO::getHolidaySettingId, holidaySettingIds));
    }

    @Override
    public List<HolidayBalanceSettingDO> list(LambdaQueryWrapper<HolidayBalanceSettingDO> lambdaQueryWrapper) {
        return holidayBalanceSettingMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public void batchUpdate(List<HolidayBalanceSettingDO> editList) {
        holidayBalanceSettingMapper.updateBatch(editList);
    }

    @Override
    public List<HolidayBalanceSettingDO> getByIds(List<Long> holidayBalanceSettingIds) {
        return holidayBalanceSettingMapper.selectBatchIds(holidayBalanceSettingIds);
    }

}
