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

import cn.hutool.core.collection.CollectionUtil;
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.workovertime.vo.WorkOvertimeMoreRulePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.workovertime.vo.WorkOvertimeMoreRuleSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.workovertime.WorkOvertimeMoreRuleDO;
import cn.iocoder.yudao.module.system.dal.mysql.workovertime.WorkOvertimeMoreRuleMapper;
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;

/**
 * 加班规则（更多规则） Service 实现类
 *
 * @author 艾楷
 */
@Service
@Validated
public class WorkOvertimeMoreRuleServiceImpl implements WorkOvertimeMoreRuleService {

    @Resource
    private WorkOvertimeMoreRuleMapper workOvertimeMoreRuleMapper;

    @Override
    public Long createWorkOvertimeMoreRule(WorkOvertimeMoreRuleSaveReqVO createReqVO) {
        // 插入
        WorkOvertimeMoreRuleDO workOvertimeMoreRule = BeanUtils.toBean(createReqVO, WorkOvertimeMoreRuleDO.class);
        workOvertimeMoreRuleMapper.insert(workOvertimeMoreRule);
        // 返回
        return workOvertimeMoreRule.getId();
    }

    @Override
    public void updateWorkOvertimeMoreRule(WorkOvertimeMoreRuleSaveReqVO updateReqVO) {
        // 更新
        WorkOvertimeMoreRuleDO updateObj = BeanUtils.toBean(updateReqVO, WorkOvertimeMoreRuleDO.class);
        workOvertimeMoreRuleMapper.updateById(updateObj);
    }

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

    @Override
    public WorkOvertimeMoreRuleDO getWorkOvertimeMoreRule(Long id) {
        return workOvertimeMoreRuleMapper.selectById(id);
    }

    @Override
    public PageResult<WorkOvertimeMoreRuleDO> getWorkOvertimeMoreRulePage(WorkOvertimeMoreRulePageReqVO pageReqVO) {
        return workOvertimeMoreRuleMapper.selectPage(pageReqVO);
    }

    @Override
    public WorkOvertimeMoreRuleDO getByRuleId(Long ruleId) {
        List<WorkOvertimeMoreRuleDO> workOvertimeMoreRuleDOS = workOvertimeMoreRuleMapper.selectList(new LambdaQueryWrapper<WorkOvertimeMoreRuleDO>().eq(WorkOvertimeMoreRuleDO::getRuleId, ruleId));
        if (CollectionUtil.isEmpty(workOvertimeMoreRuleDOS)) {
            return null;
        }
        return workOvertimeMoreRuleDOS.get(0);
    }

}
