package com.dev.swms.server.service.base.pojo;

import java.io.IOException;
import java.io.InputStream;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import com.dev.swms.server.model.base.BillType;
import com.dev.swms.server.model.base.CustomerToComp;
import com.dev.swms.server.model.base.Structure;
import com.dev.swms.server.model.base.TransSite;
import com.dev.swms.server.model.base.TransSiteToComp;
import com.dev.swms.server.model.base.TypeOfBill;
import com.dev.swms.server.model.base.UserOrganize;
import com.dev.swms.server.model.receiving.ASN;
import com.dev.swms.server.model.receiving.ASNStatus;
import com.dev.swms.server.model.shipping.PickTicket;
import com.dev.swms.server.service.base.StructureManager;
import com.mysql.jdbc.Connection;
import com.dev.energy.server.model.EntityFactory;
import com.dev.energy.server.model.security.User;
import com.dev.energy.server.service.pojo.DefaultBaseManager;
import com.dev.energy.server.web.security.UserHolder;

public class DefaultStructureManager extends DefaultBaseManager implements
		StructureManager {
	public void saveOrganization(Structure organization1) {
		commonDao.store(organization1);
		
	}

	public void inActionOrganization(List<Long> orgIds) {
		for(Long orgId : orgIds) {
			Structure org = this.load(Structure.class, orgId);
			org.setDisabled(true);
		}
	}
	public void resumptionOrganization(List<Long> orgIds) {
		for(Long orgId : orgIds) {
			Structure org = this.load(Structure.class, orgId);
			org.setDisabled(false);
		}
	}
	/**
	 * 删除了货主与收货人关系对象后，就减少了一对关联
	 * @param ids : CustomerToComp ids 
	 */
	public void cutRelationCustomerToComp(List<Long> ids){
		for (Long id : ids) {
			CustomerToComp temp = this.load(CustomerToComp.class, id);
			this.delete(temp);
		}
	}
	/**
	 * 将收货人和货主组合为货主-收货人关系
	 * @param ids : 收货人ids
	 * @param id  : 货主id
	 */
	public void addCustomerToCompRelation(List<Long> ids,Long id){
		Structure company = this.load(Structure.class, id);
		for (Long id2 : ids) {
			Structure customer = this.load(Structure.class, id2);
			CustomerToComp relation = new CustomerToComp();
			relation.setCustomer(customer);
			relation.setCompany(company);
			relation.setBeCustomer(true);
			relation.setBeSupplier(false);
			this.store(relation);
		}
	}
	public void addTransSiteToCompRelation(List<Long> ids,Long id){
		Structure company = this.load(Structure.class, id);
		for (Long id2 : ids) {
			TransSite trans = this.load(TransSite.class, id2);
			TransSiteToComp relation = new TransSiteToComp();
			relation.setTransSite(trans);
			relation.setCompany(company);
			this.commonDao.store(relation);
		}
	}
	public void cutRelationTransSiteToComp(List<Long> ids){
		for (Long id : ids) {
			TransSiteToComp temp = this.load(TransSiteToComp.class, id);
			this.delete(temp);
		}
	}

	public void addSupplierToCompRelation(List<Long> ids, Long id) {
		Structure company = this.load(Structure.class, id);
		for (Long id2 : ids) {
			Structure supplier = this.load(Structure.class, id2);
			CustomerToComp relation = new CustomerToComp();
			relation.setCustomer(supplier);
			relation.setCompany(company);
			relation.setBeCustomer(false);
			relation.setBeSupplier(true);
			this.store(relation);
		}
		
	}

	public void cutRelationSupplierToComp(List<Long> ids) {
		for (Long id : ids) {
			CustomerToComp temp = this.load(CustomerToComp.class, id);
			this.delete(temp);
		}
		
	}

	//添加供应商
	@Override
	public void saveSupplier(Structure organization) {
			if(organization.isNew()){
			organization.setBeSupplier(true);
			organization.setDisabled(false);
			organization.setCreatDate(new Date());
			organization.setCreatPeople(UserHolder.getUser().getName());
			}
		commonDao.store(organization);
	}

	//删除供应商	
	@Override
	public void deleteSupplier(List<Long> ids) {
		for (Long id : ids) {
			Structure s = this.load(Structure.class, id);
			this.delete(s);
		}
		
	}

	@Override
	public void structureInvalid(List<Long> ids) {
		for (Long id : ids) {
			Structure s = this.load(Structure.class, id);
			s.setDisabled(true);
		}
		
	}

	@Override
	public void structurerestore(List<Long> ids) {
		for (Long id : ids) {
			Structure s = this.load(Structure.class, id);
			s.setDisabled(false);
		}
		
	}
	
	//冻结
	@Override
	public void structureFrozen(List<Long> ids) {
		for (Long id : ids) {
			Structure s = this.load(Structure.class, id);
			s.setBeFrozen(true);
		}
		
	}
	
	//解冻
	@Override
	public void structureThaw(List<Long> ids) {
		for (Long id : ids) {
			Structure s = this.load(Structure.class, id);
			s.setBeFrozen(false);
		}
		
	}

	@Override
	public void storeUserOragin(Long orgId, List<Long> Ids) {
		Structure org = this.commonDao.load(Structure.class,orgId);
		if(org.isBeCompany()==true) {
			for(Long id : Ids){
				User user = this.commonDao.load(User.class,id);
				UserOrganize userOrganize=new UserOrganize();
				userOrganize.setOrganization(org);
				userOrganize.setUser(user);
				userOrganize.setType("2");
				commonDao.store(userOrganize);
			}
		}
		
	}

	@Override
	public void deleteUserOragin(List<Long> ids) {
		for(Long id : ids){
			UserOrganize group = this.commonDao.load(UserOrganize.class,id);
			this.delete(group);
		
		}
		
	}
	
	public void storePickTicket(Long id,String des) {
//			commonDao.store(pickTicket);
		System.out.println("join....");
		PickTicket pt=this.commonDao.get(PickTicket.class, id);
		pt.setDescription(des);
		this.commonDao.store(pt);
	}
	
	public void returnAsn(List<Long> ids) {
		//如果是测试，则不需要打开接口回传
		String ghjk=getPropertiesParm("ghjk.bz");
		if(ghjk.equals("OPEN")){
			Connection con=getConnection();
			ResultSet rs=null;
			try {
				for(Long id:ids){
					ASN asn=this.commonDao.get(ASN.class, id);
					Statement stmt1 =con.createStatement();
					ResultSet rs1 = stmt1.executeQuery("select ID  "
							+ " from dec_trans_s_mid where "
							+ " LISTBILLNO ='"+asn.getCode()+"'");
					Boolean temp=Boolean.TRUE;
					while(rs1.next()){
						//存在则update
						Long update_id=rs1.getLong("ID");
						temp=Boolean.FALSE;
			            	Statement stmt2 =con.createStatement();
				        	stmt2.executeUpdate("update dec_trans_s_mid set ARRDATE_FACT=now(),DT_READ=null where id ="+update_id);
				        	stmt2.close();
					}
					if(temp){
						String sql="insert into dec_trans_s_mid (DT_INSERT,LISTBILLNO,ARRDATE_FACT) "
								+ " values (now(),?,now())";
						PreparedStatement pstmt = con.prepareStatement(sql);
						pstmt.setString(1, asn.getCode());
		//				pstmt.setDate(2, (java.sql.Date) new Date()); 
						pstmt.executeUpdate();
						pstmt.close();
					}
					asn.setStatus(ASNStatus.ACTIVE);
					asn.setReceiveDateEnd(new Date());
					asn.setFromName(UserHolder.getUser().getName()+"==="+new Date());
					this.commonDao.store(asn);
					stmt1.close();
					rs1.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				 try {
					 if(rs!=null){
						 rs.close();
					 }
					 if(con!=null){
						 con.close();
					 }
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		        
			}
		}else{
			for(Long id:ids){
				ASN asn=this.commonDao.get(ASN.class, id);
				asn.setStatus(ASNStatus.ACTIVE);
				asn.setReceiveDateEnd(new Date());
				asn.setFromName(asn.getTemperature()+"=="+UserHolder.getUser().getName()+"==="+new Date());
				this.commonDao.store(asn);
			}
		}
		
	}
	
	 public String getPropertiesParm(String obj){
		   String parm = "";
		   Properties prop = new Properties();   
		   InputStream in = this.getClass().getResourceAsStream("/hibernate.properties");   
		   try {
			    prop.load(in);
			    parm = prop.getProperty(obj).trim();
		   } catch (IOException e) {
		    // TODO Auto-generated catch block
		    e.printStackTrace();
		   }   
		   return parm;
		  }
	 
	 public Connection getConnection() {
			String driverClassName ="com.mysql.jdbc.Driver";
			String url = "jdbc:mysql://139.196.165.224:53306/jr_interface?useUnicode=true&amp;characterEncoding=UTF-8&amp;useSSL=false";
			String usename = "guanheng";
			String password = "jrxt123456";
			Connection con=null;
			try {
				Class.forName(driverClassName);
				try {
					 con= (Connection) DriverManager.getConnection(url,usename,password);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				
			}
			return con;
		}

}
