1.功能需求
点击交易的复选框,然后点击修改按钮。进行数据的页面填充
1.流程图
1.代码实现
TranMapper
/**
* 根据id查询交易
*/
Tran selectTranDetailFortranId(String tranId);
<!-- Tran selectTranDetailFortranId(String tranId);-->
<select id="selectTranDetailFortranId" resultMap="BaseResultMap">
select t.id,t.owner,t.money,t.name,t.expected_date,c.name as customer_id,t.stage,t.type,t.source,t.activity_id,t.contacts_id,t.description,t.contact_summary,t.next_contact_time
from tbl_tran t
left join tbl_customer c on t.customer_id = c.id
where t.id=#{tranId}
</select>
TranService
/**
* 交易:根据id查询
*/
Tran queryTranDetailFortranId(String tranId);
@Override
public Tran queryTranDetailFortranId(String tranId) {
return tranMapper.selectTranDetailFortranId(tranId);
}
TranController
/**
* 修改交易·:加载信息
*/
@RequestMapping("/workbench/transaction/searchTranForId.do")
public String searchTranForId(HttpServletRequest request,String tranId){
// 调用service加载下拉数据
Tran tran = tranService.queryTranDetailFortranId(tranId);
Activity activity = activityService.queryActivityForDetail(tran.getActivityId());
Contacts contacts = contactsService.queryContactsById(tran.getContactsId());
List<User> userList = userService.queryAllUsers();
List<DicValue> sourceList = dicValueService.queryDicValueByTypeCode("source");
List<DicValue> stageList = dicValueService.queryDicValueByTypeCode("stage");
List<DicValue> typeList = dicValueService.queryDicValueByTypeCode("transactionType");
// 保存在请求域中
request.setAttribute("userList", userList);
request.setAttribute("sourceList", sourceList);
request.setAttribute("stageList", stageList);
request.setAttribute("typeList", typeList);
request.setAttribute("tran", tran);
request.setAttribute("activity", activity);
request.setAttribute("contacts", contacts);
return "workbench/transaction/edit";
}
前端edit.jsp 进行填充
<script type="text/javascript">
// 初始化参数
$("#edit-id").val('${tran.id}');
$("#edit-transactionOwner").val('${tran.owner}');
$("#edit-amountOfMoney").val('${tran.money}');
$("#edit-transactionName").val('${tran.name}');
$("#edit-expectedClosingDate").val('${tran.expectedDate}');
$("#edit-accountName").val('${tran.customerId}');
$("#edit-transactionStage").val('${tran.stage}');
$("#edit-transactionType").val('${tran.type}');
$("#edit-clueSource").val('${tran.source}');
$("#edit-activitySrc").val('${activity.name}');
$("#activityId").val('${tran.activityId}');
$("#edit-contactsName").val('${contacts.fullname}');
$("#contactsId").val('${tran.contactsId}');
$("#edit-describe").val('${tran.description}');
$("#edit-contactSummary").val('${tran.contactSummary}');
$("#edit-nextContactTime").val('${tran.nextContactTime}');
</script>
2.功能需求
填写修改表单后,进行表单非空验证,实现交易修改。如果阶段进行了变更,则插入交易阶段历史的表格里面。
2.流程图
2.代码实现
tranMapper
/**
* 根据id更新
*/
int updateTranDetailFortranId(Tran tran);
<!-- int updateTranDetailFortranId(Map<String,Object>map);-->
<update id="updateTranDetailFortranId">
update tbl_tran
set owner = #{owner,jdbcType=CHAR},
money = #{money,jdbcType=VARCHAR},
name = #{name,jdbcType=VARCHAR},
expected_date = #{expectedDate,jdbcType=CHAR},
customer_id = #{customerId,jdbcType=CHAR},
stage = #{stage,jdbcType=VARCHAR},
type = #{type,jdbcType=VARCHAR},
source = #{source,jdbcType=VARCHAR},
activity_id = #{activityId,jdbcType=CHAR},
contacts_id = #{contactsId,jdbcType=CHAR},
edit_by = #{editBy,jdbcType=VARCHAR},
edit_time = #{editTime,jdbcType=CHAR},
description = #{description,jdbcType=VARCHAR},
contact_summary = #{contactSummary,jdbcType=VARCHAR},
next_contact_time = #{nextContactTime,jdbcType=CHAR}
where id = #{id,jdbcType=CHAR}
</update>
tranService
/**
* 交易:根据id修改
*/
int editTranDetailFortranId(Map<String,Object>map);
@Override
public int editTranDetailFortranId(Map<String, Object> map) {
Tran tran = new Tran();
User user = (User) map.get(Contants.SESSION_USER);
tran.setId((String) map.get("id"));
tran.setOwner((String) map.get("owner"));
tran.setMoney((String) map.get("money"));
tran.setName((String) map.get("name"));
tran.setExpectedDate((String) map.get("expectedDate"));
tran.setStage((String) map.get("stage"));
tran.setType((String) map.get("type"));
tran.setSource((String) map.get("source"));
tran.setActivityId((String) map.get("activityId"));
tran.setContactsId((String) map.get("contactsId"));
tran.setEditBy(user.getId());
tran.setEditTime(DateUtils.formateDateTime(new Date()));
tran.setDescription((String) map.get("description"));
tran.setContactSummary((String) map.get("contactSummary"));
tran.setNextContactTime((String) map.get("nextContactTime"));
// 查找setCustomerId
String customerId = customerMapper.selectCustomerForNameToCustomerId((String) map.get("customerName"));
if (customerId == null || "".equals(customerId)) {
// 没有此客户,需要添加
Customer customer = new Customer();
customer.setId(UUIDUtils.getUUID());
customer.setName((String) map.get("customerName"));
customer.setCreateBy(user.getId());
customer.setCreateTime(DateUtils.formateDateTime(new Date()));
customer.setContactSummary(tran.getContactSummary());
customer.setNextContactTime(tran.getNextContactTime());
customer.setDescription(tran.getDescription());
// 保存客户
customerMapper.insertCustomer(customer);
tran.setCustomerId(customer.getId());
} else {
// 有这个客户 直接存放查询出的id
tran.setCustomerId(customerId);
}
// 查询阶段是否存在
Map<String, Object> tranHistoryMap = new HashMap<>();
tranHistoryMap.put("stage", tran.getStage());
tranHistoryMap.put("tranId", tran.getId());
TranHistory retTran = tranHistoryMapper.selectTranHistoryByStageAndTranId(tranHistoryMap);
// 此阶段不存在,那么创建记录
if (retTran == null || "".equals(retTran)) {
TranHistory tranHistory = new TranHistory();
tranHistory.setId(UUIDUtils.getUUID());
tranHistory.setStage(tran.getStage());
tranHistory.setMoney(tran.getMoney());
tranHistory.setExpectedDate(tran.getExpectedDate());
tranHistory.setCreateTime(DateUtils.formateDateTime(new Date()));
tranHistory.setCreateBy(user.getId());
tranHistory.setTranId(tran.getId());
tranHistoryMapper.insert(tranHistory);
}
return tranMapper.updateTranDetailFortranId(tran);
}
tranController
/**
* 修改交易·:保存信息
*/
@RequestMapping("/workbench/transaction/EidtTranForId.do")
public @ResponseBody Object editTran(HttpSession session,String id,String owner,String money,String name,String expectedDate,String customerName,String stage,String type,String source,String activityId,String contactsId,String description,String contactSummary,String nextContactTime){
// 封装参数
Map<String,Object> map = new HashMap<>();
User user = (User) session.getAttribute(Contants.SESSION_USER);
map.put("id",id);
map.put("owner",owner);
map.put("money",money);
map.put("name",name);
map.put("expectedDate",expectedDate);
map.put("customerName",customerName);
map.put("stage",stage);
map.put("type",type);
map.put("source",source);
map.put("activityId",activityId);
map.put("contactsId",contactsId);
map.put("description",description);
map.put("contactSummary",contactSummary);
map.put("nextContactTime",nextContactTime);
map.put("nextContactTime",nextContactTime);
map.put(Contants.SESSION_USER,user);
// 调用service,实现交易修改
ReturnObject returnObject = new ReturnObject();
try {
int ret = tranService.editTranDetailFortranId(map);
if (ret > 0) {
//成功
returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
} else {
//失败
returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
returnObject.setMessage("系统错误,稍后再试");
}
} catch (Exception e) {
e.printStackTrace();
//失败
returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
returnObject.setMessage("系统错误,稍后再试");
}
return returnObject;
}
edit.jsp
$("#saveEidtTranBtn").click(function () {
// 收集参数
var owner = $("#edit-transactionOwner").val();
var money = $.trim($("#edit-amountOfMoney").val());
var name = $.trim($("#edit-transactionName").val());
var expectedDate = $("#edit-expectedClosingDate").val();
var customerName = $.trim($("#edit-accountName").val());
var stage = $("#edit-transactionStage").val();
var type = $("#edit-transactionType").val();
var contactsId = $("#contactsId").val();
var source = $("#edit-clueSource").val();
var activityId = $("#activityId").val();
var description = $.trim($("#edit-describe").val());
var contactSummary = $.trim($("#edit-contactSummary").val());
var nextContactTime = $("#edit-nextContactTime").val();
var id = $("#edit-id").val();
// 表单验证
if (owner == "") {
alert("所有者不能为空");
return;
}
if (name == "") {
alert("名称不能为空");
return;
}
if (expectedDate == "") {
alert("预计成交日期不能为空");
return;
}
if (customerName == "") {
alert("客户名称不能为空");
return;
}
if (stage == "") {
alert("阶段不能为空");
return;
}
// 正则表达式验证成本:金额只能为非负整数
var regExp = /^(([1-9]\d*)|0)$/;
if (!regExp.test(money)) {
alert("金额要大于0");
return;
}
// 发送请求
$.ajax({
url: 'workbench/transaction/EidtTranForId.do',
data: {
owner: owner,
money: money,
name: name,
expectedDate: expectedDate,
customerName: customerName,
stage: stage,
type: type,
contactsId: contactsId,
source: source,
activityId: activityId,
description: description,
contactSummary: contactSummary,
nextContactTime: nextContactTime,
id:id
},
type: 'post',
dataType: 'json',
success: function (data) {
if (data.code == "1") {
// 成功跳转
window.location.href = 'workbench/transaction/index.do';
} else {
alert(data.message);
}
}
});
});