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

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.workovertime.vo.WorkOvertimeMoreRuleExtPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.workovertime.vo.WorkOvertimeMoreRuleExtSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.workovertime.WorkOvertimeMoreRuleExtDO;
import cn.iocoder.yudao.module.system.dal.mysql.workovertime.WorkOvertimeMoreRuleExtMapper;
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 static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;

/**
 * 加班更多规则拓展 Service 实现类
 *
 * @author 艾楷
 */
@Service
@Validated
public class WorkOvertimeMoreRuleExtServiceImpl implements WorkOvertimeMoreRuleExtService {

    @Resource
    private WorkOvertimeMoreRuleExtMapper workOvertimeMoreRuleExtMapper;

    @Override
    public Long createWorkOvertimeMoreRuleExt(WorkOvertimeMoreRuleExtSaveReqVO createReqVO) {
        // 插入
        WorkOvertimeMoreRuleExtDO workOvertimeMoreRuleExt = BeanUtils.toBean(createReqVO, WorkOvertimeMoreRuleExtDO.class);
        workOvertimeMoreRuleExtMapper.insert(workOvertimeMoreRuleExt);
        // 返回
        return workOvertimeMoreRuleExt.getId();
    }

    @Override
    public void updateWorkOvertimeMoreRuleExt(WorkOvertimeMoreRuleExtSaveReqVO updateReqVO) {
        // 更新
        WorkOvertimeMoreRuleExtDO updateObj = BeanUtils.toBean(updateReqVO, WorkOvertimeMoreRuleExtDO.class);
        workOvertimeMoreRuleExtMapper.updateById(updateObj);
    }

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

    @Override
    public WorkOvertimeMoreRuleExtDO getWorkOvertimeMoreRuleExt(Long id) {
        return workOvertimeMoreRuleExtMapper.selectById(id);
    }

    @Override
    public PageResult<WorkOvertimeMoreRuleExtDO> getWorkOvertimeMoreRuleExtPage(WorkOvertimeMoreRuleExtPageReqVO pageReqVO) {
        return workOvertimeMoreRuleExtMapper.selectPage(pageReqVO);
    }

    @Override
    public List<WorkOvertimeMoreRuleExtDO> getListByMoreRuleId(Long moreRuleId) {
        return workOvertimeMoreRuleExtMapper.selectList(new LambdaQueryWrapper<WorkOvertimeMoreRuleExtDO>()
                .eq(WorkOvertimeMoreRuleExtDO::getMoreRuleId, moreRuleId));
    }

    @Override
    public void insertUpdateDeleteBatch(Long moreRuleId, List<List<WorkOvertimeMoreRuleExtDO>> moreRuleLists) {
        if (CollUtil.isNotEmpty(moreRuleLists.get(0))) {
            moreRuleLists.get(0).forEach(a -> a.setMoreRuleId(moreRuleId));
            workOvertimeMoreRuleExtMapper.insertBatch(moreRuleLists.get(0));
        }
        if (CollUtil.isNotEmpty(moreRuleLists.get(1))) {
            workOvertimeMoreRuleExtMapper.updateBatch(moreRuleLists.get(1));
        }
        if (CollUtil.isNotEmpty(moreRuleLists.get(2))) {
            workOvertimeMoreRuleExtMapper.deleteBatchIds(convertList(moreRuleLists.get(2), WorkOvertimeMoreRuleExtDO::getId));
        }
    }

}
