|
@@ -1,18 +1,26 @@
|
|
|
package com.fuzamei.service;
|
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.LinkedHashMap;
|
|
|
+import java.util.List;
|
|
|
import java.util.Map;
|
|
|
+import java.util.UUID;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
-import javax.management.RuntimeErrorException;
|
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
import com.fuzamei.dao.UserDao;
|
|
|
+import com.fuzamei.entity.AccountInfo;
|
|
|
+import com.fuzamei.entity.Role;
|
|
|
+import com.fuzamei.entity.Token;
|
|
|
import com.fuzamei.entity.User;
|
|
|
import com.fuzamei.utils.JSONUtil;
|
|
|
+import com.fuzamei.utils.PageDTO;
|
|
|
import com.fuzamei.utils.blockchain.KeyUtils;
|
|
|
/**
|
|
|
*
|
|
@@ -40,13 +48,33 @@ public class UserService {
|
|
|
* <br><b>Date:</b> 2017年12月25日 上午10:36:10
|
|
|
* <br><b>Version:</b> 1.0
|
|
|
*/
|
|
|
+ @Transactional
|
|
|
public Map<String, Object> login(User user) {
|
|
|
// TODO 检查用户的用户名、密码和随机数,能不能对应得上
|
|
|
Map<String,Object> resultMap = new LinkedHashMap<String,Object>();
|
|
|
- int count = userDao.getUserCount(user);
|
|
|
+ List<User> userInfo = userDao.getUserCount(user);
|
|
|
resultMap = JSONUtil.getJsonMap(12431, false, "用户登录失败", null);
|
|
|
- if(count == 1){
|
|
|
- resultMap = JSONUtil.getJsonMap(200, true, "用户登录成功", null);
|
|
|
+ Token token1 = new Token();
|
|
|
+ if(userInfo.size() == 1){
|
|
|
+ String tokenRandom = UUID.randomUUID().toString().replace("-", "");
|
|
|
+ //更新token
|
|
|
+ token1.setUserId(user.getUser_id());
|
|
|
+ token1.setToken(tokenRandom);
|
|
|
+ Token token = userDao.getTokenById(token1);
|
|
|
+ if(token == null){
|
|
|
+ int tokenCount = userDao.insertToken(token1);
|
|
|
+ if(tokenCount != 1){
|
|
|
+ throw new RuntimeException();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ int updateCount = userDao.updateToken(token1);
|
|
|
+ if(updateCount != 1){
|
|
|
+ throw new RuntimeException();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ resultMap = JSONUtil.getJsonMap(200, true, "用户登录成功", userInfo);
|
|
|
}
|
|
|
return resultMap;
|
|
|
}
|
|
@@ -85,41 +113,166 @@ public class UserService {
|
|
|
@Transactional
|
|
|
public Map<String, Object> addUser(User user) {
|
|
|
Map<String,Object> resultMap = new HashMap<String,Object>();
|
|
|
- // TODO 将公私钥生成出来
|
|
|
+ //生成公私钥
|
|
|
String password = user.getPassword();
|
|
|
String random = user.getRandom();
|
|
|
String private_key = KeyUtils.getPrivateKey(password, random);
|
|
|
String public_key = KeyUtils.getPublicKey(private_key);
|
|
|
+ String accounts = user.getDrawing_account();
|
|
|
+ String sign = user.getSign();
|
|
|
+ int userId = user.getUser_id();
|
|
|
+ List<Map<String,Object>> accountList = new ArrayList<Map<String,Object>>();
|
|
|
+ int accountSize = 0;
|
|
|
+ int accountInfoCount = 0;
|
|
|
+ int userAccountCount = 0;
|
|
|
user.setPrivate_key(private_key);
|
|
|
user.setPublic_key(public_key);
|
|
|
- long currentTime = System.currentTimeMillis();
|
|
|
- user.setCreate_time(currentTime);
|
|
|
- user.setUpdate_time(currentTime);
|
|
|
- // TODO 用户添加到本地数据库
|
|
|
+ User user1 = userDao.findUserById(user);
|
|
|
+ if(user1 != null){
|
|
|
+ resultMap = JSONUtil.getJsonMap(200, false, "用户已存在,不能重复添加", null);
|
|
|
+ return resultMap;
|
|
|
+ }
|
|
|
+ //查询数据库所有账户
|
|
|
+ List<AccountInfo> accountInfo = userDao.getAllAccount();
|
|
|
+ Map<String,Object> accountIdMap = new HashMap<String,Object>();
|
|
|
+ for(int i = 0; i < accountInfo.size(); i ++){
|
|
|
+ accountIdMap.put(accountInfo.get(i).getAccount_id().toString(), "value");
|
|
|
+ }
|
|
|
+ //用户添加到本地数据库
|
|
|
int userCount = userDao.addUserToUser(user);
|
|
|
int userRoleCount = userDao.addUserToUserRole(user);
|
|
|
- if((userCount == 1)&&(userRoleCount == 1)){
|
|
|
- //区块链操作开始
|
|
|
- //区块链操作结束
|
|
|
- resultMap = JSONUtil.getJsonMap(200, false, "用户添加成功", null);
|
|
|
+ //将账户文本款中参数按照“,”进行分隔
|
|
|
+ if((accounts != null)&&(!"".equals(accounts))){
|
|
|
+ String[] accountArray =accounts.split(",");
|
|
|
+ accountSize = accountArray.length;
|
|
|
+ for(int i = 0; i < accountSize; i ++){
|
|
|
+ String account_name = accountArray[i];
|
|
|
+ String accontId = String.valueOf((int)((Math.random()*9+1)*100000));
|
|
|
+ Map<String,Object> map = new HashMap<String,Object>();
|
|
|
+ map.put("user_id", userId);
|
|
|
+ map.put("account_id", accontId);
|
|
|
+ map.put("account_name", account_name);
|
|
|
+ map.put("account_type", "划转账户");
|
|
|
+ if(accountIdMap.get(accontId) == null){
|
|
|
+ accountList.add(map);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(accountList.size() != 0){
|
|
|
+ accountInfoCount = userDao.insertAccountInfo(accountList);
|
|
|
+ userAccountCount = userDao.insertUserAccount(accountList);
|
|
|
+ }
|
|
|
+ if((userCount == 1)&&(userRoleCount == 1)&&(accountInfoCount == accountSize)&&(userAccountCount == accountSize)){
|
|
|
+ /** 正式环境要放开注释 **/
|
|
|
+// ProtobufBean protobufBean = BlockChainUtil.getProtobufBean(sign);
|
|
|
+// String result = BlockChainUtil.sendPostParam(protobufBean);
|
|
|
+// boolean flag = BlockChainUtil.vilaResult(result);
|
|
|
+ boolean flag = true;
|
|
|
+ if(!flag){
|
|
|
+ throw new RuntimeException();
|
|
|
+ }
|
|
|
+ resultMap = JSONUtil.getJsonMap(200, true, "用户添加成功", null);
|
|
|
}
|
|
|
else{
|
|
|
throw new RuntimeException();
|
|
|
}
|
|
|
return resultMap;
|
|
|
}
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * <b>Description:编辑用户信息</b><br>
|
|
|
+ * @param user
|
|
|
+ * @return
|
|
|
+ * @Note
|
|
|
+ * <b>Author:maamin
|
|
|
+ * <br><b>Date:</b> 2018年1月9日 上午11:24:47
|
|
|
+ * <br><b>Version:</b> 1.0
|
|
|
+ */
|
|
|
@Transactional
|
|
|
public Map<String, Object> editUser(User user) {
|
|
|
Map<String,Object> resultMap = new HashMap<String,Object>();
|
|
|
// TODO Auto-generated method stub
|
|
|
+ List<Object> accounts = user.getDrawing_accountList();
|
|
|
+ List<AccountInfo> accountList = new ArrayList<AccountInfo>();
|
|
|
+ Map<String, Map<String, Object>> mapTT = new HashMap<String, Map<String,Object>>();
|
|
|
+ List<String> cloumn = new ArrayList<String>();
|
|
|
+ if((accounts != null)&&(!"".equals(accounts))){
|
|
|
+ String accountsString = JSONArray.toJSONString(accounts);
|
|
|
+ accountList = JSONArray.parseArray(accountsString, AccountInfo.class);
|
|
|
+ System.out.println(">>>"+accountList);
|
|
|
+ for(int i = 0;i < accountList.size(); i ++){
|
|
|
+ Map<String,Object> parameter = new HashMap<String,Object>();
|
|
|
+ String accountId = String.valueOf(accountList.get(i).getAccount_id());
|
|
|
+ String accountName = accountList.get(i).getAccount_name();
|
|
|
+ parameter.put("account_id", accountId);
|
|
|
+ parameter.put("account_name", accountName);
|
|
|
+ mapTT.put(accountId, parameter);
|
|
|
+ }
|
|
|
+ cloumn.add("account_id");
|
|
|
+ cloumn.add("account_name");
|
|
|
+ }
|
|
|
+ int updateAccountCount = userDao.updateBatchAccountById(mapTT, cloumn);
|
|
|
int countUser = userDao.updateUserById(user);
|
|
|
int countRole = userDao.updataRoleById(user);
|
|
|
- if((countUser == 1)&&(countRole == 1)){
|
|
|
- resultMap = JSONUtil.getJsonMap(200, false, "用户信息修改成功", null);
|
|
|
+ if((countUser == 1)&&(countRole == 1)&&(updateAccountCount == accountList.size())){
|
|
|
+ resultMap = JSONUtil.getJsonMap(200, true, "用户信息修改成功", null);
|
|
|
}
|
|
|
else{
|
|
|
throw new RuntimeException("blockchain error!");
|
|
|
}
|
|
|
return resultMap;
|
|
|
}
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * <b>Description:查询所有用户信息</b><br>
|
|
|
+ * @param user
|
|
|
+ * @return
|
|
|
+ * @Note
|
|
|
+ * <b>Author:maamin
|
|
|
+ * <br><b>Date:</b> 2018年1月9日 上午11:27:53
|
|
|
+ * <br><b>Version:</b> 1.0
|
|
|
+ */
|
|
|
+ public PageDTO getUsers(User user,int page,int rows) {
|
|
|
+ return userDao.getUsers(user,page,rows);
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * <b>Description:获取所有角色信息列表</b><br>
|
|
|
+ * @return
|
|
|
+ * @Note
|
|
|
+ * <b>Author:maamin
|
|
|
+ * <br><b>Date:</b> 2018年1月10日 下午4:43:54
|
|
|
+ * <br><b>Version:</b> 1.0
|
|
|
+ */
|
|
|
+ public List<Role> getAllRoles() {
|
|
|
+ // TODO Auto-generated method stub
|
|
|
+ return userDao.getAllRoles();
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * <b>Description:通过用户id查询用户信息(包括用户的账户信息)</b><br>
|
|
|
+ * @param userId
|
|
|
+ * @return
|
|
|
+ * @Note
|
|
|
+ * <b>Author:maamin
|
|
|
+ * <br><b>Date:</b> 2018年1月10日 下午5:21:59
|
|
|
+ * <br><b>Version:</b> 1.0
|
|
|
+ */
|
|
|
+ public Map<String,Object> findUserById(String userId) {
|
|
|
+ // TODO Auto-generated method stub
|
|
|
+// return userDao.findUserById(userId);
|
|
|
+ Map<String,Object> map = new LinkedHashMap<String,Object>();
|
|
|
+ User user = new User();
|
|
|
+ List<AccountInfo> accountInfo = new ArrayList<AccountInfo>();
|
|
|
+ try {
|
|
|
+ user = userDao.findUserById(userId);
|
|
|
+ accountInfo = userDao.getAccountInfoByUser(userId);
|
|
|
+ } catch (Exception e) {
|
|
|
+ // TODO: handle exception
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ map.put("user", user);
|
|
|
+ map.put("accountData", accountInfo);
|
|
|
+ return map;
|
|
|
+ }
|
|
|
}
|