实现要求
1.片区的检验名称编号是否重复
1.1controller添加两个方法,检验片区编号和检验片区名称
作用为:调用方法判断片区编号与片区名称是否重复,并返回返回值
/**
* 检验片区编号是否重复
*/
@PostMapping( "/checkPqbhUnique")
@ResponseBody
public String checkXqbhUnique(TBxwPqZhk tBxwPqZhk){
String back = tBxwPqZhkService.checkPqbhUnique(tBxwPqZhk.getId(),tBxwPqZhk.getPqbh());
return back;
}
/**
* 校验片区名称是否重复
*/
@PostMapping("/checkPqmcUnique")
@ResponseBody
public String checkXqmcUnique(TBxwPqZhk tBxwPqZhk){
String back = tBxwPqZhkService.checkPqmcUnique(tBxwPqZhk.getId(),tBxwPqZhk.getPqmc());
return back;
}
1.2添加service和mapper中方法
service:
/**
* 检验片区编号是否重复
*
* @param id 片区主键
* @param pqbh 片区编号
* @return 结果
*/
public String checkPqbhUnique(String id, String pqbh);
/**
* 检验片区编号是否重复
*
* @param id 片区主键
* @param pqmc 片区名称
* @return 结果
*/
public String checkPqmcUnique(String id, String pqmc);
serviceImp:
/**
* 检验片区编号是否重复
*
* @param id 片区主键
* @param pqbh 片区编号
* @return 0表示唯一,1表示重复
*/
@Override
public String checkPqbhUnique(String id, String pqbh) {
// TODO Auto-generated method stub
if (id != null && id.length() != 0) {
}else {
id = "1";
}
Map <String , String> map = new HashMap <String , String> ();
map.put("id", id);
map.put("pqbh", pqbh);
int count = tBxwPqZhkMapper.checkPqbhUnique(map);
if (count > 0)
{
return "1";
}
return "0";
}
/**
* 检验片区名称是否重复
*
* @param id 片区主键
* @param pqmc 片区名称
* @return 0表示唯一,1表示重复
*/
@Override
public String checkPqmcUnique(String id, String pqmc) {
// TODO Auto-generated method stub
if (id != null && id.length() != 0) {
}else {
id = "1";
}
Map <String , String> map = new HashMap <String , String> ();
map.put("id", id);
map.put("pqmc", pqmc);
int count = tBxwPqZhkMapper.checkPqmcUnique(map);
if (count > 0)
{
return "1";
}
return "0";
}
mapper:
/**
* 校验片区编号是否唯一
*
* @param Map id:片区编号 pqbh:片区名称
* @return 结果
*/
public int checkPqbhUnique(java.util.Map Map);
/**
* 校验片区编号是否唯一
*
* @param Map id:片区编号 pqmc:片区名称
* @return 结果
*/
public int checkPqmcUnique(java.util.Map Map);
1.3添加mapper.xml文件方法
添加对应函数的连接数据库方法:
<select id="checkPqbhUnique" parameterType="java.util.Map" resultType="int">
select count(1) from t_bxw_pq_zhk where id != #{id} and pqbh=#{pqbh} and rownum <![CDATA[ <= ]]> 1
</select>
<select id="checkPqmcUnique" parameterType="java.util.Map" resultType="int">
select count(1) from t_bxw_pq_zhk where id != #{id} and pqmc=#{pqmc} and rownum <![CDATA[ <= ]]> 1
</select>
1.4修改add.html中代码,加入对应代码模块
注意前面需要加入id=''pqbh'' 和id="pqmc",否则代码功能不实现!!!
<th:block th:include="include :: footer" />
<script th:inline="javascript">
var prefix = ctx + "jcxx/pq"
$("#form-pq-add").validate({
//数据校验
onkeyup: false,
rules:{
pqbh:{
minlength: 2,
maxlength: 20,
digits:true,
remote: {
url: prefix + "/checkPqbhUnique",
type: "post",
dataType: "json",
data: {
"id": function() {
return $("#id").val();
},
"pqbh": function() {
return $.common.trim($("#pqbh").val());
}
},
dataFilter: function(data, type) {
return $.validate.unique(data);
}
}
},
pqmc:{
remote: {
url: prefix + "/checkPqmcUnique",
type: "post",
dataType: "json",
data: {
"id": function() {
return $("#id").val();
},
"pqmc": function() {
return $.common.trim($("#pqmc").val());
}
},
dataFilter: function(data, type) {
return $.validate.unique(data);
}
}
}
},
messages: {
"pqbh": {
remote: "片区编号已经存在!"
},
"pqmc": {
remote: "片区名称已经存在!"
}
},
focusCleanup: true
});
function submitHandler() {
if ($.validate.form()) {
$.operate.save(prefix + "/add", $('#form-pq-add').serialize());
}
}
</script>
编辑功能中的重复信息提示也一样,修改edit.html即可,与pq.html一致
1.5功能实现
2.按序排列
2.1法一:在pq.html中加入sort语句
2.2功能实现
按照默认值片区编号进行排序
2.3法二:在pq.html中加入sort语句
2.4功能实现
可以通过点击红框处进行对应的升序或降序排序
3.片区删除时,输出提示框,若存在子街道,则不能删除
3.1连接片区表和街道表
3.1.1分析二者关系,修改domain文件
一个片区可以有多个街道,一个街道属于一个片区,所以片区与街道是一对多的关系。
因此需要在街道的属性domain中加入片区属性,正常情况下加入片区属性TBxwPq最好,但是为了代码的简易及基本的功能的实现,本代码中只加入了片区id(外键,原本就有的)和片区名称(pqmc),并生成对应的get和set方法。
3.1.2修改街道的xml文件
在原有基础上加入片区名称(pqmc)的属性,并微调sql语句,由单表查询变为多表查询
3.1.3修改街道html文件
将原来的片区id修改为片区名称即可。在添加和修改时,在片区中多几处细微的修改
jd.html
<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
<th:block th:include="include :: header('片区街道列表')" />
</head>
<body class="gray-bg">
<div class="container-div">
<div class="row">
<div class="col-sm-12 search-collapse">
<form id="formId">
<div class="select-list">
<ul>
<li>
<label>片区名称:</label>
<input type="text" name="pqmc"/>
</li>
<li>
<label>街道编号:</label>
<input type="text" name="jdbh"/>
</li>
<li>
<label>街道名称:</label>
<input type="text" name="jdmc"/>
</li>
<li>
<label>注销标志:</label>
<select name="zxbz" th:with="type=${@dict.getType('t_jc_zxztlx')}">
<option value="">所有</option>
<option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
</select>
</li>
<li>
<a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i> 搜索</a>
<a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i> 重置</a>
</li>
</ul>
</div>
</form>
</div>
<div class="btn-group-sm" id="toolbar" role="group">
<a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="jcxx:jd:add">
<i class="fa fa-plus"></i> 添加
</a>
<a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="jcxx:jd:edit">
<i class="fa fa-edit"></i> 修改
</a>
<a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="jcxx:jd:remove">
<i class="fa fa-remove"></i> 删除
</a>
<a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="jcxx:jd:export">
<i class="fa fa-download"></i> 导出
</a>
</div>
<div class="col-sm-12 select-table table-striped">
<table id="bootstrap-table"></table>
</div>
</div>
</div>
<th:block th:include="include :: footer" />
<script th:inline="javascript">
var editFlag = [[${@permission.hasPermi('jcxx:jd:edit')}]];
var removeFlag = [[${@permission.hasPermi('jcxx:jd:remove')}]];
var zxbzDatas = [[${@dict.getType('t_jc_zxztlx')}]];
var prefix = ctx + "jcxx/jd";
$(function() {
var options = {
url: prefix + "/list",
createUrl: prefix + "/add",
updateUrl: prefix + "/edit/{id}",
removeUrl: prefix + "/remove",
exportUrl: prefix + "/export",
modalName: "片区街道",
columns: [{
checkbox: true
},
{
field: 'id',
title: '主键',
visible: false
},
{
field: 'pqmc',
title: '片区名称'
},
{
field: 'jdbh',
title: '街道编号'
},
{
field: 'jdmc',
title: '街道名称'
},
{
field: 'zxbz',
title: '注销标志',
formatter: function(value, row, index) {
return $.table.selectDictLabel(zxbzDatas, value);
}
},
{
field: 'bz',
title: '备注'
},
{
title: '操作',
align: 'center',
formatter: function(value, row, index) {
var actions = [];
actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
return actions.join('');
}
}]
};
$.table.init(options);
});
</script>
</body>
</html>
add.html
<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
<th:block th:include="include :: header('新增片区街道')" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
<form class="form-horizontal m" id="form-jd-add">
<div class="form-group">
<label class="col-sm-3 control-label is-required" >片区:</label>
<div class="col-sm-8">
<select id="pq" name="tBxwPqZhkId" class="form-control select2-single required" single>
<option th:each="pq:${pqs}" th:value="${pq.id}" th:text="${pq.pqmc}" ></option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label is-required">街道编号:</label>
<div class="col-sm-8">
<input name="jdbh" class="form-control" type="text" required>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label is-required">街道名称:</label>
<div class="col-sm-8">
<input name="jdmc" class="form-control" type="text" required>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label is-required">注销标志:</label>
<div class="col-sm-8">
<select name="zxbz" class="form-control m-b" th:with="type=${@dict.getType('t_jc_zxztlx')}" required>
<option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">备注:</label>
<div class="col-sm-8">
<input name="bz" class="form-control" type="text">
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">扩展1:</label>
<div class="col-sm-8">
<input name="kz1" class="form-control" type="text">
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">扩展2:</label>
<div class="col-sm-8">
<input name="kz2" class="form-control" type="text">
</div>
</div>
</form>
</div>
<th:block th:include="include :: footer" />
<script th:inline="javascript">
var prefix = ctx + "jcxx/jd"
$("#form-jd-add").validate({
focusCleanup: true
});
function submitHandler() {
if ($.validate.form()) {
$.operate.save(prefix + "/add", $('#form-jd-add').serialize());
}
}
</script>
</body>
</html>
edit.html
<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
<th:block th:include="include :: header('修改片区街道')" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
<form class="form-horizontal m" id="form-jd-edit" th:object="${tBxwPqJdZhk}">
<input name="id" th:field="*{id}" type="hidden">
<div class="form-group">
<label class="col-sm-3 control-label is-required" >片区:</label>
<div class="col-sm-8">
<select id="pq" name="tBxwPqZhkId" class="form-control select2-single required" single>
<option th:each="pq:${pqs}" th:value="${pq.id}" th:text="${pq.pqmc}" ></option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label is-required">街道编号:</label>
<div class="col-sm-8">
<input name="jdbh" th:field="*{jdbh}" class="form-control" type="text" required>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label is-required">街道名称:</label>
<div class="col-sm-8">
<input name="jdmc" th:field="*{jdmc}" class="form-control" type="text" required>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label is-required">注销标志:</label>
<div class="col-sm-8">
<select name="zxbz" class="form-control m-b" th:with="type=${@dict.getType('t_jc_zxztlx')}" required>
<option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{zxbz}"></option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">备注:</label>
<div class="col-sm-8">
<input name="bz" th:field="*{bz}" class="form-control" type="text">
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">扩展1:</label>
<div class="col-sm-8">
<input name="kz1" th:field="*{kz1}" class="form-control" type="text">
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">扩展2:</label>
<div class="col-sm-8">
<input name="kz2" th:field="*{kz2}" class="form-control" type="text">
</div>
</div>
</form>
</div>
<th:block th:include="include :: footer" />
<script th:inline="javascript">
var prefix = ctx + "jcxx/jd";
$("#form-jd-edit").validate({
focusCleanup: true
});
function submitHandler() {
if ($.validate.form()) {
$.operate.save(prefix + "/edit", $('#form-jd-edit').serialize());
}
}
</script>
</body>
</html>
3.1.4新建片区service方法
在街道的添加中,必须要已知现有的全部正常的片区,然后才可以在该片区下新建街道,而由于源代码中并未有函数方法能够完成这一操作,所以需要在pq中新建一个方法 selectTBxwPqZhkAll() 通过sql语句的修改,进行显示全部片区。
service:
serviceImp:
mapper:
mapper.xml:
3.1.5修改街道的controller中的add
3.1.6功能实现
正确显示片区名称:
回显全部片区:
3.2实现删除功能
3.2.1修改片区的serviceImp中删除函数的具体实现
package com.xyt.project.shggc.jcxx.pq.service.impl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.xyt.project.shggc.jcxx.pq.mapper.TBxwPqZhkMapper;
import com.xyt.project.shggc.jcxx.jd.mapper.TBxwPqJdZhkMapper;
import com.xyt.project.shggc.jcxx.pq.domain.TBxwPqZhk;
import com.xyt.project.shggc.jcxx.pq.service.ITBxwPqZhkService;
import com.xyt.common.utils.text.Convert;
import com.xyt.common.exception.BusinessException;
/**
* 片区Service业务层处理
*
* @author ruoyi
* @date 2024-08-05
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class TBxwPqZhkServiceImpl implements ITBxwPqZhkService
{
@Autowired
private TBxwPqZhkMapper tBxwPqZhkMapper;
@Autowired
private TBxwPqJdZhkMapper tBxwPqJdZhkMapper;
/**
* 查询片区
*
* @param id 片区主键
* @return 片区
*/
@Override
public TBxwPqZhk selectTBxwPqZhkById(String id)
{
return tBxwPqZhkMapper.selectTBxwPqZhkById(id);
}
/**
* 查询片区列表
*
* @param tBxwPqZhk 片区
* @return 片区
*/
@Override
public List<TBxwPqZhk> selectTBxwPqZhkList(TBxwPqZhk tBxwPqZhk)
{
return tBxwPqZhkMapper.selectTBxwPqZhkList(tBxwPqZhk);
}
/**
* 查询全部片区信息
*
*/
@Override
public List<TBxwPqZhk> selectTBxwPqZhkAll() {
// TODO Auto-generated method stub
return tBxwPqZhkMapper.selectTBxwPqZhkAll();
}
/**
* 新增片区
*
* @param tBxwPqZhk 片区
* @return 结果
*/
@Override
public int insertTBxwPqZhk(TBxwPqZhk tBxwPqZhk)
{
return tBxwPqZhkMapper.insertTBxwPqZhk(tBxwPqZhk);
}
/**
* 修改片区
*
* @param tBxwPqZhk 片区
* @return 结果
*/
@Override
public int updateTBxwPqZhk(TBxwPqZhk tBxwPqZhk)
{
return tBxwPqZhkMapper.updateTBxwPqZhk(tBxwPqZhk);
}
/**
* 批量删除片区
*
* @param ids 需要删除的片区主键
* @return 结果
*/
@Override
public int deleteTBxwPqZhkByIds(String ids)
{
String[] iids = Convert.toStrArray(ids);
for (String id : iids)
{
TBxwPqZhk tBxwPqZhk = selectTBxwPqZhkById(id);
int pqjdNum = tBxwPqJdZhkMapper.selectTBxwPqJdZhkByPqId(tBxwPqZhk.getId()).size();
if (pqjdNum > 0)
{
throw new BusinessException(String.format("%1$s下已经有街道信息,不能删除", tBxwPqZhk.getPqmc()));
}
}
return tBxwPqZhkMapper.deleteTBxwPqZhkByIds(Convert.toStrArray(ids));
}
/**
* 删除片区信息
*
* @param id 片区ID
* @return 结果
*/
@Override
public int deleteTBxwPqZhkById(String id)
{
//通过片区id查询出该片区
TBxwPqZhk tBxwPqZhk = selectTBxwPqZhkById(id);
/通过调用函数selectTBxwPqJdZhkByPqId搜索出有多少个街道的片区id为上id,并计入到pqjdNum中
int pqjdNum = tBxwPqJdZhkMapper.selectTBxwPqJdZhkByPqId(tBxwPqZhk.getId()).size();
/如果片区街道数大于0,则说明该片区存在子街道,不能删除
if (pqjdNum > 0)
{
throw new BusinessException(String.format("%1$s下已经有街道信息,不能删除", tBxwPqZhk.getPqmc()));
}
如果为0,成功删除
return tBxwPqZhkMapper.deleteTBxwPqZhkById(id);
}
/**
* 检验片区编号是否重复
*
* @param id 片区主键
* @param pqbh 片区编号
* @return 0表示唯一,1表示重复
*/
@Override
public String checkPqbhUnique(String id, String pqbh) {
// TODO Auto-generated method stub
if (id != null && id.length() != 0) {
}else {
id = "1";
}
Map <String , String> map = new HashMap <String , String> ();
map.put("id", id);
map.put("pqbh", pqbh);
int count = tBxwPqZhkMapper.checkPqbhUnique(map);
if (count > 0)
{
return "1";
}
return "0";
}
/**
* 检验片区名称是否重复
*
* @param id 片区主键
* @param pqmc 片区名称
* @return 0表示唯一,1表示重复
*/
@Override
public String checkPqmcUnique(String id, String pqmc) {
// TODO Auto-generated method stub
if (id != null && id.length() != 0) {
}else {
id = "1";
}
Map <String , String> map = new HashMap <String , String> ();
map.put("id", id);
map.put("pqmc", pqmc);
int count = tBxwPqZhkMapper.checkPqmcUnique(map);
if (count > 0)
{
return "1";
}
return "0";
}
}
3.2.2在街道的mapper文件中加入selectTBxwPqJdZhkByPqId方法
在街道的mapper.xml中具体实现:
3.2.3功能实现
提示框输出,防止误触:
存在子街道,拒绝删除: