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.OaSalaryTemplateBo;
import com.ruoyi.oa.domain.vo.OaSalaryTemplateVo;
import com.ruoyi.oa.domain.OaSalaryTemplate;
import com.ruoyi.oa.mapper.OaSalaryTemplateMapper;
import com.ruoyi.oa.service.IOaSalaryTemplateService;

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

/**
 * 薪资模板主Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-23
 */
@RequiredArgsConstructor
@Service
public class OaSalaryTemplateServiceImpl implements IOaSalaryTemplateService {

    private final OaSalaryTemplateMapper baseMapper;

    /**
     * 查询薪资模板主
     */
    @Override
    public OaSalaryTemplateVo queryById(Long salaryTemplateId){
        return baseMapper.selectVoById(salaryTemplateId);
    }

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

    /**
     * 查询薪资模板主列表
     */
    @Override
    public List<OaSalaryTemplateVo> queryList(OaSalaryTemplateBo bo) {
        LambdaQueryWrapper<OaSalaryTemplate> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OaSalaryTemplate> buildQueryWrapper(OaSalaryTemplateBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OaSalaryTemplate> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getTemplateName()), OaSalaryTemplate::getTemplateName, bo.getTemplateName());
        return lqw;
    }

    /**
     * 新增薪资模板主
     */
    @Override
    public Boolean insertByBo(OaSalaryTemplateBo bo) {
        OaSalaryTemplate add = BeanUtil.toBean(bo, OaSalaryTemplate.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setSalaryTemplateId(add.getSalaryTemplateId());
        }
        return flag;
    }

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

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

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