package com.cim.idm.mwmsextend.suppliercustomer.service;
import java.sql.Timestamp;
import java.util.Date;

import com.cim.idm.wmspackage.customer.management.CustomerService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.cim.idm.framework.IDMFrameServiceProxy;
import com.cim.idm.framework.data.EventInfo;
import com.cim.idm.framework.exception.IDMFrameDBErrorSignal;
import com.cim.idm.framework.exception.ext.NotFoundSignal;
import com.cim.idm.framework.util.time.TimeStampUtil;
import com.cim.idm.wmsextend.generic.errorHandler.CustomException;
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.wmspackage.supplier.SupplierServiceProxy;
import com.cim.idm.wmspackage.supplier.management.data.Supplier;
import com.cim.idm.wmspackage.supplier.management.data.SupplierKey;

public class SupplierCustomerServiceImpl {
	
	private static Log log = LogFactory.getLog(SupplierCustomerServiceImpl.class);
	
	public Supplier create(String supplierNo, String supplierName, String description , String telephone, String nationalNo,
			String fax,String incharge, String address, String email, String city, String postCode, String lastEventUser)
			throws Exception {
		if(supplierNo.equals(""))
		{
			throw new CustomException("PK-0001","","");
		}
		SupplierKey keyInfo = new SupplierKey();
		keyInfo.setSupplierNO(supplierNo);
		
		Supplier supplier = new Supplier();
		
		supplier.setKey(keyInfo);
		supplier.setSupplierName(supplierName);
		supplier.setDescription(description);
		supplier.setNationalNO(nationalNo);
		supplier.setTel(telephone);
		supplier.setFax(fax);
		supplier.setIncharge(incharge);
		supplier.setAddress(address);
		supplier.setEmail(email);
		supplier.setCity(city);
		supplier.setPostCode(postCode);
		Timestamp lastEventTime = new Timestamp(System.currentTimeMillis());
		supplier.setLastEventTime(lastEventTime);
		supplier.setLastEventUser(lastEventUser);
		boolean isOk = SupplierServiceProxy.getSupplierService().create(supplier);
		if(isOk)
			return supplier;
		else 
			return null;
		
	}
	
	public Supplier modify(String supplierNo, String supplierName, String description ,String telephone,String nationalNo,
			String fax,String incharge, String address, String email, String city, String postCode, String lastEventUser)
			throws Exception{
		
		if(supplierNo.equals(""))
		{
			throw new CustomException("PK-0001","","");
		}
		SupplierKey keyInfo = new SupplierKey();
		keyInfo.setSupplierNO(supplierNo);
		
		Supplier supplier = new Supplier();
		
		supplier.setKey(keyInfo);
		supplier.setSupplierName(supplierName);
		supplier.setDescription(description);
		supplier.setTel(telephone);
		supplier.setFax(fax);
		supplier.setNationalNO(nationalNo);
		supplier.setIncharge(incharge);
		supplier.setAddress(address);
		supplier.setEmail(email);
		supplier.setCity(city);
		supplier.setPostCode(postCode);
		Timestamp lastEventTime = new Timestamp(System.currentTimeMillis());
		supplier.setLastEventTime(lastEventTime);
		supplier.setLastEventUser(lastEventUser);
		try{
			SupplierServiceProxy.getSupplierService().update(supplier);
			}catch(IDMFrameDBErrorSignal ex){
				log.error(ex,ex);
				throw ex;
			}catch(NotFoundSignal ex){
				log.error(ex,ex);
				throw ex;
			}
		
			return supplier;
		
	}
	
	public void remove(String supplierNo)
	{
		SupplierKey key = new SupplierKey();
		
		key.setSupplierNO(supplierNo);
		
		Supplier supplier = new Supplier();
		supplier.setKey(key);
		
		SupplierServiceProxy.getSupplierService().remove(supplier);
	}
	
	public void createCustomer(EventInfo eventInfo,String customerNO,String customerName,String description,String destination,String address,
			String tel,String fax,String incharge,String email)
	{
		
		CustomerKey key = new CustomerKey();
		key.setCustomerNO(customerNO);
		
		Customer customer = new Customer();
		customer.setKey(key);
		customer.setCustomerName(customerName);
		customer.setDescription(description);
		customer.setDestination(destination);
		customer.setAddress(address);
		customer.setTel(tel);
		customer.setFax(fax);
		customer.setIncharge(incharge);
		customer.setEmail(email);
		
		Timestamp lastEventTime = new Timestamp(System.currentTimeMillis());
		
		customer.setLastEventTime(lastEventTime);
		customer.setLastEventUser(eventInfo.getEventUser());

		CustomerServiceProxy.getCustomerService().create(customer);
		
	}
	
	public void modifyCustomer(EventInfo eventInfo,String customerNO,String customerName,String description,String destination,String address,
			String tel,String fax,String incharge,String email)
	{
		
		CustomerKey key = new CustomerKey();
		key.setCustomerNO(customerNO);
		
		Customer customer = new Customer();
		customer.setKey(key);
		customer.setCustomerName(customerName);
		customer.setDescription(description);
		customer.setDestination(destination);
		customer.setAddress(address);
		customer.setTel(tel);
		customer.setFax(fax);
		customer.setIncharge(incharge);
		customer.setEmail(email);
		
		Timestamp lastEventTime = new Timestamp(System.currentTimeMillis());
		
		customer.setLastEventTime(lastEventTime);
		customer.setLastEventUser(eventInfo.getEventUser());
						
		CustomerServiceProxy.getCustomerService().modify(customer);
		
	}
	
	public void removeCustmoer(String customerNO)
	{
		CustomerKey key = new CustomerKey();
		
		key.setCustomerNO(customerNO);
			
		CustomerServiceProxy.getCustomerService().remove(key);
	}
	
	public void  updateUnUsualInfo(String eventName,String materialSpec,String suppliername,String qty
			,String actNo,String eventUser){
		Date datetime = TimeStampUtil.getCurrentTimestamp();
		String sql = "update BS_UNUSUALINFO set FINISHFLAG='Y',MATERIALQUANTITY=?,LASTEVENTUSER=?,FINISHTIME=? WHERE MATERIALSPECNAME=? "
                    + "AND SUPPLIERNAME=? AND RECEIVEACTNO=? ";
		Object[] binds = new Object[] { qty,eventUser,datetime, materialSpec, suppliername,actNo};
		try {
			IDMFrameServiceProxy.getSqlTemplate().update(sql, binds);
		} catch (Exception e) {
		}		 
	}
	
	public void  DeleteUnUsualInfo(String eventName,String materialSpec,String suppliername,String qty
			,String actNo,String eventUser){
		Date datetime = TimeStampUtil.getCurrentTimestamp();
		String sql = "delete BS_UNUSUALINFO  WHERE MATERIALSPECNAME=? "
                    + "AND SUPPLIERNAME=? AND RECEIVEACTNO=? ";
		Object[] binds = new Object[] { qty,eventUser,datetime, materialSpec, suppliername,actNo};
		try {
			IDMFrameServiceProxy.getSqlTemplate().update(sql, binds);
		} catch (Exception e) {
		}		 
	}
	

	public void  updateUnUsualStockOut(String eventName,String actNo,String eventUser){
		Date datetime = TimeStampUtil.getCurrentTimestamp();
		String sql = "UPDATE MATERIALRECEIVEACT  SET LASTEVENTCOMMENT='WMSTOMDC',LASTEVENTUSER=? ,LASTEVENTNAME=? ,LASTEVENTTIME=?"
                     + "WHERE RECEIVEACTNO=? ";
		Object[] binds = new Object[] { eventUser,eventName,datetime,actNo};
		try {
			IDMFrameServiceProxy.getSqlTemplate().update(sql, binds);
		} catch (Exception e) {
		}		 
	}
}
