/**
* Copyright (c) 2018 人人开源 All rights reserved.
*
* https://www.renren.io
*
* 版权所有,侵权必究!
*/
package com.iailab.framework.common.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
import com.iailab.framework.common.page.PageData;
import com.iailab.framework.common.service.CrudService;
import com.iailab.framework.common.util.object.ConvertUtils;
import org.springframework.beans.BeanUtils;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
* CRUD基础服务类
*
* @author Mark sunlightcs@gmail.com
*/
public abstract class CrudServiceImpl, T, D> extends BaseServiceImpl implements CrudService {
protected Class currentDtoClass() {
return (Class)ReflectionKit.getSuperClassGenericType(getClass(), CrudServiceImpl.class, 2);
}
@Override
public PageData page(Map params) {
IPage page = baseDao.selectPage(
getPage(params, null, false),
getWrapper(params)
);
return getPageData(page, currentDtoClass());
}
@Override
public List list(Map params) {
List entityList = baseDao.selectList(getWrapper(params));
return ConvertUtils.sourceToTarget(entityList, currentDtoClass());
}
public abstract QueryWrapper getWrapper(Map params);
@Override
public D get(Long id) {
T entity = baseDao.selectById(id);
return ConvertUtils.sourceToTarget(entity, currentDtoClass());
}
@Override
public void save(D dto) {
T entity = ConvertUtils.sourceToTarget(dto, currentModelClass());
insert(entity);
//copy主键值到dto
BeanUtils.copyProperties(entity, dto);
}
@Override
public void update(D dto) {
T entity = ConvertUtils.sourceToTarget(dto, currentModelClass());
updateById(entity);
}
@Override
public void delete(Long[] ids) {
baseDao.deleteBatchIds(Arrays.asList(ids));
}
}