package com.ruoyi.oa.service.impl;

import cn.hutool.core.bean.BeanUtil;
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.OaBonusAllocationBo;
import com.ruoyi.oa.domain.vo.OaBonusAllocationVo;
import com.ruoyi.oa.domain.OaBonusAllocation;
import com.ruoyi.oa.mapper.OaBonusAllocationMapper;
import com.ruoyi.oa.service.IOaBonusAllocationService;

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

/**
 * 奖金池分配（记录奖金池向用户的分配比例及金额）Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-24
 */
@RequiredArgsConstructor
@Service
public class OaBonusAllocationServiceImpl implements IOaBonusAllocationService {

    private final OaBonusAllocationMapper baseMapper;

    /**
     * 查询奖金池分配（记录奖金池向用户的分配比例及金额）
     */
    @Override
    public OaBonusAllocationVo queryById(Long allocationId){
        return baseMapper.selectVoById(allocationId);
    }

    /**
     * 查询奖金池分配（记录奖金池向用户的分配比例及金额）列表
     */
    @Override
    public TableDataInfo<OaBonusAllocationVo> queryPageList(OaBonusAllocationBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OaBonusAllocation> lqw = buildQueryWrapper(bo);
        Page<OaBonusAllocationVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询奖金池分配（记录奖金池向用户的分配比例及金额）列表
     */
    @Override
    public List<OaBonusAllocationVo> queryList(OaBonusAllocationBo bo) {
        LambdaQueryWrapper<OaBonusAllocation> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OaBonusAllocation> buildQueryWrapper(OaBonusAllocationBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OaBonusAllocation> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPoolId() != null, OaBonusAllocation::getPoolId, bo.getPoolId());
        lqw.like(StringUtils.isNotBlank(bo.getNickName()), OaBonusAllocation::getNickName, bo.getNickName());
        lqw.eq(bo.getAllocationRatio() != null, OaBonusAllocation::getAllocationRatio, bo.getAllocationRatio());
        lqw.orderByDesc(OaBonusAllocation::getCreateTime);
        return lqw;
    }

    /**
     * 新增奖金池分配（记录奖金池向用户的分配比例及金额）
     */
    @Override
    public Boolean insertByBo(OaBonusAllocationBo bo) {
        OaBonusAllocation add = BeanUtil.toBean(bo, OaBonusAllocation.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setAllocationId(add.getAllocationId());
        }
        return flag;
    }

    /**
     * 修改奖金池分配（记录奖金池向用户的分配比例及金额）
     */
    @Override
    public Boolean updateByBo(OaBonusAllocationBo bo) {
        OaBonusAllocation update = BeanUtil.toBean(bo, OaBonusAllocation.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 批量删除奖金池分配（记录奖金池向用户的分配比例及金额）
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
