BaseServiceImpl.java 14.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
/* Copyright (c) 2018, yiboshi.com All Rights Reserved. */
package com.yiboshi.science.base;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
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.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yiboshi.arch.exception.BusinessException;
import com.yiboshi.arch.exception.SystemException;
import com.yiboshi.science.BizConstant;
import com.yiboshi.science.utils.StringUtil;
import org.apache.commons.beanutils.BeanMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.util.*;


/**
 * Service基类实现
 *
 * @author NEGI
 * @version 2018-11
 */
@SuppressWarnings("unchecked")
public abstract class BaseServiceImpl<Dao extends BaseMapper<Entity>, QueryVO extends PaginationVO, DTO extends BaseDTO, Entity extends BaseEntity> extends ServiceImpl<Dao, Entity> implements BaseService<QueryVO, DTO, Entity> {
    protected Logger logger = LoggerFactory.getLogger(getClass());

    private StringUtil stringUtil;

    private enum Operation {
        INSERT,
        UPDATE,
        SAVE
    }

    // 设置查询条件criteria
    protected abstract void setCriteriaForQuery(QueryVO vo, QueryWrapper<QueryVO> criteria);

    // 设置排序条件criteria
    protected void setCriteriaOrder(QueryWrapper<Entity> wrapper) {

    }

    public void notNullField(Entity e, QueryWrapper<Entity> wrapper) {
        for (Field field : e.getClass().getDeclaredFields()) {
            field.setAccessible(true);
            try {
                //序列化 字段不需要查询
                if ("serialVersionUID".equals(field.getName())) {
                    continue;
                }
                //属性为空,不用查询
                if (field.get(e) == null) {
                    continue;
                }
                //主键 注解TableId
                TableId tableId = field.getAnnotation(TableId.class);
                if (tableId != null) {
                    //主键
                    wrapper.eq(tableId.value(), field.get(e));
                    continue;
                }
                //数据库中字段名和实体类属性不一致 注解TableField
                TableField tableField = field.getAnnotation(TableField.class);
                if (tableField != null) {
                    if (tableField.exist()) {
                        wrapper.eq(tableField.value(), field.get(e));
                    }// @TableField(exist = false) 不是表中内容 不形成查询条件
                    continue;
                }
                //数据库中字段名和实体类属性一致
                wrapper.eq(stringUtil.toLine(field.getName()), field.get(e));
            } catch (IllegalAccessException ex) {
                ex.printStackTrace();
            }
        }
    }

    @Override
    public String insert(Entity e) {
        e.setId(String.valueOf(UUID.randomUUID()));
        e.setCreated(new Date());
        this.save(e);
        return e.getId();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertBatch(List<Entity> dataList) {
        List<Entity> entityList = dataList;
        for (int i = 0; i < entityList.size(); i++) {
            this.insert(entityList.get(i));
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertOrUpdateBatch(List<Entity> dataList) {
        List<Entity> insertList = new ArrayList<>(dataList.size());
        List<Entity> updateList = new ArrayList<>(dataList.size());
        for (Entity vo : dataList) {
            Entity entity = newInstanceEntity();
            BeanUtils.copyProperties(vo, entity);
            if (Objects.isNull(entity.getId())) {
                insertList.add(entity);
            } else {
                updateList.add(entity);
            }
        }
        if (insertList.size() > 0) {
            this.saveBatch(insertList);
        }
        if (updateList.size() > 0) {
            this.updateBatchById(updateList);
        }
    }

    @Override
    public String update(Entity e) {
        return updateSelective(e);
    }


    @Override
    public String updateSelective(Entity e) {
        if (e.getId() == null) {
            throw new BusinessException("id不能为空");
        }
        e.setCreated(null);
        e.setUpdated(new Date());
        this.updateById(e);
        return e.getId();
    }

    @Override
    public void deleteByIds(List<String> idList) {
        this.removeByIds(idList);
    }

    @Override
    public boolean delete(Entity e) {
        QueryWrapper wrapper = new QueryWrapper();
        notNullField(e, wrapper);
        return this.remove(wrapper);
    }

    @Override
    public void deleteById(String id) {
        if (BizConstant.REAL_DELETE) {
            this.removeById(id);
        } else {
            throw new BusinessException("系统已禁止物理删除!");
        }
    }

    @Override
    public void updateBatch(List<Entity> dataList) {
        List<Entity> entityList = dataList;
//      convertVo2EntityList(dataList);
        this.updateBatchById(entityList);
    }


    @Override
    public Entity entityById(String id) {
        if (id == null) {
            throw new BusinessException("id不能为空");
        }
        Entity entity = this.getById(id);
        return entity == null ? null : entity;
    }

    @Override
    public Entity getEntity(Entity e) {
        QueryWrapper wrapper = new QueryWrapper();
        notNullField(e, wrapper);
        return (Entity) this.getOne(wrapper);
    }

    @Override
    public List<Entity> entityList(Entity e) {
        QueryWrapper criteria = new QueryWrapper();
        notNullField(e, criteria);
        setCriteriaOrder(criteria);
        long total = this.count(criteria);
        checkQueryLimit(total);
        return this.list(criteria);
    }

    @Override
    public List<Entity> entityAll() {
        List<Entity> entityList = this.list();
        return entityList;
    }


    @Override
    public DTO dtoById(String id) {
        Entity entity = this.getById(id);
        return entity == null ? null : convert2DTO(entity);
    }


    @Override
    public DTO getDto(Entity e) {
        e = getEntity(e);
        return e == null ? null : convert2DTO(e);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<DTO> dtoList(Entity e) {
        QueryWrapper criteria = new QueryWrapper();
        notNullField(e, criteria);
        setCriteriaOrder(criteria);
        long total = this.count(criteria);
        checkQueryLimit(total);
        return convertEntity2DtoList(this.list(criteria));
    }

    @Override
    public QueryVO queryById(String id) {
        Entity entity = this.getById(id);
        return entity == null ? null : convert2QueryVO(entity);
    }

    @Override
    public QueryVO getQuery(Entity e) {
        e = getEntity(e);
        return e == null ? null : convert2QueryVO(e);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<QueryVO> queryList(Entity e) {
        QueryWrapper criteria = new QueryWrapper();
        notNullField(e, criteria);
        setCriteriaOrder(criteria);
        long total = this.count(criteria);
        checkQueryLimit(total);
        return convertEntity2QueryVoList(this.list(criteria));
    }

    @Override
    public List<QueryVO> queryAll() {
        long total = this.count();
        checkQueryLimit(total);
        List<Entity> entityList = this.list();
        List<QueryVO> dtoList = new ArrayList<>();
        for (Entity entity : entityList) {
            QueryVO newQueryVO = newInstanceVo();
            BeanUtils.copyProperties(entity, newQueryVO);
            dtoList.add(newQueryVO);
        }
        return dtoList;
    }

    @Override
    public long count(QueryVO vo) {
        QueryWrapper criteria = new QueryWrapper();
        setCriteriaForQuery(vo, criteria);
        return this.count(criteria);
    }

    public Pagination<DTO> getListByPage(QueryVO vo) {
        QueryWrapper criteria = new QueryWrapper();
        setCriteriaForQuery(vo, criteria);
        long total = this.count(criteria);
        IPage<Entity> page = new Page<>(vo.getPageIndex(), vo.getPageSize(), total, false);
        List<DTO> dtoList = dtoList(page, criteria);
        return new Pagination<>(dtoList, page.getTotal(), vo.getPageSize());
    }

    private List<DTO> dtoList(IPage<Entity> page, QueryWrapper<Entity> criteria) {
        List<Entity> entityList = this.page(page, criteria).getRecords();
        return convertEntity2DtoList(entityList);
    }

    private List<QueryVO> queryList(IPage<Entity> page, QueryWrapper<Entity> criteria) {
        List<Entity> entityList = this.page(page, criteria).getRecords();
        return convertEntity2QueryVoList(entityList);
    }

    public boolean isObjectNull(DTO d) {
        boolean state = true;
        for (Field field : d.getClass().getDeclaredFields()) {
            field.setAccessible(true);
            try {
                //属性为空,不用查询
                if (field.get(d) != null) {
                    state = false;
                    break;
                }
            } catch (Exception ex) {
                state = true;
            }
        }
        return state;
    }

    /**
     * 根据id查询数据集合
     *
     * @param ids
     * @return
     */
    public List<Entity> entityListByIds(List<Long> ids) {
        List<Entity> list = (List<Entity>) this.listByIds(ids);
        return list;
    }

    /**
     * 根据id查询数据集合
     *
     * @param ids
     * @return
     */
    public List<QueryVO> queryListByIds(List<Long> ids) {
        List<Entity> list = (List<Entity>) this.listByIds(ids);
        return convertEntity2QueryVoList(list);
    }

    // entity集合转QueryVo集合
    protected List<QueryVO> convertEntity2QueryVoList(List<Entity> entityList) {
        List<QueryVO> dtoList = new ArrayList<>(entityList.size());
        for (Entity entity : entityList) {
            QueryVO newVO = newInstanceVo();
            BeanUtils.copyProperties(entity, newVO);
            dtoList.add(newVO);
        }
        return dtoList;
    }

    // QueryVo集合转Entity集合
    protected List<Entity> convertVo2EntityList(List<QueryVO> dataList) {
        List<Entity> entityList = new ArrayList<>(dataList.size());
        for (QueryVO vo : dataList) {
            Entity entity = newInstanceEntity();
            BeanUtils.copyProperties(vo, entity);
            entityList.add(entity);
        }
        return entityList;
    }


    //entity集合转dto集合
    protected List<DTO> convertEntity2DtoList(List<Entity> entityList) {
        List<DTO> dtoList = new ArrayList<>(entityList.size());
        for (Entity entity : entityList) {
            DTO newDto = newInstanceDTO();
            BeanUtils.copyProperties(entity, newDto);
            dtoList.add(newDto);
        }
        return dtoList;
    }


    //dto集合转entity集合
    protected List<Entity> convertDto2EntityList(List<DTO> dtoList) {
        List<Entity> entityList = new ArrayList<>(dtoList.size());
        for (DTO entity : dtoList) {
            Entity newDto = newInstanceEntity();
            BeanUtils.copyProperties(entity, newDto);
            entityList.add(newDto);
        }
        return entityList;
    }


    private QueryVO newInstanceVo() {
        Class<QueryVO> entityClass = (Class<QueryVO>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[1];
        try {
            return entityClass.newInstance();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new SystemException(e);
        }
    }

    private Entity newInstanceEntity() {
        Class<Entity> entityClass = (Class<Entity>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[3];
        try {
            return entityClass.newInstance();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new SystemException(e);
        }
    }

    private DTO newInstanceDTO() {
        Class<DTO> entityClass = (Class<DTO>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[2];
        try {
            return entityClass.newInstance();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new SystemException(e);
        }
    }

    //vo转entity
    protected Entity convert2Entity(QueryVO vo) {
        try {
            Entity entity = newInstanceEntity();
            BeanUtils.copyProperties(vo, entity);
            return entity;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new SystemException(e);
        }
    }


    //entity转QueryVO
    protected QueryVO convert2QueryVO(Entity e) {
        try {
            QueryVO dto = newInstanceVo();
            BeanUtils.copyProperties(e, dto);
            return dto;
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            throw new SystemException(ex);
        }
    }

    //entity转dto
    protected DTO convert2DTO(Entity e) {
        try {
            DTO dto = newInstanceDTO();
            BeanUtils.copyProperties(e, dto);
            return dto;
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            throw new SystemException(ex);
        }
    }

    //dto转Entity
    protected Entity convert2Entity(DTO dto) {
        try {
            Entity entity = newInstanceEntity();
            BeanUtils.copyProperties(dto, entity);
            return entity;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new SystemException(e);
        }
    }

    /**
     * 查询限制校验
     */
    private void checkQueryLimit(long total) {
        if (total > BizConstant.QUERY_LIMIT) {
            throw new BusinessException("查询数据量" + total + "已超过系统限制" + BizConstant.QUERY_LIMIT);
        }
    }

    /**
     * 对象转Map集合
     *
     * @param obj         查询对象
     * @param allowAddKey 返回的map对象是否允许添加新的key
     */
    protected Map beanToMap(QueryVO obj, boolean allowAddKey) {
        if (obj == null) {
            return null;
        }
        Map beanMap = new BeanMap(obj);
        return allowAddKey ? new HashMap() {{
            putAll(beanMap);
        }} : beanMap;
    }

}