ManifestService.java 42.0 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
package com.agent.service.agent;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.agent.entity.agent.*;
import com.agent.repository.system.HzWaybillRepository;
import com.agent.repository.system.SDCargoNameRepository;
import com.agent.repository.system.SDCargoTypeRepository;
import com.agent.repository.system.SecurityDeclarationRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.agent.entity.Constant;
import com.agent.repository.agent.ManifestBillRepository;
import com.agent.repository.agent.ManifestCommodityRepository;
import com.agent.repository.agent.ManifestContainerRepository;
import com.agent.repository.agent.ManifestRepository;
import com.agent.repository.agent.PreparesecondaryRepository;
import com.agent.service.BasicService;
import com.agent.xml.bill.ApplicableLogisticsAllowanceChargeXml;
import com.agent.xml.bill.ApplicableTradeCurrencyExchangeXml;
import com.agent.xml.common.ContentXml;
import com.agent.xml.common.IDXml;
import com.agent.xml.common.NameXml;
import com.agent.xml.common.XmlHead;
import com.agent.xml.deliveryInfo.AssociatedReferenceDocumentXml;
import com.agent.xml.deliveryInfo.ConsigneePartyXml;
import com.agent.xml.deliveryInfo.ConsolidationInformationXml;
import com.agent.xml.deliveryInfo.DefinedTradeContactXml;
import com.agent.xml.deliveryInfo.DeliveryInfoAssociatedPartyXml;
import com.agent.xml.deliveryInfo.DeliveryInfoConsigneePartyXml;
import com.agent.xml.deliveryInfo.DeliveryInfoConsignorPartyXML;
import com.agent.xml.deliveryInfo.DirectTelephoneCommunicationXml;
import com.agent.xml.deliveryInfo.EventTimeXml;
import com.agent.xml.deliveryInfo.FaxCommunicationxML;
import com.agent.xml.deliveryInfo.FinalDestinationLocationXml;
import com.agent.xml.deliveryInfo.HandlingInstructionsXml;
import com.agent.xml.deliveryInfo.HouseWaybillXml;
import com.agent.xml.deliveryInfo.IncludedAccountingNoteXml;
import com.agent.xml.deliveryInfo.IncludedCustomsNoteXml;
import com.agent.xml.deliveryInfo.IncludedHouseConsignmentItemXml;
import com.agent.xml.deliveryInfo.IncludedHouseConsignmentXml;
import com.agent.xml.deliveryInfo.IncludedMasterConsignmentItemXml;
import com.agent.xml.deliveryInfo.MasterConsignment;
import com.agent.xml.deliveryInfo.NatureIdentificationTransportCargoXml;
import com.agent.xml.deliveryInfo.OriginLocationXml;
import com.agent.xml.deliveryInfo.PostalStructuredAddressXml;
import com.agent.xml.deliveryInfo.ReportedStatusXml;
import com.agent.xml.deliveryInfo.SpecifiedLocationXml;
import com.agent.xml.deliveryInfo.SpecifiedLogisticsTransportMovementXml;
import com.agent.xml.deliveryInfo.TelexCommunicationXml;
import com.agent.xml.deliveryInfo.TransportContractDocumentXml;
import com.agent.xml.deliveryInfo.UtilizedLogisticsTransportEquipmentXml;
import com.agent.xml.manifestdeclare.DeclareAddressXml;
import com.agent.xml.manifestdeclare.DeclareBorderTransportMeansXml;
import com.agent.xml.manifestdeclare.DeclareConsigneeXml;
import com.agent.xml.manifestdeclare.DeclareConsignmentItemXml;
import com.agent.xml.manifestdeclare.DeclareConsignmentPackagingXml;
import com.agent.xml.manifestdeclare.DeclareConsignmentXml;
import com.agent.xml.manifestdeclare.DeclareLoadingLocationXml;
import com.agent.xml.manifestdeclare.DeclareManifestXml;
import com.agent.xml.manifestdeclare.DeclareMethodCodeXml;
import com.agent.xml.manifestdeclare.DeclarePreparemasterXmlBody;
import com.agent.xml.manifestdeclare.DeclareTransportContractDocumentXml;
import com.agent.xml.manifestdeclare.DeclareUnloadingLocationXml;
import com.agent.xml.manifestdeclare.DeclareXmlBody;
import com.agent.xml.manifestdeclare.ManifestAddressXml;
import com.agent.xml.manifestdeclare.ManifestAssociatedTransportDocumentXml;
import com.agent.xml.manifestdeclare.ManifestBorderTransportMeansXml;
import com.agent.xml.manifestdeclare.ManifestCarrierXml;
import com.agent.xml.manifestdeclare.ManifestConsigneeXml;
import com.agent.xml.manifestdeclare.ManifestConsignmentPackagingXml;
import com.agent.xml.manifestdeclare.ManifestConsignmentXml;
import com.agent.xml.manifestdeclare.ManifestConsignorXml;
import com.agent.xml.manifestdeclare.ManifestDeclarationXml;
import com.agent.xml.manifestdeclare.ManifestDeclareMetaXml;
import com.agent.xml.manifestdeclare.ManifestDeclareMsgXml;
import com.agent.xml.manifestdeclare.ManifestDesXml;
import com.agent.xml.manifestdeclare.ManifestFreightPaymentXml;
import com.agent.xml.manifestdeclare.ManifestLoadingLocationXml;
import com.agent.xml.manifestdeclare.ManifestOrgXml;
import com.agent.xml.manifestdeclare.ManifestTransportContractDocumentXml;
import com.agent.xml.manifestdeclare.ManifestUnloadingLocationXml;
import com.agent.xml.manifestdeclare.ManifestsAddressXml;
import com.agent.xml.manifestdeclare.MasterConsignmentXml;
import com.framework.util.StringUtils;
import com.google.common.base.Splitter;
import com.plugin.easyui.EasyPage;

import tools.Tools;

/**
 * Created by cohesion on 2017/4/19.
 */
@Service("manifestService")
public class ManifestService extends BasicService<ManifestEntity> {

	@Resource
	private ManifestRepository manifestRepository;

	@Resource
	private ManifestBillRepository billRepository;

	@Resource
	private ManifestCommodityRepository commodityRepository;

	@Resource
	private ManifestContainerRepository containerRepository;

	@Resource
	private PreparesecondaryRepository preparesecondaryRepository;

	@Resource
	private ManifestService manifestService;

	@Resource
	private WaybillReceiptService receiptService;

	@Resource
	private HzWaybillRepository hzWaybillRepository;

	@Resource
	private SecurityDeclarationRepository securityDeclarationRepository;

	@Resource
	private SDCargoNameRepository sdCargoNameRepository;

	@Resource
	private SDCargoTypeRepository sdCargoTypeRepository;

	/**
	 * 分页查询
	 *
	 * @param pageForm
	 *            分页对象
	 * @return 包含分页信息和数据的分页对象
	 */
	public Page<ManifestEntity> getPage(EasyPage<ManifestEntity> pageForm) {
		PageRequest pageRequest = buildPageRequest(pageForm);
		Specification<ManifestEntity> spec = buildSpecification(pageForm);

		Page<ManifestEntity> page = manifestRepository.findAll(spec, pageRequest);
		if (page != null && page.getContent() != null) {
			List<ManifestEntity> list = page.getContent();
			for (int i = 0; i < list.size(); i++) {
				ManifestEntity bean = list.get(i);
				WaybillReceiptEntity re = receiptService.findMain(bean.getWaybillnomaster());
				bean.setResponse_code(re != null ? re.getResponse_code() : "");
				bean.setResponse_text(re != null ? re.getResponse_text() : "");
			}
		}
		return page;
	}

	// 判断运单号是否存在(前提是该运单不存在)
	public boolean isExistsByWaybill(ManifestEntity manifestEntity) {
		if (manifestEntity.getId() == null) {
			return isExistsByWaybill(manifestEntity.getWaybillnomaster());
		}
		// 如果系统中已经存在了该运单,则执行返回true
		ManifestEntity me = manifestRepository.findOne(manifestEntity.getId());
		// 不存在
		if (me == null) {
			return isExistsByWaybill(manifestEntity.getWaybillnomaster());
		}
		return false;
	}

	// 判断该运单号是否已经存在
	public boolean isExistsByWaybill(String waybill) {
		List<ManifestEntity> list = manifestRepository.findByMawbNo(waybill, Tools.getUserId());
		if (list.size() > 0)
			return true;
		return false;
	}

	public ManifestEntity findOne(Long id) {
		return manifestRepository.findOne(id);
	}

	@Transactional
	public ManifestEntity save(ManifestEntity manifest) {
		manifest.setSave_time(System.currentTimeMillis());
		return manifestRepository.save(manifest);
	}

	@Transactional
	public void deleteAll(String ids) {
		List<String> list = Splitter.on(",").trimResults().omitEmptyStrings().splitToList(ids);
		for (String id : list) {

			ManifestEntity manifest = manifestRepository.findOne(Long.valueOf(id));
			if (manifest != null) {
				// 先删除子表信息
				preparesecondaryRepository.deleteAll(manifest.getId());
			}
			manifestRepository.delete(Long.valueOf(id));
		}
	}

	@Transactional
	public void trueDeleteAll(String ids) {
		try {
			List<String> list = Splitter.on(",").trimResults().omitEmptyStrings().splitToList(ids);
			for (String id : list) {
				//分单
				preparesecondaryRepository.deleteSub(Long.valueOf(id));

				ManifestEntity waybill = manifestRepository.findOne(Long.valueOf(id));
				//主单号
				String waybillnomaster = waybill.getWaybillnomaster();

				//删除主单和货主绑定关系
				hzWaybillRepository.deleteAlls(waybillnomaster);
				//主单
                ManifestEntity manifestEntity = new ManifestEntity();
                manifestEntity.setId(Long.valueOf(id));
                manifestRepository.delete(manifestEntity);
                //查看是否有安检申报   存在删除
                List<SecurityDeclarationEntity> waybill1 = securityDeclarationRepository.waybill(waybillnomaster);
                if (waybill1 != null){

                	for (SecurityDeclarationEntity securityDeclarationEntity: waybill1){

						securityDeclarationRepository.delete(securityDeclarationEntity.getId());
					}
				}

				sdCargoNameRepository.deletes(waybillnomaster);

                sdCargoTypeRepository.typeDelete(waybillnomaster);

			}
		}catch (Exception e){
			e.printStackTrace();
		}

	}

	public List<ManifestEntity> findByMawbNo(String mawbNo) {
		return manifestRepository.findByMawbNo(mawbNo, Tools.getUserId());
	}

	public List<ManifestEntity> findByManifestNo(String mawbNo) {
		return manifestRepository.findByMawbNo(mawbNo);
	}

	public ManifestEntity findFirst(String mawbNo) {
		List<ManifestEntity> list =  manifestRepository.findByMawbNo(mawbNo);
		if(list!=null&&!list.isEmpty()) {
			return list.get(0);
		}

		return null;
	}

	public List<ManifestEntity> queryAll() {
		return manifestRepository.queryAll();
	}

	public List<ManifestEntity> queryByUserId(Long user_id) {
		return manifestRepository.queryByUserId(user_id);
	}

	// 撤销主单
	public ManifestDeclareMsgXml sendBackoutXml(ManifestEntity manifest) {
		ManifestDeclareMsgXml dms = new ManifestDeclareMsgXml();
		ManifestDeclareMetaXml mdme = new ManifestDeclareMetaXml();
		mdme.setSndr("NDLR");
		mdme.setRcvr("");
		mdme.setSeqn(manifest.getWaybillnomaster());
		mdme.setDdtm(Constant.dateTimeFormatnumber.format(new Date()));
		mdme.setType("HYXX");
		mdme.setStyp("NDLR");

		dms.setMeta(mdme);
		DeclarePreparemasterXmlBody body = generateXml(manifest);
		dms.setDeclarepreparemasterxmlbody(body);
		dms.getDeclarepreparemasterxmlbody().getHead().setFunctionCode("3");

		return dms;
	}

	// 主单发送NDLR
	public ManifestDeclareMsgXml sendNDLRXml(ManifestEntity manifest) {
		// 同时生成2分报文 并且发送
		ManifestDeclareMsgXml dms = new ManifestDeclareMsgXml();
		ManifestDeclareMetaXml mdme = new ManifestDeclareMetaXml();
		mdme.setSndr("NDLR");
		mdme.setRcvr("");
		mdme.setSeqn(manifest.getWaybillnomaster());
		mdme.setDdtm(Constant.dateTimeFormatnumber.format(new Date()));
		mdme.setType("HYXX");
		mdme.setStyp("NDLR");
		dms.setMeta(mdme);
		DeclarePreparemasterXmlBody body = generateXml(manifest);
		dms.setDeclarepreparemasterxmlbody(body);
//		 System.out.println(JSONObject.toJSONString(dms));
		return dms;
	}

	// 主单发送DLTC
	public ManifestDeclareMsgXml sendDLCFXml(ManifestEntity manifest) {
		// 同时生成2分报文 并且发送
		ManifestDeclareMsgXml dms = new ManifestDeclareMsgXml();
		ManifestDeclareMetaXml mdme = new ManifestDeclareMetaXml();
		mdme.setSndr("NDLR");
		mdme.setRcvr("");
		mdme.setSeqn(manifest.getWaybillnomaster());
		mdme.setDdtm(Constant.dateTimeFormatnumber.format(new Date()));
		mdme.setType("HYXX");
		mdme.setStyp("DLTC");
		dms.setMeta(mdme);
		DeclarePreparemasterXmlBody body = generateXml(manifest);
		dms.setDeclarepreparemasterxmlbody(body);
		return dms;
	}

	// 主单公共报文
	private DeclarePreparemasterXmlBody generateXml(ManifestEntity manifest) {
		// 生成报文
		DeclarePreparemasterXmlBody body = new DeclarePreparemasterXmlBody();
		// 设置xml开头
		XmlHead head = new XmlHead();
		head.setMessageID("CN_MT2201_1P0_460470678920X_" + Constant.dateTimeFormatnumber.format(new Date()));
		head.setFunctionCode("9");
		head.setMessageType("MT2201MASTER");
		head.setSenderID("460470678920X_DXPENT0000460002_" + Constant.dateTimeFormatnumber.format(new Date()));
		head.setReceiverID("4604_" + Constant.dateTimeFormatnumber.format(new Date()));
		head.setSendTime(Constant.dateTimeFormatnumber.format(new Date()));
		head.setVersion("1.0");
		body.setHead(head);
		// 预配舱单
		ManifestDeclarationXml dec = new ManifestDeclarationXml();
		// 预配舱单主表信息
		// 承运人
		ManifestCarrierXml carr = new ManifestCarrierXml();
		carr.setId(manifest.getCarrier());
		dec.setCarrier(carr);

		// 起始站
		ManifestOrgXml mo = new ManifestOrgXml();
		mo.setId(manifest.getOriginatingstation());
		dec.setOrg(mo);

		// 目的站
		ManifestDesXml md = new ManifestDesXml();
		md.setId(manifest.getDestinationstation());
		dec.setDes(md);

		// 航班号-航班日期
		ManifestBorderTransportMeansXml mt = new ManifestBorderTransportMeansXml();
		mt.setJourneyid(manifest.getFlightno() + "/" + Constant.dateFormat.format(manifest.getFlightdate()));
		dec.setBordertransportmeans(mt);

		ManifestConsignmentXml mcs = new ManifestConsignmentXml();

		// 运单号
		ManifestTransportContractDocumentXml mac = new ManifestTransportContractDocumentXml();
		mac.setId(manifest.getWaybillnomaster());
		mcs.setTransportcontractdocument(mac);

		// 装载日期
		ManifestLoadingLocationXml ml = new ManifestLoadingLocationXml();
		ml.setId("CGO/4604");
		ml.setLoadingdate(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()));
		mcs.setLoadinglocation(ml);

		ManifestUnloadingLocationXml mu = new ManifestUnloadingLocationXml();
		mu.setId("CGO/4604");
		mcs.setUnloadinglocation(mu);
		mcs.setTransportsplitindicator("0");

		ManifestConsignmentPackagingXml mcon = new ManifestConsignmentPackagingXml();
		mcon.setQuantityquantity(manifest.getTotalpiece());
		mcs.setConsignmentpackaging(mcon);

		mcs.setTotalgrossmassmeasure(manifest.getTotalweight());
		mcs.setPrequantityquantity(manifest.getPreparetotalpiece());
		mcs.setPretotalgrossmassmeasure(manifest.getPreparetotalweight());
		mcs.setCustomsstatus(manifest.getCustomsstatus().toString());

		ManifestFreightPaymentXml mfp = new ManifestFreightPaymentXml();
		mfp.setMethodcode(manifest.getPaymode());
		mcs.setFreightpayment(mfp);

		mcs.setProductname(manifest.getProductname());
		// 装载日期与预配日期
		mcs.setPreparetime(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(manifest.getStowagedate()));
		mcs.setCustomscode(manifest.getCustomscode());
		mcs.setAgentname(manifest.getAgentman());
		mcs.setAgentCompany(manifest.getAgentcompany());
		mcs.setName_ofgoods(manifest.getProductname() != null ? manifest.getProductname() : "");
		mcs.setDelivery_station(manifest.getDelivery_station() != null ? manifest.getDelivery_station() : "");
		mcs.setUnnumber(manifest.getUnnumber() != null ? manifest.getUnnumber() : "");
		mcs.setCategory(manifest.getCategory() != null ? manifest.getCategory() : "");

		ManifestConsigneeXml consignee = new ManifestConsigneeXml();
		consignee.setName(manifest.getSh_company());

		ManifestAddressXml madd = new ManifestAddressXml();
		madd.setLine(manifest.getSh_address());
		madd.setCityname(manifest.getSh_city());
		madd.setCountrycode(manifest.getSh_country() != null ? manifest.getSh_country() : "");
		madd.setProvincecode(manifest.getSh_provincecode() != null ? manifest.getSh_provincecode() : "");
		madd.setProvincename(manifest.getSh_provincename() != null ? manifest.getSh_provincename() : "");
		madd.setZipcode(manifest.getSh_zipcode() != null ? manifest.getSh_zipcode() : "");
		madd.setSh_deltaname(manifest.getSh_deltaname() != null ? manifest.getSh_deltaname() : "");
		madd.setTelephone(manifest.getSh_telephone() != null ? manifest.getSh_telephone() : "");
		madd.setFax(manifest.getSh_fax() != null ? manifest.getSh_fax() : "");
		madd.setCneaeo(manifest.getCneaeo() != null ? manifest.getCneaeo() : "");
		madd.setCnecusid(manifest.getCnecusid() != null ? manifest.getCnecusid() : "");
		madd.setUnlodingcode(manifest.getUnlodingcode() != null ? manifest.getUnlodingcode() : "");

		consignee.setAddress(madd);
		mcs.setConsignee(consignee);

		ManifestConsignorXml consigor = new ManifestConsignorXml();
		consigor.setName(manifest.getCo_name());

		// 设置发货人货人信息
		ManifestsAddressXml consignorXml = new ManifestsAddressXml();
		consignorXml.setLine(manifest.getCo_address());
		consignorXml.setCityname(manifest.getCo_city());
		consignorXml.setCountrycode(manifest.getCo_country() != null ? manifest.getCo_country() : "");
		consignorXml.setZipcode(manifest.getCo_zipcode() != null ? manifest.getCo_zipcode() : "");
		consignorXml.setSh_deltaname(manifest.getCo_deltaname() != null ? manifest.getCo_deltaname() : "");
		consignorXml.setTelephone(manifest.getCo_telephone() != null ? manifest.getCo_telephone() : "");
		consignorXml.setFax(manifest.getCo_fax() != null ? manifest.getCo_fax() : "");
		consignorXml.setShpcusid(manifest.getShpcusid() != null ? manifest.getShpcusid() : "");
		consignorXml.setShpaeo(manifest.getShpaeo() != null ? manifest.getShpaeo() : "");

		consigor.setAddress(consignorXml);
		mcs.setConsignor(consigor);

		dec.setConsignment(mcs);
		body.setDeclaration(dec);
		return body;
	}

	public Object backoutpresenddlcfNdlrXml(PreparesecondaryEntity preparesecondary) {
		ManifestDeclareMsgXml dms = new ManifestDeclareMsgXml();
		ManifestDeclareMetaXml mdme = new ManifestDeclareMetaXml();
		mdme.setSndr("NDLR");
		mdme.setRcvr("");
		mdme.setSeqn(preparesecondary.getWaybillnosecondary());
		mdme.setDdtm(Constant.dateTimeFormatnumber.format(new Date()));
		mdme.setType("HYXX");
		mdme.setStyp("NDLR");
		dms.setMeta(mdme);
		DeclarePreparemasterXmlBody body = presendXml(preparesecondary);
		dms.setDeclarepreparemasterxmlbody(body);
		dms.getDeclarepreparemasterxmlbody().getHead().setFunctionCode("3");

		return dms;
	}

	// 分单发给NDLR
	public Object presenddlcfNdlrXml(PreparesecondaryEntity preparesecondary) {
		ManifestDeclareMsgXml dms = new ManifestDeclareMsgXml();
		ManifestDeclareMetaXml mdme = new ManifestDeclareMetaXml();
		mdme.setSndr("NDLR");
		mdme.setRcvr("");
		mdme.setSeqn(preparesecondary.getWaybillnosecondary());
		mdme.setDdtm(Constant.dateTimeFormatnumber.format(new Date()));
		mdme.setType("HYXX");
		mdme.setStyp("NDLR");
		dms.setMeta(mdme);
		DeclarePreparemasterXmlBody body = presendXml(preparesecondary);
		dms.setDeclarepreparemasterxmlbody(body);
		return dms;
	}

	// 分单发给DLTC
	public Object presenddlcfdlcfXml(PreparesecondaryEntity preparesecondary) {
		ManifestDeclareMsgXml dms = new ManifestDeclareMsgXml();
		ManifestDeclareMetaXml mdme = new ManifestDeclareMetaXml();
		mdme.setSndr("NDLR");
		mdme.setRcvr("");
		mdme.setSeqn(preparesecondary.getWaybillnosecondary());
		mdme.setDdtm(Constant.dateTimeFormatnumber.format(new Date()));
		mdme.setType("HYXX");
		mdme.setStyp("DLTC");
		dms.setMeta(mdme);
		DeclarePreparemasterXmlBody body = presendXml(preparesecondary);
		dms.setDeclarepreparemasterxmlbody(body);
		return dms;
	}

	// 分单报文
	public DeclarePreparemasterXmlBody presendXml(PreparesecondaryEntity pre) {
		// 生成报文
		DeclarePreparemasterXmlBody body = new DeclarePreparemasterXmlBody();
		// 设置xml开头
		XmlHead head = new XmlHead();
		head.setMessageID("CN_MT2201_1P0_460470678920X_" + Constant.dateTimeFormatnumber.format(new Date()));
		head.setFunctionCode("9");
		head.setMessageType("MT2201SECONDARY");
		head.setSenderID("460470678920X_DXPENT0000460002_" + Constant.dateTimeFormatnumber.format(new Date()));
		head.setReceiverID("4604");
		head.setSendTime(Constant.dateTimeFormatnumber.format(new Date()));
		head.setVersion("1.0");
		body.setHead(head);
		// 预配舱单
		ManifestDeclarationXml dec = new ManifestDeclarationXml();
		// 预配舱单主表信息
		// 承运人
		ManifestCarrierXml carr = new ManifestCarrierXml();
		carr.setId(pre.getCarrier());
		dec.setCarrier(carr);

		// 起始站
		ManifestOrgXml mo = new ManifestOrgXml();
		mo.setId(pre.getOriginatingstation());
		dec.setOrg(mo);

		// 目的站
		ManifestDesXml md = new ManifestDesXml();
		md.setId(pre.getDestinationstation());
		dec.setDes(md);

		// 航班号-航班日期
		ManifestBorderTransportMeansXml mt = new ManifestBorderTransportMeansXml();
		mt.setJourneyid(pre.getFlightno() + "/" + Constant.dateFormat.format(pre.getFlightdate()));
		dec.setBordertransportmeans(mt);

		ManifestConsignmentXml mcs = new ManifestConsignmentXml();

		// 主单号 分单号运单号
		ManifestTransportContractDocumentXml maif = new ManifestTransportContractDocumentXml();
		maif.setId(pre.getWaybillnomaster());
		mcs.setTransportcontractdocument(maif);

		// 分单号运单号
		ManifestAssociatedTransportDocumentXml mac = new ManifestAssociatedTransportDocumentXml();
		mac.setId(pre.getWaybillnosecondary());
		mcs.setAssociatedtransportdocument(mac);

		// 装载日期
		ManifestLoadingLocationXml ml = new ManifestLoadingLocationXml();
		ml.setId("CGO/4604");
		ml.setLoadingdate(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()));
		mcs.setLoadinglocation(ml);

		ManifestUnloadingLocationXml mu = new ManifestUnloadingLocationXml();
		mu.setId("CGO/4604");
		mcs.setUnloadinglocation(mu);
		mcs.setTransportsplitindicator("0");

		ManifestConsignmentPackagingXml mcon = new ManifestConsignmentPackagingXml();
		mcon.setQuantityquantity(pre.getTotalpiece());
		mcs.setConsignmentpackaging(mcon);

		mcs.setTotalgrossmassmeasure(pre.getTotalweight());
		mcs.setPrequantityquantity(pre.getPreparepiece());
		mcs.setPretotalgrossmassmeasure(pre.getPrepareweight());
		mcs.setCustomsstatus(pre.getCustomsstatus());

		ManifestFreightPaymentXml mfp = new ManifestFreightPaymentXml();
		mfp.setMethodcode(pre.getPaymode());
		mcs.setFreightpayment(mfp);

		mcs.setProductname(pre.getProductname());
		// mcs.setPreparetime(Constant.dateTimeFormatnumber.format(pre.getStowagedate()));
		mcs.setPreparetime(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(pre.getStowagedate()));
		mcs.setCustomscode(pre.getCustomscode());
		mcs.setAgentname(pre.getAgentman());
		mcs.setAgentCompany(pre.getAgentcompany());
		mcs.setName_ofgoods(pre.getProductname() != null ? pre.getProductname() : "");
		mcs.setDelivery_station(pre.getDelivery_station() != null ? pre.getDelivery_station() : "");
		mcs.setUnnumber(pre.getUnnumber() != null ? pre.getUnnumber() : "");
		mcs.setCategory(pre.getCategory() != null ? pre.getCategory() : "");

		ManifestConsigneeXml mcons = new ManifestConsigneeXml();
		mcons.setName(pre.getSh_name());

		ManifestAddressXml madd = new ManifestAddressXml();
		madd.setLine(pre.getSh_address());
		madd.setCityname(pre.getSh_city());
		madd.setProvincecode(pre.getSh_provincecode() != null ? pre.getSh_provincecode() : "");
		madd.setProvincename(pre.getSh_provincename() != null ? pre.getSh_provincename() : "");
		madd.setCountrycode(pre.getSh_country() != null ? pre.getSh_country() : "");
		madd.setZipcode(pre.getSh_zipcode() != null ? pre.getSh_zipcode() : "");
		madd.setSh_deltaname(pre.getSh_telephone() != null ? pre.getSh_deltaname() : "");
		madd.setTelephone(pre.getSh_telephone() != null ? pre.getSh_telephone() : "");
		madd.setFax(pre.getSh_fax() != null ? pre.getSh_fax() : "");
		madd.setCneaeo(pre.getCneaeo() != null ? pre.getCneaeo() : "");
		madd.setCnecusid(pre.getCnecusid() != null ? pre.getCnecusid() : "");
		madd.setUnlodingcode(pre.getUnlodingcode() != null ? pre.getUnlodingcode() : "");

		mcons.setAddress(madd);
		mcs.setConsignee(mcons);

		ManifestConsignorXml maci = new ManifestConsignorXml();
		maci.setName(pre.getCo_name());

		ManifestsAddressXml msadd = new ManifestsAddressXml();
		msadd.setLine(pre.getCo_address());
		msadd.setCityname(pre.getCo_city());
		msadd.setCountrycode(pre.getCo_country() != null ? pre.getCo_country() : "");
		msadd.setZipcode(pre.getCo_zipcode() != null ? pre.getCo_zipcode() : "");
		msadd.setSh_deltaname(pre.getCo_deltaname() != null ? pre.getCo_deltaname() : "");
		msadd.setTelephone(pre.getCo_telephone() != null ? pre.getCo_telephone() : "");
		msadd.setFax(pre.getCo_fax() != null ? pre.getCo_fax() : "");
		msadd.setShpcusid(pre.getShpcusid() != null ? pre.getShpcusid() : "");
		msadd.setShpaeo(pre.getShpaeo() != null ? pre.getShpaeo() : "");

		maci.setAddress(msadd);
		mcs.setConsignor(maci);

		dec.setConsignment(mcs);
		body.setDeclaration(dec);

		return body;
	}

	// 主单发送DLTC --开始
	public ManifestDeclareMsgXml sendDeliveryXml(ManifestEntity manifest) {
		ManifestDeclareMsgXml dms = new ManifestDeclareMsgXml();
		ManifestDeclareMetaXml mdme = new ManifestDeclareMetaXml();
		mdme.setSndr("NDLR");
		mdme.setRcvr("");
		mdme.setSeqn(manifest.getWaybillnomaster());
		mdme.setDdtm(Constant.dateTimeFormatnumber.format(new Date()));
		mdme.setType("HYXX");
		mdme.setStyp("DLTC");
		dms.setMeta(mdme);
		MasterConsignmentXml body = generateMaster(manifest);
		dms.setMasterconsignment(body);
		return dms;
	}

	private MasterConsignmentXml generateMaster(ManifestEntity manifest) {
		// 开始基本信息
		MasterConsignmentXml mas = new MasterConsignmentXml();
		mas.setId(manifest.getWaybillnomaster());
		mas.setWeight(manifest.getDe_weight());
		mas.setVolume(manifest.getDe_volume());
		mas.setPiece(manifest.getDe_number());

		// 发货人信息
		DeliveryInfoConsignorPartyXML deli = new DeliveryInfoConsignorPartyXML();
		deli.setName(manifest.getCo_name());

		PostalStructuredAddressXml pos = new PostalStructuredAddressXml();
		pos.setPostcodeCode(manifest.getCo_zipcode() != null ? manifest.getCo_zipcode() : "");
		pos.setStreetName(manifest.getCo_deltaname() != null ? manifest.getCo_deltaname() : "");
		pos.setCityName(manifest.getCo_city());
		pos.setCountryName(manifest.getCo_country());
		deli.setPostalstructuredaddress(pos);

		DefinedTradeContactXml deco = new DefinedTradeContactXml();
		deco.setPersonname(manifest.getCo_name());

		DirectTelephoneCommunicationXml dir = new DirectTelephoneCommunicationXml();
		dir.setCompleteNumber(manifest.getCo_telephone());
		deco.setDirecttelephonecommunication(dir);

		FaxCommunicationxML fax = new FaxCommunicationxML();
		fax.setCompletenumber(manifest.getCo_fax());
		deco.setFaxcommunication(fax);

		deli.setDefinedtradecontact(deco);
		mas.setConsignorparty(deli);

		// 收货人信息
		DeliveryInfoConsigneePartyXml coee = new DeliveryInfoConsigneePartyXml();
		coee.setName(manifest.getSh_name());

		PostalStructuredAddressXml ost = new PostalStructuredAddressXml();
		ost.setPostcodeCode(manifest.getSh_zipcode() != null ? manifest.getSh_zipcode() : "");
		ost.setStreetName(manifest.getSh_deltaname() != null ? manifest.getSh_deltaname() : "");
		ost.setCityName(manifest.getSh_city());
		ost.setCountryName(manifest.getSh_country());
		coee.setPostalstructuredaddress(ost);

		DefinedTradeContactXml dft = new DefinedTradeContactXml();
		dft.setPersonname(manifest.getSh_name());

		TelexCommunicationXml tel = new TelexCommunicationXml();
		tel.setCompleteNumber(manifest.getSh_telephone());
		dft.setTelexcommunication(tel);
		coee.setDefinedtradecontact(dft);

		mas.setConsigneeparty(coee);

		// 代理人信息
		DeliveryInfoAssociatedPartyXml dass = new DeliveryInfoAssociatedPartyXml();
		dass.setName(manifest.getAgentman());
		mas.setAssociatedparty(dass);

		// 起始地
		OriginLocationXml or = new OriginLocationXml();
		or.setId(manifest.getOriginatingstation());
		mas.setOriginlocation(or);

		// 目的地
		FinalDestinationLocationXml fi = new FinalDestinationLocationXml();
		fi.setId(manifest.getDestinationstation());
		mas.setFinaldestinationlocation(fi);

		// 订舱信息
		SpecifiedLogisticsTransportMovementXml spe = new SpecifiedLogisticsTransportMovementXml();
		spe.setStagecode(manifest.getFlightno());
		mas.setSpecifiedlogisticstransportmovement(spe);

		// 货物信息
		IncludedMasterConsignmentItemXml inc = new IncludedMasterConsignmentItemXml();
		inc.setSequencenumeric("1");
		inc.setGrossvolumemeasure(manifest.getDe_weight());
		inc.setGrossvolumemeasure(manifest.getDe_volume());
		inc.setPiecequantity(manifest.getDe_number());

		NatureIdentificationTransportCargoXml nat = new NatureIdentificationTransportCargoXml();
		nat.setIdentification(manifest.getProductname());
		inc.setNatureidentificationtransportcargo(nat);
		mas.setIncludedmasterconsignmentitem(inc);

		ReportedStatusXml re = new ReportedStatusXml();
		re.setReasonCode("SLI");
		re.setOperationCode("NEW");

		EventTimeXml even = new EventTimeXml();
		even.setDatetimetypecode(manifest.getFlightdate().toString());
		re.setEventTime(even);

		SpecifiedLocationXml spee = new SpecifiedLocationXml();
		spee.setName(manifest.getSh_city());
		re.setSpecifiedLocation(spee);

		mas.setReportedstatus(re);
		return mas;
	}
	// ---结束

	public Object sendPreXml(PreparesecondaryEntity preparesecondary) {
		ManifestDeclareMsgXml dms = new ManifestDeclareMsgXml();
		ManifestDeclareMetaXml mdme = new ManifestDeclareMetaXml();
		mdme.setSndr("NDLR");
		mdme.setRcvr("");
		mdme.setSeqn(preparesecondary.getWaybillnomaster());
		mdme.setDdtm(Constant.dateTimeFormatnumber.format(new Date()));
		mdme.setType("HYXX");
		mdme.setStyp("DLTC");
		dms.setMeta(mdme);
		HouseWaybillXml body = SendPreXml(preparesecondary);
		dms.setHousewaybillxml(body);
		return dms;
	}

	private HouseWaybillXml SendPreXml(PreparesecondaryEntity pre) {
		ManifestEntity man = manifestRepository.findOne(pre.getPreparemasterid());
		HouseWaybillXml ho = new HouseWaybillXml();
		MasterConsignment mas = new MasterConsignment();

		mas.setIncludedtaregrossweightmeasure(man.getDe_weight());
		mas.setTotalpiecequantity(man.getDe_number());

		// 主单总重量和 中件数
		TransportContractDocumentXml tra = new TransportContractDocumentXml();
		tra.setId(man.getWaybillnomaster());
		mas.setTransportcontractdocument(tra);

		// 主单起始站
		OriginLocationXml or = new OriginLocationXml();
		or.setName(man.getOriginatingstation());
		mas.setOriginlocation(or);

		// 主单目的站
		FinalDestinationLocationXml fina = new FinalDestinationLocationXml();
		fina.setName(man.getDestinationstation());
		mas.setFinalDestinationLocation(fina);

		IncludedHouseConsignmentXml inc = new IncludedHouseConsignmentXml();
		TransportContractDocumentXml tan = new TransportContractDocumentXml();
		tan.setId(pre.getWaybillnosecondary());
		inc.setTransportcontractdocument(tan);

		ConsolidationInformationXml cons = new ConsolidationInformationXml();
		cons.setGrossweightmeasure(pre.getDe_weight());
		cons.setPiecequantity(pre.getDe_number());
		inc.setConsolidationinformation(cons);
		mas.setIncludedhouseconsignment(inc);

		// 001代表 预付(yf) 002代表到付 (dy)
		if (pre.getPaymode() == "001") {
			inc.setTotalchargeprepaidindicator("true");
		} else {
			inc.setTotalchargeprepaidindicator("flase");
		}

		inc.setIncludedtaregrossweightmeasure(pre.getDe_weight());
		inc.setTotalpiecequantity(pre.getDe_number());
		inc.setSummarydescription(pre.getProductname());
		// 发货人信息
		com.agent.xml.deliveryInfo.ConsignorPartyXml cos = new com.agent.xml.deliveryInfo.ConsignorPartyXml();
		cos.setName(pre.getCo_name());

		PostalStructuredAddressXml add = new PostalStructuredAddressXml();
		add.setPostcodeCode(pre.getCo_zipcode());
		add.setCityName(pre.getCo_city());
		add.setCountryName(pre.getCo_country());
		cos.setPostalstructuredaddress(add);

		DefinedTradeContactXml def = new DefinedTradeContactXml();
		def.setPersonname(pre.getCo_name());

		DirectTelephoneCommunicationXml dir = new DirectTelephoneCommunicationXml();
		dir.setCompleteNumber(pre.getCo_telephone());
		def.setDirecttelephonecommunication(dir);

		FaxCommunicationxML fax = new FaxCommunicationxML();
		fax.setCompletenumber(pre.getCo_fax());
		def.setFaxcommunication(fax);
		cos.setDefinedtradecontact(def);
		inc.setConsignorparty(cos);
		// 收货人信息
		ConsigneePartyXml coi = new ConsigneePartyXml();
		coi.setName(pre.getSh_name());

		PostalStructuredAddressXml pta = new PostalStructuredAddressXml();
		pta.setCityName(pre.getSh_city());
		pta.setCountryName(pre.getSh_country());
		coi.setPostalstructuredaddress(pta);

		DefinedTradeContactXml defi = new DefinedTradeContactXml();
		defi.setPersonname(pre.getSh_name());

		DirectTelephoneCommunicationXml dre = new DirectTelephoneCommunicationXml();
		dre.setCompleteNumber(pre.getSh_telephone());
		defi.setDirecttelephonecommunication(dre);

		FaxCommunicationxML fac = new FaxCommunicationxML();
		fac.setCompletenumber(pre.getSh_fax());
		defi.setFaxcommunication(fac);
		coi.setDefinedtradecontact(defi);
		inc.setConsigneeparty(coi);

		// 填开代理人
		FinalDestinationLocationXml fin = new FinalDestinationLocationXml();
		fin.setName(pre.getAgentman());
		inc.setFinaldestinationlocation(fin);

		// 订舱信息
		SpecifiedLogisticsTransportMovementXml spc = new SpecifiedLogisticsTransportMovementXml();
		spc.setStagecode(pre.getFlightno());
		inc.setSpecifiedlogisticstransportmovement(spc);

		// UtilizedLogisticsTransportEquipmentXml
		UtilizedLogisticsTransportEquipmentXml uti = new UtilizedLogisticsTransportEquipmentXml();
		inc.setUtilizedlogisticstransportequipment(uti);

		// 处理代码
		HandlingInstructionsXml hand = new HandlingInstructionsXml();
		inc.setHandlingInstructions(hand);

		// 结算代码
		IncludedAccountingNoteXml inl = new IncludedAccountingNoteXml();
		inc.setIncludedaccountingnote(inl);

		// 包含海关信息
		IncludedCustomsNoteXml icl = new IncludedCustomsNoteXml();
		inc.setIncludedcustomsnote(icl);

		// 随附文件
		AssociatedReferenceDocumentXml aoc = new AssociatedReferenceDocumentXml();
		inc.setAssociatedreferencedocument(aoc);

		// ApplicableTradeCurrencyExchangeXml
		ApplicableTradeCurrencyExchangeXml ali = new ApplicableTradeCurrencyExchangeXml();
		inc.setApplicabletradecurrencyexchange(ali);

		// 货物服务代码
		ApplicableLogisticsAllowanceChargeXml abl = new ApplicableLogisticsAllowanceChargeXml();
		inc.setApplicablelogisticsallowancecharge(abl);

		// 货物信息
		IncludedHouseConsignmentItemXml icd = new IncludedHouseConsignmentItemXml();
		icd.setSequencenumeric("1");
		icd.setGrossweightmeasure(pre.getDe_weight());
		icd.setGrossvolumemeasure(pre.getDe_volume());
		icd.setPiecequantity(pre.getDe_number());

		NatureIdentificationTransportCargoXml nat = new NatureIdentificationTransportCargoXml();
		nat.setIdentification(pre.getProductname());
		icd.setNatureidentificationtransportcargo(nat);
		inc.setIncludedhouseconsignmentitem(icd);

		ho.setMasterconsignment(mas);
		return ho;
	}

	/**
	 * 给海关的报文;生成主单或者分担回执报文 by William Lynn
	 *
	 * @param obj
	 *            主单或者分担实体类对象
	 * @param type
	 *            包含报文功能代码
	 * @param changeReasonCode
	 *            修改原因代码
	 * @return
	 */
	public DeclareXmlBody generateWaybillReceiptXml(Object obj, WaybillReceiptType type, String changeReasonCode) {
		boolean isManifest = false;
		ManifestEntity me = null;
		PreparesecondaryEntity pe = null;
		if (obj instanceof ManifestEntity) {
			me = (ManifestEntity) obj;
			isManifest = true;
		} else if (obj instanceof PreparesecondaryEntity) {
			pe = (PreparesecondaryEntity) obj;
		}
		// 生成报文
		DeclareXmlBody body = new DeclareXmlBody();
		// 设置xml开头
		XmlHead head = new XmlHead();
		head.setMessageID("CN_MT2201_1P0_4604_70678920X_" + Constant.dateTimeFormatnumber.format(new Date()));
		head.setFunctionCode(String.valueOf(type.getValue()));
		head.setMessageType("MT2201");
		head.setSenderID("460470678920X_DXPENT0000460002_" + Constant.dateTimeFormatnumber.format(new Date()));
		head.setReceiverID("4604_" + Constant.dateTimeFormatnumber.format(new Date()));
		head.setSendTime(Constant.dateTimeFormatnumber.format(new Date()));
		head.setVersion("1.0");
		body.setHead(head);

		// 预配舱单
		DeclareManifestXml declare = new DeclareManifestXml();

		// 舱单传输人名称
		NameXml drp = new NameXml();
		drp.setName("70678920X");
		declare.setRepresentativePerson(drp);

		if (WaybillReceiptType.DELETE != type) {
			// 运输工具离境海关代码
			IDXml decox = new IDXml();
			decox.setId("CGO");
			declare.setExitCustomsOffice(decox);

			// 预配舱单主表信息
			// 承运人
			IDXml carr = new IDXml();
			carr.setId(isManifest ? me.getCarrier() : pe.getCarrier());
			declare.setCarrier(carr);
		}

		// 航班号-航班日期
		DeclareBorderTransportMeansXml mt = new DeclareBorderTransportMeansXml();
		String carrier = isManifest ? me.getCarrier() : pe.getCarrier();
		String flightno = isManifest ? me.getFlightno() : pe.getFlightno();
		String dateStr = Constant.dateFormat_.format(isManifest ? me.getFlightdate() : pe.getFlightdate());
		mt.setJourneyID(carrier + flightno + "/" + dateStr);
		// 运输方式代码
		mt.setTypeCode("4");
		declare.setBorderTransportMeans(mt);

		DeclareConsignmentXml consignment = new DeclareConsignmentXml();

		// 运单号
		DeclareTransportContractDocumentXml mac = new DeclareTransportContractDocumentXml();
		mac.setId(isManifest ? me.getWaybillnomaster() : pe.getWaybillnomaster());
		mac.setChangeReasonCode(changeReasonCode == null ? "999" : changeReasonCode);
		mac.setConditionCode("10");// 暂时固定
		consignment.setTransportContractDocument(mac);

		// 分单号运单号
		if (!isManifest) {
			IDXml matx = new IDXml();
			matx.setId(pe.getWaybillnosecondary());
			consignment.setAssociatedTransportDocument(matx);
		}

		if (WaybillReceiptType.DELETE != type) {
			// 装载日期
			DeclareLoadingLocationXml ml = new DeclareLoadingLocationXml();
			ml.setId("CGO/4604");
			ml.setLoadingDate(new SimpleDateFormat("yyyy-MM-dd HH:mm")
					.format(isManifest ? me.getStowagedate() : pe.getStowagedate()));
			consignment.setLoadingLocation(ml);

			DeclareUnloadingLocationXml mu = new DeclareUnloadingLocationXml();
			mu.setId("CGO/4604");
			consignment.setUnloadingLocation(mu);

			IDXml transitDestination = new IDXml();
			transitDestination.setId(isManifest ? me.getDe_trstation() : pe.getDe_trstation());
			consignment.setTransitDestination(transitDestination);

			consignment.setCustomsStatusCode(isManifest ? me.getCustomscode() : pe.getCustomscode());
			consignment.setTransportSplitIndicator(isManifest ? me.getCustomsstatus() : pe.getCustomsstatus());

			// 运费支付方法代码
			DeclareMethodCodeXml freightpayment = new DeclareMethodCodeXml();
			freightpayment.setMethodCode(isManifest ? me.getPaymode() : pe.getPaymode());
			consignment.setFreightPayment(freightpayment);

			// 货物总件数
			DeclareConsignmentPackagingXml mcon = new DeclareConsignmentPackagingXml();
			mcon.setQuantityQuantity(isManifest ? me.getTotalpiece() : pe.getTotalpiece());
			consignment.setConsignmentPackaging(mcon);
			consignment.setTotalGrossMassMeasure(isManifest ? me.getTotalweight() : pe.getTotalweight());

			// 收货人信息
			DeclareConsigneeXml consignee = new DeclareConsigneeXml();
			consignee.setName(isManifest ? me.getCo_name() : pe.getCo_name());
			DeclareAddressXml consigneeAddressXml = new DeclareAddressXml();
			consigneeAddressXml.setLine(isManifest ? me.getSh_address() : pe.getSh_address());
			consigneeAddressXml.setCityName(isManifest ? me.getSh_city() : pe.getSh_city());
			String sh_country = isManifest ? me.getSh_country() : pe.getSh_country();
			if (!StringUtils.isBlank(sh_country)) {
				consigneeAddressXml.setCountryCode(sh_country);
			}

			String sh_provincecode = isManifest ? me.getSh_provincecode() : pe.getSh_provincecode();
			if (!StringUtils.isBlank(sh_provincecode)) {
				consigneeAddressXml.setCountrySubEntityID(sh_provincecode);
			}

			String sh_provincename = isManifest ? me.getSh_provincename() : pe.getSh_provincename();
			if (!StringUtils.isBlank(sh_provincename)) {
				consigneeAddressXml.setCountrySubEntityName(sh_provincename);
			}

			String sh_zipcode = isManifest ? me.getSh_zipcode() : pe.getSh_zipcode();
			if (!StringUtils.isBlank(sh_zipcode)) {
				consigneeAddressXml.setPostcodeID(sh_zipcode);
			}

			consignee.setAddress(consigneeAddressXml);
			consignment.setConsignee(consignee);

			// 设置发货人货人信息
			DeclareConsigneeXml consignor = new DeclareConsigneeXml();
			consignor.setName(isManifest ? me.getSh_name() : pe.getSh_name());
			DeclareAddressXml consignorAddressXml = new DeclareAddressXml();
			consignorAddressXml.setLine(isManifest ? me.getCo_address() : pe.getCo_address());
			consignorAddressXml.setCityName(isManifest ? me.getCo_city() : pe.getCo_city());
			String co_country = isManifest ? me.getCo_country() : pe.getCo_country();
			if (!StringUtils.isBlank(co_country)) {
				consignorAddressXml.setCountryCode(co_country);
			}

			String co_deltaname = isManifest ? me.getCo_deltaname() : pe.getCo_deltaname();
			if (!StringUtils.isBlank(co_deltaname)) {
				consignorAddressXml.setCountrySubEntityName(co_deltaname);
			}

			String co_zipcode = isManifest ? me.getCo_zipcode() : pe.getCo_zipcode();
			if (!StringUtils.isBlank(co_zipcode)) {
				consignorAddressXml.setCountryCode(co_zipcode);
			}

			consignor.setAddress(consignorAddressXml);
			consignment.setConsignor(consignor);

			DeclareConsignmentItemXml consignmentitem = new DeclareConsignmentItemXml();
			consignment.setConsignmentItem(consignmentitem);
		}

		declare.setConsignment(consignment);

		if (WaybillReceiptType.DELETE == type) {
			ContentXml additionalInformation = new ContentXml();
			additionalInformation.setContent("");
			declare.setAdditionalInformation(additionalInformation);
		}

		body.setDeclaration(declare);
		return body;
	}


	//修改主单号用户
	public void userUpdate(Long userId, Long ids){

		preparesecondaryRepository.deleteSub(ids);
		manifestRepository.userUpdate(userId, ids);

	}

	//查询主单号是否存在
	public ManifestEntity findWbm(String wbm){
		return manifestRepository.findWbm(wbm);
	}

	//修改预配
//    @Transactional
	public int update(ManifestEntity manifestEntity){

		return manifestRepository.update(manifestEntity.getWaybillnomaster(),
				manifestEntity.getFlightno(), manifestEntity.getFlightdate(), manifestEntity.getCarrier(),
				manifestEntity.getTotalpiece(), manifestEntity.getTotalweight(), manifestEntity.getDestinationstation());
	}

	//删除预配
	@Transactional
	public int deleteOne(String waybill){
		return manifestRepository.deleteOne(waybill);
	}
}