package com.ruoyi.oa.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.oa.domain.bo.OaBonusProjectRelBo;
import com.ruoyi.oa.domain.vo.OaBonusProjectRelVo;
import com.ruoyi.oa.domain.OaBonusProjectRel;
import com.ruoyi.oa.mapper.OaBonusProjectRelMapper;
import com.ruoyi.oa.service.IOaBonusProjectRelService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 奖金池与项目关联Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-23
 */
@RequiredArgsConstructor
@Service
public class OaBonusProjectRelServiceImpl implements IOaBonusProjectRelService {

    private final OaBonusProjectRelMapper baseMapper;

    /**
     * 查询奖金池与项目关联
     */
    @Override
    public OaBonusProjectRelVo queryById(Long relId){
        return baseMapper.selectVoById(relId);
    }

    /**
     * 查询奖金池与项目关联列表
     */
    @Override
    public TableDataInfo<OaBonusProjectRelVo> queryPageList(OaBonusProjectRelBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OaBonusProjectRel> lqw = buildQueryWrapper(bo);
        Page<OaBonusProjectRelVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询奖金池与项目关联列表
     */
    @Override
    public List<OaBonusProjectRelVo> queryList(OaBonusProjectRelBo bo) {
        LambdaQueryWrapper<OaBonusProjectRel> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OaBonusProjectRel> buildQueryWrapper(OaBonusProjectRelBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OaBonusProjectRel> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPoolId() != null, OaBonusProjectRel::getPoolId, bo.getPoolId());
        lqw.eq(bo.getProjectId() != null, OaBonusProjectRel::getProjectId, bo.getProjectId());
        lqw.eq(bo.getAllocatedAmount() != null, OaBonusProjectRel::getAllocatedAmount, bo.getAllocatedAmount());
        return lqw;
    }

    /**
     * 新增奖金池与项目关联
     */
    @Override
    public Boolean insertByBo(OaBonusProjectRelBo bo) {
        OaBonusProjectRel add = BeanUtil.toBean(bo, OaBonusProjectRel.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setRelId(add.getRelId());
        }
        return flag;
    }

    /**
     * 修改奖金池与项目关联
     */
    @Override
    public Boolean updateByBo(OaBonusProjectRelBo bo) {
        OaBonusProjectRel update = BeanUtil.toBean(bo, OaBonusProjectRel.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(OaBonusProjectRel entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除奖金池与项目关联
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }


}
