f914253c3d5413da1fc82cc432287505a99ccb97.svn-base 52.8 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 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.reg.web.base;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import javax.servlet.http.HttpSession;
import javax.swing.text.html.StyleSheet;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.mapper.JaxbMapper;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.utils.IdGen;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.utils.excel.ExcelTemplate;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.reg.entity.base.RegBaseC;
import com.thinkgem.jeesite.modules.reg.entity.base.RegBaseH;
import com.thinkgem.jeesite.modules.reg.entity.base.RegBaseLjz;
import com.thinkgem.jeesite.modules.reg.entity.base.RegBasePerson;
import com.thinkgem.jeesite.modules.reg.entity.base.RegBaseXm;
import com.thinkgem.jeesite.modules.reg.entity.base.RegBaseZdjbxx;
import com.thinkgem.jeesite.modules.reg.entity.base.RegBaseZrz;
import com.thinkgem.jeesite.modules.reg.entity.base.RegBaseZrzchxx;
import com.thinkgem.jeesite.modules.reg.entity.bus.RegBusHisHouseBdcqzsdjxx;
import com.thinkgem.jeesite.modules.reg.entity.bus.RegBusRights;
import com.thinkgem.jeesite.modules.reg.entity.bus.RegBusSjmain;
import com.thinkgem.jeesite.modules.reg.service.base.RegBaseCService;
import com.thinkgem.jeesite.modules.reg.service.base.RegBaseHService;
import com.thinkgem.jeesite.modules.reg.service.base.RegBaseLjzService;
import com.thinkgem.jeesite.modules.reg.service.base.RegBasePersonService;
import com.thinkgem.jeesite.modules.reg.service.base.RegBaseXmService;
import com.thinkgem.jeesite.modules.reg.service.base.RegBaseZdjbxxService;
import com.thinkgem.jeesite.modules.reg.service.base.RegBaseZrzService;
import com.thinkgem.jeesite.modules.reg.service.base.RegBaseZrzchxxService;
import com.thinkgem.jeesite.modules.reg.utils.RegUtils;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import com.thinkgem.jeesite.modules.sys.utils.DictUtils;

/**
 * 项目(小区)信息维护Controller
 * @author xuyg
 * @version 2015-11-11
 */
@Controller
@RequestMapping(value = "${adminPath}/reg/base/regBaseXm")
public class RegBaseXmController extends BaseController {

	@Autowired
	private RegBaseXmService regBaseXmService;
	@Autowired
	private SystemService systemService;
	@Autowired
	private RegBasePersonService regBasePersonService;
	@Autowired
	private RegBaseHService regBaseHService;
	@Autowired
	private RegBaseCService regBaseCService;
	@Autowired
	private RegBaseZrzService regBaseZrzService;
	@Autowired
	private RegBaseZdjbxxService regBaseZdjbxxService;
	@Autowired
	private RegBaseZrzchxxService regBaseZrzchxxService;
	@Autowired
	private RegBaseLjzService regBaseLjzService;
	
	@ModelAttribute
	public RegBaseXm get(@RequestParam(required=false) String id) {
		RegBaseXm entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = regBaseXmService.get(id);
		}
		if (entity == null){
			entity = new RegBaseXm();
		}
		return entity;
	}
	
	@RequiresPermissions("reg:base:regBaseXm:view")
	@RequestMapping(value = {"list", ""})
	public String list(RegBaseXm regBaseXm, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<RegBaseXm> page = regBaseXmService.findPage(new Page<RegBaseXm>(request, response), regBaseXm);
		//通过项目添加人ID获取项目添加人名称-每次都获取当前页数据
		List<RegBaseXm> xmList = page.getList();
		for(int i=0; i<xmList.size(); i++){
			RegBaseXm regBaseXmT = xmList.get(i);
			User creater = regBaseXmT.getCreateBy();
			regBaseXmT.setCreater(creater.getName());
			User updater = regBaseXmT.getUpdateBy();
			regBaseXmT.setUpdater(updater.getName());
			regBaseXmT.setUpdater(updater.getName());
			regBaseXmT.setCreater(creater.getName());

		}
		page.setList(xmList);
		model.addAttribute("page", page);
		if(StringUtils.isNotBlank(request.getParameter("zchinfo"))){
			model.addAttribute("zchinfo", request.getParameter("zchinfo"));
		}
		if(StringUtils.isNotBlank(request.getParameter("ywh"))){
			model.addAttribute("ywh", request.getParameter("ywh"));
		}
		//--------------------------记忆查询20180516
		regBaseXm=new RegBaseXm();
		if(StringUtils.isNotBlank(request.getParameter("xmbh"))){
			System.out.println(request.getParameter("xmbh"));
			regBaseXm.setXmbh(request.getParameter("xmbh"));
		}
		if(StringUtils.isNotBlank(request.getParameter("xmmc"))){
			System.out.println(request.getParameter("xmmc"));
			regBaseXm.setXmmc(request.getParameter("xmmc"));
		}
		if(StringUtils.isNotBlank(request.getParameter("jsdwmc"))){
			System.out.println(request.getParameter("jsdwmc"));
			regBaseXm.setJsdwmc(request.getParameter("jsdwmc"));
		}
		if(StringUtils.isNotBlank(request.getParameter("sm"))){
			System.out.println(request.getParameter("sm"));
			regBaseXm.setSm(request.getParameter("sm"));
		}
		model.addAttribute("regBaseXm", regBaseXm);
		//-----------------------------
		//System.out.println(JaxbMapper.toXml(page.getList(), "regBaseXms", RegBaseXm.class));
		return "modules/reg/base/regBaseXmList";
	}
	
	@RequestMapping(value = "guideList")
	public String guideList(RegBaseXm regBaseXm, HttpServletRequest request, HttpServletResponse response, Model model) {
		RegBusRights regBusRights = new RegBusRights();
		regBusRights.setStxq(null);
		String djlx = request.getParameter("DJLX");
		if(StringUtils.isNotBlank(djlx)){
//			if(djlx.equals("213") || djlx.equals("207") || djlx.equals("803")){//此处过滤土地是抵押状态的宗地
//				Page<RegBaseXm> page = regBaseXmService.findTdPage(new Page<RegBaseXm>(request, response), regBaseXm); 
//				model.addAttribute("page", page);
//			}else {
			Page<RegBaseXm> page = regBaseXmService.findPage(new Page<RegBaseXm>(request, response), regBaseXm); 
			model.addAttribute("page", page);
//			}
			model.addAttribute("DJLX", djlx);
		}else {
			Page<RegBaseXm> page = regBaseXmService.findPage(new Page<RegBaseXm>(request, response), regBaseXm); 
			model.addAttribute("page", page);
		}
		if(StringUtils.isNotBlank(request.getParameter("YWH"))){
			model.addAttribute("ywh", request.getParameter("YWH"));
		}
		if(StringUtils.isNotBlank(request.getParameter("tz"))){
			model.addAttribute("tz", request.getParameter("tz"));
		}
		request.getSession().removeAttribute(request.getParameter("YWH")+"_selectedIDs");
		return "modules/reg/bus/dialog/ForenoticeHouseGuide";
	}
	
	/**
	 * 项目内多幢登记向导
	 * @param regBaseXm
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "dzguideList")
	public String dzguideList(RegBaseXm regBaseXm, HttpServletRequest request, HttpServletResponse response, Model model) {
		String djlx = request.getParameter("DJLX");
		regBaseXm.setIsaudit("1");
		Page<RegBaseXm> page = regBaseXmService.findPage(new Page<RegBaseXm>(request, response), regBaseXm); 
		model.addAttribute("page", page);
		model.addAttribute("DJLX", djlx);
		model.addAttribute("ywh", request.getParameter("YWH"));
		return "modules/reg/base/regBaseXmGuideList";
	}

	@RequestMapping(value = "guideHousesbgList")
	public String guideHousesbgList(RegBaseXm regBaseXm, HttpServletRequest request, HttpServletResponse response, Model model) {
		RegBusRights regBusRights = new RegBusRights();
		regBusRights.setStxq(null);
		String djlx = request.getParameter("DJLX");
		if(StringUtils.isNotBlank(djlx)){
			Page<RegBaseXm> page = regBaseXmService.findPage(new Page<RegBaseXm>(request, response), regBaseXm); 
			model.addAttribute("page", page);
			model.addAttribute("DJLX", djlx);
		}else {
			Page<RegBaseXm> page = regBaseXmService.findPage(new Page<RegBaseXm>(request, response), regBaseXm); 
			model.addAttribute("page", page);
		}
		if(StringUtils.isNotBlank(request.getParameter("YWH"))){
			model.addAttribute("ywh", request.getParameter("YWH"));
		}
		if(StringUtils.isNotBlank(request.getParameter("tz"))){
			model.addAttribute("tz", request.getParameter("tz"));
		}
		request.getSession().removeAttribute(request.getParameter("YWH")+"_selectedIDs");
		return "modules/reg/bus/dialog/ForenoticeHousebgGuide";
	}
	
		/**
	 * 楼盘查封登记业务
	 * 过滤未登记、有效的、已审核楼盘数据
	 * @param regBaseXm
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("reg:base:regBaseXm:view")
	@RequestMapping(value = "lpcfguideList")
	public String lpcfguideList(RegBaseXm regBaseXm, HttpServletRequest request, HttpServletResponse response, Model model) {
		RegBusRights regBusRights = new RegBusRights();
		regBusRights.setStxq(null);
		String djlx = request.getParameter("DJLX");
		if(StringUtils.isNotBlank(djlx)){
			Page<RegBaseXm> page = regBaseXmService.findPage(new Page<RegBaseXm>(request, response), regBaseXm); 
			model.addAttribute("page", page);
			model.addAttribute("DJLX", djlx);
		}else {
			Page<RegBaseXm> page = regBaseXmService.findPage(new Page<RegBaseXm>(request, response), regBaseXm); 
			model.addAttribute("page", page);
		}
		if(StringUtils.isNotBlank(request.getParameter("YWH"))){
			model.addAttribute("ywh", request.getParameter("YWH"));
		}
		if(StringUtils.isNotBlank(request.getParameter("tz"))){
			model.addAttribute("tz", request.getParameter("tz"));
		}
		request.getSession().removeAttribute(request.getParameter("YWH")+"_selectedIDs");
		return "modules/reg/bus/dialog/lpcfGuide";
	}
	
	/**
	 * 楼盘查封登记业务
	 * 过滤未登记、有效的、已审核楼盘数据
	 * @param regBaseXm
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("reg:base:regBaseXm:view")
	@RequestMapping(value = "lpcfzxguideList")
	public String lpcfzxguideList(RegBaseXm regBaseXm, HttpServletRequest request, HttpServletResponse response, Model model) {
		RegBusRights regBusRights = new RegBusRights();
		regBusRights.setStxq(null);
		String djlx = request.getParameter("DJLX");
		if(StringUtils.isNotBlank(djlx)){
			Page<RegBaseXm> page = regBaseXmService.findPage(new Page<RegBaseXm>(request, response), regBaseXm); 
			model.addAttribute("page", page);
			model.addAttribute("DJLX", djlx);
		}else {
			Page<RegBaseXm> page = regBaseXmService.findPage(new Page<RegBaseXm>(request, response), regBaseXm); 
			model.addAttribute("page", page);
		}
		if(StringUtils.isNotBlank(request.getParameter("YWH"))){
			model.addAttribute("ywh", request.getParameter("YWH"));
		}
		if(StringUtils.isNotBlank(request.getParameter("tz"))){
			model.addAttribute("tz", request.getParameter("tz"));
		}
		request.getSession().removeAttribute(request.getParameter("YWH")+"_selectedIDs");
		return "modules/reg/bus/dialog/lpcfGuide";
	}
	
	@RequiresPermissions("reg:base:regBaseXm:view")
	@RequestMapping(value = "form")
	public String form(RegBaseXm regBaseXm,Model model,HttpServletRequest request) {
		//项目编码生成规则:6位数,从100000开始;项目申请时间默认当前时间[前台显示]
		if(StringUtils.isBlank(regBaseXm.getId())){
			model.addAttribute("XMBM", RegUtils.getCXmStrNum(regBaseXmService.getXmbmStr()));
		}
		
		model.addAttribute("views", request.getParameter("view"));
		model.addAttribute("regBaseXm", regBaseXm);
		//System.out.println(JaxbMapper.toXml(regBaseXm));
		return "modules/reg/base/regBaseXmForm";
	}

	@RequiresPermissions("reg:base:regBaseXm:edit")
	@RequestMapping(value = "save")
	public synchronized String save(RegBaseXm regBaseXm, Model model, RedirectAttributes redirectAttributes, HttpServletRequest request) {
		if (!beanValidator(model, regBaseXm)){
			return form(regBaseXm, model,request);
		}
		if(StringUtils.isBlank(regBaseXm.getId())){
			String xmbm = RegUtils.getCXmStrNum(regBaseXmService.getXmbmStr());
			regBaseXm.setXmbh(xmbm);
		}
		//处理项目名称特殊字符
		String xmmc = StringEscapeUtils.unescapeHtml4(regBaseXm.getXmmc());
		regBaseXm.setXmmc(xmmc);
		regBaseXmService.save(regBaseXm);
		//修改人员信息的状态为已引用
		if(StringUtils.isNotBlank(regBaseXm.getZjh())){
			RegBasePerson regBasePerson = new RegBasePerson();
			regBasePerson.setZjh(regBaseXm.getZjh());
			regBasePerson.setZjzl(regBaseXm.getZjzl());
			List<RegBasePerson> findPersonList = regBasePersonService.findList(regBasePerson);
			if(findPersonList.size()>0){
				regBasePerson = findPersonList.get(0);
				regBasePerson.setIsaudit("2");
				regBasePersonService.save(regBasePerson);
			}
		}
		/*
		//判断下是否存在同名的项目名称
		RegBaseXm regBaseXmT = new RegBaseXm();
		regBaseXmT.setXmmc(regBaseXm.getXmmc());
		List<RegBaseXm> findXmmcList = regBaseXmService.findList(regBaseXmT);
		if(findXmmcList.size()>=2){
			addMessage(redirectAttributes, "已经存在项目名称【"+regBaseXm.getXmmc()+"】,保存项目(小区)信息成功");
		}else{
			addMessage(redirectAttributes, "保存项目(小区)信息成功");
		}*/
		addMessage(redirectAttributes, "保存项目(小区)信息成功");
		return "redirect:"+Global.getAdminPath()+"/reg/base/regBaseXm/";
	}
	
	@RequiresPermissions("reg:base:regBaseXm:edit")
	@RequestMapping(value = "audit")
	@Transactional
	public String audit(RegBaseXm regBaseXm, HttpServletRequest request, RedirectAttributes redirectAttributes) {
		regBaseXm = regBaseXmService.get(regBaseXm.getId());
		//设置该数据已经被审核
		regBaseXm.setIsaudit("1");
		regBaseXmService.save(regBaseXm);
		addMessage(redirectAttributes, "项目信息审核成功!");
		return "redirect:"+Global.getAdminPath()+"/reg/base/regBaseXm/list?&xmbh="+request.getParameter("paraxmbh")+"&xmmc="+request.getParameter("paraxmmc")+"&jsdwmc="+request.getParameter("parajsdwmc")+"&sm="+request.getParameter("parasm");
	}
	
	@RequiresPermissions("reg:base:regBaseXm:edit")
	@RequestMapping(value = "unaudit")
	@Transactional
	public String unaudit(RegBaseXm regBaseXm, HttpServletRequest request, RedirectAttributes redirectAttributes) {
		regBaseXm = regBaseXmService.get(regBaseXm.getId());
		//设置该数据已经被审核
		regBaseXm.setIsaudit("0");
		regBaseXmService.save(regBaseXm);
		addMessage(redirectAttributes, "项目信息反审核成功!");
		return "redirect:"+Global.getAdminPath()+"/reg/base/regBaseXm/list?&xmbh="+request.getParameter("paraxmbh")+"&xmmc="+request.getParameter("paraxmmc")+"&jsdwmc="+request.getParameter("parajsdwmc")+"&sm="+request.getParameter("parasm");
	}
	
	@RequiresPermissions("reg:base:regBaseXm:edit")
	@RequestMapping(value = "isdelete")
	@ResponseBody
	public ModelMap isdelete(RegBaseXm regBaseXm, HttpServletRequest request) {
		ModelMap modelMap = new ModelMap();
		modelMap.put("data", "0");//默认没有被引用
		List<String> bdcdyhs = new ArrayList<String>();
		List<RegBaseH> regBaseHList = new ArrayList<RegBaseH>();
		RegBaseZrz regBaseZrz = new RegBaseZrz();
		regBaseZrz.setXmid(regBaseXm.getId());
		List<RegBaseZrz> regBaseZrzList = regBaseZrzService.findList(regBaseZrz);
		//删除权籍系统覆盖导入后遗留的空项目
		if(regBaseZrzList.size()>0) {
			modelMap.addAttribute("title","1");
		}
		int zrzcount = regBaseZrzList.size();
		Out:
		for(int i=0; i<zrzcount; i++){
			RegBaseC regBaseC = new RegBaseC();
			RegBaseZrz regBaseZrzTemp = regBaseZrzList.get(i);
			if(StringUtils.isNotBlank(regBaseZrzTemp.getZb())){
				modelMap.addAttribute("chxx", "1");//已有楼栋坐标
				bdcdyhs.add(regBaseZrzTemp.getBdcdyh());
			}
			regBaseC.setZid(regBaseZrzTemp.getId());
			List<RegBaseC> regBaseCList = regBaseCService.findList(regBaseC);
			int ccount = regBaseCList.size();
			for(int j=0; j<ccount; j++){
				RegBaseC regBaseCTemp = regBaseCList.get(j);
				RegBaseH regBaseHTemp = new RegBaseH();
				regBaseHTemp.setCid(regBaseCTemp.getId());
				//注意这里很重要,只查询已引用的户信息
				regBaseHTemp.setIsaudit("2");
				regBaseHList = regBaseHService.findList(regBaseHTemp);
				if(regBaseHList.size()>0){
					modelMap.put("data", "1");//已经有已引用的户信息记录
					break Out;
				}
			}
		} 
		modelMap.addAttribute("bdcdyhs", bdcdyhs);
        return modelMap;
	}
	
	@RequiresPermissions("reg:base:regBaseXm:edit")
	@RequestMapping(value = "isxmmc")
	@ResponseBody
	public ModelMap isxmmc(HttpServletRequest request) {
		ModelMap modelMap = new ModelMap();
		String xmmc = request.getParameter("xmmc");
		RegBaseXm regBaseXm = new RegBaseXm();
		regBaseXm.setXmmc(xmmc);
		List<RegBaseXm> findList = regBaseXmService.findXmmcList(regBaseXm);
		if(findList.size() > 0){
			modelMap.put("data", "1");//已经有了当前项目名称
		}else{
			modelMap.put("data", "0");
		}
		return modelMap;
	}
	

	@RequiresPermissions("reg:base:regBaseXm:edit")
	@RequestMapping(value = "delete")
	public String delete(RegBaseXm regBaseXm, RedirectAttributes redirectAttributes) {
		//开始删除幢下的层户信息
		List<RegBaseH> regBaseHList = new ArrayList<RegBaseH>();
		RegBaseZrz regBaseZrz = new RegBaseZrz();
		RegBaseZrzchxx regBaseZrzchxx = new RegBaseZrzchxx();
		regBaseZrz.setXmid(regBaseXm.getId());
		List<RegBaseZrz> regBaseZrzList = regBaseZrzService.findList(regBaseZrz);
		int zrzcount = regBaseZrzList.size();
		for(int i=0; i<zrzcount; i++){
			RegBaseC regBaseC = new RegBaseC();
			RegBaseZrz regBaseZrzTemp = regBaseZrzList.get(i);
			regBaseZrzchxx = regBaseZrzchxxService.getdy(regBaseZrzTemp.getBdcdyh());
			regBaseC.setZid(regBaseZrzTemp.getId());
			List<RegBaseC> regBaseCList = regBaseCService.findList(regBaseC);
			int ccount = regBaseCList.size();
			for(int j=0; j<ccount; j++){
				RegBaseC regBaseCTemp = regBaseCList.get(j);
				regBaseCService.cdelete(regBaseCTemp);//彻底删除
				RegBaseH regBaseHTemp = new RegBaseH();
				regBaseHTemp.setCid(regBaseCTemp.getId());
				regBaseHList.addAll(regBaseHService.findList(regBaseHTemp));
			}
			//删除层后再删除自然幢信息
			regBaseZrzService.zrzdelete(regBaseZrzTemp);
			regBaseZrzchxxService.chdelete(regBaseZrzchxx);
			
		}
		//最后删除户信息
		int hcount = regBaseHList.size();
		for(int a=0; a<hcount; a++){
			RegBaseH regBaseHTemp = regBaseHList.get(a);
			regBaseHService.hdelete(regBaseHTemp);
		}
		//宗地基本信息项目引用置空
		RegBaseZdjbxx regBaseZdjbxx = new RegBaseZdjbxx();
		regBaseZdjbxx.setXmid(regBaseXm.getId());
		List<RegBaseZdjbxx> findZdList = regBaseZdjbxxService.findList(regBaseZdjbxx);
		int count = findZdList.size();
		if(count > 0){
			for(int i = 0; i < count; i++){
				regBaseZdjbxx = findZdList.get(i);
				regBaseZdjbxx.setXmid(null);
				regBaseZdjbxxService.save(regBaseZdjbxx);
			}
		}
	
		regBaseXmService.xmdelete(regBaseXm);//彻底删除
		addMessage(redirectAttributes, "删除项目(小区)以及该项目下的幢、层和户信息成功");
		return "redirect:"+Global.getAdminPath()+"/reg/base/regBaseXm/list";

	}
	
	/**
	 * 选择xm后验证是否可用,项目内所有幢在同一宗地,项目内户信息没有已引用
	 * @param regBaseFdcqxm
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "canbeused")
	@ResponseBody
	public Map<String, String> canbeused(RegBaseXm regBaseXm , String id) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("result", "0");//默认0,满足条件;1不满足条件
		RegBaseZrz regBaseZrz = new RegBaseZrz();
		regBaseZrz.setXmid(id);
		List<RegBaseZrz> zrzList = regBaseZrzService.findList(regBaseZrz);
		if(zrzList.size()>1){
			regBaseZrz.setZddm(zrzList.get(0).getZddm());
			List<RegBaseZrz> zrzList1 = regBaseZrzService.findList(regBaseZrz);
			if(zrzList.size()!=zrzList1.size()){
				map.put("msg", "项目内多幢不在同一宗地,请在独幢入口进行登记。");
				map.put("result", "1");
			}else{
				for(int i=0;i<zrzList.size();i++){
					regBaseZrz = zrzList.get(i);
					if(!("1".equals(regBaseZrz.getIsaudit()))){
						map.put("msg", "幢("+regBaseZrz.getGzwmc()+")信息未审核或已被引用,请核实。");
						map.put("result", "1");
					}else{
						RegBaseH regBaseH = new RegBaseH();
						regBaseH.setBdcdyh(regBaseZrz.getBdcdyh().substring(0, 24));
						List<RegBaseH> hlistemp = regBaseHService.findAllList(regBaseH);
						regBaseH.setRights("0");
						regBaseH.setIsaudit("1");
						List<RegBaseH> hlistemp1 = regBaseHService.findAllList(regBaseH);
						if(hlistemp.size()>0){
							if(hlistemp.size()!=hlistemp1.size()){
								map.put("msg", "幢("+regBaseZrz.getGzwmc()+")存在未审核或已引用户信息,请核实。");
								map.put("result", "1");
							}
						}else{
							map.put("msg", "幢("+regBaseZrz.getGzwmc()+")请添加户信息。");
							map.put("result", "1");
						}
					}
				}
			}
		}else if(zrzList.size()==1){
			map.put("msg", "该项目下只有一幢信息,请在独幢入口进行登记。");
			map.put("result", "1");
		}else{
			map.put("msg", "请添加幢信息!");
			map.put("result", "1");
		}
		return map;
	}
	
	@RequestMapping(value = "upload")
	public String uploadXm() {
//		model.addAttribute("path", "D:"+File.separator+"备份文件"+File.separator+"(项目+栋)数据收集模板.xlsx");
		return "modules/reg/base/regBaseXmImport";
	}
	/**
	 * 项目小区导入模板下载
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "download")
	public String downloadTemplet(HttpServletRequest request,HttpServletResponse response, Model model){
		response.setCharacterEncoding("UTF-8");
//		try {
//			request.setCharacterEncoding("UTF-8");
//		} catch (UnsupportedEncodingException e1) {
//			//  Auto-generated catch block
//			e1.printStackTrace();
//		}
//		String filepath =  new String(request.getParameter("file"));//.getBytes("UTF-8"),"UTF-8"
//		String filepath =  Global.getUserfilesBaseDir() + "/static/" + "download" + File.separator+ "宅基地权籍信息批量导入模板V1.0.xlsx";
//		String filepath =  "."+File.separator+"src"+File.separator+"main"+File.separator+"webapp"+File.separator+"static"+File.separator+"download"+File.separator+"(项目+栋)数据收集模板.xlsx";
		String filepath = "D:" + File.separator + "download" + File.separator + "(项目+栋)数据收集模板.xlsx";
//		System.out.println("============================"+filepath);
		if (filepath != null) {
			OutputStream os = null;
			FileInputStream fis = null;
			try {
				String file = filepath;
				if (!(new File(file)).exists()) {
					model.addAttribute("msg", "没有找到下载源文件。");
					return "modules/reg/base/regBaseXmImport";
				}
				String filename = file.substring(file.lastIndexOf(File.separator)+1);
				//System.out.println("文件名为:"+filename);
				os = response.getOutputStream();
				response.setHeader("content-disposition", "attachment;filename=" + new String(filename.getBytes("GBK"), "ISO-8859-1"));
				response.setContentType("application/octet-stream");//八进制流 与文件类型无关
				byte temp[] = new byte[1024];
				fis = new FileInputStream(file);
				int n = 0;
				while ((n = fis.read(temp)) != -1) {
					os.write(temp, 0, n);
				}
				os.flush();
			} catch (IOException e) {
				model.addAttribute("msg", "出错了,请稍后重试。");
				return "modules/reg/base/regBaseXmImport";
			} finally {
				if (os != null){
					try {
						os.close();
					} catch (IOException e) {
						System.out.println("输出流关闭异常");
						model.addAttribute("msg", "出错了,请稍后重试。");
						return "modules/reg/base/regBaseXmImport";
					}
				}
				if (fis != null){
					try {
						fis.close();
					} catch (IOException e) {
						System.out.println("输入流关闭异常");
						model.addAttribute("msg", "出错了,请稍后重试。");
						return "modules/reg/base/regBaseXmImport";
					}
				}
			}
		}
		
		return "modules/reg/base/regBaseXmImport";
	}
	
	
	@SuppressWarnings("unchecked")
	@RequiresPermissions("reg:base:regBaseXm:edit")
	@RequestMapping(value = "importFile", method = RequestMethod.POST)
	//@RequestMapping(value = "importFile", method = RequestMethod.POST)
	public synchronized String importXm(@RequestParam("filename") MultipartFile file,Model model, HttpServletRequest request,HttpServletResponse response, RedirectAttributes redirectAttributes) {
		List<Object> lists = null;
		//issure层面积大于幢占地面积时,人工判断是否导入标记
		if(StringUtils.isBlank(request.getParameter("issure"))){
			String temp = request.getSession().getServletContext().getRealPath(File.separator) + "temp"; // 临时目录
		    File tempFile = new File(temp);
		    if (!tempFile.exists()){
		      tempFile.mkdirs();
		    }
		    if (file == null){
		        return null;
		    }
		     // logger.info(file.getOriginalFilename());
		      
		     String name = file.getOriginalFilename().trim();// 获取上传文件名,包括路径
		     //name = name.substring(name.lastIndexOf("\\") + 1);// 从全路径中提取文件名
		     long size = file.getSize();
		     if ((name == null || name.equals("")) || size == 0){
		        return null;
		     }
		     InputStream in = null;
			 try {
			 in = file.getInputStream();
			 lists = regBaseXmService.importRegBaseXm(in,name);
//				System.out.println("====121212121");

			} catch (IOException e) {
				return "redirect:"+Global.getAdminPath()+"/error/500";
			}
		}
		request.getSession().setAttribute("lists", lists);
		if(lists != null && "1".equals(lists.get(0).toString())){
			model.addAttribute("errorList", lists);
			model.addAttribute("VIEW", "1");
			return "modules/reg/base/regBaseXmImport";
		}else if(lists != null && lists.size()>6){
			List<String> listtem = new ArrayList<String>();
			for(int i=6;i<lists.size();i++){
				listtem.add((String)lists.get(i));
			}
			model.addAttribute("listtem",listtem);
			model.addAttribute("issure","1");
			return "modules/reg/base/regBaseXmImport";
		}else{
			String views = importSave(model,request,response,redirectAttributes);
			return views;
		}
	}
	
	@SuppressWarnings("unchecked")
	@RequiresPermissions("reg:base:regBaseXm:edit")
	@RequestMapping(value = "importSave")
	public synchronized String importSave(Model model, HttpServletRequest request,HttpServletResponse response,RedirectAttributes redirectAttributes) {
		List<Object> lists = (List<Object>) request.getSession().getAttribute("lists");
		request.getSession().removeAttribute("lists");
		RegBasePerson regBasePerson = (RegBasePerson) lists.get(1);
		RegBaseXm regBaseXm = (RegBaseXm) lists.get(2);
		RegBaseZrz regBaseZrz = (RegBaseZrz) lists.get(3);
		List<RegBaseC> regBaseCs = (List<RegBaseC>) lists.get(4);
		List<RegBaseH> regBaseHs = (List<RegBaseH>) lists.get(5);
		//针对项目名称进行特殊字符处理
		if(regBaseXm.getXmmc().contains("&middot;")){
			regBaseXm.setXmmc(regBaseXm.getXmmc().replace("&middot;", "·"));
		}else if(regBaseXm.getXmmc().contains("&bull;")){
			regBaseXm.setXmmc(regBaseXm.getXmmc().replace("&bull;", "•"));
		}
		if(regBaseZrz.getXmmc().contains("&middot;")){
			regBaseZrz.setXmmc(regBaseZrz.getXmmc().replace("&middot;", "·"));
		}else if(regBaseXm.getXmmc().contains("&bull;")){
			regBaseZrz.setXmmc(regBaseZrz.getXmmc().replace("&bull;", "•"));
		}
//		for(int i=0;i<lists.size();i++){
//			@SuppressWarnings("unchecked")
//			List<RegBaseXm> regBaseXms = (List<RegBaseXm>) lists.get(0);
//			@SuppressWarnings("unchecked")
//			List<RegBaseZrz> regBaseZrzs = (List<RegBaseZrz>) lists.get(1);
//			@SuppressWarnings("unchecked")
//			List<RegBaseC> regBaseCs = (List<RegBaseC>) lists.get(2);
//			@SuppressWarnings("unchecked")
//			List<RegBaseH> regBaseHs = (List<RegBaseH>) lists.get(3);
		//项目
//			System.out.println(regBaseXms.size()+"||"+regBaseZrzs.size()+"||"+regBaseCs.size()+"||"+regBaseHs.size());
		//for(RegBaseXm regBaseXm : regBaseXms){
//			RegBaseXm regBaseXm = regBaseXms.get(0);
		//处理人员信息,若有不处理,若没有就存
		if(!isPersonExist(regBasePerson.getRymc(), regBasePerson.getZjzl(), regBasePerson.getZjh())){
			//System.out.println("新增人员信息");
			String pCode = "1000000";
			pCode = RegUtils.getHMaxFwbm(regBasePersonService.getHMaxPCode());//没有的时候返回100000,然后+1最为第一笔
			regBasePerson.setPcode(pCode);
			regBasePerson.setIsaudit("2");
			regBasePersonService.save(regBasePerson);
		}
		//处理项目小区信息,若有将项目信息取出来,若没有就添加
		RegBaseXm regBaseXmtemp = new RegBaseXm();
		regBaseXmtemp.setXmmc(regBaseXm.getXmmc());
		List<RegBaseXm> findXmList = regBaseXmService.findXmmcList(regBaseXmtemp);
		//System.out.println("找到同名项目:"+findXmList.size()+"个");
		int count = 0;
		if(findXmList.size() == 1){
			regBaseXm = findXmList.get(0);
		}else if(findXmList.size() > 1){
			model.addAttribute("msg", "找到同名项目:"+findXmList.size()+"个,无法导入。");
			return "modules/reg/base/regBaseXmImport";
		} else {
			String xmbm = RegUtils.getCXmStrNum(regBaseXmService.getXmbmStr());
			regBaseXm.setXmbh(xmbm);
			regBaseXm.setIsaudit("1");
			Date date = new Date();
			regBaseXm.setSqsj(date);
			// 设置项目的ID
			String Xmid = IdGen.uuid();
			regBaseXm.setId(Xmid);
			count++;
//				regBaseXmService.save1(regBaseXm);
		}
//			System.out.println(regBaseXm.getXmmc());
//			System.out.println(regBaseXm.getId());
		
		
		//幢
		//for(RegBaseZrz regBaseZrz : regBaseZrzs){
		//处理幢信息,若有就提示幢已存在,若没有判断是该项目的第几幢,并添加幢信息
			if(isZrzExist(regBaseXm.getId(), regBaseZrz.getGzwmc())){
				model.addAttribute("msg", "该楼栋信息系统已存在,请检查建筑物名称。");
				return "modules/reg/base/regBaseXmImport";
			}
			//判断该楼栋选择的宗地是否被其他项目引用
			if(zdIsUsedByOtherXm(regBaseZrz.getZddm(), regBaseXm.getId())){
				model.addAttribute("msg","该宗地已被其他项目引用。" );
				return "modules/reg/base/regBaseXmImport";
			}
			//写项目ID到宗地基本信息表中;项目楼盘跟宗地基本信息的关联
			RegBaseZdjbxx regBaseZdjbxxtemp = regBaseZdjbxxService.getdy(regBaseZrz.getZddm());
			if(regBaseZdjbxxtemp.getXmid()==null){
				regBaseZdjbxxtemp.setXmid(regBaseXm.getId());
				regBaseZdjbxxService.save(regBaseZdjbxxtemp);
			}
			double tdftxs = regBaseZrz.getZzdmj()/regBaseZrz.getScjzmj();
			int zrzh = getNextZrzh(regBaseXm.getId(), regBaseZrz.getZddm());
			//regBaseZrz.setZrzh(String.valueOf(zrzh));
			String ZHSort = getZHSort(zrzh);
			String zbdcdyh = regBaseZrz.getZddm()+"F"+ ZHSort +"0000";
			regBaseZrz.setBdcdyh(zbdcdyh);
			regBaseZrz.setZrzh(zbdcdyh.substring(0,24));
			long bsm = 0;
			bsm = RegUtils.getCZrzBsm(regBaseZrzService.getZrzMaxBsm())+1;
			regBaseZrz.setBsm(bsm);
			String ZID = IdGen.uuid();
			regBaseZrz.setId(ZID);
			regBaseZrz.setXmid(regBaseXm.getId());
			regBaseZrz.setYsdm("2001030110");
			regBaseZrz.setIsaudit("1");
			regBaseZrz.setYtmc(DictUtils.getDictLabel(regBaseZrz.getGhyt(),"reg_bus_fwyt", ""));
			//在所有幢信息合法后,再保存新增的xm信息后再保存幢信息
			if(count>0){
				regBaseXmService.save1(regBaseXm);
			}
			regBaseZrzService.save1(regBaseZrz);
			//创建逻辑幢
			RegBaseLjz regBaseLjz = new RegBaseLjz();
			regBaseLjz.setDscs(regBaseZrz.getDscs());
			regBaseLjz.setDxcs(regBaseZrz.getDxcs());
			regBaseLjz.setFwjg1(regBaseZrz.getFwjg());
			regBaseLjz.setFwyt1(DictUtils.getDictValue(regBaseZrz.getJzwjbyt(), "reg_bus_fwyt", ""));
		    String ljzh = regBaseZrz.getBdcdyh().substring(4,19)+regBaseZrz.getBdcdyh().substring(20,24)+"01";
			regBaseLjz.setLjzh(ljzh);
			regBaseLjz.setZrzh(regBaseZrz.getZrzh());
			regBaseLjz.setYsdm("6001030120");
			regBaseLjz.setZcs(regBaseZrz.getZcs()+"");
			regBaseLjz.setYtmc(regBaseZrz.getJzwjbyt());
			regBaseLjz.setScjzmj(regBaseZrz.getScjzmj());
			regBaseLjz.setJgrq(DateUtils.parseDate(regBaseZrz.getJgrq()));
			regBaseLjzService.save(regBaseLjz);
			
			//层
			int currentHCount = 0;
			for(RegBaseC regBaseC : regBaseCs){
				//if(regBaseZrz.getZrzh().equals(regBaseC.getZrzh())){
					regBaseC.setZrzh(regBaseZrz.getZrzh());
					regBaseC.setLjzh(regBaseLjz.getLjzh());
					regBaseC.setZid(ZID);
					regBaseC.setYsdm("2001030130");
					String CID = IdGen.uuid();
					regBaseC.setId(CID);
					int sjc = Integer.valueOf(regBaseC.getSjc());
					int zsjc = Math.abs(sjc);
//						String STR_FORMAT = "0000";
//						DecimalFormat df = new DecimalFormat(STR_FORMAT); 
					String ch = getZHSort(zsjc);
					if(sjc<0){
						//层号
//						String Ch = "-"+df.format(zsjc);
						regBaseC.setCh("-"+Integer.parseInt(ch)+"");
						if(StringUtils.isBlank(regBaseC.getMyc())){
							//名义层
							regBaseC.setMyc("负"+Integer.parseInt(ch)+"");
						}
					}else{
//						String Ch = df.format(zsjc);
						//层号
						regBaseC.setCh(Integer.parseInt(ch)+"");
						if(StringUtils.isBlank(regBaseC.getMyc())){
							//名义层
							regBaseC.setMyc(Integer.parseInt(ch)+"");
						}
					}
					regBaseC.setIsaudit("1");
					regBaseCService.save1(regBaseC);
					
					//户
					for(RegBaseH regBaseH : regBaseHs){
						if(regBaseH.getXmmc().contains("&middot;")){
							regBaseH.setXmmc(regBaseH.getXmmc().replace("&middot;", "·"));
						}else if(regBaseH.getXmmc().contains("&bull;")){
							regBaseXm.setXmmc(regBaseH.getXmmc().replace("&bull;", "•"));
						}
						//System.out.println(regBaseH.getZrzh()+"|"+regBaseC.getZrzh()+"***"+regBaseH.getCh()+"|"+regBaseC.getCh());
						if(regBaseH.getCh().equals(regBaseC.getCh())){
							regBaseH.setSjcs(String.valueOf(regBaseZrz.getZcs()));
							regBaseH.setZrzh(regBaseC.getZrzh());
							regBaseC.setLjzh(regBaseLjz.getLjzh());
							regBaseH.setYsdm("2001030140");
							regBaseH.setCid(CID);
							String fwbm = RegUtils.getHMaxFwbm(regBaseHService.getHMaxFwbm());//没有的时候返回100000,然后+1最为第一笔
							regBaseH.setFwbm(fwbm);
							regBaseH.setRights("0");
							regBaseH.setIsdiya("0");
							regBaseH.setIschafeng("0");
							regBaseH.setIsyiyi("0");
							regBaseH.setIsaudit("1");
							regBaseH.setId(IdGen.uuid());
							if(zbdcdyh.length()>24){
								String Hbdcdyh1 = zbdcdyh.substring(0,24);
								String Hbdcdyh2 = getZHSort(++currentHCount);
//									if (StringUtils.isNotBlank(regBaseZrz.getZddm())) {
//										RegBaseH  H = new RegBaseH();
//										H.setBdcdyh(regBaseZrz.getBdcdyh().substring(0,24));
//										List<RegBaseH> hlist =  regBaseHService.findsxhList(H);
//										
//										if(hlist.size() > 0){
//											H  = hlist.get(0);
//											currentHCount = Integer.parseInt(H.getBdcdyh().substring(25,28));
//										}else{
//											currentHCount = 0;
//										}
//										
//									}
//									regBaseH.setBdcdyh(zbdcdyh.substring(0, 24) + RegUtils.getNextZrzSort(Integer.toString(currentHCount)));//已做过加1操作了
//								}
								regBaseH.setBdcdyh(Hbdcdyh1+Hbdcdyh2);
							}
							
							//根据宗地代码获取宗地坐落。户坐落:宗地坐落+栋号+层号+室号部位 
							RegBaseZdjbxx regBaseZdjbxx = new RegBaseZdjbxx();
							regBaseZdjbxx.setZddm(regBaseZrz.getZddm());
							List<RegBaseZdjbxx> findZdjbxxList = regBaseZdjbxxService.findList(regBaseZdjbxx);
							if(findZdjbxxList.size()>0){
								regBaseZdjbxx = findZdjbxxList.get(0);
							}
							String zlc = regBaseH.getBeginc()+"层";
							String zlshbw = regBaseH.getShbw();
							String hzl = "";
							//以模板坐落为准,若模板为空,则系统生成
							if(StringUtils.isBlank(regBaseH.getZl())) {
							if(zlshbw.startsWith(zlc)){
								hzl = regBaseZdjbxx.getZl()+regBaseH.getXmmc()+regBaseH.getJgzwmc()+zlshbw;//+"室";
							}else{
								hzl = regBaseZdjbxx.getZl()+regBaseH.getXmmc()+regBaseH.getJgzwmc()+zlc+zlshbw;//+"室";
							}
							regBaseH.setZl(hzl);
							}
							regBaseH.setIslogout("0");
							regBaseH.setMjdw("1");
							regBaseH.setZt("1");
							regBaseH.setScftjzmj(regBaseH.getScjzmj()-regBaseH.getSctnjzmj());
							regBaseH.setFttdmj(tdftxs*regBaseH.getScjzmj());
							regBaseHService.save1(regBaseH);
						}
						
					}
					
				//}
			}
		//}
	    //}
	//}
		addMessage(redirectAttributes, "成功导入幢信息");
		return "redirect:"+Global.getAdminPath()+"/reg/base/regBaseXm/?repage";
	
	}
	
	@RequiresPermissions("reg:base:regBaseXm:view")
	@RequestMapping(value = {"exportXm"})
	public String exportXm(RegBaseXm regBaseXm, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<RegBaseXm> page = regBaseXmService.findPage(new Page<RegBaseXm>(request, response), regBaseXm);
		//通过项目添加人ID获取项目添加人名称-每次都获取当前页数据
		List<RegBaseXm> xmList = page.getList();
		for(int i=0; i<xmList.size(); i++){
			RegBaseXm regBaseXmT = xmList.get(i);
			User creater = systemService.getUser(regBaseXmT.getCreateBy().getId());
			User updater = systemService.getUser(regBaseXmT.getUpdateBy().getId());
			regBaseXmT.setCreater(creater.getName());
			regBaseXmT.setUpdater(updater.getName());
		}
		page.setList(xmList);
		model.addAttribute("page", page);
		if(StringUtils.isNotBlank(request.getParameter("zchinfo"))){
			model.addAttribute("zchinfo", request.getParameter("zchinfo"));
		}
		if(StringUtils.isNotBlank(request.getParameter("ywh"))){
			model.addAttribute("ywh", request.getParameter("ywh"));
		}
		return "modules/reg/base/regBaseXmExportList";
	}
	
	private boolean zdIsUsedByOtherXm(String zddm,String xmid){
		boolean bl = true;
		RegBaseZdjbxx regBaseZdjbxx = regBaseZdjbxxService.getdy(zddm);
		if(regBaseZdjbxx.getXmid()==null || xmid.equals(regBaseZdjbxx.getXmid())){
			bl = false;
		}
		return bl;
	}
	
	/**
	 * 检查同一证件类型情况下只能有一个证件号
	 * @param rymc
	 * @param zjzl
	 * @param zjh
	 * @return
	 */
	private boolean isPersonExist(String rymc, String zjzl, String zjh){
		boolean bl = false;
		RegBasePerson regBasePerson = new RegBasePerson();
		regBasePerson.setRymc(rymc);
		regBasePerson.setZjh(zjh);
		regBasePerson.setZjzl(zjzl);
		List<RegBasePerson> findPersonList = regBasePersonService.findIsexistList(regBasePerson);
		if(findPersonList.size() > 0){
			bl = true;
			return bl;
		}
		return bl;
	}
	/**
	 * 判断同一个项目小区是否存在相同的建筑物名称的幢
	 * @param xmid
	 * @param gzwmc
	 * @return
	 */
	private boolean isZrzExist(String xmid, String gzwmc){
		boolean bl = false;
		RegBaseZrz regBaseZrzTmp = new RegBaseZrz();
		regBaseZrzTmp.setGzwmc(gzwmc);
		regBaseZrzTmp.setXmid(xmid);
		List<RegBaseZrz> findList1 = regBaseZrzService.findJzwmcList(regBaseZrzTmp);
		if(findList1.size() > 0){
			bl = true;
			return bl;
		}
		return bl;
	}
	/**
	 * 获取自然幢号
	 * @param xmid
	 * @param zddm
	 * @return
	 */
	private int getNextZrzh(String xmid,String zddm){
		//获取当前项目中已有幢的数量
		RegBaseZrz regBaseZrzTmp = new RegBaseZrz();
		regBaseZrzTmp.setXmid(xmid);
		List<RegBaseZrz> findZrzList = regBaseZrzService.findList(regBaseZrzTmp);
		int currentZrzCount = findZrzList.size();
		//取自然幢号最大数加一
		if (StringUtils.isNotBlank(zddm)) {
			RegBaseZrz zrz = new RegBaseZrz();
			zrz.setBdcdyh(zddm+"F");
			List<RegBaseZrz> zrzlist =  regBaseZrzService.findzrzhsxhList(zrz);
			if(zrzlist.size() > 0){
				zrz  = zrzlist.get(0);
				currentZrzCount = Integer.parseInt(zrz.getBdcdyh().substring(21,24));
			}else{
				currentZrzCount = 0;
			}
		}
		return currentZrzCount+1;
	}
	/**
     * 获取幢号字段
     * @param n
     * @return
     */
    private String getZHSort(int n){
    	String STR_FORMAT = "0000";
		DecimalFormat df = new DecimalFormat(STR_FORMAT);
    	String nextZHSort = df.format(n);
//		if(StringUtils.isNotBlank(String.valueOf(n))){
//			if (n < 10) {
//				nextZHSort = "000" + n;
//			}else if (n < 100) {
//				nextZHSort = "00" + n;
//			}else if (n < 1000) {
//				nextZHSort = "0" + n;
//			}else{
//				nextZHSort = n +"";
//			}
//		}
		return nextZHSort;
    }
	
	/*
	@RequestMapping(value = "importFile", method = RequestMethod.POST)
	//@RequestMapping(value = "importFile", method = RequestMethod.POST)
	public String importXm(@RequestParam("filename") MultipartFile file, HttpServletRequest request,HttpServletResponse response) {
		String temp = request.getSession().getServletContext().getRealPath(File.separator) + "temp"; // 临时目录
	    File tempFile = new File(temp);
	    if (!tempFile.exists()){
	      tempFile.mkdirs();
	    }
	    if (file == null){
	        return null;
	    }
	     // logger.info(file.getOriginalFilename());
	      
	      String name = file.getOriginalFilename().trim();// 获取上传文件名,包括路径
	      //name = name.substring(name.lastIndexOf("\\") + 1);// 从全路径中提取文件名
	      long size = file.getSize();
	      if ((name == null || name.equals("")) || size == 0)
	        return null;
	      InputStream in = null;
		try {
			in = file.getInputStream();
			List<List<?>> lists = regBaseXmService.importRegBaseXm(in);
			//for(int i=0;i<lists.size();i++){
				@SuppressWarnings("unchecked")
				List<RegBaseXm> regBaseXms = (List<RegBaseXm>) lists.get(0);
				@SuppressWarnings("unchecked")
				List<RegBaseZrz> regBaseZrzs = (List<RegBaseZrz>) lists.get(1);
				@SuppressWarnings("unchecked")
				List<RegBaseC> regBaseCs = (List<RegBaseC>) lists.get(2);
				@SuppressWarnings("unchecked")
				List<RegBaseH> regBaseHs = (List<RegBaseH>) lists.get(3);
				//项目
				System.out.println(regBaseXms.size()+"||"+regBaseZrzs.size()+"||"+regBaseCs.size()+"||"+regBaseHs.size());
				//for(RegBaseXm regBaseXm : regBaseXms){
				RegBaseXm regBaseXm = regBaseXms.get(0);
				String xmbm = RegUtils.getCXmStrNum(regBaseXmService.getXmbmStr());
				regBaseXm.setXmbh(xmbm);
				regBaseXm.setIsaudit("0");
				Date date = new Date();
				regBaseXm.setSqsj(date);
				// 设置项目的ID
				String Xmid = IdGen.uuid();
				regBaseXm.setId(Xmid);
				//System.out.println(Xmid);
				regBaseXmService.save1(regBaseXm);
				
				//幢
				for(RegBaseZrz regBaseZrz : regBaseZrzs){
					long bsm = 0;
					bsm = RegUtils.getCZrzBsm(regBaseZrzService.getZrzMaxBsm())+1;
					regBaseZrz.setBsm(bsm);
					String ZID = IdGen.uuid();
					regBaseZrz.setId(ZID);
					regBaseZrz.setXmid(Xmid);
					regBaseZrz.setYsdm("2001030110");
					regBaseZrz.setIsaudit("0");
					regBaseZrzService.save1(regBaseZrz);
					
					//层
					for(RegBaseC regBaseC : regBaseCs){
						String CID = "";
						if(regBaseZrz.getZrzh().equals(regBaseC.getZrzh())){
							regBaseC.setZid(ZID);
							regBaseC.setYsdm("2001030130");
							CID = IdGen.uuid();
							regBaseC.setId(CID);
							int sjc = regBaseC.getSjc();
							int zsjc = Math.abs(sjc);
							String STR_FORMAT = "0000";
							DecimalFormat df = new DecimalFormat(STR_FORMAT); 
							if(sjc<0){
								//层号
								String Ch = "-"+df.format(zsjc);
								regBaseC.setCh(Ch);
								if(StringUtils.isBlank(regBaseC.getMyc())){
									//名义层
									regBaseC.setMyc("负"+Ch);
								}
								
							}else{
								String Ch = df.format(zsjc);
								//层号
								regBaseC.setCh(Ch);
								if(StringUtils.isBlank(regBaseC.getMyc())){
									//名义层
									regBaseC.setMyc(Ch);
								}
								
							}
							regBaseC.setIsaudit("0");
							regBaseCService.save1(regBaseC);
							
							//户
							for(RegBaseH regBaseH : regBaseHs){
								System.out.println(regBaseH.getZrzh()+"|"+regBaseC.getZrzh()+"***"+regBaseH.getCh()+"|"+regBaseC.getCh());
								if(regBaseH.getZrzh().equals(regBaseC.getZrzh()) && regBaseH.getCh().equals(regBaseC.getCh())){
									regBaseH.setYsdm("2001030140");
									regBaseH.setCid(CID);
									String fwbm = RegUtils.getHMaxFwbm(regBaseHService.getHMaxFwbm());//没有的时候返回100000,然后+1最为第一笔
									regBaseH.setFwbm(fwbm);
									regBaseH.setRights("0");
									regBaseH.setIsdiya("0");
									regBaseH.setIschafeng("0");
									regBaseH.setIsyiyi("0");
									regBaseH.setId(IdGen.uuid());
									regBaseH.setBdcdyh("w000000000");
									regBaseH.setZl("待确定");
									regBaseH.setScftjzmj(0.01);
									regBaseHService.save1(regBaseH);
								}
								
							}
							
						}
					}
				}
			    //}
			//}
			
		} catch (IOException e) {
			// z
			e.printStackTrace();
		}
			
		
	      
	
	      return "redirect:"+Global.getAdminPath()+"/reg/base/regBaseXm/?repage";
	
		
	    //String xmbm = RegUtils.getCXmStrNum(regBaseXmService.getXmbmStr())
//	String fileName=file.getOriginalFilename();
//	if(StringUtils.isNotBlank(fileName))
//	 {
//		return "redirect:"+Global.getAdminPath()+"/reg/base/regBaseXm/List";
//	 }
//	 else
//	 {
//		 System.out.println(Global.getAdminPath());
//		 addMessage(redirectAttributes, "导入失败,请先选择文件");
//		 return "redirect:"+Global.getAdminPath()+"/reg/base/regBaseXm/List/?repage";
//	 }


		}
	
	*/
    
    @RequiresPermissions("reg:base:regBaseZrz:view")
	@RequestMapping(value = { "xmExport" })
    @ResponseBody
	public String zrzExport (HttpServletRequest request, HttpServletResponse response, Model model) {
    	response.setCharacterEncoding("UTF-8");
    	ExcelTemplate excelTemplate = ExcelTemplate.getInstance();
		String xmid = request.getParameter("XMID");
		String xmmc = regBaseXmService.get(xmid).getXmmc();
		List<List<RegBaseH>> listHlists = new ArrayList<List<RegBaseH>>();
		List<RegBaseH> listHs = new ArrayList<RegBaseH>();
		List<String> listZddms = regBaseZrzService.findZddms(xmid);
		
		if(listZddms.size()>0){
			for(String zddm : listZddms){
				RegBaseH regBaseH = new RegBaseH();
				regBaseH.setBdcdyh(zddm);
				listHs = regBaseHService.findZdHs(regBaseH);
				listHlists.add(listHs);
			}
		}
		//Global.getUserfilesBaseDir() + "/static/download" + File.separator+"(项目+栋)数据收集模板.xlsx";
		excelTemplate.readExcelTemplateByPath("D:" + File.separator + "download" + File.separator + "3、房屋登记信息导入模板.xlsx");//正式
//		excelTemplate.readExcelTemplateByPath("C:"+File.separator+"Users"+File.separator+"Administrator"+File.separator+"Desktop"+File.separator+"static"+File.separator+"download"+File.separator+"3、房屋登记信息导入模板.xlsx");//测试
		if(listHlists.size()>0){
			for(List<RegBaseH> hs : listHlists){
				for(RegBaseH h : hs){
					excelTemplate.createNewRow();
					excelTemplate.createCell(h.getXmmc());
					excelTemplate.createCell(h.getJgzwmc());
					excelTemplate.createCell(h.getCh());
					excelTemplate.createCell(h.getShbw());
					excelTemplate.createCell(h.getBdcdyh());
					excelTemplate.createCell(h.getZl());
				}
			}
		}
		
		
		OutputStream os = null;
		
		try {
			os = response.getOutputStream();
			response.setContentType("application/vnd.ms-excel");// 定义输出类型
			response.setHeader("content-disposition", "attachment;filename=" + new String((xmmc+".xlsx").getBytes("UTF-8"), "ISO8859-1"));
			excelTemplate.writeStream(os);
			System.out.println("写入完成");
		} catch (IOException e) {
			System.out.println("有异常");
			e.printStackTrace();
		}
//		finally {
//			
//			return "modules/reg/base/regBaseXmExportList";
//		}
//		excelTemplate.writeFilePath("D:"+File.separator+"123"+File.separator+xmmc+".xlsx");
		System.out.println("准备返回");
//		return "redirect:"+Global.getAdminPath()+"/reg/base/regBaseXm/exportXm?repage";
		return "modules/reg/base/regBaseXmExportList?repage";
	}
    
    public void createExcel(List<Map<String, String>> list, OutputStream output, HttpServletRequest request) {  
    	  
        Workbook tempWorkBook = null;
        Sheet tempSheet = null;  
        int rowIndex = 5;
        Row tempRow = null;
        Cell tempCell = null;
        InputStream inputstream = null;
        try {
            inputstream = request.getSession().getServletContext().getResourceAsStream("/templates/FZL-SUMMARY.xlsx");
            // 获取模板  
            tempWorkBook = new XSSFWorkbook(inputstream);
            // 获取模板sheet页  
            tempSheet = tempWorkBook.getSheetAt(0);
  
            // 用于统计的变量  
            double preAmount = 0;
            double plusAmount = 0;
            double reduceAmount = 0;
            double afterAmount = 0;
  
            // 将数据写入excel  
            for (int i = 0; i < list.size(); i++) {
  
                int cellNo = 1;
                // 获取指定行  
                tempRow = tempSheet.getRow(rowIndex++);
                // 获取指定行的单元格  
                tempCell = tempRow.getCell(cellNo++);
                // 给单元格设值
                tempCell.setCellValue(Integer.valueOf(list.get(i).get("accountsType")));
  
                double curpreAmount = Double.valueOf(list.get(i).get("preAmount"));  
                tempCell = tempRow.getCell(cellNo++);  
                tempCell.setCellValue(new BigDecimal(curpreAmount).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());  
  
                double curplusAmount = Double.valueOf(list.get(i).get(  
                        "plusAmount"));  
                tempCell = tempRow.getCell(cellNo++);  
                tempCell.setCellValue(new BigDecimal(curplusAmount).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());  
  
                double curreduceAmount = Double.valueOf(list.get(i).get("reduceAmount"));  
                tempCell = tempRow.getCell(cellNo++);  
                tempCell.setCellValue(new BigDecimal(curreduceAmount).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());  
  
                cellNo++;  
  
                double curafterAmount = Double.valueOf(list.get(i).get("afterAmount"));  
                tempCell = tempRow.getCell(cellNo++);  
                tempCell.setCellValue(new BigDecimal(curafterAmount).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());  
  
                plusAmount += Double.valueOf(list.get(i).get("preAmount"));  
                reduceAmount += Double.valueOf(list.get(i).get("plusAmount"));  
                afterAmount += Double.valueOf(list.get(i).get("reduceAmount"));  
                preAmount += Double.valueOf(list.get(i).get("afterAmount"));  
  
                if (i == (list.size() - 1)) {  
  
                    // 创建一行 统计  
                    tempRow = tempSheet.getRow(rowIndex);  
                    int cellNo2 = 2;
                    // 获取单元格  
                    tempCell = tempRow.getCell(cellNo2++);  
                    // 给单元格设值  
                    tempCell.setCellValue(new BigDecimal(plusAmount).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());  
                    tempCell = tempRow.getCell(cellNo2++);  
                    tempCell.setCellValue(new BigDecimal(reduceAmount).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());  
                    tempCell = tempRow.getCell(cellNo2++);  
                    tempCell.setCellValue(new BigDecimal(afterAmount).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());  
                    cellNo2++;  
                    tempCell = tempRow.getCell(cellNo2++);  
                    tempCell.setCellValue(new BigDecimal(preAmount).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());  
                }  
  
            }  
            // 将内容写入Excel  
            tempWorkBook.write(output);  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            try {
                output.close();
            } catch (IOException e) {
                e.printStackTrace();  
            }  
        }
  
    } 
    
    /*
     * 删除空项目
     */
    @RequiresPermissions("reg:base:regBaseXm:edit")
	@RequestMapping(value = "deleteTitle")
	public String deleteTitle(RegBaseXm regBaseXm, RedirectAttributes redirectAttributes) {
    	regBaseXmService.xmdelete(regBaseXm);//彻底删除
    	addMessage(redirectAttributes, "删除项目成功");
		return "redirect:"+Global.getAdminPath()+"/reg/base/regBaseXm/list";
    }
}