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

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

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

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

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;

/**
 * 加班扣除规则 Service 实现类
 *
 * @author 艾楷
 */
@Service
@Validated
public class WorkOvertimeDeductRuleServiceImpl implements WorkOvertimeDeductRuleService {

    @Resource
    private WorkOvertimeDeductRuleMapper workOvertimeDeductRuleMapper;

    @Override
    public Long createWorkOvertimeDeductRule(WorkOvertimeDeductRuleSaveReqVO createReqVO) {
        // 插入
        WorkOvertimeDeductRuleDO workOvertimeDeductRule = BeanUtils.toBean(createReqVO, WorkOvertimeDeductRuleDO.class);
        workOvertimeDeductRuleMapper.insert(workOvertimeDeductRule);
        // 返回
        return workOvertimeDeductRule.getId();
    }

    @Override
    public void updateWorkOvertimeDeductRule(WorkOvertimeDeductRuleSaveReqVO updateReqVO) {
        // 更新
        WorkOvertimeDeductRuleDO updateObj = BeanUtils.toBean(updateReqVO, WorkOvertimeDeductRuleDO.class);
        workOvertimeDeductRuleMapper.updateById(updateObj);
    }

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

    @Override
    public WorkOvertimeDeductRuleDO getWorkOvertimeDeductRule(Long id) {
        return workOvertimeDeductRuleMapper.selectById(id);
    }

    @Override
    public PageResult<WorkOvertimeDeductRuleDO> getWorkOvertimeDeductRulePage(WorkOvertimeDeductRulePageReqVO pageReqVO) {
        return workOvertimeDeductRuleMapper.selectPage(pageReqVO);
    }

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

    @Override
    public void insertUpdateDeleteBatch(List<List<WorkOvertimeDeductRuleDO>> deductRuleLists) {
        if (CollUtil.isNotEmpty(deductRuleLists.get(0))) {
            workOvertimeDeductRuleMapper.insertBatch(deductRuleLists.get(0));
        }
        if (CollUtil.isNotEmpty(deductRuleLists.get(1))) {
            workOvertimeDeductRuleMapper.updateBatch(deductRuleLists.get(1));
        }
        if (CollUtil.isNotEmpty(deductRuleLists.get(2))) {
            workOvertimeDeductRuleMapper.deleteBatchIds(convertList(deductRuleLists.get(2), WorkOvertimeDeductRuleDO::getId));
        }
    }

}
