SSH物流开发系统设计:定区的相关操作和设计

一、添加定区

1、前端逻辑

<!-- 添加 修改分区 -->
    <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>
                <script type="text/javascript">
                    $(function(){
                        $("#save").click(function(){
                            var v = $("#addDecidedzoneForm").form("validate");
                            if(v){
                                $("#addDecidedzoneForm").submit();
                            }
                        });
                    });
                </script>
            </div>
        </div>

        <div style="overflow:auto;padding:5px;" border="false">
            <form id="addDecidedzoneForm" action="${pageContext.request.contextPath }/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="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:'${pageContext.request.contextPath }/staffAction_listajax.action'" />  
                        </td>
                    </tr>
                    <tr height="300">
                        <td valign="top">关联分区</td>
                        <td>
                            <table id="subareaGrid"  class="easyui-datagrid" border="false" style="width:300px;height:300px" data-options="url:'${pageContext.request.contextPath }/subareaAction_listajax.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>

2、基础pojo和映射文件

package com.crm.bos.domain;

import java.util.HashSet;
import java.util.Set;

/**
 * 定区实体
 */
public class Decidedzone implements java.io.Serializable {

    // Fields

    private String id;
    private Staff staff;
    private String name;
    private Set subareas = new HashSet(0);

    // Constructors

    /** default constructor */
    public Decidedzone() {
    }

    /** minimal constructor */
    public Decidedzone(String id) {
        this.id = id;
    }

    /** full constructor */
    public Decidedzone(String id, Staff staff, String name, Set subareas) {
        this.id = id;
        this.staff = staff;
        this.name = name;
        this.subareas = subareas;
    }

    // Property accessors

    public String getId() {
        return this.id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Staff getStaff() {
        return this.staff;
    }

    public void setStaff(Staff staff) {
        this.staff = staff;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Set getSubareas() {
        return this.subareas;
    }

    public void setSubareas(Set subareas) {
        this.subareas = subareas;
    }

}

映射文件

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.crm.bos.domain.Decidedzone" table="bc_decidedzone" catalog="bos19">
        <id name="id" type="java.lang.String">
            <column name="id" length="32" />
            <generator class="uuid" />
        </id>
        <many-to-one lazy="false" name="staff" class="com.crm.bos.domain.Staff" fetch="select">
            <column name="staff_id" length="32" />
        </many-to-one>
        <property name="name" type="java.lang.String">
            <column name="name" length="30" />
        </property>
        <set name="subareas" inverse="true">
            <key>
                <column name="decidedzone_id" length="32" />
            </key>
            <one-to-many class="com.crm.bos.domain.Subarea" />
        </set>
    </class>
</hibernate-mapping>

3、在StaffAction中提供listajax方法,查询没有作废的取派员,返回json数据

//查询没有作废的取派员,即只查询deltag=0的数据,返回json数据
    public String listajax() throws IOException{
        List<Staff> list=staffService.findListNotDelete();
        String[] excludes=new String[]{"telephone","haspda","deltag","station","standard","decidedzones"};
        this.writeList2Json(list, excludes);
        return NONE;
    }

在StaffService中提供方法查询没有作废的取派员

    public List<Staff> findListNotDelete() {
        DetachedCriteria dc=DetachedCriteria.forClass(Staff.class);
        dc.add(Restrictions.eq("deltag", "0"));
        return staffDao.findByCriteria(dc);
    }

在BaseDao中提供通用的条件查询方法

public List<T> findByCriteria(DetachedCriteria detachedCriteria) {
        return this.getHibernateTemplate().findByCriteria(detachedCriteria);
    }

在SubareaAction中提供listajax方法,查询未关联到定分区的分区数据,返回json。并修改相应的service类

//查询没有进行分配的分区数据,返回json
    public String listajax() throws IOException{
        List<Subarea> list = subareaService.findListNotAssociation();
        String[] excludes = new String[]{"decidedzone","region","startnum","endnum","single"};
        this.writeList2Json(list, excludes );
        return NONE;
    }

service层

/**
     * 查询没有关联到定区的分区
     */
    public List<Subarea> findListNotAssociation() {
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Subarea.class);
        detachedCriteria.add(Restrictions.isNull("decidedzone"));
        return subareaDao.findByCriteria(detachedCriteria );
    }

4、创建定区管理的Action,提供add方法保存定区,提供subareaid 数组属性接收多个分区id。

@Controller
@Scope("prototype")
public class DecidedzoneAction extends BaseAction<Decidedzone>{
    //使用数组接受分区id
    private String[] subareaid;
    public void setSubareaid(String[] subareaid) {
        this.subareaid = subareaid;
    }

    //添加分区
    public String add(){
        decidedzoneService.save(model,subareaid);
        return "list";
    }

service层:

@Service
@Transactional
public class DecidedzoneServiceImpl implements IDecidedzoneService{

    @Autowired
    private IDecidedzoneDao decidedzoneDao;
    @Autowired
    private ISubareaDao subareaDao;
    public void save(Decidedzone model, String[] subareaid) {
        decidedzoneDao.save(model);
        for (String sid : subareaid) {
            Subarea subarea = subareaDao.findById(sid);
            subarea.setDecidedzone(model);//分区关联定区

        }
    }

配置struts.xml

        <!-- 定区管理action -->
        <action name="decidedzoneAction_*" class="decidedzoneAction" method="{1}">
            <result name="list">/WEB-INF/pages/base/decidedzone.jsp</result>
        </action>

二、定区分页查询

1、页面修改datagrid的URL:

        // 定区数据表格
        $('#grid').datagrid( {
            iconCls : 'icon-forward',
            fit : true,
            border : true,
            rownumbers : true,
            striped : true,
            pageList: [30,50,100],
            pagination : true,
            toolbar : toolbar,
            url : "${pageContext.request.contextPath}/decidedzoneAction_pageQuery.action",
            idField : 'id',
            columns : columns,
            onDblClickRow : doDblClickRow
        });

2、在定区Action中提供分页查询方法

    //分页查询
    public String pageQuery(){
        decidedzoneService.pageQuery(pageBean);
        String[] excludes=new String[]{"subareas","detachedCriteria","pageSize", "currentPage","decidedzones"};
        this.writePageBean2Json(pageBean, excludes);
        return NONE;
    }

service层:

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

修改Decidedzone.hbm.xml关闭延时加载避免懒加载异常

 <many-to-one lazy="false" name="staff" class="com.crm.bos.domain.Staff" fetch="select">
            <column name="staff_id" length="32" />
        </many-to-one>

三、定区关联客户

发布crm服务

第一步:创建动态的web项目crm,导入hessian的jar
第二步:创建一个crm数据库和t_customer表

/*
Navicat MySQL Data Transfer

Source Server         : root
Source Server Version : 50022
Source Host           : localhost:3306
Source Database       : crm

Target Server Type    : MYSQL
Target Server Version : 50022
File Encoding         : 65001

Date: 2015-04-19 17:46:45
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `t_customer`
-- ----------------------------
DROP TABLE IF EXISTS `t_customer`;
CREATE TABLE `t_customer` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(255) default NULL,
  `station` varchar(255) default NULL,
  `telephone` varchar(255) default NULL,
  `address` varchar(255) default NULL,
  `decidedzone_id` varchar(255) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of t_customer
-- ----------------------------
INSERT INTO `t_customer` VALUES ('1', '张三', '百度', '1', '北京', null);
INSERT INTO `t_customer` VALUES ('2', '李四', '哇哈哈', '2', '上海', null);
INSERT INTO `t_customer` VALUES ('3', '王五', '搜狗', '3', '天津', null);
INSERT INTO `t_customer` VALUES ('4', '赵六', '联想', '4', '石家庄', null);
INSERT INTO `t_customer` VALUES ('5', '小白', '测试空间', '5', '内蒙古', null);
INSERT INTO `t_customer` VALUES ('6', '小黑', '联想', '6', '天津', null);
INSERT INTO `t_customer` VALUES ('7', '小花', '百度', '7', '北京', null);
INSERT INTO `t_customer` VALUES ('8', '小李', '长城', '8', '北京', null);

第三步:在web.xml中配置spring的DispatcherServlet

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>crm</display-name>

    <servlet>
        <servlet-name>remoting</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>remoting</servlet-name>
        <url-pattern>/remoting/*</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
</web-app>

第四步:提供接口CustomerService和Customer类、hbm映射文件。
customer类

package com.crm.domain;

import java.io.Serializable;

public class Customer implements Serializable {
    private Integer id;
    private String name;
    private String station;
    private String telephone;
    private String address;

    private String decidedzone_id;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getStation() {
        return station;
    }

    public void setStation(String station) {
        this.station = station;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getDecidedzone_id() {
        return decidedzone_id;
    }

    public void setDecidedzone_id(String decidedzone_id) {
        this.decidedzone_id = decidedzone_id;
    }

}

映射文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.crm.domain.Customer" table="t_customer">
        <id name="id">
            <generator class="native"></generator>
        </id>
        <property name="name"></property>
        <property name="station"></property>
        <property name="telephone"></property>
        <property name="address"></property>
        <property name="decidedzone_id"></property>
    </class>
</hibernate-mapping>

CustomerService:

package com.crm.service;

import java.util.List;

import com.crm.domain.Customer;


// 客户服务接口 
public interface CustomerService {
    // 未关联定区客户
    public List<Customer> findnoassociationCustomers();

    // 查询已经关联指定定区的客户
    public List<Customer> findhasassociationCustomers(String decidedZoneId);

    // 将未关联定区客户关联到定区上
    public void assignCustomersToDecidedZone(Integer[] customerIds, String decidedZoneId);

    //根据手机号查询客户信息
    public Customer findCustomerByPhoneNumber(String phoneNumber);

    //根据取件地址查询定区id
    public String findDecidedzoneIdByAddress(String address);
}

第五步:为上面的CustomerService接口提供实现类

package com.crm.service.impl;

import java.util.List;

import org.hibernate.Session;

import com.crm.domain.Customer;
import com.crm.service.CustomerService;
import com.crm.utils.HibernateUtils;


public class CustomerServiceImpl implements CustomerService {

    public List<Customer> findnoassociationCustomers() {
        Session session = HibernateUtils.openSession();
        session.beginTransaction();

        String hql = "from Customer where decidedzone_id is null";
        List<Customer> customers = session.createQuery(hql).list();

        session.getTransaction().commit();
        session.close();

        return customers;
    }

    public List<Customer> findhasassociationCustomers(String decidedZoneId) {
        Session session = HibernateUtils.openSession();
        session.beginTransaction();

        String hql = "from Customer where decidedzone_id = ?";
        List<Customer> customers = session.createQuery(hql).setParameter(0, decidedZoneId).list();

        session.getTransaction().commit();
        session.close();

        return customers;
    }

    public void assignCustomersToDecidedZone(Integer[] customerIds, String decidedZoneId) {
        Session session = HibernateUtils.openSession();
        session.beginTransaction();

        // 取消定区所有关联客户
        String hql2 = "update Customer set decidedzone_id=null where decidedzone_id=?";
        session.createQuery(hql2).setParameter(0, decidedZoneId).executeUpdate();

        // 进行关联
        String hql = "update Customer set decidedzone_id=? where id =?";
        if (customerIds != null) {
            for (Integer id : customerIds) {
                session.createQuery(hql).setParameter(0, decidedZoneId).setParameter(1, id).executeUpdate();
            }
        }
        session.getTransaction().commit();
        session.close();
    }

    public Customer findCustomerByPhoneNumber(String phoneNumber) {
        Session session = HibernateUtils.openSession();
        session.beginTransaction();
        String hql = "from Customer where telephone = ?";
        List<Customer> customers = session.createQuery(hql).setParameter(0, phoneNumber).list();
        Customer c=null;
        if(customers!=null&&customers.size()>0){
            c=customers.get(0);
        }
        session.getTransaction().commit();
        session.close();
        return c;
    }

    public String findDecidedzoneIdByAddress(String address) {
        Session session = HibernateUtils.openSession();
        session.beginTransaction();
        String hql = "select decidedzone_id from Customer where address = ?";
        List<String> list = session.createQuery(hql).setParameter(0, address).list();
        String addr=null;
        if(list!=null&&list.size()>0){
            addr=list.get(0);
        }
        session.getTransaction().commit();
        session.close();
        return addr;
    }

}

第六步:在WEB-INF目录提供spring的配置文件remoting-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!-- 业务类  -->
    <bean id="customerService" class="com.crm.service.impl.CustomerServiceImpl" />

    <!-- 注册hessian服务 -->
    <bean id="/customer" class="org.springframework.remoting.caucho.HessianServiceExporter">
        <!-- 业务接口实现类 -->
        <property name="service" ref="customerService" />
        <!-- 业务接口 -->
        <property name="serviceInterface" value="com.crm.service.CustomerService" />
    </bean>
</beans>    

在bos项目中调用crm服务获得客户数据

第一步:在bos项目中导入hessian的jar
第二步:从crm项目中复制CustomerService接口和Customer类到bos项目中
第三步:在spring配置文件中配置一个代理对象,可以调用crm服务

<!-- 配置远程服务的代理对象 -->
    <bean id="customerService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
        <!-- 注入接口类型 -->
        <property name="serviceInterface" value="com.crm.bos.crm.CustomerService"/>
        <!-- 服务访问路径 -->
        <property name="serviceUrl" value="http://localhost:8080/crm/remoting/customer"/>
    </bean>

第四步:将上面的代理对象通过注解方式注入到Action中

//注入代理对象
    @Autowired
    private CustomerService customerService;

第五步:为定区列表页面中的“关联客户”按钮绑定事件,发送2次ajax请求访问Action,在Action中调用hessian代理对象,通过代理对象可以远程访问crm获取客户数据

//全局变量,定区选中的定区id
    var id;
    function doAssociations(){
        //判断当前是否选中了一个定区
        var rows = $("#grid").datagrid("getSelections");
        if(rows.length == 1){
            id = rows[0].id;
            //选中了一个
            $('#customerWindow').window('open');
            $("#noassociationSelect").empty();//清空下拉框
            $("#associationSelect").empty();//清空下拉框

            //发送ajax请求获取没有关联到定区的客户
            var url1 = "${pageContext.request.contextPath}/decidedzoneAction_findnoassociationCustomers.action";
            $.post(url1,{},function(data){
                //解析json数据,填充到下拉框中
                ///
                for(var i=0;i<data.length;i++){
                    var id = data[i].id;
                    var name = data[i].name;
                    $("#noassociationSelect").append("<option value='"+id+"'>"+name+"</option>");
                }
            },'json');

            //发送ajax请求获取关联到当前选中定区的客户
            var url2 = "${pageContext.request.contextPath}/decidedzoneAction_findhasassociationCustomers.action";
            $.post(url2,{"id":rows[0].id},function(data){
                //解析json数据,填充到下拉框中
                ///
                for(var i=0;i<data.length;i++){
                    var id = data[i].id;
                    var name = data[i].name;
                    $("#associationSelect").append("<option value='"+id+"'>"+name+"</option>");
                }
            },'json');
        }else{
            $.messager.alert("提示信息","请选择一个定区操作!","warning");
        }
    }

第六步:为左右移动按钮绑定事件

<script type="text/javascript">
                                $(function(){
                                    //为左右移动按钮绑定事件
                                    $("#toRight").click(function(){
                                        $("#associationSelect").append($("#noassociationSelect option:selected"));
                                    });
                                    $("#toLeft").click(function(){
                                        $("#noassociationSelect").append($("#associationSelect option:selected"));
                                    });
                            </script>

第七步:为关联客户窗口中的“关联客户”按钮绑定事件

//为关联客户按钮绑定事件
                                    $("#associationBtn").click(function(){
                                        //在提交表单之前,选中右侧下拉框中所有的选项(否则只会提交被选中的数据)
                                        $("#associationSelect option").attr("selected","selected");
                                        //在提交表单之前设置隐藏域的值(定区id)
                                        $("input[name=id]").val(id);
                                        $("#customerForm").submit();
                                    });
                                });

第八步:在定区Action中接收提交的参数,调用crm服务实现定区关联客户业务功能

private Integer[] customerIds;
    //定区关联客户
    public void setCustomerIds(Integer[] customerIds) {
        this.customerIds = customerIds;
    }

    public String assigncustomerstodecidedzone(){
        customerService.assignCustomersToDecidedZone(customerIds, model.getId());
        return "list";
    }

service层如下:

public void save(Decidedzone model, String[] subareaid) {
        decidedzoneDao.save(model);
        for (String sid : subareaid) {
            Subarea subarea = subareaDao.findById(sid);
            subarea.setDecidedzone(model);//分区关联定区

        }
    }

四、源码点我

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值