SSH-BOS项目:定区管理模块

DecidedzoneAction:

package com.xushuai.bos.web.action;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.xushuai.bos.entity.Decidedzone;
import com.xushuai.bos.entity.Staff;
import com.xushuai.bos.service.DecidedzoneService;
import com.xushuai.bos.utils.BOSUtils;

@Controller("decidedzoneAction")
@Scope("prototype")
public class DecidedzoneAction extends BaseAction<Decidedzone> {
	
	@Autowired
	@Qualifier("decidedzoneService")
	private DecidedzoneService decidedzoneService;
	public void setDecidedzoneService(DecidedzoneService decidedzoneService) {
		this.decidedzoneService = decidedzoneService;
	}
	
	//接收分区ID
	private String[] subareaId;
	public String[] getSubareaId() {
		return subareaId;
	}
	public void setSubareaId(String[] subareaId) {
		this.subareaId = subareaId;
	}
	


	/**
	 * 添加定区
	 * @return
	 */
	public String add(){
		//调用service#save(model,subareaId)保存新增的定区数据
		decidedzoneService.save(model,subareaId);
		
		return LIST;
	}
	
	/**
	 * 分页查询定区数据
	 * @return
	 */
	public String pageQuery(){
		//获取离线查询条件
		DetachedCriteria dc = pageBean.getCriteria();
		//健壮性判断
		String name = model.getName();
		if(StringUtils.isNotBlank(name)){
			dc.add(Restrictions.like("name", "%"+name+"%"));
		}
		//添加关联查询
		Staff staff = model.getStaff();
		if(staff != null){
			dc.createAlias("staff", "s");
			String staffName = staff.getName();
			if(StringUtils.isNotBlank(staffName)){
				dc.add(Restrictions.like("s.name", "%"+staffName+"%"));
			}
		}
		
		decidedzoneService.pageQuery(pageBean);
		BOSUtils.writerJson(pageBean, new String[]{"currentPage","pageSize","criteria","subareas","decidedzones"});
		
		return NONE;
	}
	
	/**
	 * 删除指定的定区
	 * @return
	 */
	public String delete(){
		//调用service#delete(model)
		decidedzoneService.delete(model);
		
		return LIST;
	}
	
	/**
	 * 修改指定定区
	 * @return
	 */
	public String edit(){
		//调用service#edit(model)
		decidedzoneService.edit(model,subareaId);
		
		return LIST;
	}
}

SubareaAction(新增方法):

	/**
	 * 异步加载未关联定去的分区数据
	 * @return
	 */
	public String ajaxList(){
		List<Subarea> list = subareaService.findByNotRelation();
		BOSUtils.writerJson(list, new String[]{"region","decidedzone","startnum","endnum","single","deltag"});
		return NONE;
	}

	
	/**
	 * 异步加载与指定定区相关联的分区
	 * @return
	 */
	public String ajaxFindByDecidedzone(){
		List<Subarea> list = subareaService.findByDecidedzone(model.getDecidedzone());
		BOSUtils.writerJson(list, new String[]{"subareas","decidedzone"});
		return NONE;
	}
	
	/**
	 * 异步删除定区中的分区
	 * @return
	 */
	public String ajaxDecidedzoneDelete(){
		//调用service#decidedzoneDelete(model)
		subareaService.decidedzoneDelete(model);
		
		return NONE;
	}

DecidedzoneService:

package com.xushuai.bos.service;

import com.xushuai.bos.entity.Decidedzone;
import com.xushuai.bos.utils.PageBean;

public interface DecidedzoneService {

	/**
	 * 添加定区
	 * @param model
	 * @param subareaId
	 */
	void save(Decidedzone model, String[] subareaId);

	/**
	 * 分页查询定区数据
	 * @param pageBean
	 */
	void pageQuery(PageBean pageBean);

	/**
	 * 删除指定的定区
	 * @param model
	 */
	void delete(Decidedzone model);

	/**
	 * 修改指定的定区
	 * @param model
	 */
	void edit(Decidedzone model,String[] subareaId);

}

DecidedzoneServiceImpl:

package com.xushuai.bos.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xushuai.bos.dao.DecidedzoneDao;
import com.xushuai.bos.dao.SubareaDao;
import com.xushuai.bos.entity.Decidedzone;
import com.xushuai.bos.entity.Subarea;
import com.xushuai.bos.service.DecidedzoneService;
import com.xushuai.bos.utils.PageBean;

@Service("decidedzoneService")
@Transactional
public class DecidedzoneServiceImpl implements DecidedzoneService {
	
	@Autowired
	@Qualifier("decidedzoneDao")
	private DecidedzoneDao decidedzoneDao;
	public void setDecidedzoneDao(DecidedzoneDao decidedzoneDao) {
		this.decidedzoneDao = decidedzoneDao;
	}
	
	@Autowired
	@Qualifier("subareaDao")
	private SubareaDao subareaDao;
	public void setSubareaDao(SubareaDao subareaDao) {
		this.subareaDao = subareaDao;
	}


	@Override
	public void save(Decidedzone decidedzone, String[] subareaId) {
		//Decidedzone的生成策略为 assigned,这个生成策略会在事务提交的时候进行保存操作
		decidedzoneDao.save(decidedzone);
		//循环遍历ID,并按ID查询出分区信息
		for (String id : subareaId) {
			Subarea _subarea = subareaDao.findById(id);
			/*
			 * 因为定区和分区为:一对多关系
			 * 且关联关系由多的一方维护,即分区
			 * 所以需要使用分区进行关联
			 */
			_subarea.setDecidedzone(decidedzone);
		}
	}


	@Override
	public void pageQuery(PageBean pageBean) {
		decidedzoneDao.findByPage(pageBean);
	}


	@Override
	public void delete(Decidedzone decidedzone) {
		//按照ID查询出定区对象
		Decidedzone _decidedzone = decidedzoneDao.findById(decidedzone.getId());
		//删除查询到的decidedzone对象
		decidedzoneDao.delete(_decidedzone);
	}


	@Override
	public void edit(Decidedzone model, String[] subareaId) {
		//按照ID查询出decidedzone
		Decidedzone _decidedzone = decidedzoneDao.findById(model.getId());
		//将model中的值赋值给_decidedzone
		_decidedzone.setName(model.getName());
		_decidedzone.setStaff(model.getStaff());
		for (String id : subareaId) {
			Subarea _subarea = subareaDao.findById(id);
			_subarea.setDecidedzone(_decidedzone);
		}
		decidedzoneDao.update(_decidedzone);
	}

}

SubareaService(新增方法):

	/**
	 * 获取未关联定去的分区数据
	 * @return
	 */
	List<Subarea> findByNotRelation();

	/**
	 * 获取指定定区相关联的分区
	 * @param id
	 * @return
	 */
	List<Subarea> findByDecidedzone(Decidedzone decidedzone);

	/**
	 * 删除定区中的分区
	 * @param model
	 */
	void decidedzoneDelete(Subarea model);

SubareaServiceImpl:

	@Override
	public List<Subarea> findByNotRelation() {
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Subarea.class);
		//返回未被删除且未关联定区的分区数据
		detachedCriteria.add(Restrictions.eq("deltag", "0")).add(Restrictions.isNull("decidedzone"));
		return subareaDao.findByCriteria(detachedCriteria );
	}


	@Override
	public List<Subarea> findByDecidedzone(Decidedzone decidedzone) {
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Subarea.class);
		detachedCriteria.add(Restrictions.eq("deltag", "0")).add(Restrictions.eq("decidedzone", decidedzone));
		return subareaDao.findByCriteria(detachedCriteria);
	}


	@Override
	public void decidedzoneDelete(Subarea model) {
		//按ID查询出分区数据
		Subarea _subarea = subareaDao.findById(model.getId());
		//删除分区中的定区数据
		_subarea.setDecidedzone(null);
	}

页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>管理定区/调度排班</title>
<!-- 导入jquery核心类库 -->
<script type="text/javascript"
	src="${pageContext.request.contextPath }/js/jquery-1.8.3.js"></script>
<!-- 导入easyui类库 -->
<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath }/js/easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath }/js/easyui/themes/icon.css">
<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath }/js/easyui/ext/portal.css">
<link rel="stylesheet" type="text/css"
	href="${pageContext.request.contextPath }/css/default.css">	
<script type="text/javascript"
	src="${pageContext.request.contextPath }/js/easyui/jquery.easyui.min.js"></script>
<script type="text/javascript"
	src="${pageContext.request.contextPath }/js/easyui/ext/jquery.portal.js"></script>
<script type="text/javascript"
	src="${pageContext.request.contextPath }/js/easyui/ext/jquery.cookie.js"></script>
<script
	src="${pageContext.request.contextPath }/js/easyui/locale/easyui-lang-zh_CN.js"
	type="text/javascript"></script>
<script type="text/javascript"
	src="${pageContext.request.contextPath }/js/jquery.formToJson.js"></script>
<script type="text/javascript">

	function doClear(){
		$("#grid").datagrid('clearSelections');
	}

	function doAdd(){
		$('#addDecidedzoneWindow').window("open");
	}
	
	function doEdit(){
		//获取被选中的行
		var row = $("#grid").datagrid('getSelections')[0];
		if(row == undefined){
			$.messager.alert('提示信息','请选中您要修改的定区','warning'); 
		}else{
			$('#editDecidedzoneWindow').window("open");
			console.info(row);
			//回显取派员信息
			$('#editDecidedzoneWindow').form("load",row);
			$("#editStaff").combobox('setValue',row.staff.id);
			$("#editStaff").combobox('setText',row.staff.name);
			$("#subareaGrid2").datagrid('reload');
			
		}
	}
	
	//删除按钮
	function doDelete(){
		//获取被选中的行
		var rows = $("#grid").datagrid('getSelections');
		//判断是否有行被选中
		if(rows.length == 0){
			$.messager.alert('提示信息','请选中您要删除的定区','warning');
		}else{
			$.messager.confirm('提示信息', '确定要执行删除操作吗?', function(r){
				if (r){ 
					location.href = "DecidedzoneAction_delete.action?id=" + rows[0].id;
				} 
			}); 
		}
	}
	
	function doSearch(){
		$('#searchWindow').window("open");
	}
	
	function subareaDelete(){
		var row = $("#association_subarea").datagrid('getSelections')[0];
		if(row == undefined){
			$.messager.alert('提示信息','请选中您要删除的分区','warning'); 
		}else{
			$.post('SubareaAction_ajaxDecidedzoneDelete.action',{'id':row.id},function(data){
				$("#association_subarea").datagrid('reload');
			});
		}
	}
	
	//工具栏
	var toolbar = [ {
		id : 'button-search',	
		text : '查询',
		iconCls : 'icon-search',
		handler : doSearch
	}, {
		id : 'button-add',
		text : '增加',
		iconCls : 'icon-add',
		handler : doAdd
	}, {
		id : 'button-edit',	
		text : '修改',
		iconCls : 'icon-edit',
		handler : doEdit
	},{
		id : 'button-delete',
		text : '删除',
		iconCls : 'icon-cancel',
		handler : doDelete
	},{
		id : 'button-association',
		text : '关联客户',
		iconCls : 'icon-sum',
		handler : doAssociations
	}, {
		id : 'button-clearSel',	
		text : '取消选中',
		handler : doClear
	}];
	// 定义列
	var columns = [ [ {
		field : 'id',
		title : '定区编号',
		width : 120,
		align : 'center'
	},{
		field : 'name',
		title : '定区名称',
		width : 120,
		align : 'center'
	}, {
		field : 'staff.name',
		title : '取派员',
		width : 120,
		align : 'center',
		formatter : function(data,row ,index){
			return row.staff.name;
		}
	}, {
		field : 'staff.telephone',
		title : '联系电话',
		width : 120,
		align : 'center',
		formatter : function(data,row ,index){
			return row.staff.telephone;
		}
	}, {
		field : 'staff.station',
		title : '所属公司',
		width : 120,
		align : 'center',
		formatter : function(data,row ,index){
			return row.staff.station;
		}
	} ] ];
	
	$(function(){
		// 先将body隐藏,再显示,不会出现页面刷新效果
		$("body").css({visibility:"visible"});
		
		// 收派标准数据表格
		$('#grid').datagrid( {
			iconCls : 'icon-forward',
			fit : true,
			border : true,
			rownumbers : true,
			striped : true,
			singleSelect:true,
			pageList: [15,20,28],
			pageSize:28,
			pagination : true,
			toolbar : toolbar,
			url : "DecidedzoneAction_pageQuery.action",
			idField : 'id',
			columns : columns,
			onDblClickRow : doDblClickRow
		});
		
		// 添加、修改定区
		$('#addDecidedzoneWindow').window({
	        title: '添加定区',
	        width: 800,
	        modal: true,
	        shadow: true,
	        closed: true,
	        height: 400,
	        resizable:false
	    });
		$("#save").click(function(){
			var formFlag = $("#addDecidedzoneWindow").form('validate');
			if(formFlag){
				$("#addDecidedzoneForm").submit();
			}
		});
		
		//修改定区
		$('#editDecidedzoneWindow').window({
	        title: '修改定区',
	        width: 800,
	        modal: true,
	        shadow: true,
	        closed: true,
	        height: 400,
	        resizable:false
	    });
		$("#edit").click(function(){
			$("#editDecidedzoneForm").submit();
		});		
		
		// 查询定区
		$('#searchWindow').window({
	        title: '查询定区',
	        width: 400,
	        modal: true,
	        shadow: true,
	        closed: true,
	        height: 300,
	        resizable:false
	    });
		$("#btn").click(function(){
			//将form中的表单数据转换为json,用于提交
			var params = $("#searchForm").serializeJson();
			//console.info(params);
			//提交参数,加载数据,关闭查询窗口
			$("#grid").datagrid('load',params);
			$("#searchWindow").window('close');
		});
		
	});
	
	//加载关联信息
	function doDblClickRow(rowIndex, rowData){
		$('#association_subarea').datagrid( {
			fit : true,
			border : true,
			rownumbers : true,
			striped : true,
			method:'get',
			singleSelect:true,
			url : "SubareaAction_ajaxFindByDecidedzone.action?decidedzone.id="+rowData.id,
			toolbar:[{
				id : 'subarea-delete',
				text : '删除分区',
				iconCls : 'icon-cancel',
				handler : subareaDelete
			}],
			columns : [ [{
				field : 'id',
				title : '分区编号',
				width : 235,
				align : 'center'
			},{
				field : 'province',
				title : '省',
				width : 120,
				align : 'center',
				formatter : function(data,row ,index){
					return row.region.province;
				}
			}, {
				field : 'city',
				title : '市',
				width : 120,
				align : 'center',
				formatter : function(data,row ,index){
					return row.region.city;
				}
			}, {
				field : 'district',
				title : '区',
				width : 120,
				align : 'center',
				formatter : function(data,row ,index){
					return row.region.district;
				}
			}, {
				field : 'addresskey',
				title : '关键字',
				width : 120,
				align : 'center'
			}, {
				field : 'startnum',
				title : '起始号',
				width : 100,
				align : 'center'
			}, {
				field : 'endnum',
				title : '终止号',
				width : 100,
				align : 'center'
			} , {
				field : 'single',
				title : '单双号',
				width : 100,
				align : 'center'
			} , {
				field : 'position',
				title : '位置',
				width : 250,
				align : 'center'
			} ] ]
		});
		
	}
</script>	
</head>
<body class="easyui-layout" style="visibility:hidden;">
	<div region="center" border="false">
    	<table id="grid"></table>
	</div>
	<div region="south" border="false" style="height:150px">
		<div id="tabs" fit="true" class="easyui-tabs">
			<div title="关联分区" id="subArea"
				style="width:100%;height:100%;overflow:hidden">
				<table id="association_subarea"></table>
			</div>	
			<!-- <div title="关联客户" id="customers"
				style="width:100%;height:100%;overflow:hidden">
				<table id="association_customer"></table>
			</div>	 -->
		</div>
	</div>
	
	<!-- 添加分区 -->
	<div class="easyui-window" title="定区添加" id="addDecidedzoneWindow" collapsible="false" minimizable="false" maximizable="false" style="top:20px;left:200px">
		<div style="height:31px;overflow:hidden;" split="false" border="false" >
			<div class="datagrid-toolbar">
				<a id="save" icon="icon-save" href="#" class="easyui-linkbutton" plain="true" >保存</a>
			</div>
		</div>
		
		<div style="overflow:auto;padding:5px;" border="false">
			<form id="addDecidedzoneForm" action="DecidedzoneAction_add.action" method="post">
				<table class="table-edit" width="80%" align="center">
					<tr class="title">
						<td colspan="2">定区信息</td>
					</tr>
					<tr>
						<td>定区编码</td>
						<td><input type="text" name="id" class="easyui-validatebox" required="true"/></td>
					</tr>
					<tr>
						<td>定区名称</td>
						<td><input type="text" name="name" class="easyui-validatebox" required="true"/></td>
					</tr>
					<tr>
						<td>选择取派员</td>
						<td>
							<input class="easyui-combobox" name="staff.id"  
    							data-options="valueField:'id',textField:'name',url:'StaffAction_ajaxList.action'" />  
						</td>
					</tr>
					<tr height="300">
						<td valign="top">关联分区</td>
						<td>
							<table id="subareaGrid"  class="easyui-datagrid" border="false" style="width:500px;height:300px" data-options="url:'SubareaAction_ajaxList.action',fitColumns:true,singleSelect:false">
								<thead>  
							        <tr>  
							            <th data-options="field:'subareaId',width:30,checkbox:true">编号</th>  
							            <th data-options="field:'addresskey',width:150">关键字</th>  
							            <th data-options="field:'position',width:200,align:'right'">位置</th>  
							        </tr>  
							    </thead> 
							</table>
						</td>
					</tr>
				</table>
			</form>
		</div>
	</div>
	
	<!-- 修改分区 -->
	<div class="easyui-window" title="定区添加" id="editDecidedzoneWindow" collapsible="false" minimizable="false" maximizable="false" style="top:20px;left:200px">
		<div style="height:31px;overflow:hidden;" split="false" border="false" >
			<div class="datagrid-toolbar">
				<a id="edit" icon="icon-edit" href="#" class="easyui-linkbutton" plain="true" >修改</a>
			</div>
		</div>
		
		<div style="overflow:auto;padding:5px;" border="false">
			<form id="editDecidedzoneForm" action="DecidedzoneAction_edit.action" method="post">
				<input type="hidden" name="id">
				<table class="table-edit" width="80%" align="center">
					<tr class="title">
						<td colspan="2">定区信息</td>
					</tr>
					<tr>
						<td>定区名称</td>
						<td><input type="text" name="name" class="easyui-validatebox" required="true"/></td>
					</tr>
					<tr>
						<td>选择取派员</td>
						<td>
							<input id="editStaff" class="easyui-combobox" name="staff.id"  
    							data-options="valueField:'id',textField:'name',url:'StaffAction_ajaxList.action'" />  
						</td>
					</tr>
					<tr height="300">
						<td valign="top">关联分区</td>
						<td>
							<table id="subareaGrid2"  class="easyui-datagrid" border="false" style="width:500px;height:300px" data-options="url:'SubareaAction_ajaxList.action',fitColumns:true,singleSelect:false">
								<thead>  
							        <tr>  
							            <th data-options="field:'subareaId',width:30,checkbox:true">编号</th>  
							            <th data-options="field:'addresskey',width:150">关键字</th>  
							            <th data-options="field:'position',width:200,align:'right'">位置</th>  
							        </tr>  
							    </thead> 
							</table>
						</td>
					</tr>
				</table>
			</form>
		</div>
	</div>
	
	<!-- 查询定区 -->
	<div class="easyui-window" title="查询定区窗口" id="searchWindow" collapsible="false" minimizable="false" maximizable="false" style="top:20px;left:200px">
		<div style="overflow:auto;padding:5px;" border="false">
			<form id="searchForm">
				<table class="table-edit" width="80%" align="center">
					<tr class="title">
						<td colspan="2">查询条件</td>
					</tr>
					<tr>
						<td>定区名称</td>
						<td><input type="text" name="name" class="easyui-validatebox"/></td>
					</tr>
					<tr>
						<td>取派员姓名</td>
						<td><input type="text" name="staff.name" class="easyui-validatebox"/></td>
					</tr>					
					<tr>
						<td colspan="2"><a id="btn" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'">查询</a> </td>
					</tr>
				</table>
			</form>
		</div>
	</div>
</body>
</html>



猜你喜欢

转载自blog.csdn.net/qq1031893936/article/details/79835927