package nc.bs.gl.addvoucher;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import nc.bs.dao.BaseDAO;
import nc.bs.dao.DAOException;
import nc.bs.ncsendnc.util.MyPostFile;
import nc.jdbc.framework.processor.MapListProcessor;
import nc.vo.jcom.xml.XMLUtil;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDateTime;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class QueryDataMakeVoucher {
	
	 private BaseDAO baseDao = null;
	  public BaseDAO getBaseDao() {
		    if (this.baseDao == null) {
		      this.baseDao = new BaseDAO();
		    }
		    return this.baseDao;
		  }
	
public void makevoucher() throws BusinessException{
	String nowts = new UFDateTime(System.currentTimeMillis()).toString();
	List<Map<String, Object>> dztzlist = getdztz(nowts.substring(0, 10));
		if (dztzlist != null && dztzlist.size() > 0) {
			for (Map<String, Object> data : dztzlist) {
				String corpname=String.valueOf(data.get("oppunitname"));//对方单位名称
				String oppbankaccount=String.valueOf(data.get("oppbankaccount"));//对方单位银行账户
				String oppunitname=String.valueOf(data.get("oppunitname"))+"定时归集至现金池账户";//对方单位名称拼接备注
				String custcode=String.valueOf(data.get("custcode"));
				String moneyy=String.valueOf(data.get("moneyy"));
				String pk_informer=String.valueOf(data.get("pk_informer"));//到账通知主键
				Document doc = getXmlData(custcode,moneyy,oppunitname,corpname,oppbankaccount);
//				String sendurl = "http://172.16.10.175:8088/service/XChangeServlet?account=01&groupcode=00";// 测试环境
				String sendurl = "http://172.16.0.177:9088/service/XChangeServlet?account=01&groupcode=00";// 正式环境
				MyPostFile.sendMyDocument(doc, sendurl, "UTF-8");
				updateSign(pk_informer);
			}
		}
}

private Document getXmlData(String custcode,String money,String oppunitname,String corpname,String oppbankaccount) throws BusinessException {
	// TODO 自动生成的方法存根
	String nowts = new UFDateTime(System.currentTimeMillis()).toString();	
//	String nowts ="2020-01-06 17:24:00";
	Document doc = XMLUtil.getDocumentBuilder().newDocument();
	// 不显示standalone="no"
	doc.setXmlStandalone(true);
	Element ufinterface = doc.createElement("ufinterface");
	ufinterface.setAttribute("sender", "addVoucher");//发送发编码
	ufinterface.setAttribute("account", "01");//账套编码
	ufinterface.setAttribute("groupcode", "00");
	ufinterface.setAttribute("billtype", "vouchergl");
	
	
		Element assjie = doc.createElement("ass");
		Element assdai = doc.createElement("ass");	
	    Element assitemjie = doc.createElement("item");
	     //assitemjie创建子元素,添加数据值
	        Element pk_Checktype=doc.createElement("pk_Checktype");   pk_Checktype.setTextContent("0011"); assitemjie.appendChild(pk_Checktype);
	        Element pk_Checkvalue=doc.createElement("pk_Checkvalue");   pk_Checkvalue.setTextContent("310066179018170265589"); assitemjie.appendChild(pk_Checkvalue);
//	        Element pk_Checkvalue=doc.createElement("checkvaluecode");   pk_Checkvalue.setTextContent("310066179018170265589"); assitemjie.appendChild(pk_Checkvalue);
	        assjie.appendChild(assitemjie);	    
	    Element assitemdai = doc.createElement("item");
	     //assitemdai创建子元素,添加数据值
	    if("上海天华建筑设计有限公司".equals(corpname)){
	    	 Element pk_Checktypedai=doc.createElement("pk_Checktype");   pk_Checktypedai.setTextContent("0011"); assitemdai.appendChild(pk_Checktypedai);
			 Element pk_Checkvaluedai=doc.createElement("pk_Checkvalue");   pk_Checkvaluedai.setTextContent(oppbankaccount); assitemdai.appendChild(pk_Checkvaluedai);
	    }else{
	        Element pk_Checktypedai=doc.createElement("pk_Checktype");   pk_Checktypedai.setTextContent("0004"); assitemdai.appendChild(pk_Checktypedai);
			Element pk_Checkvaluedai=doc.createElement("pk_Checkvalue");   pk_Checkvaluedai.setTextContent(custcode); assitemdai.appendChild(pk_Checkvaluedai);
//	        Element pk_Checkvaluedai=doc.createElement("checkvaluecode");   pk_Checkvaluedai.setTextContent(daibanknum); assitemdai.appendChild(pk_Checkvaluedai);
	    }
			assdai.appendChild(assitemdai);
	
		Element details = doc.createElement("details");
		Element itemjie = doc.createElement("item");
		 //itemjie创建子元素,添加数据值
			Element detailindex=doc.createElement("detailindex");   detailindex.setTextContent("1"); itemjie.appendChild(detailindex);
			Element accsubjcode=doc.createElement("accsubjcode");   accsubjcode.setTextContent("1002");  itemjie.appendChild(accsubjcode);
			Element explanation=doc.createElement("explanation");   explanation.setTextContent(oppunitname);  itemjie.appendChild(explanation);
			Element excrate2=doc.createElement("excrate2");   excrate2.setTextContent("1");  itemjie.appendChild(excrate2);
			Element excrate3=doc.createElement("excrate3");   excrate3.setTextContent("1");  itemjie.appendChild(excrate3);
			Element excrate4=doc.createElement("excrate4");   excrate4.setTextContent("1");  itemjie.appendChild(excrate4);
			Element debitamount=doc.createElement("debitamount");   debitamount.setTextContent(money);  itemjie.appendChild(debitamount);
			Element groupdebitamount=doc.createElement("groupdebitamount");   groupdebitamount.setTextContent(money);  itemjie.appendChild(groupdebitamount);
			Element globaldebitamount=doc.createElement("globaldebitamount");   globaldebitamount.setTextContent(money);  itemjie.appendChild(globaldebitamount);
			Element localdebitamount=doc.createElement("localdebitamount");   localdebitamount.setTextContent(money);  itemjie.appendChild(localdebitamount);
			Element pk_currtype=doc.createElement("pk_currtype");   pk_currtype.setTextContent("CNY");  itemjie.appendChild(pk_currtype);
			Element pk_accasoa=doc.createElement("pk_accasoa");   pk_accasoa.setTextContent("1002");  itemjie.appendChild(pk_accasoa);
			itemjie.appendChild(assjie);
			details.appendChild(itemjie);
		
		Element itemdai = doc.createElement("item");
		//itemjdai创建子元素,添加数据值
			Element detailindexdai=doc.createElement("detailindex");   detailindexdai.setTextContent("2"); itemdai.appendChild(detailindexdai);
			if("上海天华建筑设计有限公司".equals(corpname)){
				Element accsubjcodedai=doc.createElement("accsubjcode");   accsubjcodedai.setTextContent("1002");itemdai.appendChild(accsubjcodedai);
			}else{
				Element accsubjcodedai=doc.createElement("accsubjcode");   accsubjcodedai.setTextContent("22410401");itemdai.appendChild(accsubjcodedai);
			}
			Element explanationdai=doc.createElement("explanation");   explanationdai.setTextContent(oppunitname);itemdai.appendChild(explanationdai);
			Element excrate2dai=doc.createElement("excrate2");   excrate2dai.setTextContent("1"); itemdai.appendChild(excrate2dai); 
			Element excrate3dai=doc.createElement("excrate3");   excrate3dai.setTextContent("1"); itemdai.appendChild(excrate3dai);
			Element excrate4dai=doc.createElement("excrate4");   excrate4dai.setTextContent("1"); itemdai.appendChild(excrate4dai);
			Element creditamount=doc.createElement("creditamount");   creditamount.setTextContent(money);  itemdai.appendChild(creditamount);
			Element groupcreditamount=doc.createElement("groupcreditamount");   groupcreditamount.setTextContent(money);  itemdai.appendChild(groupcreditamount);
			Element localcrditamount=doc.createElement("localcreditamount");   localcrditamount.setTextContent(money);  itemdai.appendChild(localcrditamount);
			Element globaleditamount=doc.createElement("globalcreditamount");   globaleditamount.setTextContent(money);  itemdai.appendChild(globaleditamount);
			Element pk_currtypedai=doc.createElement("pk_currtype");   pk_currtypedai.setTextContent("CNY");itemdai.appendChild(pk_currtypedai);			
			if("上海天华建筑设计有限公司".equals(corpname)){
				Element pk_accasoadai=doc.createElement("pk_accasoa");   pk_accasoadai.setTextContent("1002");itemdai.appendChild(pk_accasoadai);
			}else{
				Element pk_accasoadai=doc.createElement("pk_accasoa");   pk_accasoadai.setTextContent("22410401");itemdai.appendChild(pk_accasoadai);
			}
		    itemdai.appendChild(assdai);
		    details.appendChild(itemdai);	
		
	    Element voucher = doc.createElement("voucher");
		Element voucher_head = doc.createElement("voucher_head");	
			 //voucher_head创建子元素,添加数据值
			if("上海天华建筑设计有限公司".equals(corpname)){
				Element vouchertype=doc.createElement("pk_vouchertype");   vouchertype.setTextContent("03"); voucher_head.appendChild(vouchertype);	
			}else {
				Element vouchertype=doc.createElement("pk_vouchertype");   vouchertype.setTextContent("04"); voucher_head.appendChild(vouchertype);
			}
			Element system=doc.createElement("pk_system");   system.setTextContent("GL");  voucher_head.appendChild(system);
			Element year=doc.createElement("year");   year.setTextContent(nowts.substring(0, 4));  voucher_head.appendChild(year);
			Element voucherkind=doc.createElement("voucherkind");   voucherkind.setTextContent("0");  voucher_head.appendChild(voucherkind);
			Element accountingbook=doc.createElement("pk_accountingbook");   accountingbook.setTextContent("TH0101-0002");  voucher_head.appendChild(accountingbook);
			Element discardflag=doc.createElement("discardflag");   discardflag.setTextContent("N");  voucher_head.appendChild(discardflag);
			Element period=doc.createElement("period");   period.setTextContent(nowts.substring(5, 7));  voucher_head.appendChild(period);
			Element prepareddate=doc.createElement("prepareddate");   prepareddate.setTextContent(nowts);  voucher_head.appendChild(prepareddate);
			Element pk_prepared=doc.createElement("pk_prepared");   pk_prepared.setTextContent("liyupeng");  voucher_head.appendChild(pk_prepared);
			Element billmaker=doc.createElement("billmaker");   billmaker.setTextContent("liyupeng");  voucher_head.appendChild(billmaker);
			Element signflag=doc.createElement("signflag");   signflag.setTextContent("N");  voucher_head.appendChild(signflag);
			Element pk_org=doc.createElement("pk_org");   pk_org.setTextContent("000101");  voucher_head.appendChild(pk_org);
			Element pk_group=doc.createElement("pk_group");   pk_group.setTextContent("00");  voucher_head.appendChild(pk_group);
			voucher_head.appendChild(details);
			voucher.appendChild(voucher_head);
	
		ufinterface.appendChild(voucher);
	    doc.appendChild(ufinterface);
//	    String dataXmlStr = chgXml2Str(doc,"gb2312");
	return doc;
}

//查询到账通知消息
private List<Map<String, Object>> getdztz(String date) throws BusinessException {
	String sql ="select bd_customer.code custcode, cmp_informer.moneyy moneyy ,cmp_informer.oppbankaccount oppbankaccount"+ 
                    ", cmp_informer.oppunitname oppunitname ,cmp_informer.pk_informer pk_informer \n" +
					" from cmp_informer cmp_informer\n" + 
					" left join bd_customer bd_customer\n" + 
					" on  cmp_informer.pk_oppunit = bd_customer.pk_customer\n" + 
					"where (1 = 1 and cmp_informer.pk_acc_sub = '1001C010000000004T6U'\n" + 
					"and cmp_informer.direction = 'receivemoney'\n" + 
					" and cmp_informer.memo = '定时归集')\n" + 
					"  and nvl(cmp_informer.dr, 0) = 0"+
					" and( cmp_informer.vouchflag is null or cmp_informer.vouchflag=1) "+
					"   and (infodate like '"+date+"%')";
/*	String sql="select cmp_informer.oppbankaccount dfbanknum, cmp_informer.moneyy moneyy\n" +
					"  from cmp_informer cmp_informer  where nvl(cmp_informer.dr, 0) = 0 and cmp_informer.pk_informer='0001A8100000007GSAEZ'";*/
    List<Map<String,Object>>  dztzList = new ArrayList<Map<String,Object>>();
    try {
    	dztzList = (List<Map<String, Object>>)getBaseDao().executeQuery(sql, new MapListProcessor());
    } catch (DAOException e) {
      throw new BusinessException("根据到账通知日期查询数据发生异常！");
    }
    return dztzList;
  }

//更新制证标志
private void updateSign(String pk_informer) throws DAOException {
	// TODO 自动生成的方法存根
	String sql ="update cmp_informer set vouchflag ='0' where pk_informer ='"+pk_informer+"'";
	getBaseDao().executeUpdate(sql);
}


/**作者：  时间：
 * <H3>方法作用</H3>XML转成XMLSTR<BR>
 * @param doc
 * @return
 * @throws Exception
 */
public String chgXml2Str(Document doc, String encoding) throws BusinessException {
	try {
		TransformerFactory tf = TransformerFactory.newInstance();
		Transformer t = tf.newTransformer();
		t.setOutputProperty("encoding", encoding);
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		t.transform(new DOMSource(doc), new StreamResult(bos));
		String xmlStr = bos.toString();
		return xmlStr;
	} catch (Exception e) {
		throw new BusinessException();
	}
}

}
