1. 程式人生 > >029 定區關聯客戶功能 - bos

029 定區關聯客戶功能 - bos

import right 數組 append 字段 成功 soci 成了 spring


一、在BOS項目中配置遠程代理對象,遠程調用CRM

1.使用命令wsimport命令解析wsdl文件生成本地源代碼

wsimport -s . -p com.hao.crm.service http://localhost:8080/CRM/service/customer?wsdl

2.復制CustomerService和Customer類對象啊到bos項目中

3.引入cxf的依賴(已經在porm.xml中引過)

4.在Spring配置文件中註冊crm客戶端的代理對象

<!-- 註冊CRM客戶端代理對象 -->
<jaxws:client id="customerService" serviceClass="com.hao.crm.service.CustomerService"
address="http://localhost:8080/CRM/service/customer?wsdl"></jaxws:client>


6.在UserAction中註入代理對象,添加斷點測試CustomerService是否註入成功(可以在login方法中),最後刪除剛剛添加的代碼

二、完善crm的Service

1.在CRM的CustomerService接口中擴展查詢未關聯定區的客戶和查詢已關聯定區的客戶的方法

@WebService
public interface CustomerService {

/**
* 查詢所有客戶
* @return
*/
List<Customer> list();

/**
* 查詢未關聯定區的客戶
* @return
*/
List<Customer> listNotAssociation();

/**
* 查詢已關聯定區的客戶
* @param decidedzoneId 定區id
* @return
*/
List<Customer> listHasAssociation(String decidedzoneId);
}

2.在CustomerServiceImpl中實現擴展的方法,並使用靜態內部類重構CustomerServiceImpl

@Transactional(isolation=Isolation.REPEATABLE_READ, readOnly=false)
public class CustomerServiceImpl implements CustomerService{

@Override
public List<Customer> list() {
String sql = "select * from t_customer";
List<Customer> customerList = jdbcTemplate.query(sql, new CustomerRowMapper());
return customerList;
}

@Override
public List<Customer> listNotAssociation() {
String sql = "select * from t_customer where decidedzone_id is null";
List<Customer> customerList = jdbcTemplate.query(sql, new CustomerRowMapper());
return customerList;
}

@Override
public List<Customer> listHasAssociation(String decidedzoneId) {
String sql = "select * from t_customer where decidedzone_id =?";
List<Customer> customerList = jdbcTemplate.query(sql, new CustomerRowMapper(), decidedzoneId);
return customerList;
}

private JdbcTemplate jdbcTemplate;

public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}

private static class CustomerRowMapper implements RowMapper<Customer>{

@Override
public Customer mapRow(ResultSet rs, int arg1) throws SQLException {
//根據字段名稱從結果集中獲取對應的值
int id = rs.getInt("id");
String name = rs.getString("name");
String station = rs.getString("station");
String telephone = rs.getString("telephone");
String address = rs.getString("address");
String decidedzoneId = rs.getString("decidedzone_id");
Customer c = new Customer(id, name, station, telephone, address, decidedzoneId);
return c;
}
}
}

3.註意服務端代碼更改後,客戶端要重新獲取新的服務類

三、調整定區關聯客戶的頁面以及數據的查詢

1.必須且只選擇一個定區時,點擊關聯客才有效

2.為DecidedzoneAction註入CustomerService代理對象

//註入代理的CustomerService
@Autowired
private CustomerService customerServiceProxy;

3.編寫Decidedzone.listNotAssociation和listHasAssociation方法

/**
* 調用遠程服務crm查詢未關聯定區的用戶
* @return
*/
public String listNotAssociation(){
//調用代理對象執行請求獲取客戶數據
List<Customer> customerList = customerServiceProxy.listNotAssociation();
list2JsonAndWriteResponse(customerList);
return NONE;
}

/**
* 調用遠程服務crm查詢已關聯定區的用戶
* @return
*/
public String listHasAssociation(){
String id = model.getId();
List<Customer> customerList = customerServiceProxy.listHasAssociation(id);
list2JsonAndWriteResponse(customerList);
return NONE;
}

4.註意這個時候我把BaseAction中的list2JsonAndWriteResponse和objec2JsonAndWriteResponse的第二個參數類型改成了可變參數類型,這樣更加靈活

5.在頁面響應函數中填充數據,最終關聯客戶按鈕的響應函數實現如下:

function doAssociations(){
//獲取當前數據表格所有選中的行,返回數組
var rows = $("#grid").datagrid("getSelections");
if(rows.length != 1){
//彈出提示
$.messager.alert("提示信息", "請選擇一個定區進行操作", "warning");
}else{
//清理數據
$("#noassociationSelect").empty();
$("#associationSelect").empty();
//選中了一個定區,可以彈出窗口
$(‘#customerWindow‘).window(‘open‘);
//發送ajax請求請求定區管理的Action,在定區Action中通過crm代理對象完成對於crm服務遠程調用
var url_1 = "decidedzoneAction_listNotAssociation.action";
$.ajax({
"url":url_1,
"dataType":"json",
"type":"get",
"success":function(respData, textStatus, xmlHttp){
//遍歷json數組
for(var i = 0; i < respData.length; ++i){
var id = respData[i].id;
var name = respData[i].name;
var telephone = respData[i].telephone;
name = name+"("+telephone+")";
$("#noassociationSelect").append("<option value=‘"+id+"‘>"+name+"</option>");
}
},
"error":function(xmlHttp, textStatus, exception){
alert(textStatus);
}
});

var decidedzoneId = rows[0].id;
//發送ajax請求請求定區管理的Action,在定區Action中通過crm代理對象完成對於crm服務遠程調用
var url_2 = "decidedzoneAction_listHasAssociation.action";
$.ajax({
"url":url_2,
"data":{"id":decidedzoneId},
"dataType":"json",
"type":"get",
"success":function(respData, textStatus, xmlHttp){
//遍歷json數組
for(var i = 0; i < respData.length; ++i){
var id = respData[i].id;
var name = respData[i].name;
var telephone = respData[i].telephone;
name = name+"("+telephone+")";
$("#associationSelect").append("<option value=‘"+id+"‘>"+name+"</option>");
}
},
"error":function(xmlHttp, textStatus, exception){
alert(textStatus);
}
});
}
}

6.為左右移動按鈕添加響應函數

$(function(){
$("#toRight").click(function(){
$("#associationSelect").append($("#noassociationSelect option:selected"));
});
$("#toLeft").click(function(){
$("#noassociationSelect").append($("#associationSelect option:selected"));
});
});

7.為關聯客戶的提交按鈕綁定事件,提交關聯客戶請求

$(function(){
//為關聯客戶按鈕綁定事件
$("#associationBtn").click(function(){
//為隱藏域(存放定區id)賦值
var rows = $("#grid").datagrid("getSelections");
var id = rows[0].id;
$("input[name=id]").val(id);

//選中右側的所有表單項
$("#associationSelect option").prop("selected", "selected");
console.log($("#customerForm").serialize());
$("#customerForm").submit();
});
});

四、定區關聯客戶的服務端實現

1.在crm服務端擴展定區關聯客戶方法

/**
* 定區關聯客戶
* @param decidedzoneId
* @param customerIds
*/
void assigncustomerstodecidedzone(String decidedzoneId, Integer[] customerIds);

2.擴展方法的實現

@Override
public void assigncustomerstodecidedzone(String decidedzoneId,
Integer[] customerIds) {
//清空該定區的所有關聯
String sql = "update t_customer set decidedzone_id = null where decidedzone_id = ?";
jdbcTemplate.update(sql, decidedzoneId);
//客戶重新關聯定區

if(customerIds != null && customerIds.length > 0){
StringBuilder sb = new StringBuilder("update t_customer set decidedzone_id = ? where id in(");
for (Integer id : customerIds) {
sb.append(id).append(",");
}
sb.deleteCharAt(sb.length()-1);
sb.append(")");
jdbcTemplate.update(sb.toString(), decidedzoneId);
}
}

3.重新生成客戶端調用代碼,復制新的類到bos項目

4.編寫DecidedzoneAction.assigncustomerstodecidedzone

/**
* 遠程調用CRM服務,將客戶關聯到定區
* @return
*/
public String assigncustomerstodecidedzone(){
customerServiceProxy.assigncustomerstodecidedzone(model.getId(), customerIds);
return NONE;
}



029 定區關聯客戶功能 - bos