客户拜访记录概述
需求概述:
一个系统的用户(公司业务员),业务员需要对客户进行拜访,对拜访的过程进行记录。业务员(用户)和客户(客户)之间关系(需要具体业务具体分析:一对多—>公司产品比较单一,只允许一个业务员对应多个客户。多对多—>大公司有不同的产品,不同产品下有不同业务员都可以接触到同一个客户,一个客户可以对应多个业务员)。大部分情况创建成多对多。
-
多对多创建原则:
- 创建中间表,中间表中至少两个字段分别作为外键指向多对多双方的主键
客户拜访记录的准备工作
创建表
CREATE TABLE `sale_visit` (
`visit_id` varchar(32) NOT NULL,
`visit_cust_id` bigint(32) DEFAULT NULL COMMENT '客户id',
`visit_user_id` bigint(32) DEFAULT NULL COMMENT '负责人id',
`visit_time` date DEFAULT NULL COMMENT '拜访时间',
`visit_interviewee` varchar(32) DEFAULT NULL COMMENT '被拜访人',
`visit_addr` varchar(128) DEFAULT NULL COMMENT '拜访地点',
`visit_detail` varchar(256) DEFAULT NULL COMMENT '拜访详情',
`visit_nexttime` date DEFAULT NULL COMMENT '下次拜访时间',
PRIMARY KEY (`visit_id`),
KEY `FK_sale_visit_cust_id` (`visit_cust_id`),
KEY `FK_sale_visit_user_id` (`visit_user_id`),
CONSTRAINT `FK_sale_visit_cust_id` FOREIGN KEY (`visit_cust_id`) REFERENCES `cst_customer` (`cust_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `FK_sale_visit_user_id` FOREIGN KEY (`visit_user_id`) REFERENCES `sys_user` (`user_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
创建实体和映射
- 创建实体
public class SaleVisit {
private String visit_id;
private Date visit_time;
private String visit_interviewee;
private String visit_addr;
private String visit_detail;
private Date visit_nexttime;
private Customer customer;
private User user;
get/set...
}
- 创建映射
<hibernate-mapping>
<class name="com.ithou.crm.domain.SaleVisit" table="sale_visit">
<id name="visit_id" column="visit_id">
<generator class="uuid"/>
</id>
<property name="visit_time" column="visit_time"/>
<property name="visit_interviewee" column="visit_interviewee"/>
<property name="visit_addr" column="visit_addr"/>
<property name="visit_detail" column="visit_detail"/>
<property name="visit_nexttime" column="visit_nexttime"/>
<many-to-one name="customer" class="com.ithou.crm.domain.Customer" column="visit_cust_id"></many-to-one>
<many-to-one name="user" class="com.ithou.crm.domain.User" column="visit_user_id"></many-to-one>
</class>
</hibernate-mapping>
创建相关类
将相关类进行配置
- Bean管理交给Spring的XML
- 属性注入使用注解方式
<!-- 配置客户拜访记录管理相关类 -->
<bean id="saleVisitDao" class="com.ithou.crm.dao.impl.SaleVisitDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="saleVisitService" class="com.ithou.crm.service.impl.SaleVisitServiceImpl">
</bean>
<bean id="saleVisitAction" class="com.ithou.crm.web.action.SaleVisitAction">
</bean>
客户拜访记录的列表显示
1、修改menu.jsp的提交路径
2、编写Action的findAll的方法
public String findAll() {
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(SaleVisit.class);
PageBean<SaleVisit> pageBean = saleVisitService.findAll(detachedCriteria,currPage,pageSize);
ActionContext.getContext().getValueStack().push(pageBean);
return "findAll";
}
3、编写Service
public PageBean<SaleVisit> findAll(DetachedCriteria detachedCriteria, Integer currPage, Integer pageSize) {
PageBean<SaleVisit> pageBean = new PageBean<SaleVisit>();
pageBean.setCurrPage(currPage);
pageBean.setPageSize(pageSize);
Integer totalCount = saleVisitDao.findCount(detachedCriteria);
pageBean.setTotalCount(totalCount);
Double tc = totalCount.doubleValue();
Double num = Math.ceil(tc/pageSize);
pageBean.setTotalPage(num.intValue());
Integer begin = (currPage -1)*pageSize;
List<SaleVisit> list = saleVisitDao.findByPage(detachedCriteria, begin, pageSize);
pageBean.setList(list);
return pageBean;
}
4、在页面显示数据
客户拜访记录的保存
1、修改menu.jsp的链接地址
2、编写Action中的saveUI方法
public String saveUI() {
return "saveUI";
}
3、跳转到添加页面
4、异步加载数据
<script type="text/javascript">
$(function(){
// 异步加载客户信息
$.post("${pageContext.request.contextPath }/customer_findAllCustomer.action",{},function(data){
$(data).each(function(i,n){
$("#customer").append("<option value='"+n.cust_id+"'>"+n.cust_name+"</option>");
});
},"json");
// 异步加载用户信息
$.post("${pageContext.request.contextPath }/user_findAllUser.action",{},function(data){
$(data).each(function(i,n){
$("#user").append("<option value='"+n.user_id+"'>"+n.user_name+"</option>");
});
},"json");
});
</script>
5、编写Action中的save方法
public String save() {
saleVisitService.save(saleVisit);
return "saveSuccess";
}
6、编写Service
public void save(SaleVisit saleVisit) {
saleVisitDao.save(saleVisit);
}
客户拜访记录的条件查询
public String findAll() {
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(SaleVisit.class);
if(saleVisit.getVisit_time()!=null) {
detachedCriteria.add(Restrictions.ge("visit_time", saleVisit.getVisit_time()));
}
if(visit_end_time!=null) {
detachedCriteria.add(Restrictions.le("visit_time", visit_end_time));
}
PageBean<SaleVisit> pageBean = saleVisitService.findAll(detachedCriteria,currPage,pageSize);
ActionContext.getContext().getValueStack().push(pageBean);
return "findAll";
}
权限拦截器
编写类继承拦截器类
public class PrivilegeInterceptor extends MethodFilterInterceptor {
@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
User existUser = (User) ServletActionContext.getRequest().getSession().getAttribute("existUser");
if(existUser == null) {
ActionSupport actionSupport = (ActionSupport) invocation.getAction();
actionSupport.addActionError("您还没有登陆,没有权限访问");
return "login";
}else {
return invocation.invoke();
}
}
}
配置拦截器
- struts.xml
<interceptors>
<interceptor name="privilegeInterceptor" class="com.ithou.crm.web.interceptor.PrivilegeInterceptor"/>
</interceptors>
<global-results>
<result name="login">/login.jsp</result>
</global-results>
版权声明:本文为HYN205原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。