sgj
6 小时以前 3a5e49666af1da441633b0a9bae4c3c2b18974b3
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
package com.fzzy.appwx.manager;
 
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fzzy.appwx.param.WeChatBaseParam;
import com.fzzy.appwx.util.WeChatUtil;
import com.fzzy.igds.constant.Constant;
import com.fzzy.igds.constant.RespCodeEnum;
import com.fzzy.igds.data.IgdsBaseParam;
import com.fzzy.igds.data.PageResponse;
import com.fzzy.igds.domain.PledgeContract;
import com.fzzy.igds.domain.PledgeContractDepot;
import com.fzzy.igds.domain.SnapReply;
import com.fzzy.igds.service.PledgeContractDepotService;
import com.fzzy.igds.service.PledgeContractService;
import com.fzzy.igds.service.SnapReplyService;
import com.google.code.kaptcha.Constants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.service.ISysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
 
@Component
public class WeChatManager {
 
    @Resource
    private WeChatUtil weChatUtil;
    @Autowired
    private ISysUserService sysUserService;
    @Resource
    private SnapReplyService snapReplyService;
 
    @Resource
    private PledgeContractService pledgeContractService;
 
    @Resource
    private PledgeContractDepotService pledgeContractDepotService;
 
 
    public String getOpenid(String code) {
        //根据code获取用户openid
        return weChatUtil.getOpenid(code);
    }
 
 
    public SysUser getUser(String openid) {
        //根据openid获取用户信息
        return sysUserService.selectUserByOpenId(openid);
    }
 
    //获取用户绑定信息
    public PageResponse<SysUser> getUserBindInfo(WeChatBaseParam param) {
        //判断参数
        if (null == param) {
            return new PageResponse<SysUser>(RespCodeEnum.CODE_1007.getCode(), "参数不完整,请核查");
        }
 
        SysUser user = null;
 
        //微信公众号
        if (StringUtils.isEmpty(param.getOpenid())) {
            return new PageResponse<SysUser>(RespCodeEnum.CODE_1007.getCode(), "参数不完整,请核查");
 
        }
        //根据openid查询用户是否已经绑定
        user = sysUserService.selectUserByOpenId(param.getOpenid());
        if (user == null) {
            return new PageResponse<SysUser>(RespCodeEnum.CODE_1008.getCode(), "业务数据返回为空");
 
        }
 
        Long deptId = user.getDeptId();
        if (deptId == null) {
            deptId = Long.valueOf(user.getCompanyId());
            user.setDeptId(deptId);
        }
        return new PageResponse(RespCodeEnum.CODE_0000.getCode(), "查询成功", user);
    }
 
    //校验验证码
    public boolean validateResponse(HttpServletRequest request, String validateCode) {
        // ① 从Session获取生成的验证码
        Object obj = ShiroUtils.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
        String code = String.valueOf(obj != null ? obj : "");
 
        // ② 立即清除Session中的验证码(防止重复使用)
        request.getSession().removeAttribute(Constants.KAPTCHA_SESSION_KEY);
 
        // ③ 比对用户输入和正确答案(忽略大小写)
        if (StringUtils.isEmpty(validateCode) || !validateCode.equalsIgnoreCase(code)) {
            return false;
 
        }
        return true;
    }
 
    //绑定openId
    public PageResponse<String> bandOpenId(HttpServletRequest httpRequest,WeChatBaseParam param) {
 
        //判断参数
        if (null == param || StringUtils.isEmpty(param.getOpenid())) {
            return new PageResponse<String>(RespCodeEnum.CODE_1007.getCode(), "参数不完整,请核查");
 
        }
        if (StringUtils.isEmpty(param.getMobile())) {
            return new PageResponse<String>(RespCodeEnum.CODE_1007.getCode(), "手机号为空,请填写");
 
        }
        if (StringUtils.isEmpty(param.getMsgCode())) {
            return new PageResponse<String>(RespCodeEnum.CODE_1007.getCode(), "验证码不能为空");
 
        }
        //验证码验证
        boolean validateResponse = validateResponse(httpRequest, param.getMsgCode());
        if (!validateResponse) {
            return new PageResponse<String>(RespCodeEnum.CODE_1007.getCode(), "验证码错误");
        }
 
        //根据手机号查看用户是否是系统用户
        SysUser user = sysUserService.selectUserByPhoneNumber(param.getMobile());
        if (user == null || StringUtils.isEmpty(user.getPhonenumber())) {
            return new PageResponse<String>(RespCodeEnum.CODE_1006.getCode(), "手机号不在系统内,请联系管理员!");
        }
 
        //更新数据库,用户信息加入微信认证ID
        user.setOpenId(param.getOpenid());
        sysUserService.updateUserInfo(user);
 
//        redisService.deleteObject(WeChatConst.WX_MOBILE_CODE + param.getMobile());
 
        return new PageResponse<String>(RespCodeEnum.CODE_0000.getCode(), "绑定成功");
 
    }
 
 
    //解绑openId
    public PageResponse<String> unBandOpenId(HttpServletRequest httpRequest,WeChatBaseParam param) {
 
        //判断参数
        if (null == param || StringUtils.isEmpty(param.getOpenid())) {
            return new PageResponse<String>(RespCodeEnum.CODE_1007.getCode(), "参数不完整,请核查");
 
        }
        if (StringUtils.isEmpty(param.getMobile())) {
            return new PageResponse<String>(RespCodeEnum.CODE_1007.getCode(), "手机号为空,请填写");
        }
        if (StringUtils.isEmpty(param.getMsgCode())) {
            return new PageResponse<String>(RespCodeEnum.CODE_1007.getCode(), "验证码不能为空");
 
        }
        //验证码验证
        boolean validateResponse = validateResponse(httpRequest, param.getMsgCode());
        if (!validateResponse) {
            return new PageResponse<String>(RespCodeEnum.CODE_1007.getCode(), "验证码错误");
        }
        //根据手机号查询用户是否已经绑定
        SysUser user = sysUserService.selectUserByPhoneNumber(param.getMobile());
        if (user == null || StringUtils.isEmpty(user.getPhonenumber())) {
            return new PageResponse<String>(RespCodeEnum.CODE_1006.getCode(), "手机号不在系统内,请联系管理员!");
 
        }
 
        user.setOpenId("");
        sysUserService.updateUserInfo(user);
 
        return new PageResponse<String>(RespCodeEnum.CODE_0000.getCode(), "请求成功");
 
    }
 
    public PageResponse<Page<SnapReply>> getSnapReplyPage(WeChatBaseParam param) {
 
        if (null == param) {
            return new PageResponse<Page<SnapReply>>(RespCodeEnum.CODE_1007.getCode(), "参数不完整,请核查");
        }
 
        SysUser user;
 
        //微信公众号
        if (StringUtils.isEmpty(param.getOpenid())) {
            return new PageResponse<Page<SnapReply>>(RespCodeEnum.CODE_1007.getCode(), "参数不完整,请核查");
 
        }
        //根据openid查询用户是否已经绑定
        user = sysUserService.selectUserByOpenId(param.getOpenid());
        if (user == null) {
            return new PageResponse<Page<SnapReply>>(RespCodeEnum.CODE_1006.getCode(), "尚未绑定服务,请先绑定!");
 
        }
        //获取业务经理绑定的质押合同
        List<PledgeContract> dateByUser = pledgeContractService.getDateByUser(String.valueOf(user.getUserId()));
        if (dateByUser == null || dateByUser.isEmpty()) {
            //返回空数据
            return new PageResponse<Page<SnapReply>>(RespCodeEnum.CODE_0000.getCode(), "请求成功", new Page<>(param.getPageNo(), param.getPageSize()));
        }
        List<PledgeContractDepot> pledgeContractDepots = new ArrayList<>() ;
        for (PledgeContract pledgeContract : dateByUser) {
            String id = pledgeContract.getId();
            IgdsBaseParam depotPParam = new IgdsBaseParam();
            depotPParam.setParentId(id);
            List<PledgeContractDepot> pledgeContractDepots1 = pledgeContractDepotService.listAll(depotPParam);
            pledgeContractDepots.addAll(pledgeContractDepots1);
        }
        if(pledgeContractDepots.isEmpty()){
            //返回空数据
            return new PageResponse<Page<SnapReply>>(RespCodeEnum.CODE_0000.getCode(), "请求成功", new Page<>(param.getPageNo(), param.getPageSize()));
        }
 
        // 获取pledgeContractDepots中去重后的库区IDList
        List<String> deptIdList = pledgeContractDepots.stream().map(PledgeContractDepot::getPledgeDept).distinct().collect(Collectors.toList());
 
        //分页拼接
        Page<SnapReply> page = new Page<>(param.getPageNo(), param.getPageSize());
        //查询参数组装
        IgdsBaseParam queryParam = new IgdsBaseParam();
        queryParam.setCompanyId(user.getCompanyId());
        queryParam.setKey(Constant.YN_N);
        snapReplyService.listPageByListDept(page, queryParam,deptIdList);
        return new PageResponse<Page<SnapReply>>(RespCodeEnum.CODE_0000.getCode(), "请求成功", page);
    }
 
    public PageResponse<SnapReply> getSnapReplyInfoById(WeChatBaseParam param) {
 
        //判断参数
        if (null == param || StringUtils.isEmpty(param.getId())) {
            return new PageResponse<SnapReply>(RespCodeEnum.CODE_1007.getCode(), "参数不完整,请核查");
 
        }
 
        SnapReply warn = snapReplyService.getById(param.getId());
        if (warn == null) {
            return new PageResponse<SnapReply>(RespCodeEnum.CODE_1007.getCode(), "未获取到该警告信息!");
 
        }
 
        return new PageResponse<SnapReply>(RespCodeEnum.CODE_0000.getCode(), "请求成功", warn);
 
    }
 
    public PageResponse<String> handleSnapReply(WeChatBaseParam param) {
 
        //判断参数
        if (null == param || StringUtils.isEmpty(param.getId())) {
            return new PageResponse<String>(RespCodeEnum.CODE_1007.getCode(), "参数不完整,请核查");
 
        }
 
        //处理时间
        Date date = new Date();
 
//        if(StringUtils.isEmpty(companyId)){
//            companyId = ShiroUtils.getLoginUserCompanyId();
//        }
        String info = (String) param.getRemark();
        String id = param.getId();
        String userName = param.getUserName();
 
        //告警处理
        SnapReply warn = snapReplyService.getById(id);
        if(Constant.YN_N.equals(warn.getIsHandle())){
            return new PageResponse<String>(RespCodeEnum.CODE_1008.getCode(), "改告警已处理,无需重复批复!");
        }
        warn.setId(id);
        warn.setUpdateBy(userName);
        warn.setUpdateTime(date);
        warn.setIsHandle(Constant.YN_Y);
        warn.setReplyText(info);
        warn.setDays(param.getDays());
        snapReplyService.updateData(warn);
 
        return new PageResponse<String>(RespCodeEnum.CODE_0000.getCode(), "请求成功");
 
    }
 
}