From 7f36a55767f870937a473f9c0eebf0240300a5ef Mon Sep 17 00:00:00 2001
From: Administrator <admin>
Date: Wed, 08 Jun 2022 22:07:21 +0800
Subject: [PATCH] ftp 个人保安新增修改

---
 src/main/java/org/springblade/modules/system/controller/UserController.java |  401 +++++++++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 343 insertions(+), 58 deletions(-)

diff --git a/src/main/java/org/springblade/modules/system/controller/UserController.java b/src/main/java/org/springblade/modules/system/controller/UserController.java
index a3fb872..ac732ac 100644
--- a/src/main/java/org/springblade/modules/system/controller/UserController.java
+++ b/src/main/java/org/springblade/modules/system/controller/UserController.java
@@ -33,6 +33,7 @@
 import lombok.AllArgsConstructor;
 import net.sf.json.JSONObject;
 import org.apache.commons.codec.Charsets;
+import org.apache.commons.lang3.StringUtils;
 import org.springblade.common.cache.DictCache;
 import org.springblade.common.config.FtpConfig;
 import org.springblade.common.enums.DictEnum;
@@ -107,11 +108,13 @@
 import java.security.NoSuchAlgorithmException;
 import java.text.SimpleDateFormat;
 import java.util.*;
+import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.stream.Collectors;
 
 import static org.springblade.common.config.FtpConfig.*;
 import static org.springblade.core.cache.constant.CacheConstant.SYS_CACHE;
 import static org.springblade.core.cache.constant.CacheConstant.USER_CACHE;
+import static org.springblade.modules.FTP.DataHandler.handlerUserInfo;
 
 /**
  * 控制器
@@ -229,20 +232,37 @@
 	//@PreAuth(RoleConstant.HAS_ROLE_ADMIN)
 	public R submit(@Valid @RequestBody User user) {
 		if (null == user.getId()) {
-			//新增
-			//密码加密
-			if (Func.isNotEmpty(user.getPassword())) {
-				user.setPassword(DigestUtil.encrypt(user.getPassword()));
-			}
 			Integer userCount = userService.selectCountAccount(user.getAccount());
 			if (userCount > 0) {
 				throw new org.springblade.core.log.exception.ServiceException(StringUtil.format("当前用户 [{}] 已存在!", user.getAccount()));
 			}
+			//判断角色
+			if(user.getRoleId().equals("1412226235153731586")){
+				//如果是保安员,默认为无证
+				user.setHold("2");
+			}
 			user.setTenantId("000000");
+			user.setDispatch("1");
+			//待审查
+			user.setExaminationType("2");
+//			String realName = user.getRealName();
+//			String cardid = user.getCardid();
+////			user.setAccount(user.getRealName());
+//			user.setAccount(realName.substring(0,1)+cardid.substring(cardid.length()-4));
+			//默认在职
 			user.setStatus(1);
 			user.setIsDeleted(0);
 			user.setCreateTime(new Date());
 			user.setUpdateTime(new Date());
+
+			//密码加密
+			if (Func.isNotEmpty(user.getCardid())) {
+				//取身份证号码后6位作为密码
+				user.setPassword(DigestUtil.encrypt(user.getCardid().substring(user.getCardid().length() - 6)));
+			}
+			user.setCreateTime(new Date());
+			user.setTenantId("000000");
+
 			//判断是否为民警 code
 			if (null != user.getCode() && !user.getCode().equals("")) {
 				//民警
@@ -254,32 +274,73 @@
 				Jurisdiction one = jurisdictionService.getOne(Condition.getQueryWrapper(jurisdiction));
 				user.setJurisdiction(one.getId().toString());
 			}
-			//新增
-			boolean stats = userService.save(user);
-			if (stats) {
-				//内网同步
-				String s = "insert into blade_user(" +
-					"id,tenant_id,code,account,password,real_name,phone,sex,role_id,dept_id," +
-					"create_time,update_time,cardid,status,is_deleted,jurisdiction) " +
-					"values(" + "'" + user.getId() + "'" + "," +
-					"'" + user.getTenantId() + "'" + "," +
-					"'" + user.getCode() + "'" + "," +
-					"'" + user.getAccount() + "'" + "," +
-					"'" + user.getPassword() + "'" + "," +
-					"'" + user.getRealName() + "'" + "," +
-					"'" + user.getPhone() + "'" + "," +
-					"'" + user.getSex() + "'" + "," +
-					"'" + user.getRoleId() + "'" +
-					"," + "'" + user.getDeptId() + "'" +
-					"," + "'" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(user.getCreateTime()) + "'" +
-					"," + "'" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(user.getUpdateTime()) + "'" +
-					"," + "'" + user.getCardid() + "'" +
-					"," + "'" + user.getStatus() + "'" +
-					"," + "'" + user.getIsDeleted() + "'" +
-					"," + "'" + user.getJurisdiction() + "'"
-					+ ")";
-				myAsyncService.FTP(s);
+			//用户新增
+//		boolean status = userService.save(user);
+
+
+			User user1 = Objects.requireNonNull(BeanUtil.copy(user,User.class));
+
+			//生成随机数
+			String uuid = UUID.randomUUID().toString();
+			//将 user 存入 redis
+			redisTemplate.opsForValue().set(uuid,JSON.toJSONString(user));
+			//user1 临时设置uuid 到 reason_for_leav 离职原因字段
+			user1.setReasonForLeav(uuid);
+
+			//数据推送
+			Map<String, Object> map = new HashMap<>(1);
+			map.put("user",user1);
+			myAsyncService.FTPObject(map);
+
+			int count = 0;
+			int flag = 1;
+			Result result = new Result();
+			//调用ftp获取返回数据
+			while (true){
+				try {
+					Thread.sleep(1000);
+				} catch (InterruptedException e) {
+					e.printStackTrace();
+				}
+				//调用ftp获取返回数据
+				result = Monitor.isFTPFileExist(uuid);
+				//如果返回true,就退处
+				if (result.getCode()==200){
+					flag = 1;
+					break;
+				}else if (result.getCode()==201){
+					flag = 2;
+					break;
+				}else if (result.getCode()==202){
+					flag = 3;
+					break;
+				}else if (result.getCode()==203){
+					flag = 4;
+					break;
+				}else{
+					//计数
+					count++;
+					//如果超过10s 没有读取到,则退出
+					if (count == 60) {
+						System.out.println("等待超时.....................");
+						break;
+					}
+				}
 			}
+			if (flag==1 || flag==2) {
+				//返回
+				return R.data(200, null, "新增成功!");
+			}else if (flag==3){
+				throw new ServiceException(result.getMsg());
+			}else if (flag==4){
+//			return R.data(201, null, "保安证编号不匹配,请核实!也可通过提供保安证件信息提交核实申请!");
+				return R.data(201, null, "保安证编号不匹配,请核实!");
+			}else{
+				//返回
+				return R.data(400, null, "新增失败!");
+			}
+
+
 		} else {
 			//修改
 			User user1 = userService.getById(user.getId());
@@ -306,6 +367,93 @@
 		//返回
 		return R.success("成功");
 	}
+
+	/**
+//	 * 新增或修改
+//	 */
+//	@PostMapping("/submit")
+//	@ApiOperationSupport(order = 4)
+//	@ApiOperation(value = "新增或修改", notes = "传入User")
+//	//@PreAuth(RoleConstant.HAS_ROLE_ADMIN)
+//	public R submit(@Valid @RequestBody User user) {
+//		if (null == user.getId()) {
+//			//新增
+//			//密码加密
+//			if (Func.isNotEmpty(user.getPassword())) {
+//				user.setPassword(DigestUtil.encrypt(user.getPassword()));
+//			}
+//			Integer userCount = userService.selectCountAccount(user.getAccount());
+//			if (userCount > 0) {
+//				throw new org.springblade.core.log.exception.ServiceException(StringUtil.format("当前用户 [{}] 已存在!", user.getAccount()));
+//			}
+//			user.setTenantId("000000");
+//			user.setStatus(1);
+//			user.setIsDeleted(0);
+//			user.setCreateTime(new Date());
+//			user.setUpdateTime(new Date());
+//			//判断是否为民警 code
+//			if (null != user.getCode() && !user.getCode().equals("")) {
+//				//民警
+//				//根据 deptId 查询dept信息,公安管理员
+//				Dept dept = iDeptService.getById(user.getDeptId());
+//				//获取辖区的数据
+//				Jurisdiction jurisdiction = new Jurisdiction();
+//				jurisdiction.setDeptName(dept.getDeptName());
+//				Jurisdiction one = jurisdictionService.getOne(Condition.getQueryWrapper(jurisdiction));
+//				user.setJurisdiction(one.getId().toString());
+//			}
+//			//新增
+//			boolean stats = userService.save(user);
+//			if (stats) {
+//				//内网同步
+//				String s = "insert into blade_user(" +
+//					"id,tenant_id,code,account,password,real_name,phone,sex,role_id,dept_id," +
+//					"create_time,update_time,cardid,status,is_deleted,jurisdiction) " +
+//					"values(" + "'" + user.getId() + "'" + "," +
+//					"'" + user.getTenantId() + "'" + "," +
+//					"'" + user.getCode() + "'" + "," +
+//					"'" + user.getAccount() + "'" + "," +
+//					"'" + user.getPassword() + "'" + "," +
+//					"'" + user.getRealName() + "'" + "," +
+//					"'" + user.getPhone() + "'" + "," +
+//					"'" + user.getSex() + "'" + "," +
+//					"'" + user.getRoleId() + "'" +
+//					"," + "'" + user.getDeptId() + "'" +
+//					"," + "'" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(user.getCreateTime()) + "'" +
+//					"," + "'" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(user.getUpdateTime()) + "'" +
+//					"," + "'" + user.getCardid() + "'" +
+//					"," + "'" + user.getStatus() + "'" +
+//					"," + "'" + user.getIsDeleted() + "'" +
+//					"," + "'" + user.getJurisdiction() + "'"
+//					+ ")";
+//				myAsyncService.FTP(s);
+//			}
+//		} else {
+//			//修改
+//			User user1 = userService.getById(user.getId());
+//			user.setPassword(user1.getPassword());
+//			user.setUpdateTime(new Date());
+//			boolean b = userService.updateById(user);
+//			if (b) {
+//				String s1 =
+//					"update blade_user set account = " + "'" + user.getAccount() + "'"
+//						+ ",code = " + "'" + user.getCode() + "'"
+//						+ ",real_name = " + "'" + user.getRealName() + "'"
+//						+ ",phone = " + "'" + user.getPhone() + "'"
+//						+ ",sex = " + "'" + user.getSex() + "'"
+//						+ ",update_time = " + "'" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(user.getUpdateTime()) + "'"
+//						+ ",role_id = " + "'" + user.getRoleId() + "'"
+//						+ ",dept_id = " + "'" + user.getDeptId() + "'"
+//						+ ",cardid = " + "'" + user.getCardid() + "'"
+//						+ ",guncode = " + "'" + user.getGuncode() + "'"
+//						+ " " + "where id = " + "'" + user.getId() + "'";
+//				myAsyncService.FTP(s1);
+//			}
+//		}
+//
+//		//返回
+//		return R.success("成功");
+//	}
 
 	/**
 	 * 修改
@@ -572,6 +720,41 @@
 		return R.data(200,null,"修改成功!");
 	}
 
+	/**
+	 * 账号修改
+	 * @param user
+	 * @return
+	 */
+	@PostMapping("/update_account")
+	public R updataAccount(@RequestBody User user){
+		boolean status = false;
+		//校验账户是否重复
+		User user1 = new User();
+		user1.setIsDeleted(0);
+		user1.setStatus(1);
+		user1.setAccount(user.getAccount());
+		List<User> list = userService.list(new QueryWrapper<>(user1));
+		if (list.size()>0){
+			throw new ServiceException("账号:["+user.getAccount()+"]已存在!");
+		}
+		//如果不重复,则进行修改操作
+		//更新用户账户信息
+		status = userService.updateById(user);
+		if (status){
+			System.out.println("user = " + user);
+			UserDTO userDTO = new UserDTO();
+			userDTO.setAccount(user.getAccount());
+			userDTO.setPassword(user.getPassword());
+			userDTO.setSecurityId(user.getId().toString());
+			userDTO.setPhone(user.getPhone());
+			userDTO.setAvatar(user.getAvatar());
+			//异步修改群防群控
+			myAsyncService.updateUserByAccount(userDTO);
+		}
+		//返回
+		return R.status(status);
+	}
+
 
 	/**
 	 * 修改
@@ -693,7 +876,7 @@
 				.headers(headers)
 				.build());
 		InputStream inputStream = new ByteArrayInputStream(b);
-		FtpUtil.uploadFile(FtpConfig.ftpHost, ftpPort, FtpConfig.ftpUserName, ftpPassword, ftpPath, "/", split[2], inputStream);
+		FtpUtil.uploadFile(FtpConfig.ftpHost, ftpPort, FtpConfig.ftpUserName, ftpPassword, ftpPath, "/", "f"+split[2], inputStream);
 		in.close();
 		//外围url
 		String urls = "http://61.131.136.25:2081/zhba/" + newName;
@@ -1133,6 +1316,7 @@
 	@GetMapping("/security-apply-tree")
 	public R<List<TreeNode>> securityApplyTree(UserVO user) {
 		List<TreeNode> tree = userService.getSecurityApplyTree(user);
+		System.out.println("tree = " + tree);
 		return R.data(tree);
 	}
 
@@ -1389,8 +1573,12 @@
 		Role oneRole = roleService.getOne(Condition.getQueryWrapper(role));
 		user.setRoleId(oneRole.getId().toString());
 		user.setDispatch("1");
-		user.setExaminationType("0");
-		user.setAccount(user.getRealName());
+		//待审查
+		user.setExaminationType("2");
+		String realName = user.getRealName();
+		String cardid = user.getCardid();
+//			user.setAccount(user.getRealName());
+		user.setAccount(realName.substring(0,1)+cardid.substring(cardid.length()-4));
 		user.setStatus(1);
 
 		//密码加密
@@ -1404,7 +1592,7 @@
 //		boolean status = userService.save(user);
 
 
-		User user1 = user;
+		User user1 = Objects.requireNonNull(BeanUtil.copy(user,User.class));
 		//头像
 		if (null != user.getAvatar() && !user.getAvatar().equals("")) {
 			user1.setAvatar(FtpConfig.ip + user.getAvatar().substring(26));
@@ -1438,43 +1626,127 @@
 		Result result = new Result();
 		//调用ftp获取返回数据
 		while (true){
-			Thread.sleep(1000);
+			System.out.println("用户新增开始接收消息......................");
+			Thread.sleep(2000);
 			//调用ftp获取返回数据
 			result = Monitor.isFTPFileExist(uuid);
 			//如果返回true,就退处
 			if (result.getCode()==200){
-				flag = 1;
-				break;
-			}else if (result.getCode()==201){
-				flag = 2;
-				break;
-			}else if (result.getCode()==202){
-				flag = 3;
-				break;
-			}else if (result.getCode()==203){
-				flag = 4;
 				break;
 			}else{
 				//计数
 				count++;
-				//如果超过10s 没有读取到,则退出
-				if (count == 10) {
+				//如果超过60s 没有读取到,则退出
+				if (count == 60) {
 					break;
 				}
 			}
 		}
-		if (flag==1 || flag==2) {
-			//返回
-			return R.data(200, null, "新增成功!");
-		}else if (flag==3){
-			throw new ServiceException(result.getMsg());
-		}else if (flag==4){
+
+		//将不能导入的保安员账号存起来
+		List<String> errorList = new ArrayList<>();
+		//保安员证编号不符的保安员信息存入集合
+		List<String> securityInvalidList = new ArrayList<>();
+		//导入状态,默认为true ,如果有一个出现问题则为 false
+		AtomicBoolean status = new AtomicBoolean(true);
+		AtomicBoolean securityInvalidStatus = new AtomicBoolean(true);
+		if (null!=result){
+			//用户数处理
+			User users = handlerUserInfo(uuid);
+			System.out.println("用户数据处理---------------");
+			System.out.println("用户数据 = " + users);
+			//判断结果
+			if (result.getCode()==200){
+				//新增
+				users.setId(result.getUserId());
+				users.setStatus(1);
+				//插入数据库
+				boolean save = userService.save(users);
+				if (save){
+					//异步同步到群防群控
+					myAsyncService.qfqkUserSave(users);
+				}
+			}else if (result.getCode()==201) {
+				//修改
+				users.setId(result.getUserId());
+				//先查询当前库是否有改用户?
+				User user2 = userService.getById(result.getUserId());
+				if (null!=user2) {
+					//数据修改
+					boolean b = userService.updateById(users);
+					if (b) {
+						//异步同步到群防群控
+						myAsyncService.updateUserByQfqk(user);
+					}
+				}else {
+					//新增
+					boolean save = userService.save(users);
+					if (save){
+						//异步同步到群防群控
+						myAsyncService.qfqkUserSave(users);
+					}
+				}
+			}else if (result.getCode()==203){
+				//保安证编号不匹配,请核实!也可通过提供保安证件信息提交核实申请! 用户为新增
+				//新增
+				users.setId(result.getUserId());
+				//插入数据库
+				boolean save = userService.save(users);
+				if (save) {
+					//异步同步到群防群控
+					myAsyncService.qfqkUserSave(users);
+				}
+				securityInvalidStatus.set(false);
+				//保安证编号不匹配
+				securityInvalidList.add(user.getCardid());
+			}else if (result.getCode()==204){
+				//保安证编号不匹配,请核实!也可通过提供保安证件信息提交核实申请! 内网用户已存在情况
+				users.setId(result.getUserId());
+				//先查询当前库是否有改用户?
+				User user2 = userService.getById(result.getUserId());
+				//判断
+				if (null!=user2) {
+					//修改
+					//数据修改
+					boolean b = userService.updateById(users);
+					if (b){
+						//异步同步到群防群控
+						myAsyncService.updateUserByQfqk(users);
+					}
+				}else {
+					//新增
+					boolean save = userService.save(users);
+					if (save){
+						//异步同步到群防群控
+						myAsyncService.qfqkUserSave(users);
+					}
+				}
+				securityInvalidStatus.set(false);
+				//保安证编号不匹配
+				securityInvalidList.add(user.getCardid());
+			}else {
+				status.set(false);
+				//已在其他单位存在
+				errorList.add(user.getCardid());
+			}
+		}
+
+		//如果所有数据导入有一个异常
+		StringBuilder errorBuilder = new StringBuilder();
+		if (!status.get()){
+			String errorAccount = StringUtils.join(errorList, "\\\n");
+			errorBuilder.append("用户:[" + errorAccount + "]导入失败!已在其他单位存在!");
+		}
+		//抛出异常
+		if (errorBuilder.length()>0){
+			throw new org.springblade.core.log.exception.ServiceException(errorBuilder.toString());
+		}
+
+		if (!securityInvalidStatus.get()){
 //			return R.data(201, null, "保安证编号不匹配,请核实!也可通过提供保安证件信息提交核实申请!");
 			return R.data(201, null, "保安证编号不匹配,请核实!");
-		}else{
-			//返回
-			return R.data(400, null, "新增失败!");
 		}
+		return R.data(200, null, "新增成功!");
 	}
 
 	/**
@@ -1552,7 +1824,6 @@
 				"," + "'" + experience.getCompanyname() + "'" +
 				"," + "'" + experience.getSecurityid() + "'"
 				+ ")";
-			//FtpUtil.sqlFileUpload(s);
 			myAsyncService.FTP(s);
 		}
 	}
@@ -1595,7 +1866,6 @@
 		if (null != user1.getFingerprint() && !user1.getFingerprint().equals("")) {
 			//url 转base64
 			String base64Url = ImageUtils.imageUrlToBase64(user1.getFingerprint());
-			System.out.println("base64Url = " + base64Url);
 			user1.setFingerprint(base64Url);
 		}
 		return R.data(user1);
@@ -1676,4 +1946,19 @@
 	public R getNotUpdatePwdInfo() {
 		return R.data(userService.getNotUpdatePwdInfo());
 	}
+
+	/**
+	 * 用户手动离职(用户数据未同步成功的)
+	 * @param user
+	 * @return
+	 */
+	@GetMapping("/leave")
+	public void leave(User user){
+		user.setStatus(2);
+		String s1 =
+			"update blade_user set status = " + "'" + user.getStatus() + "'"
+				+ ",update_time = " + "'" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "'"
+				+ " " + "where cardid = " + "'" + user.getCardid() + "'";
+		myAsyncService.FTP(s1);
+	}
 }

--
Gitblit v1.9.3