package com.zjrs.ggfw.portal.bpo.impl;

import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.mohrss.leaf.core.framework.domain.bpo.impl.BPOImpl;
import org.mohrss.leaf.core.framework.persistens.PageResult;
import org.mohrss.leaf.core.framework.util.CurrentUser;
import org.mohrss.leaf.core.framework.util.SecurityUtils;
import org.mohrss.leaf.core.framework.web.controller.AjaxResponse;
import com.zjrs.ggfw.portal.blo.CommBLO;
import com.zjrs.ggfw.portal.blo.SxblBLO;
import com.zjrs.ggfw.portal.blo.UserBLO;
import com.zjrs.ggfw.portal.bpo.SxblBPO;
import com.zjrs.ggfw.portal.dto.*;
import com.zjrs.ggfw.portal.po.*;
import com.zjrs.ggfw.shbzk.bpo.ShbzkBPO;
import com.zjrs.ggfw.shbzk.dto.ShbzkDTO;
import com.zjrs.ggfw.shbzk.dto.ShbzkEditDTO;
import com.zjrs.ggfw.shbzk.dto.ShbzkQueryDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Service
public class SxblBPOImpl extends BPOImpl implements SxblBPO {
	
	@Resource
    private SxblBLO sxblBLO;
	
	@Resource
    private UserBLO userBLO;
	
	@Resource
	ShbzkBPO shbzkBPO;

	@Resource
	CommBLO commBLO;

	@Override
	public PageResult getBjList(SxblQueryDTO dto) {
		//获取当前用户
    	CurrentUser cUser = SecurityUtils.getCurrentUser();
    	dto.setYhid(cUser.getUsername());
		return sxblBLO.getBjList(dto);
	}
	
	@Override
	public AjaxResponse getZzzdBjList(BjjdZzzdQueryDTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		
		//根据身份证号管理基本信息
		VAc01 vAc01 = userBLO.getAc01ByZj("01", dto.getSfzh());
		if(vAc01 != null) {
			dto.setZtid(vAc01.getZtid());
		}else {
			ajaxResponse.setAppcode("1");
			ajaxResponse.setMsg("该人员无相关办事进度信息！");
			return ajaxResponse;
		}
		
		PageResult pageResult = sxblBLO.getZzzdBjList(dto);
        ajaxResponse.setData("data", pageResult);
        ajaxResponse.setData("pagesize", dto.getPagesize());
        ajaxResponse.setData("pagenum", dto.getPagenum());
		
		return ajaxResponse;
	}
	
	@Override
	public AjaxResponse getSlxxHj(SlxxHjQueryDTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		
		//校验入参，办件流水号、受理流水号至少一个不为空
		if(StringUtils.isBlank(dto.getRsWbBjLsh()) && StringUtils.isBlank(dto.getSllsh())) {
			ajaxResponse.setAppcode("1");
			ajaxResponse.setMsg("办件流水号、受理流水号至少一个不为空！");
			return ajaxResponse;
		}

		Map<String,Object> resultMap = sxblBLO.getSlxxHj(dto);
		ajaxResponse.setData("data", resultMap);
		
		return ajaxResponse;
	}
	
	@Override
	public AjaxResponse getRsSxConfig(SxblInitDTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		//事项基本信息
    	VRsSxConfig vRsSxConfig = sxblBLO.getRsSxConfig(dto.getSxbm());
    	ajaxResponse.setData("data", vRsSxConfig);
    	//自助终端仅查看事项详细信息
    	if(!"4".equals(dto.getSlqd())) {
    		//若为综合科业务，从劳动系统查询基本信息
        	if("4408001002".equals(vRsSxConfig.getSxbm()) 
        			|| "4408001003".equals(vRsSxConfig.getSxbm())
        			|| "4408001004".equals(vRsSxConfig.getSxbm())
        			|| "4408001005".equals(vRsSxConfig.getSxbm())
					|| "4408001006".equals(vRsSxConfig.getSxbm())) {
            	CurrentUser cUser = SecurityUtils.getCurrentUser();//获取当前用户
            	//查询本地用户信息
            	VRsGgfwUser vRsGgfwUser = userBLO.getVRsGgfwUser(cUser.getUsername());
            	if("2".equals(vRsGgfwUser.getFwdx())) {//单位
            		Ab33 ab33 = sxblBLO.getAb33(vRsGgfwUser.getMc(),dto.getSxbm());
            		if(ab33 != null) {
            			ajaxResponse.setData("ldjbxx", ab33);
            		}
            	}else {
            		ajaxResponse.setAppcode("1");
    				ajaxResponse.setMsg("个人不可办理此项业务！");
    				return ajaxResponse;
            	}
        	}
    	}
		return ajaxResponse;
	}
	
	@Override
	public AjaxResponse getRsSxConfigFile(SxConfigFileQueryDTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		
		PageResult pageResult = sxblBLO.getRsSxConfigFile(dto);
        ajaxResponse.setData("data", pageResult);
        ajaxResponse.setData("pagesize", dto.getPagesize());
        ajaxResponse.setData("pagenum", dto.getPagenum());
		
		return ajaxResponse;
	}
	
	@Override
	public AjaxResponse getInitBj(SxblInitDTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();
    	Map<String,Object> bjMap = new HashMap<String,Object>();
    	
    	//当前用户
    	CurrentUser cUser = SecurityUtils.getCurrentUser();
    	bjMap.put("yhid", cUser.getUsername());
    	//查询本地用户信息
    	VRsGgfwUser vRsGgfwUser = userBLO.getVRsGgfwUser(cUser.getUsername());
    	bjMap.put("fwdx", vRsGgfwUser.getFwdx());
    	if(StringUtils.isBlank(vRsGgfwUser.getZtid())) {
    		bjMap.put("isjbxx", "0");
    		//从省统一认证带出信息
    		if("1".equals(vRsGgfwUser.getFwdx())) {//个人
    			bjMap.put("zjlx", vRsGgfwUser.getZjlx());
    			bjMap.put("zjhm", vRsGgfwUser.getZjhm());
    			bjMap.put("xm", vRsGgfwUser.getMc());
    			bjMap.put("xb", vRsGgfwUser.getXb());
				bjMap.put("sjhm", vRsGgfwUser.getSjhm());
    		}else if("2".equals(vRsGgfwUser.getFwdx())) {//单位
    			bjMap.put("tyshxydm", vRsGgfwUser.getZjhm());//统一社会信用代码
    			bjMap.put("dwmc", vRsGgfwUser.getMc());
    		}
    	}else {
    		bjMap.put("isjbxx", "1");
    		bjMap.put("ztid", vRsGgfwUser.getZtid());
    		//带出基本信息
    		if("1".equals(vRsGgfwUser.getFwdx())) {//个人
    			VAc01 vAc01 = userBLO.getAc01ById(vRsGgfwUser.getZtid());
    			bjMap.put("xm", vAc01.getAac003());//姓名
    			bjMap.put("zjlx", vAc01.getAac058());//证件类型
    			bjMap.put("zjhm", vAc01.getAac147());//证件号码
    			bjMap.put("xb", vAc01.getAac004());//性别
    			bjMap.put("sjhm", vAc01.getAac067());//手机号码
    		}else if("2".equals(vRsGgfwUser.getFwdx())) {//单位
    			VAb01 vAb01 = userBLO.getAb01ById(vRsGgfwUser.getZtid());
    			bjMap.put("tyshxydm", vAb01.getAab998());//统一社会信用代码
    			bjMap.put("dwmc", vAb01.getAab004());//单位名称
    			bjMap.put("dwlx", vAb01.getAab019());//单位类型
    			bjMap.put("dwdz", vAb01.getAae006());//单位地址
    			bjMap.put("ssdq", vAb01.getAab301());//所属地区
    		}
    	}
    	
    	//收件地址信息
    	VRsWbSjdz vRsWbSjdz = userBLO.getMrSjdz(cUser.getUsername());
    	if(vRsWbSjdz != null) {
    		bjMap.put("sjrxm", vRsWbSjdz.getSjrxm());
    		bjMap.put("sjrsj", vRsWbSjdz.getSjrdh());
    		bjMap.put("sjrdz", vRsWbSjdz.getSjdz());
    		bjMap.put("yzbm", vRsWbSjdz.getYzbm());
			bjMap.put("tcustProvince", vRsWbSjdz.getTcustProvince());
			bjMap.put("tcustCity", vRsWbSjdz.getTcustCity());
			bjMap.put("tcustCounty", vRsWbSjdz.getTcustCounty());
    	}
    	ajaxResponse.setData("data", bjMap);
    	
    	return ajaxResponse;
	}
	
	@Override
	public AjaxResponse getBjDetail(BjQueryDTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();
    	BjDTO bjDTO = new BjDTO();

		//获取办件信息
		VRsWbBj vRsWbBjList = sxblBLO.getRsWbBjById(dto);
		//校验是否有效，防止多端同时操作，删除记录
		if(vRsWbBjList == null) {
			ajaxResponse.setAppcode("1");
			ajaxResponse.setErrorMsg("该办件记录已被删除，无法办理，请重新申请！");
			return ajaxResponse;
		}
		//受理信息
		BeanUtils.copyProperties(vRsWbBjList, bjDTO);

    	//当前用户
    	CurrentUser cUser = SecurityUtils.getCurrentUser();
    	VRsGgfwUser vRsGgfwUser = userBLO.getVRsGgfwUser(cUser.getUsername());
    	//若为综合科业务，从劳动系统查询基本信息
    	if("4408001002".equals(vRsWbBjList.getSbsx()) 
    			|| "4408001003".equals(vRsWbBjList.getSbsx())
    			|| "4408001004".equals(vRsWbBjList.getSbsx())
    			|| "4408001005".equals(vRsWbBjList.getSbsx())
				|| "4408001006".equals(vRsWbBjList.getSbsx())) {
    		Ab33 ab33 = sxblBLO.getAb33(vRsGgfwUser.getMc(),vRsWbBjList.getSbsx());
    		ajaxResponse.setData("ldjbxx", ab33);
    	}else {
    		//获取办件主体基本信息
        	VRsSlxxJbxx vRsSlxxJbxx = sxblBLO.getRsSlxxJbxx(dto);
        	BeanUtils.copyProperties(vRsSlxxJbxx, bjDTO);
        	//关联省厅基本信息
        	if(StringUtils.isBlank(vRsGgfwUser.getZtid())) {
        		bjDTO.setIsjbxx("0");
        	}else {
        		bjDTO.setIsjbxx("1");
        	}
    	}
    	ajaxResponse.setData("data", bjDTO);
    	
    	return ajaxResponse;
	}
	
	@Override
	public AjaxResponse saveBj(BjDTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		DateTimeFormatter formatters = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    	//String dqsj = LocalDateTime.now().format(formatters);//当前时间
		String dqsj = this.commBLO.getCurrenttime();//当前时间
    	CurrentUser cUser = SecurityUtils.getCurrentUser();//当前用户
    	
		//校验必录
		String msg = "";
		if("1".equals(dto.getFwdx())) {//个人
			if(StringUtils.isBlank(dto.getZjlx())) {
				msg += "证件类型不可为空！";
			}
			if(StringUtils.isBlank(dto.getZjhm())) {
				msg += "证件号码不可为空！";
			}
			if(StringUtils.isBlank(dto.getXm())) {
				msg += "姓名不可为空！";
			}
			if(StringUtils.isBlank(dto.getXb())) {
				msg += "性别不可为空！";
			}
			if(StringUtils.isBlank(dto.getSjhm())) {
				msg += "手机号码不可为空！";
			}
			if(!"".equals(msg)) {
				ajaxResponse.setAppcode("1");
				ajaxResponse.setMsg(msg);
				return ajaxResponse;
			}
			
			//默认本人姓名、手机号为申报人姓名、手机号
			dto.setSbrxm(dto.getXm());
			dto.setSbrsj(dto.getSjhm());
		}else if("2".equals(dto.getFwdx())){//单位
			//综合科业务
	    	if("4408001002".equals(dto.getSbsx()) 
	    			|| "4408001003".equals(dto.getSbsx())
	    			|| "4408001004".equals(dto.getSbsx())
	    			|| "4408001005".equals(dto.getSbsx())
					|| "4408001006".equals(dto.getSbsx())) {
	    		if(StringUtils.isBlank(dto.getZtid())) {
					msg += "机关单位流水号不可为空！";
				}
	    		if(StringUtils.isBlank(dto.getZtlb())) {
					msg += "主体类别不可为空！";
				}
	    	}else {
	    		if(StringUtils.isBlank(dto.getDwmc())) {
					msg += "单位名称不可为空！";
				}
				if(StringUtils.isBlank(dto.getTyshxydm())) {
					msg += "统一社会信用代码不可为空！";
				}
				if(StringUtils.isBlank(dto.getDwlx())) {
					msg += "单位类型不可为空！";
				}
				if(StringUtils.isBlank(dto.getDwdz())) {
					msg += "单位地址不可为空！";
				}
				if(StringUtils.isBlank(dto.getSsdq())) {
					msg += "所属地区不可为空！";
				}
	    	}
			if(StringUtils.isBlank(dto.getSbrxm())) {
				msg += "申办人姓名不可为空！";
			}
			if(StringUtils.isBlank(dto.getSbrsj())) {
				msg += "申办人手机不可为空！";
			}
			if(!"".equals(msg)) {
				ajaxResponse.setAppcode("1");
				ajaxResponse.setMsg(msg);
				return ajaxResponse;
			}
		}else {
			ajaxResponse.setAppcode("1");
			ajaxResponse.setMsg("fwdx参数错误!");
		}
		List<String> filterCheck = Arrays.asList(new String[]{"4408008001", "4408008002","4408008003"});
		//收件地址校验
		if("1".equals(dto.getSfzcwlkd()) && !filterCheck.contains(dto.getSbsx())) {
			if(StringUtils.isBlank(dto.getJglqfs())) {
				if(StringUtils.isBlank(dto.getJglqfs())) {
					msg += "结果领取方式不可为空！";
				}
			}else if("2".equals(dto.getJglqfs())){//邮寄
				if(StringUtils.isBlank(dto.getSjrxm())) {
					msg += "收件人姓名不可为空！";
				}
				if(StringUtils.isBlank(dto.getSjrsj())) {
					msg += "收件人手机号码不可为空！";
				}
				if(StringUtils.isBlank(dto.getSjrdz())) {
					msg += "收件人地址不可为空！";
				}
			}
			if(!"".equals(msg)) {
				ajaxResponse.setAppcode("1");
				ajaxResponse.setMsg(msg);
				return ajaxResponse;
			}
		}
		
		//保存办件信息
		VRsWbBj vRsWbBj = new VRsWbBj();
		BeanUtils.copyProperties(dto, vRsWbBj);
		//默认参数
		if(StringUtils.isBlank(vRsWbBj.getRsWbBjLsh())) {
			vRsWbBj.setCjr(cUser.getOrgname());//经办人
			vRsWbBj.setCjsj(dqsj);
		}
		if("1".equals(dto.getFwdx())) {
			vRsWbBj.setZtmc(dto.getXm());
		}else {
			vRsWbBj.setZtmc(dto.getDwmc());
		}
		vRsWbBj.setYhid(cUser.getUsername());
		vRsWbBj.setSqzt("0");//申请状态
		vRsWbBj.setTsYxbz("1");//有效标志
		vRsWbBj.setZhxgr(cUser.getOrgname());//经办人
		vRsWbBj.setZhxgsj(dqsj);

		//结果领取方式为现场领取时，收件信息为空
		if(StringUtils.isNotBlank(dto.getJglqfs()) && dto.getJglqfs().equals("1")){
			vRsWbBj.setTcustProvince(null);
			vRsWbBj.setTcustCity(null);
			vRsWbBj.setTcustCounty(null);
		}

		vRsWbBj = sxblBLO.saveBj(vRsWbBj);
		
		//非综合科业务
    	if(!"4408001002".equals(dto.getSbsx()) 
    			&& !"4408001003".equals(dto.getSbsx())
    			&& !"4408001004".equals(dto.getSbsx())
    			&& !"4408001005".equals(dto.getSbsx())
				&& !"4408001006".equals(dto.getSbsx())) {
    		//保存办件主体基本信息
    		VRsSlxxJbxx vRsSlxxJbxx = new VRsSlxxJbxx();
    		BeanUtils.copyProperties(dto, vRsSlxxJbxx);
    		vRsSlxxJbxx.setRsWbBjLsh(vRsWbBj.getRsWbBjLsh());
    		sxblBLO.saveRsSlxxJbxx(vRsSlxxJbxx);
    	}
		//返回办件流水号
		ajaxResponse.setData("data", vRsWbBj);
		
		return ajaxResponse;
	}
	
	@Override
	public AjaxResponse deleteBj(BjQueryDTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		
		//获取办件信息
    	VRsWbBjList vRsWbBjList = sxblBLO.getRsWbBj(dto);
    	//校验是否有效，防止多端同时操作，删除记录
		if(vRsWbBjList == null) {
			ajaxResponse.setAppcode("1");
			ajaxResponse.setErrorMsg("该办件记录已被删除！");
			return ajaxResponse;
		}
    	//提交审核状态的不可删除（未提交及退回的可删除）
    	if(!"0".equals(vRsWbBjList.getSqzt()) && !"5".equals(vRsWbBjList.getSqzt())) {
    		ajaxResponse.setAppcode("1");
			ajaxResponse.setMsg("已提交的数据不可删除！");
			return ajaxResponse;
    	}
    	
    	VRsWbBj vRsWbBj = new VRsWbBj();
    	vRsWbBj.setRsWbBjLsh(dto.getRsWbBjLsh());
    	vRsWbBj.setTsYxbz("0");
    	sxblBLO.saveBj(vRsWbBj);
		
		return ajaxResponse;
	}
	
	@Override
	public AjaxResponse saveGrZfxxysqgk(ZfxxysqgkGrDTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		
		VRsWbZfxxysqgk vRsWbZfxxysqgk = new VRsWbZfxxysqgk();
		BeanUtils.copyProperties(dto, vRsWbZfxxysqgk);
		sxblBLO.saveGrZfxxysqgk(vRsWbZfxxysqgk);
		
		return ajaxResponse;
	}
	
	@Override
	public AjaxResponse saveDwZfxxysqgk(ZfxxysqgkDwDTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		
		VRsWbZfxxysqgk vRsWbZfxxysqgk = new VRsWbZfxxysqgk();
		BeanUtils.copyProperties(dto, vRsWbZfxxysqgk);
		sxblBLO.saveGrZfxxysqgk(vRsWbZfxxysqgk);
		
		return ajaxResponse;
	}
	
	@Override
	public AjaxResponse saveXfsxtc(XfsxtcDTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		
		VRsWbXfsxtc vRsWbXfsxtc = new VRsWbXfsxtc();
		BeanUtils.copyProperties(dto, vRsWbXfsxtc);
		sxblBLO.saveXfsxtc(vRsWbXfsxtc);
		
		return ajaxResponse;
	}
	
	@Override
	public AjaxResponse saveGccrcrd(GccrcrdDTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		
		//校验入参
		if("1".equals(dto.getLdhtqx()) && StringUtils.isBlank(dto.getLdhtjzrq())) {//固定期限
			ajaxResponse.setAppcode("1");
			ajaxResponse.setMsg("劳动合同截至日期不可为空！");
			return ajaxResponse;
		}
		if("1".equals(dto.getShzlqk()) && StringUtils.isBlank(dto.getShzlmc())) {//所获专利情况
			ajaxResponse.setAppcode("1");
			ajaxResponse.setMsg("所获专利名称不可为空！");
			return ajaxResponse;
		}
		
		VRsZjGccrcSq vRsZjGccrcSq = new VRsZjGccrcSq();
		BeanUtils.copyProperties(dto, vRsZjGccrcSq);
		sxblBLO.saveGccrcrd(vRsZjGccrcSq);
		
		return ajaxResponse;
	}
	
	@Override
	public AjaxResponse saveShbzkxx(ShbzkxxDTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		
		//当姓名为生僻字时，“生僻字描述”、“证件图片1”、“证件图片2”均不能为空，图像采用BASE64编码
		if(StringUtils.isNotBlank(dto.getSpzms())
				&& (StringUtils.isBlank(dto.getZjtpxm()) || StringUtils.isBlank(dto.getZjtpgh()))) {
			ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("当姓名为生僻字时,证件正反面图片不可为空!");
            return ajaxResponse;
		}
		
		VRsXxzxShbzkxx vRsXxzxShbzkxx = new VRsXxzxShbzkxx();
		BeanUtils.copyProperties(dto, vRsXxzxShbzkxx);
		vRsXxzxShbzkxx = sxblBLO.saveShbzkxx(vRsXxzxShbzkxx);
		ajaxResponse.setData("rsXxzxShbzkxxLsh", vRsXxzxShbzkxx.getRsXxzxShbzkxxLsh());
		
		return ajaxResponse;
	}
	
	@Override
	public AjaxResponse savePsamYwxx(PsamDTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		
		//获取办件信息
		BjQueryDTO bjQueryDTO = new BjQueryDTO();
		bjQueryDTO.setRsWbBjLsh(dto.getRsWbBjLsh());
		VRsWbBjList vRsWbBjList = sxblBLO.getRsWbBj(bjQueryDTO);
		//校验是否有效，防止多端同时操作，删除记录
		if(vRsWbBjList == null) {
			ajaxResponse.setAppcode("1");
			ajaxResponse.setErrorMsg("该办件记录已被删除，无法办理，请重新申请！");
			return ajaxResponse;
		}
		//卡回收时校验PSAM卡状态为“在用”
		if(("sblc-ywxx-psam-hs".equals(vRsWbBjList.getWwYwxxUrl())
				|| "sblc-ywxx-psam-shhs".equals(vRsWbBjList.getWwYwxxUrl())
				|| "sblc-ywxx-psam-ys".equals(vRsWbBjList.getWwYwxxUrl()))
				&& StringUtils.isNotBlank(dto.getPsambh())) {
			if(dto.getPsambh().contains(",") && dto.getPsambh().contains("-")){
				ajaxResponse.setAppcode("1");
				ajaxResponse.setErrorMsg("仅支持连续的卡号或不连续的卡号，若卡号不连续，请用“,”分隔！");
				return ajaxResponse;
		    }
			//连续卡号
			if(dto.getPsambh().contains("-")) {
				String[] psambhArr = dto.getPsambh().split("-");
				if(psambhArr.length > 2) {
					ajaxResponse.setAppcode("1");
					ajaxResponse.setErrorMsg("请按规则录入卡号，如440800100001-440800100002！");
					return ajaxResponse;
				}
				long beginNum;
		        long endNum;
		        try{
		        	beginNum = Long.parseLong(psambhArr[0].trim());
		        	endNum = Long.parseLong(psambhArr[1].trim());
		        }catch (Exception e){
		        	ajaxResponse.setAppcode("1");
		        	ajaxResponse.setErrorMsg("请录入正确的卡号！");
					return ajaxResponse;
		        }
		        if(beginNum > endNum) {
		        	ajaxResponse.setAppcode("1");
		        	ajaxResponse.setErrorMsg("请按编号顺序录入卡号，如440800100001-440800100002！");
					return ajaxResponse;
		        }
		        while (beginNum <= endNum) {
		        	VRsPsamRkxxb vRsPsamRkxxb = sxblBLO.getRsPsamRkxxByBh(beginNum+"");
		        	if(vRsPsamRkxxb == null) {
		        		ajaxResponse.setAppcode("1");
			        	ajaxResponse.setErrorMsg("PSAM卡号不存在，请检查！");
						return ajaxResponse;
		        	}else if(!"1".equals(vRsPsamRkxxb.getKzt())) {
		        		ajaxResponse.setAppcode("1");
			        	ajaxResponse.setErrorMsg("存在PSAM卡号状态异常的数据，请检查！");
						return ajaxResponse;
		        	}
		        	beginNum++;
		        }
			}else {
				String[] psambhArr = dto.getPsambh().split(",");
				for(int i=0;i<psambhArr.length;i++) {
					VRsPsamRkxxb vRsPsamRkxxb = sxblBLO.getRsPsamRkxxByBh(psambhArr[i]);
		        	if(vRsPsamRkxxb == null) {
		        		ajaxResponse.setAppcode("1");
			        	ajaxResponse.setErrorMsg("PSAM卡号不存在，请检查！");
						return ajaxResponse;
		        	}else if(!"1".equals(vRsPsamRkxxb.getKzt())) {
		        		ajaxResponse.setAppcode("1");
			        	ajaxResponse.setErrorMsg("存在PSAM卡号状态异常的数据，请检查！");
						return ajaxResponse;
		        	}
				}
			}
		}
		
		VRsPsamYwxxSqb VRsPsamYwxxSqb = new VRsPsamYwxxSqb();
		BeanUtils.copyProperties(dto, VRsPsamYwxxSqb);
		
		DateTimeFormatter formatters = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    	String czsj = LocalDateTime.now().format(formatters);//获取当前时间
		CurrentUser cUser = SecurityUtils.getCurrentUser();//当前用户
		VRsPsamYwxxSqb.setCjr(cUser.getOrgname());//经办人
		VRsPsamYwxxSqb.setCjsj(czsj);
		VRsPsamYwxxSqb.setZhxgr(cUser.getOrgname());//经办人
		VRsPsamYwxxSqb.setZhxgsj(czsj);
		sxblBLO.savePsamYwxx(VRsPsamYwxxSqb);
		
		return ajaxResponse;
	}
	
	@Override
	public AjaxResponse getZfxxysqgk(BjQueryDTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		
		VRsWbZfxxysqgk vRsWbZfxxysqgk = sxblBLO.getZfxxysqgk(dto);
		if(vRsWbZfxxysqgk == null) {
			vRsWbZfxxysqgk = new VRsWbZfxxysqgk();
			vRsWbZfxxysqgk.setRsWbBjLsh(dto.getRsWbBjLsh());
			
			CurrentUser cUser = SecurityUtils.getCurrentUser();//当前用户
			VRsGgfwUser vRsGgfwUser = userBLO.getVRsGgfwUser(cUser.getUsername());
			if("1".equals(vRsGgfwUser.getFwdx())) {//个人
				//是否关联基本信息
				if(StringUtils.isNotBlank(vRsGgfwUser.getZtid())) {
					VAc01 vAc01 = userBLO.getAc01ById(vRsGgfwUser.getZtid());
					vRsWbZfxxysqgk.setLxdh(vAc01.getAae005());
					vRsWbZfxxysqgk.setYzbm(vAc01.getAae007());
					vRsWbZfxxysqgk.setTxdz(vAc01.getAae006());
				}
				//从办件主体基本信息带出，防止修改了办件申请界面基本信息，无法体现
				VRsSlxxJbxx vRsSlxxJbxx = sxblBLO.getRsSlxxJbxx(dto);
				if("01".equals(vRsSlxxJbxx.getZjlx())) {
					vRsWbZfxxysqgk.setSfzhm(vRsSlxxJbxx.getZjhm());
				}
				vRsWbZfxxysqgk.setXm(vRsSlxxJbxx.getXm());
				vRsWbZfxxysqgk.setSjhm(vRsSlxxJbxx.getSjhm());
			}else if("2".equals(vRsGgfwUser.getFwdx())) {//单位
				//是否关联基本信息
				if(StringUtils.isNotBlank(vRsGgfwUser.getZtid())) {
					VAb01 vAb01 = userBLO.getAb01ById(vRsGgfwUser.getZtid());
					vRsWbZfxxysqgk.setFrdm(vAb01.getAab013()); 
					vRsWbZfxxysqgk.setLxr(vAb01.getAae004());
					vRsWbZfxxysqgk.setLxdh(vAb01.getAae005());
					vRsWbZfxxysqgk.setLxrdzyx(vAb01.getAae159());
				}
				//从办件主体基本信息带出，防止修改了办件申请界面基本信息，无法体现
				VRsSlxxJbxx vRsSlxxJbxx = sxblBLO.getRsSlxxJbxx(dto);
				vRsWbZfxxysqgk.setDwmc(vRsSlxxJbxx.getDwmc());
				vRsWbZfxxysqgk.setTyshxydm(vRsSlxxJbxx.getTyshxydm());
				vRsWbZfxxysqgk.setDwtxdz(vRsSlxxJbxx.getDwdz());
			}
		}
		ajaxResponse.setData("data", vRsWbZfxxysqgk);
		
		return ajaxResponse;
	}
	
	@Override
	public AjaxResponse getXfsxtc(BjQueryDTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		
		VRsWbXfsxtc vRsWbXfsxtc = sxblBLO.getXfsxtc(dto);
		if(vRsWbXfsxtc == null) {
			vRsWbXfsxtc = new VRsWbXfsxtc();
			vRsWbXfsxtc.setRsWbBjLsh(dto.getRsWbBjLsh());
			
			CurrentUser cUser = SecurityUtils.getCurrentUser();//当前用户
			VRsGgfwUser vRsGgfwUser = userBLO.getVRsGgfwUser(cUser.getUsername());
			if("1".equals(vRsGgfwUser.getFwdx())) {//个人
				//是否关联基本信息
				if(StringUtils.isNotBlank(vRsGgfwUser.getZtid())) {
					VAc01 vAc01 = userBLO.getAc01ById(vRsGgfwUser.getZtid());
					vRsWbXfsxtc.setLxdz(vAc01.getAae006());
					vRsWbXfsxtc.setHjszd(vAc01.getAac300());
				}
				//从办件主体基本信息带出，防止修改了办件申请界面基本信息，无法体现
				VRsSlxxJbxx vRsSlxxJbxx = sxblBLO.getRsSlxxJbxx(dto);
				if("01".equals(vRsSlxxJbxx.getZjlx())) {
					vRsWbXfsxtc.setSfzhm(vRsSlxxJbxx.getZjhm());
				}
				vRsWbXfsxtc.setXm(vRsSlxxJbxx.getXm());
				vRsWbXfsxtc.setXb(vRsSlxxJbxx.getXb());
				vRsWbXfsxtc.setLxdh(vRsSlxxJbxx.getSjhm());
			}else if("2".equals(vRsGgfwUser.getFwdx())) {//单位
				//是否关联基本信息
				if(StringUtils.isNotBlank(vRsGgfwUser.getZtid())) {
					VAb01 vAb01 = userBLO.getAb01ById(vRsGgfwUser.getZtid());
					vRsWbXfsxtc.setDwlxr(vAb01.getAae004());
					vRsWbXfsxtc.setDwlxdh(vAb01.getAae005());
				}
				//从办件主体基本信息带出，防止修改了办件申请界面基本信息，无法体现
				VRsSlxxJbxx vRsSlxxJbxx = sxblBLO.getRsSlxxJbxx(dto);
				vRsWbXfsxtc.setDwmc(vRsSlxxJbxx.getDwmc());
				vRsWbXfsxtc.setDwdz(vRsSlxxJbxx.getDwdz());
			}
		}
		ajaxResponse.setData("data", vRsWbXfsxtc);
		
		return ajaxResponse;
	}
	
	@Override
	public AjaxResponse getGccrcrd(BjQueryDTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		
		//根据办件流水号查询是否存在已保存的记录
		VRsZjGccrcSq vRsZjGccrcSq = sxblBLO.getGccrcSq(dto);
		//若无，则查询高层次人才是否存在历史申办结果
		if(vRsZjGccrcSq == null) {
			CurrentUser cUser = SecurityUtils.getCurrentUser();//当前用户
			//是否关联基本信息
			VRsGgfwUser vRsGgfwUser = userBLO.getVRsGgfwUser(cUser.getUsername());
			//初始化数据
			vRsZjGccrcSq = new VRsZjGccrcSq();
			vRsZjGccrcSq.setRsWbBjLsh(dto.getRsWbBjLsh());
			if(StringUtils.isNotBlank(vRsGgfwUser.getZtid())) {
				//从结果集查询
				VRsZjGccrc vRsZjGccrc = userBLO.getGccrc(vRsGgfwUser.getZtid());
				if(vRsZjGccrc != null) {
					BeanUtils.copyProperties(vRsZjGccrc, vRsZjGccrcSq);
					vRsZjGccrcSq.setSqlx("2");//变更人才卡
				}else {
					vRsZjGccrcSq.setSqlx("1");//申请人才卡
				}
				//填充基本信息
				VAc01 vAc01 = userBLO.getAc01ById(vRsGgfwUser.getZtid());
				vRsZjGccrcSq.setZjlx(vAc01.getAac058());
				vRsZjGccrcSq.setZjhm(vAc01.getAac147());
				vRsZjGccrcSq.setXm(vAc01.getAac003());
				vRsZjGccrcSq.setXb(vAc01.getAac004());
				vRsZjGccrcSq.setGj(vAc01.getAac161());
				vRsZjGccrcSq.setSjhm(vAc01.getAac067());
				if(vAc01.getAac006() != null && vAc01.getAac006() > 100000) {
					String csny = vAc01.getAac006().toString();
					vRsZjGccrcSq.setCsny(csny.substring(0, 4)+"-"+csny.substring(4, 6)+"-"+csny.substring(6));
				}
				vRsZjGccrcSq.setZzmm(vAc01.getAac024());
				//户口所在代码只取6位
				String hkszd = vAc01.getAac300() == null ?"":vAc01.getAac300();
				if(hkszd.length()>6){
					hkszd = hkszd.substring(0, 6);
				}
				vRsZjGccrcSq.setHkszd(hkszd);
			}else {
				vRsZjGccrcSq.setSqlx("1");//申请人才卡
			}
			
			//从办件主体基本信息带出，防止修改了办件申请界面基本信息，无法体现
			VRsSlxxJbxx vRsSlxxJbxx = sxblBLO.getRsSlxxJbxx(dto);
			vRsZjGccrcSq.setZjlx(vRsSlxxJbxx.getZjlx());
			vRsZjGccrcSq.setZjhm(vRsSlxxJbxx.getZjhm());
			vRsZjGccrcSq.setXm(vRsSlxxJbxx.getXm());
			vRsZjGccrcSq.setXb(vRsSlxxJbxx.getXb());
			vRsZjGccrcSq.setSjhm(vRsSlxxJbxx.getSjhm());
		}
		
		//返回数据
		ajaxResponse.setData("data", vRsZjGccrcSq);
		
		return ajaxResponse;
	}
	
	@Override
	public AjaxResponse getShbzkxx(BjQueryDTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		
		VRsXxzxShbzkxx vRsXxzxShbzkxx = sxblBLO.getRsXxzxShbzkxx(dto);
		if(vRsXxzxShbzkxx == null) {
			vRsXxzxShbzkxx = new VRsXxzxShbzkxx();
			vRsXxzxShbzkxx.setRsWbBjLsh(dto.getRsWbBjLsh());
			
			CurrentUser cUser = SecurityUtils.getCurrentUser();//当前用户
			VRsGgfwUser vRsGgfwUser = userBLO.getVRsGgfwUser(cUser.getUsername());
			if("1".equals(vRsGgfwUser.getFwdx())) {//个人
				//是否关联基本信息
				if(StringUtils.isNotBlank(vRsGgfwUser.getZtid())) {
					VAc01 vAc01 = userBLO.getAc01ById(vRsGgfwUser.getZtid());
					vRsXxzxShbzkxx.setLxdh(vAc01.getAae005());
					vRsXxzxShbzkxx.setYzbm(vAc01.getAae007());
					vRsXxzxShbzkxx.setTxdz(vAc01.getAae006());
					vRsXxzxShbzkxx.setGj(vAc01.getAac161());
				}
				//从办件主体基本信息带出，防止修改了办件申请界面基本信息，无法体现
				VRsSlxxJbxx vRsSlxxJbxx = sxblBLO.getRsSlxxJbxx(dto);
				if("01".equals(vRsSlxxJbxx.getZjlx())) {
					vRsXxzxShbzkxx.setZjlx(vRsSlxxJbxx.getZjlx());
					vRsXxzxShbzkxx.setZjhm(vRsSlxxJbxx.getZjhm());
					vRsXxzxShbzkxx.setShbzh(vRsSlxxJbxx.getZjhm());
					vRsXxzxShbzkxx.setXm(vRsSlxxJbxx.getXm());
					vRsXxzxShbzkxx.setLxsj(vRsSlxxJbxx.getSjhm());
				}else {
					ajaxResponse.setAppcode("1");
					ajaxResponse.setMsg("仅支持身份证用户办理！");
					return ajaxResponse;
				}
			}
		}
		ajaxResponse.setData("data", vRsXxzxShbzkxx);
		
		return ajaxResponse;
	}
	
	@Override
	public AjaxResponse getShbzkJkrz(BjQueryDTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		List<VRsXxzxShbzkxxJkrz> shbzkJkrzList = sxblBLO.getRsXxzxShbzkxxJkrz(dto);
		ajaxResponse.setData("data", shbzkJkrzList);
		return ajaxResponse;
	}
	
	@Override
	public AjaxResponse getPsamYwxx(BjQueryDTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		
		//根据办件流水号查询是否存在已保存的记录
		VRsPsamYwxxSqb vRsPsamYwxxSqb = sxblBLO.getRsPsamYwxx(dto);
		if(vRsPsamYwxxSqb == null) {
			vRsPsamYwxxSqb = new VRsPsamYwxxSqb();
			vRsPsamYwxxSqb.setRsWbBjLsh(dto.getRsWbBjLsh());
			CurrentUser cUser = SecurityUtils.getCurrentUser();//当前用户
			VRsGgfwUser vRsGgfwUser = userBLO.getVRsGgfwUser(cUser.getUsername());
			vRsPsamYwxxSqb.setZtid(vRsGgfwUser.getZtid());
		}
		ajaxResponse.setData("data", vRsPsamYwxxSqb);
		
		return ajaxResponse;
	}
	
	@Override
	public AjaxResponse getPsamAzjg() {
		AjaxResponse ajaxResponse = new AjaxResponse();
		List<DictDTO> dicts = new ArrayList<DictDTO>();
		
		List<VRsPsamAzjgxx> azjgList = sxblBLO.getRsPsamAzjgList();
		for(VRsPsamAzjgxx VRsPsamAzjgxx : azjgList) {
			DictDTO dictDTO = new DictDTO();
			dictDTO.setCode(VRsPsamAzjgxx.getRsPsamAzjgxxLsh());
			dictDTO.setDetail(VRsPsamAzjgxx.getJgmc());
			dicts.add(dictDTO);
		}
		
		ajaxResponse.setData("data", dicts);
		return ajaxResponse;
	}
	
	@Override
	public PageResult getSxclList(SxZllbQueryDTO dto) {
		return sxblBLO.getSxclList(dto);
	}
	
	@Override
	public PageResult getSxSdzlList(BjZllbQueryDTO dto) {
		return sxblBLO.getSxSdzlList(dto);
	}
	
	@Override
	public PageResult getBjSxclList(ClfjQueryDTO dto) {
		return sxblBLO.getBjSxclList(dto);
	}
	
	@Override
	public AjaxResponse uploadfile(ClfjDTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		
		VRsWbClfj vRsWbClfj = new VRsWbClfj();
		vRsWbClfj.setRsWbBjLsh(dto.getRsWbBjLsh());
		vRsWbClfj.setZllbid(dto.getZllbid());
		vRsWbClfj.setTsYxbz("1");
		DateTimeFormatter formatters = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    	//获取当前时间
    	String dqsj = LocalDateTime.now().format(formatters);
		CurrentUser cUser = SecurityUtils.getCurrentUser();
		vRsWbClfj.setCjr(cUser.getOrgname());//经办人
		vRsWbClfj.setCjsj(dqsj);
		for(FjDTO fjDTO : dto.getFjList()) {
			vRsWbClfj.setWjmc(fjDTO.getWjmc());
			vRsWbClfj.setTyccid(fjDTO.getTyccid());
			sxblBLO.saveClfj(vRsWbClfj);
		}
		
		return ajaxResponse;
	}
	
	@Override
	public void delClfj(ClfjDelDTO dto) {
		sxblBLO.delClfj(dto);
	}
	
	@Override
	public AjaxResponse submitBj(BjQueryDTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		
		//获取办件信息
		VRsWbBjList vRsWbBjList = sxblBLO.getRsWbBj(dto);//换成查List取第一条，不然一件事的会报错
		//校验是否有效，防止多端同时操作，删除记录
		if(vRsWbBjList == null) {
			ajaxResponse.setAppcode("1");
			ajaxResponse.setErrorMsg("该办件记录已被删除，无法办理，请重新申请！");
			return ajaxResponse;
		}
		//未提交或退回可提交
		if(!"0".equals(vRsWbBjList.getSqzt()) && !"5".equals(vRsWbBjList.getSqzt())) {
			ajaxResponse.setAppcode("1");
			ajaxResponse.setErrorMsg("该办件信息已提交！");
			return ajaxResponse;
		}
		//校验事项是否已下架
		if(!"1".equals(vRsWbBjList.getFbzt())) {
			ajaxResponse.setAppcode("1");
			ajaxResponse.setErrorMsg("该事项已下架！");
			return ajaxResponse;
		}
		//校验资料是否都已经上传
		if(!sxblBLO.isUploadAllClfj(dto.getRsWbBjLsh())) {
			ajaxResponse.setAppcode("1");
			ajaxResponse.setErrorMsg("存在必要材料未上传附件，请检查！");
			return ajaxResponse;
		}
		
		DateTimeFormatter formatters = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    	//String czsj = LocalDateTime.now().format(formatters);//获取当前时间
		String czsj = this.commBLO.getCurrenttime();//获取当前时间
    	CurrentUser cUser = SecurityUtils.getCurrentUser();//当前用户
    	
		//综合科业务
    	if("4408001002".equals(vRsWbBjList.getSbsx()) 
    			|| "4408001003".equals(vRsWbBjList.getSbsx())
    			|| "4408001004".equals(vRsWbBjList.getSbsx())
    			|| "4408001005".equals(vRsWbBjList.getSbsx())
				|| "4408001006".equals(vRsWbBjList.getSbsx())){
    		if(StringUtils.isBlank(vRsWbBjList.getZtid())) {
    			ajaxResponse.setAppcode("1");
				ajaxResponse.setErrorMsg("劳动系统不存在记录，不可提交！");
				return ajaxResponse;
    		}
	    } else {
    		//查询是否关联到基本信息，若无则把采集的基本信息写入省厅基本信息，并保存对应关系
        	VRsGgfwUser vRsGgfwUser = userBLO.getVRsGgfwUser(cUser.getUsername());
    		if(StringUtils.isBlank(vRsGgfwUser.getZtid())) {
    			//保存省基本信息表
    			String ztid = UUID.randomUUID().toString().replace("-", "");
    			DateTimeFormatter tsFormatters = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
    	    	String aae036 = LocalDateTime.now().format(tsFormatters);//获取当前时间

    			VRsSlxxJbxx vRsSlxxJbxx = sxblBLO.getRsSlxxJbxx(dto);
    			if("1".equals(vRsWbBjList.getFwdx())) {//个人
    				//再次查询是否存在基本信息（1、非身份证登录用户；2、基本信息在申办数据保存过程中新增了数据）
    				VAc01 vAc01 = userBLO.getAc01ByZj(vRsSlxxJbxx.getZjlx(), vRsSlxxJbxx.getZjhm());
    				if(vAc01 == null) {
    					vAc01 = new VAc01();
    					vAc01.setAac003(vRsSlxxJbxx.getXm());
    					vAc01.setAac004(vRsSlxxJbxx.getXb());
    					vAc01.setAac058(vRsSlxxJbxx.getZjlx());
						//证件类型为身份证时，国籍默认为中国
						if("01".equals(vRsSlxxJbxx.getZjlx())){
							vAc01.setAac161("CHN");
						}
    					vAc01.setAac147(vRsSlxxJbxx.getZjhm());
    					vAc01.setAac067(vRsSlxxJbxx.getSjhm());
    					vAc01.setZtid(ztid);
    					vAc01.setLy("2");//来源：本地创建
    					vAc01.setAae036(Long.parseLong(aae036));//经办时间
    					vAc01.setBae001("440800");
    					vAc01.setBae002("公共服务用户");
    					vAc01.setBae003(czsj);
    					vAc01.setBae004("公共服务用户");
    					vAc01.setBae005(czsj);
    					userBLO.saveAc01(vAc01);
    				}else {
    					//获取基本信息主键
    					ztid = vAc01.getZtid();
    				}
    			}else if("2".equals(vRsWbBjList.getFwdx())) {//单位
    				//再次查询是否存在基本信息（1、非统一社会信用代码登录用户；2、基本信息在申办数据保存过程中新增了数据）
    				VAb01 vAb01 = userBLO.getAb01ByAab998OrAab004(vRsSlxxJbxx.getTyshxydm(), vRsSlxxJbxx.getDwmc());
    				if(vAb01 == null) {
    					vAb01 = new VAb01();
    					vAb01.setAab998(vRsSlxxJbxx.getTyshxydm());
    					vAb01.setAab004(vRsSlxxJbxx.getDwmc());
    					vAb01.setAab019(vRsSlxxJbxx.getDwlx());
    					vAb01.setAae006(vRsSlxxJbxx.getDwdz());
    					vAb01.setAab301(vRsSlxxJbxx.getSsdq());
    					vAb01.setZtid(ztid);
    					vAb01.setLy("2");//来源：本地创建
    					vAb01.setAae036(Long.parseLong(aae036));//经办时间
    					vAb01.setBae001("440800");
    					vAb01.setBae002("公共服务用户");
    					vAb01.setBae003(czsj);
    					vAb01.setBae004("公共服务用户");
    					vAb01.setBae005(czsj);
    					userBLO.saveAb01(vAb01);
    				}else {
    					//获取基本信息主键
    					ztid = vAb01.getZtid();
    				}
    			}
    			//保存对应关系
    			vRsGgfwUser.setZtid(ztid);
    			userBLO.updateRsGgfwUser(vRsGgfwUser);
    			
    			//记录省厅主键
    			vRsWbBjList.setZtid(ztid);
    		}else {
    			//防止在未提交保存基本信息时，新增多个办件信息
    			vRsWbBjList.setZtid(vRsGgfwUser.getZtid());
    		}
    		
    		//湛江市高层次人才认定
    		if("4408005901".equals(vRsWbBjList.getSbsx())) {
    			//校验是否存在同个事项在审核流程
    			if(sxblBLO.isSxsh(vRsWbBjList.getSbsx(), vRsWbBjList.getYhid())) {
    				ajaxResponse.setAppcode("1");
    				ajaxResponse.setErrorMsg("该事项已有正在办理中的业务，您可在【办事进度】栏目查询办理进度，请勿重复申请！");
    				return ajaxResponse;
    			}
    			//查询高层次人才结果信息
    			VRsZjGccrc vRsZjGccrc = userBLO.getGccrc(vRsWbBjList.getZtid());
    			if(vRsZjGccrc != null) {
    				//未签发状态不可提交
    				if("2".equals(vRsZjGccrc.getQfzt())) {
    					ajaxResponse.setAppcode("1");
    					ajaxResponse.setErrorMsg("您的人才卡待签发，请勿重复申请！");
    					return ajaxResponse;
    				}
    				//存在人才卡信息，不可提交认定申请（防止外网提交认定申请前，内网已新增记录）
    				VRsZjGccrcSq vRsZjGccrcSq = sxblBLO.getGccrcSq(dto);
    				if("1".equals(vRsZjGccrcSq.getSqlx())) {//认定申请
    					ajaxResponse.setAppcode("1");
    					ajaxResponse.setErrorMsg("您已认定人才卡，请勿重复申请！");
    					return ajaxResponse;
    				}
    			}
    		}
    		
    		//PSAM卡回收、损坏回收、遗失等业务，校验同一个PSAM卡号不可多次提交
    		if("sblc-ywxx-psam-hs".equals(vRsWbBjList.getWwYwxxUrl()) 
    				|| "sblc-ywxx-psam-shhs".equals(vRsWbBjList.getWwYwxxUrl()) 
    				|| "sblc-ywxx-psam-ys".equals(vRsWbBjList.getWwYwxxUrl())) {
    			if(!checkPsamBh(vRsWbBjList)) {
    				ajaxResponse.setAppcode("1");
					ajaxResponse.setErrorMsg("您办理的psam卡业务，已有在办理其他业务，不能重复申请！");
					return ajaxResponse;
    			}
    		}
    		
    		//人才中心业务，同一个人/单位，同一事项只能有一个在审核流程
    		if("44080062,44080204,44080309,44080406,44081107,44082304,44082506,44084004,44088105,44088203,44088306".contains(vRsWbBjList.getSxlb())){
    			//校验是否存在同个事项在审核流程
    			if(sxblBLO.isSxsh(vRsWbBjList.getSbsx(), vRsWbBjList.getYhid())) {
    				ajaxResponse.setAppcode("1");
    				ajaxResponse.setErrorMsg("该事项已有正在办理中的业务，您可在【办事进度】栏目查询办理进度，请勿重复申请！");
    				return ajaxResponse;
    			}
    		}
    	}

    	//查询是否属于一件事的,存在则更新
		List<RsWbBjSub> subBjList = sxblBLO.getSubBjXx(dto.getRsWbBjLsh());
    	if (CollectionUtils.isNotEmpty(subBjList)) {
			for (RsWbBjSub rsWbBjSub: subBjList){
				rsWbBjSub.setZtid(vRsWbBjList.getZtid());
				rsWbBjSub.setSqzt("1");//待审核
				rsWbBjSub.setShyj("");//清空审核意见
				rsWbBjSub.setSqsj(czsj);
				rsWbBjSub.setZhxgr(cUser.getOrgname());//经办人
				rsWbBjSub.setZhxgsj(czsj);
				sxblBLO.updateSubBj(rsWbBjSub);
			}
		}

		//修改提交状态
		VRsWbBj vRsWbBj = new VRsWbBj();
		vRsWbBj.setRsWbBjLsh(dto.getRsWbBjLsh());
		vRsWbBj.setZtid(vRsWbBjList.getZtid());
		vRsWbBj.setSqzt("1");//待审核
		vRsWbBj.setShyj("");//清空审核意见
		vRsWbBj.setSqsj(czsj);
		vRsWbBj.setZhxgr(cUser.getOrgname());//经办人
		vRsWbBj.setZhxgsj(czsj);
		sxblBLO.saveBj(vRsWbBj);
		
		return ajaxResponse;
	}
	
	private boolean checkPsamBh(VRsWbBjList vRsWbBjList) {
		boolean flag = true;
		
		//查询PSAM卡其他办件
		List<VRsWbBjList> psamBjList = sxblBLO.getPsamBj(vRsWbBjList);
		if(psamBjList != null) {
			//查询当前PSAM卡编号
			VRsPsamYwxxSqb vRsPsamYwxxSqb = sxblBLO.getRsPsamBh(vRsWbBjList);
			//若为单个号，则拼接一个“,”，统一处理
			if(!vRsPsamYwxxSqb.getPsambh().contains(",") && !vRsPsamYwxxSqb.getPsambh().contains("-")) {
				vRsPsamYwxxSqb.setPsambh(vRsPsamYwxxSqb.getPsambh()+",");
			}
			for(VRsWbBjList vRsWbBjListTemp : psamBjList) {
				//查询其他办件PSAM卡编号
				VRsPsamYwxxSqb vRsPsamYwxxSqbTemp = sxblBLO.getRsPsamBh(vRsWbBjListTemp);
				//一窗通办数据可能还没保存业务信息
				if(vRsPsamYwxxSqbTemp == null) {
					continue;
				}else {
					if(StringUtils.isBlank(vRsPsamYwxxSqbTemp.getPsambh())) {
						continue;
					}
				}
				//若为单个号，则拼接一个“,”，统一处理
				if(!vRsPsamYwxxSqbTemp.getPsambh().contains(",") && !vRsPsamYwxxSqbTemp.getPsambh().contains("-")) {
					vRsPsamYwxxSqbTemp.setPsambh(vRsPsamYwxxSqbTemp.getPsambh()+",");
				}
				if(vRsPsamYwxxSqb.getPsambh().contains(",") 
						&& vRsPsamYwxxSqbTemp.getPsambh().contains(",")) {//逗号分隔
					//当前PSAM卡编号
					String[] psambhs=vRsPsamYwxxSqb.getPsambh().split(",");
					for(int i=0;i<psambhs.length;i++) {
						//其他在办业务PSAM卡编号是否包含当前PSAM卡编号
						if(vRsPsamYwxxSqbTemp.getPsambh().contains(psambhs[i])) {
							return false;
						}
					}
				}else if(vRsPsamYwxxSqb.getPsambh().contains("-") 
						&& vRsPsamYwxxSqbTemp.getPsambh().contains("-")) {//减号分隔
					//当前PSAM卡编号
					String[] psambhs = vRsPsamYwxxSqb.getPsambh().split("-");
					long psambhBeginNum = Long.parseLong(psambhs[0].trim());
			        long psambhEndNum = Long.parseLong(psambhs[1].trim());
					//其他在办业务PSAM卡编号
					String[] psambhTemps = vRsPsamYwxxSqbTemp.getPsambh().split("-");
					long psambhTempBeginNum = Long.parseLong(psambhTemps[0].trim());
			        long psambhTempEndNum = Long.parseLong(psambhTemps[1].trim());
			        if((psambhBeginNum >= psambhTempBeginNum && psambhBeginNum <= psambhTempEndNum) 
			        		|| (psambhEndNum >= psambhTempBeginNum && psambhEndNum <= psambhTempEndNum) 
			        		|| (psambhBeginNum <= psambhTempBeginNum && psambhEndNum >= psambhTempEndNum)) {
			        	return false;
			        }
				}else {//逗号，减号分隔
					String[] psambhs;
					String[] psambhTemps;
					if(vRsPsamYwxxSqb.getPsambh().contains(",")) {
						psambhs = vRsPsamYwxxSqb.getPsambh().split(",");
						psambhTemps = vRsPsamYwxxSqbTemp.getPsambh().split("-");
					}else {
						psambhs = vRsPsamYwxxSqbTemp.getPsambh().split(",");
						psambhTemps = vRsPsamYwxxSqb.getPsambh().split("-");
					}
					long psambhBeginNum = Long.parseLong(psambhTemps[0].trim());
			        long psambhEndNum = Long.parseLong(psambhTemps[1].trim());
			        for(int i=0;i<psambhs.length;i++) {
			        	long psambhNum = Long.parseLong(psambhs[i]);
			        	if(psambhNum >= psambhBeginNum && psambhNum <= psambhEndNum) {
			        		return false;
			        	}
			        }
				}
			}
		}
		return flag;
	}
	
	@Override
	public AjaxResponse submitShbzk(ShbzkBjDTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		
		//获取办件信息
		BjQueryDTO bjQueryDTO = new BjQueryDTO();
		bjQueryDTO.setRsWbBjLsh(dto.getRsWbBjLsh());
		VRsWbBjList vRsWbBjList = sxblBLO.getRsWbBj(bjQueryDTO);
		//校验是否有效，防止多端同时操作，删除记录
		if(vRsWbBjList == null) {
			ajaxResponse.setAppcode("1");
			ajaxResponse.setErrorMsg("该办件记录已被删除，无法办理，请重新申请！");
			return ajaxResponse;
		}
		//校验事项是否已下架
		if(!"1".equals(vRsWbBjList.getFbzt())) {
			ajaxResponse.setAppcode("1");
			ajaxResponse.setErrorMsg("该事项已下架！");
			return ajaxResponse;
		}
		
		DateTimeFormatter formatters = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    	String czsj = LocalDateTime.now().format(formatters);//获取当前时间
    	CurrentUser cUser = SecurityUtils.getCurrentUser();//当前用户
    	
    	//申请类型1查询2申办3补换卡4修改5挂失6解挂7激活启用
    	if("2".equals(dto.getSqlx()) || "3".equals(dto.getSqlx()) || "4".equals(dto.getSqlx())) {
    		VRsXxzxShbzkxx vRsXxzxShbzkxx = sxblBLO.getRsXxzxShbzkxx(bjQueryDTO);
    		if("2".equals(dto.getSqlx())) {//2申办
    			ShbzkDTO shbzkDTO = new ShbzkDTO();
    			BeanUtils.copyProperties(vRsXxzxShbzkxx,shbzkDTO);
    			if(StringUtils.isBlank(dto.getSjly())){//数据来源
					shbzkDTO.setSjly("2");
				}else{
					shbzkDTO.setSjly(dto.getSjly());
				}
    			ajaxResponse = shbzkBPO.applyShbzk(shbzkDTO);
    		}else if("3".equals(dto.getSqlx())) {//3补换卡
    			ShbzkDTO shbzkDTO = new ShbzkDTO();
    			BeanUtils.copyProperties(vRsXxzxShbzkxx,shbzkDTO);
				if(StringUtils.isBlank(dto.getSjly())){//数据来源
					shbzkDTO.setSjly("2");
				}else{
					shbzkDTO.setSjly(dto.getSjly());
				}
    			ajaxResponse = shbzkBPO.changeShbzk(shbzkDTO);
    		}else if("4".equals(dto.getSqlx())) {//4修改
    			ShbzkEditDTO shbzkEditDTO = new ShbzkEditDTO();
    			BeanUtils.copyProperties(vRsXxzxShbzkxx,shbzkEditDTO);
    			ajaxResponse = shbzkBPO.editShbzk(shbzkEditDTO);
    		}
    	}else {//1查询5挂失6解挂7激活启用
			if(StringUtils.isBlank(dto.getShbzh())) {
				ajaxResponse.setAppcode("1");
	            ajaxResponse.setErrorMsg("社会保障号不可为空!");
	            return ajaxResponse;
			}
			if(StringUtils.isBlank(dto.getSscs())) {
				dto.setSscs("440800");//默认湛江市
			}
			//记录接口调用日志，用于小程序、网办查看历史办件记录
	    	VRsXxzxShbzkxxJkrz vRsXxzxShbzkxxJkrz = new VRsXxzxShbzkxxJkrz();
			ShbzkQueryDTO shbzkQueryDTO = new ShbzkQueryDTO();
			BeanUtils.copyProperties(dto,shbzkQueryDTO);
			if("1".equals(dto.getSqlx())) {//1查询
		    	vRsXxzxShbzkxxJkrz.setCznw("社保卡号："+ dto.getSbkh() +"，社会保障卡查询");
				ajaxResponse = shbzkBPO.getShbzk(shbzkQueryDTO);
			}else if("5".equals(dto.getSqlx())) {//挂失
				vRsXxzxShbzkxxJkrz.setCznw("社保卡号："+ dto.getSbkh() +"，临时挂失");
				ajaxResponse = shbzkBPO.setShbzk("网上临时挂失", shbzkQueryDTO);
			}else if("6".equals(dto.getSqlx())) {//解挂
				vRsXxzxShbzkxxJkrz.setCznw("社保卡号："+ dto.getSbkh() +"，解除挂失");
				ajaxResponse = shbzkBPO.setShbzk("网上解除挂失", shbzkQueryDTO);
			}else if("7".equals(dto.getSqlx())) {//激活启用
				vRsXxzxShbzkxxJkrz.setCznw("社保卡号："+ dto.getSbkh() +"，激活启用");
				ajaxResponse = shbzkBPO.startUseShbzk(shbzkQueryDTO);
			}
			if("0".equals(ajaxResponse.getAppcode())) {
				vRsXxzxShbzkxxJkrz.setCzjg("1");//成功
			}else {
				vRsXxzxShbzkxxJkrz.setCzjg("0");//失败
			}
			VRsGgfwUser vRsGgfwUser = userBLO.getVRsGgfwUser(cUser.getUsername());
			vRsXxzxShbzkxxJkrz.setOpername(vRsGgfwUser.getMc());
			vRsXxzxShbzkxxJkrz.setSbyy(ajaxResponse.getMsg());
			vRsXxzxShbzkxxJkrz.setRsWbBjLsh(dto.getRsWbBjLsh());
			sxblBLO.saveRsXxzxShbzkxxJkrz(vRsXxzxShbzkxxJkrz);
		}
    	
    	//调用省社保卡接口成功后，办结事项
    	if("0".equals(ajaxResponse.getAppcode())) {
    		//修改提交状态
    		VRsWbBj vRsWbBj = new VRsWbBj();
    		vRsWbBj.setRsWbBjLsh(dto.getRsWbBjLsh());
    		vRsWbBj.setSqzt("3");//审核通过
    		vRsWbBj.setSqsj(czsj);
    		vRsWbBj.setZhxgr(cUser.getOrgname());//经办人
    		vRsWbBj.setZhxgsj(czsj);
    		sxblBLO.saveBj(vRsWbBj);
    	}
		
		return ajaxResponse;
	}
	
	@Override
	public AjaxResponse getRsPsamAzfkList(PsamFkxxListQueryDTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		//校验案件是否办结
		VRsPsamYwxxSqb vRsPsamYwxxSqb = sxblBLO.getRsPsamYwxxSqbById(dto.getRsPsamYwxxSqbLsh());//查询申请业务信息
		VRsWbBjList vRsWbBjList = sxblBLO.getRsWbBjLchj(vRsPsamYwxxSqb.getSllsh(), vRsPsamYwxxSqb.getRsWbBjLsh());
		//办件审核通过方可反馈
		if(vRsWbBjList != null) {
			if("3".equals(vRsWbBjList.getSqzt())) {
				List<VRsPsamAzfkxxb> azfkList = sxblBLO.getRsPsamAzfkList(dto);
				ajaxResponse.setData("data", azfkList);
			}else if("4".equals(vRsWbBjList.getSqzt())) {//审核不通过
				ajaxResponse.setAppcode("1");
				ajaxResponse.setErrorMsg("您当前办理的PSAM卡申领审核不通过！");
			}else if("6".equals(vRsWbBjList.getSqzt())) {//不予受理
				ajaxResponse.setAppcode("1");
				ajaxResponse.setErrorMsg("您当前办理的PSAM卡申领不予受理！");
			}else {
				ajaxResponse.setAppcode("1");
				ajaxResponse.setErrorMsg("您当前办理的PSAM卡申领尚未审核通过，请在审核通过后反馈！");
			}
		}else {
			ajaxResponse.setAppcode("1");
			ajaxResponse.setErrorMsg("未查询到记录！");
		}
		return ajaxResponse;
	}
	
	@Override
	public AjaxResponse getRsPsamAzfkxx(PsamFkxxQueryDTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		Map<String,Object> azfkxxMap = sxblBLO.getRsPsamAzfkxx(dto);
		ajaxResponse.setData("data", azfkxxMap);
		return ajaxResponse;
	}
	
	@Override
	public AjaxResponse saveRsPsamAzfkxx(PsamFkxxDTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		VRsPsamAzfkxxb vRsPsamAzfkxxb = new VRsPsamAzfkxxb();
		BeanUtils.copyProperties(dto,vRsPsamAzfkxxb);
		sxblBLO.saveRsPsamAzfkxx(vRsPsamAzfkxxb);
		return ajaxResponse;
	}
	
	@Override
	public AjaxResponse submitRsPsamAzfkxx(PsamFkxxListQueryDTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		//校验与安装机构统一社会信用代码是否一致
		CurrentUser cUser = SecurityUtils.getCurrentUser();//当前用户
		VRsGgfwUser vRsGgfwUser = userBLO.getVRsGgfwUser(cUser.getUsername());
		if(StringUtils.isNotBlank(vRsGgfwUser.getZjhm())) {
			//获取申请表信息
			VRsPsamYwxxSqb vRsPsamYwxxSqb = sxblBLO.getRsPsamYwxxSqbById(dto.getRsPsamYwxxSqbLsh());
			//获取安装机构信息
			VRsPsamAzjgxx vRsPsamAzjgxx = sxblBLO.getRsPsamAzjgxxById(vRsPsamYwxxSqb.getRsPsamAzjgxxLsh());
			if(vRsGgfwUser.getZjhm().equals(vRsPsamAzjgxx.getCorpCode())) {
				sxblBLO.submitRsPsamAzfkxx(dto);
			}else {
				ajaxResponse.setAppcode("1");
				ajaxResponse.setErrorMsg("提交失败，请使用PSAM卡申领的安装机构进行反馈！");
			}
		}else {
			ajaxResponse.setAppcode("1");
			ajaxResponse.setErrorMsg("提交失败，当前登录用户无统一社会信用代码数据！");
		}
		return ajaxResponse;
	}
	
	@Override
	public AjaxResponse saveRsJkHcp(HcpDTO dto) {

		AjaxResponse ajaxResponse = new AjaxResponse();
		try {
			//校验入参，办件流水号、受理流水号至少一个不为空
			if (StringUtils.isBlank(dto.getRsWbBjLsh()) && StringUtils.isBlank(dto.getSllsh())) {
				ajaxResponse.setAppcode("1");
				ajaxResponse.setMsg("办件流水号、受理流水号至少一个不为空！");
				return ajaxResponse;
			}

			DateTimeFormatter formatters = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
			String czsj = LocalDateTime.now().format(formatters);//获取当前时间
			CurrentUser cUser = SecurityUtils.getCurrentUser();//当前用户

			//判断是否是子项
			Map<String, String> resultMap = commBLO.getSubBjXx(dto);
			String[] wblshs = null;
			String[] sllshs = null;
			if (null != resultMap) {
				wblshs = resultMap.get("WBLSHS").split(",");
				sllshs = resultMap.get("SLLSHS").split(",");
			}
			String[] pjjd = dto.getEvalprocess().split(",");
			for (int i = 0; i < pjjd.length; i++) {
				if (null != resultMap) {
					for (int j = 0; j < wblshs.length; j++) {
						VRsJkHcp vRsJkHcp = new VRsJkHcp();
						BeanUtils.copyProperties(dto, vRsJkHcp);
						vRsJkHcp.setRsWbBjLsh(wblshs[j]);
						if (!sllshs[j].equals("empty")) {
							vRsJkHcp.setSllsh(sllshs[j]);
						}
						vRsJkHcp.setEvalprocess(pjjd[i]);
						vRsJkHcp.setPjlx("1");//主动评价
						vRsJkHcp.setTszt("1");//待推送
						vRsJkHcp.setYhpjr(cUser.getOrgname());//经办人
						vRsJkHcp.setYhpjsj(czsj);//评价时间
						sxblBLO.saveRsJkHcp(vRsJkHcp);
					}
				} else {
					VRsJkHcp vRsJkHcp = new VRsJkHcp();
					BeanUtils.copyProperties(dto, vRsJkHcp);
					vRsJkHcp.setEvalprocess(pjjd[i]);
					vRsJkHcp.setPjlx("1");//主动评价
					vRsJkHcp.setTszt("1");//待推送
					vRsJkHcp.setYhpjr(cUser.getOrgname());//经办人
					vRsJkHcp.setYhpjsj(czsj);//评价时间
					sxblBLO.saveRsJkHcp(vRsJkHcp);
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
    	
		return ajaxResponse;
	}
	
	@Override
	public AjaxResponse getRsJkHcp(SlxxHjQueryDTO dto){
		AjaxResponse ajaxResponse = new AjaxResponse();
		//校验入参，办件流水号、受理流水号至少一个不为空
		if(StringUtils.isBlank(dto.getRsWbBjLsh()) && StringUtils.isBlank(dto.getSllsh())) {
			ajaxResponse.setAppcode("1");
			ajaxResponse.setMsg("办件流水号、受理流水号至少一个不为空！");
			return ajaxResponse;
		}
		List<VRsJkHcp> hcpList = sxblBLO.getRsJkHcp(dto);
		ajaxResponse.setData("data", hcpList);
		
		return ajaxResponse;
	}
	
	@Override
	public AjaxResponse getHcpBjCnt() {
		AjaxResponse ajaxResponse = new AjaxResponse();
		CurrentUser cUser = SecurityUtils.getCurrentUser();//当前用户
		int cnt = sxblBLO.getHcpBjCnt(cUser.getUsername());
		ajaxResponse.setData("data", cnt);
		return ajaxResponse;
	}
	
	//办件信息结果附件
	@Override
	public PageResult getFjList(SxblFjDTO dto){
		return this.sxblBLO.getFjList(dto);
	}


	@Override
	public AjaxResponse saveDazd(DazdDTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		VRsWbDazd vRsWbDazd = new VRsWbDazd();
		BeanUtils.copyProperties(dto, vRsWbDazd);
		sxblBLO.saveDazd(vRsWbDazd);
		return ajaxResponse;
	}

	@Override
	public AjaxResponse saveDyclzd(DazdDTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		VRsWbDyclzd vRsWbDyclzd = new VRsWbDyclzd();
		BeanUtils.copyProperties(dto, vRsWbDyclzd);
		sxblBLO.saveDyclzd(vRsWbDyclzd);
		return ajaxResponse;
	}

	@Override
	public AjaxResponse getDazd(BjQueryDTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		VRsWbDazd dazd = sxblBLO.getDazd(dto);
		ajaxResponse.setData("data",dazd);
		return ajaxResponse;
	}

	@Override
	public AjaxResponse getDyclzd(BjQueryDTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		VRsWbDyclzd dyclzd = sxblBLO.getDyclzd(dto);
		ajaxResponse.setData("data",dyclzd);
		return ajaxResponse;
	}

	@Override
	public AjaxResponse saveTempGccrcrd(GccrcrdDTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		VRsZjGccrcSq vRsZjGccrcSq = new VRsZjGccrcSq();
		BeanUtils.copyProperties(dto, vRsZjGccrcSq);
		sxblBLO.saveTempGccrcrd(vRsZjGccrcSq);
		return ajaxResponse;
	}

	@Override
	public AjaxResponse saveQxxsfy(QxxxfyxxDTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		RsJcQxksclSqPO rsJcQxksclSqPO = new RsJcQxksclSqPO();
		BeanUtils.copyProperties(dto, rsJcQxksclSqPO);
		sxblBLO.saveQxxsfy(rsJcQxksclSqPO);
		return ajaxResponse;
	}

	@Override
	public AjaxResponse getQxxsfy(BjQueryDTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		RsJcQxksclSqPO qxxsfy = sxblBLO.getQxxsfy(dto);
		ajaxResponse.setData("data",qxxsfy);
		return ajaxResponse;
	}

	@Override
	public void saveYjsYwxx(YjsQueryDTO dto) {
		 sxblBLO.saveYjsYwxx(dto);
	}

	@Override
	public List<RsWbBjSub> getYjsYwxx(YjsQueryDTO dto) {
		return sxblBLO.getYjsYwxx(dto);
	}

	@Override
	public void checkCondition(String sxbm) {
		sxblBLO.checkCondition(sxbm);
	}
}