package com.ruoyi.wgz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.wgz.bo.WgzPayCalculationFilesQueryBo;
import com.ruoyi.wgz.domain.WgzPayCalculationFiles;
import com.ruoyi.wgz.mapper.WgzPayCalculationFilesMapper;
import com.ruoyi.wgz.service.IWgzPayCalculationFilesService;

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

/**
 * 工资结算附件Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-22
 */
@Service
public class WgzPayCalculationFilesServiceImpl extends ServicePlusImpl<WgzPayCalculationFilesMapper, WgzPayCalculationFiles> implements IWgzPayCalculationFilesService {

    @Override
    public WgzPayCalculationFiles queryById(Long id){
        return getById(id);
    }

    @Override
    public TableDataInfo<WgzPayCalculationFiles> queryPageList(WgzPayCalculationFilesQueryBo bo) {
        Page<WgzPayCalculationFiles> result = page(PageUtils.buildPage(), buildQueryWrapper(bo));
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<WgzPayCalculationFiles> queryList(WgzPayCalculationFilesQueryBo bo) {
        return list(buildQueryWrapper(bo));
    }

    private LambdaQueryWrapper<WgzPayCalculationFiles> buildQueryWrapper(WgzPayCalculationFilesQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WgzPayCalculationFiles> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCalculationId() != null, WgzPayCalculationFiles::getCalculationId, bo.getCalculationId());
        lqw.eq(StrUtil.isNotBlank(bo.getTypeOfAttachment()), WgzPayCalculationFiles::getTypeOfAttachment, bo.getTypeOfAttachment());
        lqw.like(StrUtil.isNotBlank(bo.getName()), WgzPayCalculationFiles::getName, bo.getName());
        lqw.eq(StrUtil.isNotBlank(bo.getType()), WgzPayCalculationFiles::getType, bo.getType());
        lqw.eq(StrUtil.isNotBlank(bo.getAddress()), WgzPayCalculationFiles::getAddress, bo.getAddress());
        return lqw;
    }

    @Override
    public Boolean insert(WgzPayCalculationFiles bo) {
        WgzPayCalculationFiles add = BeanUtil.toBean(bo, WgzPayCalculationFiles.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean update(WgzPayCalculationFiles bo) {
        WgzPayCalculationFiles update = BeanUtil.toBean(bo, WgzPayCalculationFiles.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(WgzPayCalculationFiles entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
