package com.cim.idm.mwmsextend.customer.service.service;

import java.sql.Timestamp;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.cim.idm.wmspackage.customer.CustomerServiceProxy;
import com.cim.idm.wmspackage.customer.management.data.Customer;
import com.cim.idm.wmspackage.customer.management.data.CustomerKey;
import com.cim.idm.framework.exception.IDMFrameDBErrorSignal;
import com.cim.idm.framework.exception.ext.NotFoundSignal;

public class CustomerServiceImpl {
	
	private static Log log = LogFactory.getLog(CustomerServiceImpl.class);
	
	public Customer create(String customerNo, String customerName, String description , String destination, 
			String telephone, String fax,String incharge, String address, String email, String lastEventUser){
		
		CustomerKey keyInfo = new CustomerKey();
		keyInfo.setCustomerNO(customerNo);
		
		Customer customer = new Customer();
		customer.setKey(keyInfo);
		customer.setCustomerName(customerName);
		customer.setDescription(description);
		customer.setDestination(destination);
		customer.setTel(telephone);
		customer.setFax(fax);
		customer.setIncharge(incharge);
		customer.setAddress(address);
		customer.setEmail(email);
		Timestamp lastEventTime = new Timestamp(System.currentTimeMillis());
		customer.setLastEventTime(lastEventTime);
		customer.setLastEventUser(lastEventUser);
/*zl
 * boolean isOk = CustomerServiceProxy.getCustomerService().create(customer);
		if(isOk)
			return customer;
		else 
			return null;*/
		CustomerServiceProxy.getCustomerService().create(customer);//zl
		return customer;//zl
	}
	
	public Customer modify(String customerNo, String customerName, String description,
			String destination, String telephone, String fax,String incharge , String address, String email, String lastEventUser)
	{
		CustomerKey customerKey = new CustomerKey();
		customerKey.setCustomerNO(customerNo);
		
		Customer customer = new Customer();
		
		Timestamp lastEventTime = new Timestamp(System.currentTimeMillis());
		
		customer.setKey(customerKey);
		customer.setCustomerName(customerName);
		customer.setDescription(description);
		customer.setDestination(destination);
		customer.setTel(telephone);
		customer.setFax(fax);
		customer.setIncharge(incharge);
		customer.setAddress(address);
		customer.setEmail(email);
		customer.setLastEventTime(lastEventTime);
		customer.setLastEventUser(lastEventUser);
		
		try {
			CustomerServiceProxy.getCustomerService().update(customer);
		} catch (IDMFrameDBErrorSignal ex) {
			log.error(ex,ex);
			throw ex;
		} catch (NotFoundSignal ex){
			log.error(ex,ex);
			throw ex;
		}
		return customer;
	}
	
	
	public void remove(String customerNo,String customerName)
	{
		CustomerKey key = new CustomerKey();
		
		key.setCustomerNO(customerNo);
		Customer customer = new Customer();
		customer.setKey(key);
		
		//CustomerServiceProxy.getCustomerService().remove(customer);//zl
		CustomerServiceProxy.getCustomerService().remove(key);//zl
	}
}
