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

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.system.controller.admin.workovertime.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.workovertime.WorkOvertimeRuleItemDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.system.dal.mysql.workovertime.WorkOvertimeRuleItemMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;

/**
 * 加班规则子表(按日类型区分) Service 实现类
 *
 * @author 艾楷
 */
@Service
@Validated
public class WorkOvertimeRuleItemServiceImpl implements WorkOvertimeRuleItemService {

    @Resource
    private WorkOvertimeRuleItemMapper workOvertimeRuleItemMapper;

    @Override
    public Long createWorkOvertimeRuleItem(WorkOvertimeRuleItemSaveReqVO createReqVO) {
        // 插入
        WorkOvertimeRuleItemDO workOvertimeRuleItem = BeanUtils.toBean(createReqVO, WorkOvertimeRuleItemDO.class);
        workOvertimeRuleItemMapper.insert(workOvertimeRuleItem);
        // 返回
        return workOvertimeRuleItem.getId();
    }

    @Override
    public void updateWorkOvertimeRuleItem(WorkOvertimeRuleItemSaveReqVO updateReqVO) {
        // 更新
        WorkOvertimeRuleItemDO updateObj = BeanUtils.toBean(updateReqVO, WorkOvertimeRuleItemDO.class);
        workOvertimeRuleItemMapper.updateById(updateObj);
    }

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

    @Override
    public WorkOvertimeRuleItemDO getWorkOvertimeRuleItem(Long id) {
        return workOvertimeRuleItemMapper.selectById(id);
    }

    @Override
    public PageResult<WorkOvertimeRuleItemDO> getWorkOvertimeRuleItemPage(WorkOvertimeRuleItemPageReqVO pageReqVO) {
        return workOvertimeRuleItemMapper.selectPage(pageReqVO);
    }

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

    @Override
    public void insertUpdateDeleteBatch(Long ruleId, List<List<WorkOvertimeRuleItemDO>> ruleItemLists) {
        if (CollUtil.isNotEmpty(ruleItemLists.get(0))) {
            ruleItemLists.get(0).forEach(a -> a.setId(IdWorker.getId()).setRuleId(ruleId));
            workOvertimeRuleItemMapper.insertBatch(ruleItemLists.get(0));
        }
        if (CollUtil.isNotEmpty(ruleItemLists.get(1))) {
            workOvertimeRuleItemMapper.updateBatch(ruleItemLists.get(1));
        }
        if (CollUtil.isNotEmpty(ruleItemLists.get(2))) {
            workOvertimeRuleItemMapper.deleteBatchIds(convertList(ruleItemLists.get(2), WorkOvertimeRuleItemDO::getId));
        }
    }

}
