代码生成器新增多选删除功能

pull/214/head
dqjdda 2019-11-19 13:09:22 +08:00
parent 3f80af6641
commit aaeb67b280
14 changed files with 71 additions and 380 deletions

View File

@ -1,51 +0,0 @@
package me.zhengjie.gen.domain;
import lombok.Data;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import javax.persistence.*;
import javax.validation.constraints.*;
import javax.persistence.Entity;
import javax.persistence.Table;
import org.hibernate.annotations.*;
import java.sql.Timestamp;
import java.io.Serializable;
/**
* @author Zheng Jie
* @date 2019-11-19
*/
@Entity
@Data
@Table(name="gen_test")
public class GenTest implements Serializable {
// ID
@Id
@Column(name = "id")
private Long id;
// 名称
@Column(name = "name",nullable = false)
@NotBlank
private String name;
// 状态
@Column(name = "status",nullable = false)
@NotNull
private Boolean status;
// 日期
@Column(name = "date",nullable = false)
@NotNull
private Timestamp date;
// 创建日期
@Column(name = "create_time")
@CreationTimestamp
private Timestamp createTime;
public void copy(GenTest source){
BeanUtil.copyProperties(source,this, CopyOptions.create().setIgnoreNullValue(true));
}
}

View File

@ -1,12 +0,0 @@
package me.zhengjie.gen.repository;
import me.zhengjie.gen.domain.GenTest;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
/**
* @author Zheng Jie
* @date 2019-11-19
*/
public interface GenTestRepository extends JpaRepository<GenTest, Long>, JpaSpecificationExecutor<GenTest> {
}

View File

@ -1,73 +0,0 @@
package me.zhengjie.gen.rest;
import me.zhengjie.aop.log.Log;
import me.zhengjie.gen.domain.GenTest;
import me.zhengjie.gen.service.GenTestService;
import me.zhengjie.gen.service.dto.GenTestQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
/**
* @author Zheng Jie
* @date 2019-11-19
*/
@Api(tags = "GenTest管理")
@RestController
@RequestMapping("/api/genTest")
public class GenTestController {
private final GenTestService genTestService;
public GenTestController(GenTestService genTestService) {
this.genTestService = genTestService;
}
@Log("导出数据")
@ApiOperation("导出数据")
@GetMapping(value = "/download")
@PreAuthorize("@el.check('genTest:list')")
public void download(HttpServletResponse response, GenTestQueryCriteria criteria) throws IOException {
genTestService.download(genTestService.queryAll(criteria), response);
}
@GetMapping
@Log("查询GenTest")
@ApiOperation("查询GenTest")
@PreAuthorize("@el.check('genTest:list')")
public ResponseEntity getGenTests(GenTestQueryCriteria criteria, Pageable pageable){
return new ResponseEntity<>(genTestService.queryAll(criteria,pageable),HttpStatus.OK);
}
@PostMapping
@Log("新增GenTest")
@ApiOperation("新增GenTest")
@PreAuthorize("@el.check('genTest:add')")
public ResponseEntity create(@Validated @RequestBody GenTest resources){
return new ResponseEntity<>(genTestService.create(resources),HttpStatus.CREATED);
}
@PutMapping
@Log("修改GenTest")
@ApiOperation("修改GenTest")
@PreAuthorize("@el.check('genTest:edit')")
public ResponseEntity update(@Validated @RequestBody GenTest resources){
genTestService.update(resources);
return new ResponseEntity(HttpStatus.NO_CONTENT);
}
@DeleteMapping(value = "/{id}")
@Log("删除GenTest")
@ApiOperation("删除GenTest")
@PreAuthorize("@el.check('genTest:del')")
public ResponseEntity delete(@PathVariable Long id){
genTestService.delete(id);
return new ResponseEntity(HttpStatus.OK);
}
}

View File

@ -1,47 +0,0 @@
package me.zhengjie.gen.service;
import me.zhengjie.gen.domain.GenTest;
import me.zhengjie.gen.service.dto.GenTestDTO;
import me.zhengjie.gen.service.dto.GenTestQueryCriteria;
import org.springframework.data.domain.Pageable;
import java.util.Map;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
/**
* @author Zheng Jie
* @date 2019-11-19
*/
public interface GenTestService {
/**
*
* @param criteria
* @param pageable
* @return Map<String,Object>
*/
Map<String,Object> queryAll(GenTestQueryCriteria criteria, Pageable pageable);
/**
*
* @param criteria
* @return List<GenTestDTO>
*/
List<GenTestDTO> queryAll(GenTestQueryCriteria criteria);
/**
* ID
* @param id ID
* @return GenTestDTO
*/
GenTestDTO findById(Long id);
GenTestDTO create(GenTest resources);
void update(GenTest resources);
void delete(Long id);
void download(List<GenTestDTO> all, HttpServletResponse response) throws IOException;
}

View File

@ -1,33 +0,0 @@
package me.zhengjie.gen.service.dto;
import lombok.Data;
import java.sql.Timestamp;
import java.io.Serializable;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
/**
* @author Zheng Jie
* @date 2019-11-19
*/
@Data
public class GenTestDTO implements Serializable {
// ID
// 处理精度丢失问题
@JsonSerialize(using= ToStringSerializer.class)
private Long id;
// 名称
private String name;
// 状态
private Boolean status;
// 日期
private Timestamp date;
// 创建日期
private Timestamp createTime;
}

View File

@ -1,35 +0,0 @@
package me.zhengjie.gen.service.dto;
import lombok.Data;
import java.sql.Timestamp;
import me.zhengjie.annotation.Query;
/**
* @author Zheng Jie
* @date 2019-11-19
*/
@Data
public class GenTestQueryCriteria{
// 模糊
@Query(type = Query.Type.INNER_LIKE)
private String name;
// 精确
@Query
private Boolean status;
// 时间段查询
@Query(type = Query.Type.GREATER_THAN, propName = "date")
private Timestamp dateStart;
@Query(type = Query.Type.LESS_THAN, propName = "date")
private Timestamp dateEnd;
// 时间段查询
@Query(type = Query.Type.GREATER_THAN, propName = "createTime")
private Timestamp createTimeStart;
@Query(type = Query.Type.LESS_THAN, propName = "createTime")
private Timestamp createTimeEnd;
}

View File

@ -1,109 +0,0 @@
package me.zhengjie.gen.service.impl;
import me.zhengjie.gen.domain.GenTest;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.gen.repository.GenTestRepository;
import me.zhengjie.gen.service.GenTestService;
import me.zhengjie.gen.service.dto.GenTestDTO;
import me.zhengjie.gen.service.dto.GenTestQueryCriteria;
import me.zhengjie.gen.service.mapper.GenTestMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
/**
* @author Zheng Jie
* @date 2019-11-19
*/
@Service
@CacheConfig(cacheNames = "genTest")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class GenTestServiceImpl implements GenTestService {
private final GenTestRepository genTestRepository;
private final GenTestMapper genTestMapper;
public GenTestServiceImpl(GenTestRepository genTestRepository, GenTestMapper genTestMapper) {
this.genTestRepository = genTestRepository;
this.genTestMapper = genTestMapper;
}
@Override
@Cacheable
public Map<String,Object> queryAll(GenTestQueryCriteria criteria, Pageable pageable){
Page<GenTest> page = genTestRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
return PageUtil.toPage(page.map(genTestMapper::toDto));
}
@Override
@Cacheable
public List<GenTestDTO> queryAll(GenTestQueryCriteria criteria){
return genTestMapper.toDto(genTestRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
}
@Override
@Cacheable(key = "#p0")
public GenTestDTO findById(Long id) {
GenTest genTest = genTestRepository.findById(id).orElseGet(GenTest::new);
ValidationUtil.isNull(genTest.getId(),"GenTest","id",id);
return genTestMapper.toDto(genTest);
}
@Override
@CacheEvict(allEntries = true)
@Transactional(rollbackFor = Exception.class)
public GenTestDTO create(GenTest resources) {
Snowflake snowflake = IdUtil.createSnowflake(1, 1);
resources.setId(snowflake.nextId());
return genTestMapper.toDto(genTestRepository.save(resources));
}
@Override
@CacheEvict(allEntries = true)
@Transactional(rollbackFor = Exception.class)
public void update(GenTest resources) {
GenTest genTest = genTestRepository.findById(resources.getId()).orElseGet(GenTest::new);
ValidationUtil.isNull( genTest.getId(),"GenTest","id",resources.getId());
genTest.copy(resources);
genTestRepository.save(genTest);
}
@Override
@CacheEvict(allEntries = true)
@Transactional(rollbackFor = Exception.class)
public void delete(Long id) {
genTestRepository.deleteById(id);
}
@Override
public void download(List<GenTestDTO> all, HttpServletResponse response) throws IOException {
List<Map<String, Object>> list = new ArrayList<>();
for (GenTestDTO genTest : all) {
Map<String,Object> map = new LinkedHashMap<>();
map.put("名称", genTest.getName());
map.put("状态", genTest.getStatus());
map.put("日期", genTest.getDate());
map.put("创建日期", genTest.getCreateTime());
list.add(map);
}
FileUtil.downloadExcel(list, response);
}
}

View File

@ -1,16 +0,0 @@
package me.zhengjie.gen.service.mapper;
import me.zhengjie.base.BaseMapper;
import me.zhengjie.gen.domain.GenTest;
import me.zhengjie.gen.service.dto.GenTestDTO;
import org.mapstruct.Mapper;
import org.mapstruct.ReportingPolicy;
/**
* @author Zheng Jie
* @date 2019-11-19
*/
@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface GenTestMapper extends BaseMapper<GenTestDTO, GenTest> {
}

View File

@ -70,4 +70,13 @@ public class ${className}Controller {
${changeClassName}Service.delete(${pkChangeColName}); ${changeClassName}Service.delete(${pkChangeColName});
return new ResponseEntity(HttpStatus.OK); return new ResponseEntity(HttpStatus.OK);
} }
@Log("多选删除${className}")
@ApiOperation("多选删除${className}")
@PreAuthorize("@el.check('${changeClassName}:del')")
@DeleteMapping
public ResponseEntity deleteAll(@RequestBody ${pkColumnType}[] ids) {
${changeClassName}Service.deleteAll(ids);
return new ResponseEntity(HttpStatus.OK);
}
} }

View File

@ -43,5 +43,7 @@ public interface ${className}Service {
void delete(${pkColumnType} ${pkChangeColName}); void delete(${pkColumnType} ${pkChangeColName});
void deleteAll(${pkColumnType}[] ids);
void download(List<${className}DTO> all, HttpServletResponse response) throws IOException; void download(List<${className}DTO> all, HttpServletResponse response) throws IOException;
} }

View File

@ -133,6 +133,13 @@ public class ${className}ServiceImpl implements ${className}Service {
${changeClassName}Repository.deleteById(${pkChangeColName}); ${changeClassName}Repository.deleteById(${pkChangeColName});
} }
@Override
@CacheEvict(allEntries = true)
public void deleteAll(${pkColumnType}[] ids) {
for (${pkColumnType} id : ids) {
${changeClassName}Repository.deleteById(${pkChangeColName});
}
}
@Override @Override
public void download(List<${className}DTO> all, HttpServletResponse response) throws IOException { public void download(List<${className}DTO> all, HttpServletResponse response) throws IOException {

View File

@ -14,6 +14,13 @@ export function del(${pkChangeColName}) {
method: 'delete' method: 'delete'
}) })
} }
export function delAll(ids) {
return request({
url: 'api/${changeClassName}/',
method: 'delete',
data: ids
})
}
export function edit(data) { export function edit(data) {
return request({ return request({

View File

@ -51,7 +51,7 @@ export default {
required: true required: true
}<#if hasDict>, }<#if hasDict>,
dicts: { dicts: {
type: Array, type: Object,
required: true required: true
} }
</#if> </#if>

View File

@ -47,11 +47,23 @@
icon="el-icon-download" icon="el-icon-download"
@click="download">导出</el-button> @click="download">导出</el-button>
</div> </div>
<!-- 多选删除 -->
<div v-permission="['admin','${changeClassName}:del']" style="display: inline-block;">
<el-button
:loading="delAllLoading"
:disabled="data.length === 0 || $refs.table.selection.length === 0"
class="filter-item"
size="mini"
type="danger"
icon="el-icon-delete"
@click="open">删除</el-button>
</div>
</div> </div>
<!--表单组件--> <!--表单组件-->
<eForm ref="form" :is-add="isAdd" <#if hasDict>:dicts="dict"</#if>/> <eForm ref="form" :is-add="isAdd" <#if hasDict>:dicts="dict"</#if>/>
<!--表格渲染--> <!--表格渲染-->
<el-table v-loading="loading" :data="data" size="small" style="width: 100%;"> <el-table v-loading="loading" ref="table" :data="data" size="small" style="width: 100%;">
<el-table-column type="selection" width="55"/>
<#if columns??> <#if columns??>
<#list columns as column> <#list columns as column>
<#if column.columnShow> <#if column.columnShow>
@ -105,7 +117,7 @@
<script> <script>
import checkPermission from '@/utils/permission' import checkPermission from '@/utils/permission'
import initData from '@/mixins/initData' import initData from '@/mixins/initData'
import { del, download${className} } from '@/api/${changeClassName}' import { del, download${className}, delAll } from '@/api/${changeClassName}'
<#if hasTimestamp> <#if hasTimestamp>
import { parseTime, downloadFile } from '@/utils/index' import { parseTime, downloadFile } from '@/utils/index'
</#if> </#if>
@ -118,7 +130,7 @@ export default {
</#if> </#if>
data() { data() {
return { return {
delLoading: false, delLoading: false, delAllLoading: false,
<#if hasQuery> <#if hasQuery>
queryTypeOptions: [ queryTypeOptions: [
<#if queryColumns??> <#if queryColumns??>
@ -208,6 +220,36 @@ export default {
}).catch(() => { }).catch(() => {
this.downloadLoading = false this.downloadLoading = false
}) })
},
doDelete() {
this.delAllLoading = true
const data = this.$refs.table.selection
const ids = []
for (let i = 0; i < data.length; i++) {
ids.push(data[i].id)
}
delAll(ids).then(res => {
this.delAllLoading = false
this.init()
this.dleChangePage(ids.length)
this.$notify({
title: '删除成功',
type: 'success',
duration: 2500
})
}).catch(err => {
this.delAllLoading = false
console.log(err.response.data.message)
})
},
open() {
this.$confirm('你确定删除选中的数据吗?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
this.doDelete()
})
} }
} }
} }