?? onlineordermgrimpl.java
字號:
package com.yuanchung.sales.service.service.impl;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
import com.yuanchung.sales.config.ConfigMgr;
import com.yuanchung.sales.dao.service.ServiceOrderDAO;
import com.yuanchung.sales.exception.ApplicationException;
import com.yuanchung.sales.model.customer.Customer;
import com.yuanchung.sales.model.customer.CustomerContact;
import com.yuanchung.sales.model.service.Category;
import com.yuanchung.sales.model.service.CustAccount;
import com.yuanchung.sales.model.service.ServiceOrder;
import com.yuanchung.sales.model.service.ServiceOrderFile;
import com.yuanchung.sales.model.user.User;
import com.yuanchung.sales.service.service.OnlineOrderMgr;
import com.yuanchung.sales.struts.service.form.ServiceOrderForm;
import com.yuanchung.sales.util.Constants;
import com.yuanchung.sales.util.DateTimeTool;
import com.yuanchung.sales.util.OnlineSessionMgr;
import com.yuanchung.sales.util.Page;
import com.yuanchung.sales.util.Upload;
import com.yuanchung.sales.vo.service.XMLFileVo;
public class OnlineOrderMgrImpl implements OnlineOrderMgr {
private static Logger logger = Logger.getLogger(OnlineOrderMgrImpl.class);
private ServiceOrderDAO serviceOrderDAO;
public ServiceOrderDAO getServiceOrderDAO() {
return serviceOrderDAO;
}
public void setServiceOrderDAO(ServiceOrderDAO serviceOrderDAO) {
this.serviceOrderDAO = serviceOrderDAO;
}
// 在線獲取客戶服務(wù)單
public Page getMyOnlineOrder(String state, int flag, int customerId,
String path, int currentPage, int rowsPerPage) {
Page page = null;
List<ServiceOrder> serviceOrderList = null;
int count = 0;
try{
serviceOrderList = serviceOrderDAO.getMyOnlineOrder(state, flag, customerId, path, currentPage, rowsPerPage);
count = serviceOrderDAO.getMyOnlineOrderCount(state, flag, customerId);
page = new Page(path,count,currentPage,rowsPerPage,serviceOrderList);
}catch(Exception e){
logger.error(e.getMessage());
throw new ApplicationException(e.getMessage());
}
return page;
}
// 更新服務(wù)單
public void updateOnlineOrder(ServiceOrder serviceOrder) {
serviceOrderDAO.attachDirty(serviceOrder);
}
public void saveSOFile(ServiceOrderFile soFile) {
serviceOrderDAO.saveSOFile(soFile);
}
// 保存服務(wù)單
public void saveServiceOrder(ServiceOrder serviceOrder) {
serviceOrderDAO.save(serviceOrder);
}
// 查看用戶信息
public String[] viewUser(int id) {
User user = null;
try {
user = serviceOrderDAO.getUserById(id);
System.out.println("在線查看用戶的名字是:" + user.getFamilyName());
String[] userInfo = { user.getFamilyName(), user.getPhone(),
user.getEmail(), user.getPosition() };
return userInfo;
} catch (Exception re) {
re.printStackTrace();
throw new ApplicationException(re.getMessage());
}
}
// 保存在線填的問題單
public void newOnlineOrder(ServiceOrderForm serviceOrderForm,
HttpServletRequest request, ActionForm form) {
ServiceOrder serviceOrder = new ServiceOrder();
serviceOrder.setSerialNum(serviceOrderForm.getSerialNum());
serviceOrder.setContent(serviceOrderForm.getContent());
serviceOrder.setState(Constants.SERVICE_ORDER_STATE_NO_ASSIGN);// 默認(rèn)為未處理服務(wù)單
int flag = Constants.SERVICE_ORDER_OUTER;
serviceOrder.setFlag(flag);// online在線填的單
serviceOrder.setCreateTime(new Date());
CustAccount custAccount = OnlineSessionMgr
.getCustAccountSession(request);
Customer customer = custAccount.getCustomer();
if (customer != null) {
serviceOrder.setCustomer(customer);
}
ServiceOrder justSaveSO = null;
// 聯(lián)系人的保存操作
String contactIdStr = request.getParameter("contactId");
try {
if (contactIdStr != null && !"".equals(contactIdStr)) {
serviceOrder.setCustomerContact(serviceOrderDAO
.getContactById(Integer.parseInt(contactIdStr)));
} else {
CustomerContact customerContact = new CustomerContact();
customerContact.setName(request
.getParameter("otherContactName"));
customerContact.setMobilePhone(request
.getParameter("mobilePhone"));
customerContact.setEmail(request.getParameter("email"));
customerContact.setFax(request.getParameter("fax"));
customerContact.setComPhone(request.getParameter("comPhone"));
// 設(shè)置聯(lián)系人的flag = 4為臨時聯(lián)系人
customerContact.setFlag(Constants.CASUAL_FLAG);
customerContact.setInDate(new Date());
customerContact.setCustomer(customer);
customerContact.setUser(customer.getUser());
customerContact.setModifyManId(0);//客戶創(chuàng)建的聯(lián)系人
serviceOrderDAO.saveCustomerContact(customerContact);
CustomerContact justSaveCC = serviceOrderDAO.getCustomerContactByMaxId();//取得剛保存的聯(lián)系人
if(justSaveCC!=null){
serviceOrder.setCustomerContact(justSaveCC);
}
}
// end
serviceOrderDAO.save(serviceOrder);
logger.debug("保存成功了");
justSaveSO = serviceOrderDAO.getServiceOrderByMaxId();
logger.debug("保存成功了");
} catch (Exception re) {
logger.debug(re.getMessage());
throw new ApplicationException(re.getMessage());
}
// 上傳
logger.debug("開始上傳");
Set<ServiceOrderFile> serviceOrderFileSet = new HashSet<ServiceOrderFile>();
Hashtable fileh = form.getMultipartRequestHandler().getFileElements();// form是傳過來的參數(shù)
for (Iterator it = fileh.keySet().iterator(); it.hasNext();) {
ServiceOrderFile soFile = new ServiceOrderFile();
String key = (String) it.next();
FormFile formfile = (FormFile) fileh.get(key);// 獲取一個FormFile
String fileName = "";
if (formfile.getFileName() != null
&& !"".equals(formfile.getFileName())) {
String path = ConfigMgr.getInstance().getXmlFileVo().getPath();// 取得上傳的路徑
logger.debug(formfile.getFileName());
logger.debug("要上傳的路徑是:" + path);
fileName = Upload.upload(formfile, path);
soFile.setFileName(fileName);
soFile.setPath(path);
if (justSaveSO != null) {
soFile.setServiceOrder(justSaveSO);
}
try {
serviceOrderDAO.saveSOFile(soFile);
} catch (Exception re) {
logger.debug(re.getMessage());
throw new ApplicationException(
Constants.ADD_SERVICEORDER_EXCEPTION);
}
}
}
logger.debug("上傳的數(shù)量=====" + serviceOrderFileSet.size());
}
//判斷要上傳的文件是否附合條件
public String checkFile(ActionForm form){
String resultMsg = null;
XMLFileVo XMLFileVo = ConfigMgr.getInstance().getXmlFileVo();
int size = XMLFileVo.getSize();
logger.debug("系統(tǒng)限制的大小為:"+size);
String [] expandNames = XMLFileVo.getExpandNames().split(",");
List<String> lString = new ArrayList<String>();
for(int i=0;i<expandNames.length;i++){
lString.add(expandNames[i]);
}
Hashtable fileh = form.getMultipartRequestHandler().getFileElements();// form是傳過來的參數(shù)
for (Iterator it = fileh.keySet().iterator(); it.hasNext();) {
String key = (String) it.next();
FormFile formfile = (FormFile) fileh.get(key);// 獲取一個FormFile
logger.debug("上傳的文件大小是:"+formfile.getFileSize());
if(formfile.getFileSize()>size){
resultMsg = Constants.OVER_SIZE_FILE_EXCEPTION;
break;
}
if (formfile.getFileName() != null && !"".equals(formfile.getFileName())) {
String expandName = formfile.getFileName().substring(formfile.getFileName().lastIndexOf('.')+1);
if(!lString.contains(expandName)){
resultMsg = Constants.NO_UPLOAD_FILE_EXCEPTION;
break;
}
}
}
return resultMsg;
}
//取最大ID的問題單
public ServiceOrder getServiceOrderByMaxId(){
return serviceOrderDAO.getServiceOrderByMaxId();
}
//通過客戶ID查找聯(lián)系人
public List<CustomerContact> getContactByCustomerId(int customerId){
return serviceOrderDAO.getContactByCustomerId(customerId);
}
// 編輯在線問題單
public void editOnlineOrder(ActionForm form, HttpServletRequest request) {
ServiceOrderForm serviceOrderForm = (ServiceOrderForm) form;
int id = 0;
String idStr = request.getParameter("id");
if (idStr != null && !"".equals(idStr)) {
id = Integer.parseInt(idStr);
}
CustAccount custAccount = OnlineSessionMgr
.getCustAccountSession(request);
Customer customer = custAccount.getCustomer();
ServiceOrder serviceOrder = null;
try {
serviceOrder = serviceOrderDAO.findById(id);
serviceOrder.setContent(serviceOrderForm.getContent());
logger.debug("打印聯(lián)系人ID" + serviceOrderForm.getContactId());
// 判斷聯(lián)系人ID是否為0,0表示用戶有指定非系統(tǒng)存在的聯(lián)系人
if (serviceOrderForm.getContactId() == 0) {
CustomerContact customerContact = new CustomerContact();
customerContact.setName(request
.getParameter("otherContactName"));
customerContact.setMobilePhone(request
.getParameter("mobilePhone"));
customerContact.setEmail(request.getParameter("email"));
customerContact.setFax(request.getParameter("fax"));
customerContact.setComPhone(request.getParameter("comPhone"));
// 設(shè)置聯(lián)系人的flag = 4為臨時聯(lián)系人
customerContact.setFlag(Constants.CASUAL_FLAG);
customerContact.setInDate(new Date());
if(customer!=null){
customerContact.setCustomer(customer);
}
customerContact.setUser(customer.getUser());
customerContact.setModifyManId(0);// 客戶創(chuàng)建的聯(lián)系人
serviceOrderDAO.saveCustomerContact(customerContact);
CustomerContact justSaveCC = serviceOrderDAO
.getCustomerContactByMaxId();// 取得剛保存的聯(lián)系人
if(justSaveCC!=null){
serviceOrder.setCustomerContact(justSaveCC);
}
}else if(serviceOrder.getCustomerContact().getId()!=serviceOrderForm.getContactId()){//客戶有重新選擇聯(lián)系人
serviceOrder.setCustomerContact(serviceOrderDAO.getContactById(serviceOrderForm.getContactId()));
}
} catch (Exception re) {
logger.debug(re.getMessage());
throw new ApplicationException(re.getMessage());
}
/****************附件信息的修改************/
Set<ServiceOrderFile> ServiceOrderFileSet = serviceOrder.getServiceOrderFiles();
Iterator<ServiceOrderFile> sofIterator = ServiceOrderFileSet.iterator();
if(ServiceOrderFileSet.size()>0){
while(sofIterator.hasNext()){
ServiceOrderFile serviceOrderFile = sofIterator.next();
//判斷該文件是否要刪除
String delFlag = request.getParameter("delFlag"+serviceOrderFile.getId());
if(delFlag!=null){
serviceOrderDAO.deleteSOFileById(serviceOrderFile.getId());//刪除數(shù)據(jù)庫文件
//同時刪除硬盤上的文件
String path = ConfigMgr.getInstance().getXmlFileVo().getPath();// 取得上傳的路徑
String file = path+serviceOrderFile.getFileName();
if(new File(file).exists()){
new File(file).delete();
logger.debug("刪除硬盤上的文件成功");
}
}
}
}
// 上傳
Hashtable fileh = form.getMultipartRequestHandler().getFileElements();// form是傳過來的參數(shù)
for (Iterator it = fileh.keySet().iterator(); it.hasNext();) {
ServiceOrderFile soFile = new ServiceOrderFile();
String key = (String) it.next();
FormFile formfile = (FormFile) fileh.get(key);// 獲取一個FormFile
String fileName = "";
if (formfile.getFileName() != null
&& !"".equals(formfile.getFileName())) {
String path = ConfigMgr.getInstance().getXmlFileVo().getPath();// 取得上傳的路徑
logger.debug(formfile.getFileName());
logger.debug("要上傳的路徑是:" + path);
fileName = Upload.upload(formfile, path);
soFile.setFileName(fileName);
soFile.setPath(path);
soFile.setServiceOrder(serviceOrder);
try {
saveSOFile(soFile);
} catch (RuntimeException re) {
logger.debug(re.getMessage());
throw new ApplicationException(
Constants.ADD_SERVICEORDER_EXCEPTION);
}
}
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -