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.OaSalaryTemplateDetailBo;
import com.ruoyi.oa.domain.vo.OaSalaryTemplateDetailVo;
import com.ruoyi.oa.domain.OaSalaryTemplateDetail;
import com.ruoyi.oa.mapper.OaSalaryTemplateDetailMapper;
import com.ruoyi.oa.service.IOaSalaryTemplateDetailService;

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

/**
 * 薪资模板明细Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-23
 */
@RequiredArgsConstructor
@Service
public class OaSalaryTemplateDetailServiceImpl implements IOaSalaryTemplateDetailService {

    private final OaSalaryTemplateDetailMapper baseMapper;

    /**
     * 查询薪资模板明细
     */
    @Override
    public OaSalaryTemplateDetailVo queryById(Long salaryDetailId){
        return baseMapper.selectVoById(salaryDetailId);
    }

    /**
     * 查询薪资模板明细列表
     */
    @Override
    public TableDataInfo<OaSalaryTemplateDetailVo> queryPageList(OaSalaryTemplateDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OaSalaryTemplateDetail> lqw = buildQueryWrapper(bo);
        Page<OaSalaryTemplateDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询薪资模板明细列表
     */
    @Override
    public List<OaSalaryTemplateDetailVo> queryList(OaSalaryTemplateDetailBo bo) {
        LambdaQueryWrapper<OaSalaryTemplateDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OaSalaryTemplateDetail> buildQueryWrapper(OaSalaryTemplateDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OaSalaryTemplateDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSalaryTemplateId() != null, OaSalaryTemplateDetail::getSalaryTemplateId, bo.getSalaryTemplateId());
        lqw.eq(StringUtils.isNotBlank(bo.getSalaryItem()), OaSalaryTemplateDetail::getSalaryItem, bo.getSalaryItem());
        lqw.eq(bo.getAmount() != null, OaSalaryTemplateDetail::getAmount, bo.getAmount());
        lqw.eq(bo.getRate() != null, OaSalaryTemplateDetail::getRate, bo.getRate());
        return lqw;
    }

    /**
     * 新增薪资模板明细
     */
    @Override
    public Boolean insertByBo(OaSalaryTemplateDetailBo bo) {
        OaSalaryTemplateDetail add = BeanUtil.toBean(bo, OaSalaryTemplateDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setSalaryDetailId(add.getSalaryDetailId());
        }
        return flag;
    }

    /**
     * 修改薪资模板明细
     */
    @Override
    public Boolean updateByBo(OaSalaryTemplateDetailBo bo) {
        OaSalaryTemplateDetail update = BeanUtil.toBean(bo, OaSalaryTemplateDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 批量删除薪资模板明细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
