cy
2022-06-21 129904537f66509f97b285e7eb4f42b3dc349dd0
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
package cn.ksource.web.facade.impl;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import javax.annotation.Resource;
 
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
 
import cn.ksource.core.dao.BaseDao;
import cn.ksource.core.dao.SqlParameter;
import cn.ksource.core.util.ConvertUtil;
import cn.ksource.core.util.DateUtil;
import cn.ksource.core.util.EqualUtil;
import cn.ksource.core.util.JsonUtil;
import cn.ksource.core.util.StringUtil;
import cn.ksource.core.web.OnlineUserListener;
import cn.ksource.web.entity.LoginEntity;
import cn.ksource.web.entity.PermissionEntity;
import cn.ksource.web.facade.BusinessFacade;
import cn.ksource.web.service.UserService;
 
@Service("businessFacade")
public class BusinessFacadeImpl implements BusinessFacade {
 
    @Resource(name="baseDao")
    private BaseDao baseDao;
    
    @Autowired
    private UserService userService;
    
    @SuppressWarnings("rawtypes")
    @Override
    public LoginEntity doUserLogin(String loginname, String password,String client_ip) {
        Map userMap = userService.getUserByLoginName(loginname);
        LoginEntity entity = new LoginEntity();
        entity.setUser(userMap);
        //用户名不存在
        if (userMap == null || userMap.isEmpty()) {
            entity.setResult(LoginEntity.Login_Result_UserNameNotExists);
            return entity;
        }
        //用户名或密码错误
        if(!EqualUtil.isStringEqual(userMap.get("PASSWORD"), userService.getEncodedPassword(password))){
            entity.setResult(LoginEntity.Login_Result_PassowrdError);
            return entity;
        }
        //用户被禁用
        if (EqualUtil.isStringEqual(userMap.get("ZT"), "2")) {
            entity.setResult(LoginEntity.Login_Result_LoginForbid);
            return entity;
        }
        
        Map info = baseDao.queryForMap("SELECT * FROM SC_PARTNER_INFO ");
        //判断用户是否过期
        if (ConvertUtil.obj2Long(info.get("VALID_END_DATE")) < DateUtil.getCurrentDate8()) {
            entity.setResult(LoginEntity.Login_Result_LoginTimeOut);
            return entity;
        }
        
        
        //用户已经登录
        if (OnlineUserListener.container.containsKey(userMap.get("ID").toString())) {
            entity.setAlreadyLogined(true);
            return entity;
        }
        
        //于默认密码一致
        if (password.equalsIgnoreCase(userService.getDefaultPwd())) {
            entity.setDefaultPassword(true);
            return entity;
        }
        
        PermissionEntity permissionEntity = userService.getAccessPermission(userMap.get("ID").toString());
        entity.setPermissionEntity(permissionEntity);
        entity.setLoginSuccess(true);
        return entity;
    }
    
    public LoginEntity doChangePwdSubmit(String loginname,String newPwd){
        userService.updatePwd(loginname, newPwd);
        Map userMap = userService.getUserByLoginName(loginname);
        LoginEntity entity = new LoginEntity();
        entity.setUser(userMap);
        //用户已经登录
        if (OnlineUserListener.container.containsKey(userMap.get("ID").toString())) {
            entity.setAlreadyLogined(true);
            return entity;
        }
        PermissionEntity permissionEntity = userService.getAccessPermission(userMap.get("ID").toString());
        entity.setPermissionEntity(permissionEntity);
        entity.setLoginSuccess(true);
        return entity;
    }
    
    public LoginEntity doKickedOutSubmit(String loginname,String client_ip){
        Map userMap = userService.getUserByLoginName(loginname);
        OnlineUserListener.container.get(userMap.get("ID")).invalidate();
        LoginEntity entity = new LoginEntity();
        entity.setUser(userMap);
        
        PermissionEntity permissionEntity = userService.getAccessPermission(userMap.get("ID").toString());
        entity.setPermissionEntity(permissionEntity);
        entity.setLoginSuccess(true);
        
        return entity;
    }
    
    
    public boolean updateUserPwd(String userid,String oldPwd,String newPwd){
        String sql = "SELECT LOGINNAME FROM GG_USER WHERE PASSWORD=:password AND ID=:id";
        String loginname = baseDao.queryForString(sql,new SqlParameter("password",
                userService.getEncodedPassword(oldPwd)).addValue("id", userid));
        if (StringUtils.isBlank(loginname)) {
            return false;
        } else {
            userService.updatePwd(loginname, newPwd);
            return true;
        }
    }
    
    
    @Override
    public Map<String, String> getRoleMapByUserId(String userId) {
        StringBuilder sql = new StringBuilder();
        sql.append("select r.id,r.rolename ");
        sql.append("from ac_role r ");
        sql.append("where exists (select id from ac_user_ref_role u where u.yhbh=:yhbh and u.jsbh=r.id) ");
        
        Map param = new HashMap();
        param.put("yhbh", userId);
        
        List<Map> list = baseDao.queryForList(sql.toString(), param);
        
        Map tMap = new HashMap();
        if (list != null && list.size() > 0) {
            for (Map map : list) {
                tMap.put(map.get("id"), map.get("rolename"));
            }
        }
        return tMap;
    }
    
    @Override
    public Map queryUserMsgByQQ(String openId) {
        String selectSql = "SELECT LOGINNAME FROM GG_USER WHERE QQ_OPEN_ID = :openId";
        Map msg = baseDao.queryForMap(selectSql,new SqlParameter("openId",openId));
        return msg;
    }
    
    @Override
    public LoginEntity doQQUserLogin(String loginname,String client_ip) {
        Map userMap = userService.getUserByLoginName(loginname);
        LoginEntity entity = new LoginEntity();
        entity.setUser(userMap);
        //用户被禁用
        if (EqualUtil.isStringEqual(userMap.get("ZT"), "2")) {
            entity.setResult(LoginEntity.Login_Result_LoginForbid);
            return entity;
        }
        
        Map info = baseDao.queryForMap("SELECT * FROM SC_PARTNER_INFO ");
        //判断用户是否过期
        if (ConvertUtil.obj2Long(info.get("VALID_END_DATE")) < DateUtil.getCurrentDate8()) {
            entity.setResult(LoginEntity.Login_Result_LoginTimeOut);
            return entity;
        }
        
        
        //用户已经登录
        if (OnlineUserListener.container.containsKey(userMap.get("ID").toString())) {
            entity.setAlreadyLogined(true);
            return entity;
        }
        
        
        PermissionEntity permissionEntity = userService.getAccessPermission(userMap.get("ID").toString());
        entity.setPermissionEntity(permissionEntity);
        entity.setLoginSuccess(true);
        
        return entity;
    }
    
    @Override
    public boolean doResetPwd(String username, String email, String password) {
        if(StringUtil.notEmpty(password)) {
            Map paramMap = new HashMap();
            paramMap.put("username", username);
            paramMap.put("email", email);
            String sql = "SELECT ID,LOGINNAME,EMAIL,SJHM FROM GG_USER WHERE LOGINNAME = :username AND (EMAIL = :email OR SJHM = :email)";
            List list =  baseDao.queryForList(sql, paramMap);
            if(null!=list && list.size() == 1) {
                userService.updatePwd(username, password);
                return true;
            } 
        }
        return false;
    }
    
    @Override
    public Map queryUserByName(String loginname){
        Map userMap = userService.getUserByLoginName(loginname);
        return userMap;
    }
 
    @Override
    public boolean saveData(String relations, String nodes) {
        baseDao.execute("DELETE FROM LARKS_NODES_RELATION",null);
        baseDao.execute("DELETE FROM LARKS_NODES",null);
        if(StringUtil.notEmpty(relations)) {
            String[] rs = relations.split(";");
            String insertSql = "INSERT INTO LARKS_NODES_RELATION(ID,NAME,FROM_ID,TO_ID,EDGE_TYPE) VALUES (:id,:name,:from_id,:to_id,:edge_type)";
            if(null!=rs && rs.length>0) {
                List<SqlParameter> paramsList = new ArrayList<SqlParameter>();
                for(String relation : rs) {
                    Map map = JsonUtil.json2Map(relation);
                    SqlParameter sqlParameter = new SqlParameter();
                    String name = ConvertUtil.obj2StrBlank(map.get("name"));
                    String from = ConvertUtil.obj2StrBlank(map.get("fromNode"));
                    String to = ConvertUtil.obj2StrBlank(map.get("toNode"));
                    String edgeType = ConvertUtil.obj2StrBlank(map.get("edgeType"));
                    sqlParameter.addValue("id", StringUtil.getUUID()).addValue("name", name).addValue("from_id", from).addValue("to_id", to).addValue("edge_type", edgeType);
                    paramsList.add(sqlParameter);
                }
                
                baseDao.executeBatch(insertSql, paramsList);
            }
        }
        if(StringUtil.notEmpty(nodes)) {
            String insertSql = "INSERT INTO LARKS_NODES(ID,NAME,IMAGE,XLOT,YLOT) VALUES (:id,:name,:image,:xlot,:ylot)";
            String[] ns = nodes.split(";");
            if(null!=ns && ns.length>0) {
                List<SqlParameter> paramsList = new ArrayList<SqlParameter>();
                for(String node : ns) {
                    Map map = JsonUtil.json2Map(node);
                    SqlParameter sqlParameter = new SqlParameter();
                    String id = ConvertUtil.obj2StrBlank(map.get("id"));
                    String name = ConvertUtil.obj2StrBlank(map.get("name"));
                    String image = ConvertUtil.obj2StrBlank(map.get("imageType"));
                    String xlot = ConvertUtil.obj2StrBlank(map.get("xlot"));
                    String ylot = ConvertUtil.obj2StrBlank(map.get("ylot"));
                    sqlParameter.addValue("id", id).addValue("name", name).addValue("image", image).addValue("xlot", xlot).addValue("ylot", ylot);
                    paramsList.add(sqlParameter);
                }
                baseDao.executeBatch(insertSql, paramsList);
            }
        }
        return true;
    }
 
    @Override
    public String getJsonData() {
        Map result = new HashMap();
        String nodeSql = "SELECT id,name,image,xlot as x,ylot as y FROM LARKS_NODES ";
        List<Map> nodes = baseDao.queryForList(nodeSql);
        result.put("nodes", nodes);
        
        String relationSql = "SELECT id,name,from_id,to_id,edge_type FROM LARKS_NODES_RELATION";
        List<Map> relations = baseDao.queryForList(relationSql);
        result.put("relations", relations);
        String json = JsonUtil.map2Json(result);
        return json;
    }
 
    @Override
    public Map queryUserMsgByWechat(String openId) {
        String selectSql = "SELECT LOGINNAME FROM GG_USER WHERE WECHAT_LOGIN_OPEN_ID = :openId";
        Map msg = baseDao.queryForMap(selectSql,new SqlParameter("openId",openId));
        return msg;
    }
 
    private static int a = 1;
    @Override
    @Cacheable(value="WeiXinCache")
    public String testEhache() {
        a++;
        return String.valueOf(a);
    }
 
    @Override
    public void updatePartnerInfo(String endDate, Long curDate) {
        SqlParameter param = new SqlParameter();
        param.addValue("endDate", endDate);
        param.addValue("curDate", curDate);
        String sql = " update sc_partner_info set valid_end_date=:endDate,lc_up_time=:curDate ";
        baseDao.execute(sql, param);
    }
 
    @Override
    public String getV() {
        String sql="select VERSION_CODE from LARKS_PLATFORM_VERSION where STATUS=1";
        String VERSION_CODE=baseDao.queryForString(sql);
        return VERSION_CODE;
    }
 
}