package com.kym.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.enums.SqlMethod;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.github.yulichang.base.MPJBaseMapper;
import com.github.yulichang.base.MPJBaseService;
import org.apache.ibatis.binding.MapperMethod;

import java.util.Collection;
import java.util.function.Function;

public class MyBaseServiceImpl<M extends MPJBaseMapper<T>, T> extends ServiceImpl<M, T> implements MPJBaseService<T>, MyBaseService<T> {

    @Override
    public boolean updateBatchByQueryWrapper(Collection<T> entityList, Function<T, QueryWrapper<T>> queryWrapperFunction) {
        String sqlStatement = this.getSqlStatement(SqlMethod.UPDATE);
        return this.executeBatch(entityList, DEFAULT_BATCH_SIZE, (sqlSession, entity) -> {
            MapperMethod.ParamMap param = new MapperMethod.ParamMap();
            param.put(Constants.ENTITY, entity);
            param.put(Constants.WRAPPER, queryWrapperFunction.apply(entity));
            sqlSession.update(sqlStatement, param);
        });
    }

    @Override
    public boolean updateByQueryWrapper(T entity, Function<T, QueryWrapper<T>> queryWrapperFunction) {
        return SqlHelper.retBool(getBaseMapper().update(entity, queryWrapperFunction.apply(entity)));
    }
}
