jiangping
2024-12-30 3af254f1b36e7722673fcebe110da524b276105c
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
package com.doumee.service.business.impl.hksync;
 
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.haikang.model.HKConstants;
import com.doumee.core.haikang.model.param.BaseListPageResponse;
import com.doumee.core.haikang.model.param.BaseResponse;
import com.doumee.core.haikang.model.param.request.*;
import com.doumee.core.haikang.model.param.respose.*;
import com.doumee.core.haikang.service.HKService;
import com.doumee.core.utils.Constants;
import com.doumee.core.utils.DateUtil;
import com.doumee.dao.business.dao.MemberMapper;
import com.doumee.dao.business.model.Member;
import com.doumee.dao.system.join.NoticesJoinMapper;
import com.doumee.dao.system.model.Notices;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.*;
 
/**
 * 海康访客业务Service实现
 * @author 江蹄蹄
 * @date 2023/11/30 15:33
 */
@Service
@Slf4j
public class HkSyncNoticeServiceImpl extends HkSyncBaseServiceImpl {
    @Autowired
    private NoticesJoinMapper noticesJoinMapper;
    @Autowired
    private MemberMapper memberMapper;
 
    @Override
    public String syncHkNotices(String username){
        if(Constants.DEALING_HK_NOTICE_LIST){
            return   null;
        }
        Constants.DEALING_HK_NOTICE_LIST =true;
        try {
            List<String> users = new ArrayList<>();
            users.add(username);
 
            List<Integer> userIds = new ArrayList<>();
            List<Member > userList = memberMapper.selectList(new QueryWrapper<Member>().lambda()
                    .in(Member::getPhone,users)
                    .eq(Member::getType,Constants.TWO)
                    .eq(Member::getIsdeleted,Constants.ZERO));
            Member tu= getFromUserList(username,userList);
            if(tu!=null){
                userIds.add(tu.getId());
            }else{
                throw  new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "对不起,无效账号~");
            }
            if(userIds.size()>0){
                //清空海康全部的代办数据
                noticesJoinMapper.delete(new UpdateWrapper<Notices>().lambda()
                        .eq(Notices::getType, Constants.noticesObjectType.hknotice)
                        .in(Notices::getUserId, userIds));
            }
            Date date = new Date();
            List<GetTodoListResponse> allHkList = new ArrayList<>();
            boolean hasNext = true;
            int curTotal = 0;
            int curPage = 1;
            while (hasNext){
                //分页遍历循环查询所有门禁设备数据
                GetTodoListRequest param = new GetTodoListRequest();
                param.setComId("dfe");
                param.setUserId(username);
                param.setStatus(1);//只查待处理的数据
                param.setPageNo(curPage);
                param.setLocaleType("zh_CN");
                param.setPageSize(100);
                BaseResponse<BaseListPageResponse<GetTodoListResponse>>   response = HKService.getTodoListPage(param);
                if(response == null || !StringUtils.equals(response.getCode(), HKConstants.RESPONSE_SUCCEE)){
                    throw  new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "对不起,海康同步数据失败~");
                }
                BaseListPageResponse<GetTodoListResponse> r = response.getData();
                curTotal += 100;
                if(curTotal >= r.getTotal()){
                    hasNext = false;
                }
                if(r.getList() == null || r.getList().size()==0){
                    hasNext =false;
                }else{
                    allHkList.addAll(r.getList());
                }
                curPage++;
            }
 
 
            if(allHkList!=null && allHkList.size()>0){
                 List<Notices> list = new ArrayList<>();
                for(GetTodoListResponse data :allHkList){
                   Member u = getFromUserList(data.getUserId(),userList);
                   if(u==null){
                       continue;
                   }
                    userIds.add(u.getId());
                    JSONObject param = new JSONObject();
 
                    param.put("componentId","dfe");
                    param.put("msgType","tlnc");
                    param.put("componentMenuId", data.getMenuCode());
                    JSONObject c = new JSONObject();
                    c.put("method","dealTlncMsg");
                    c.put("argument",JSONObject.toJSONString(data));
                    param.put("callback",c);
                    Notices notices = new Notices();
                    notices.setCreateDate(DateUtil.getISO8601DateByStr(data.getMsgCreateTimeIso()));
                    notices.setUserId(u.getId());
                    notices.setStatus(Constants.ZERO);
                    notices.setSendacopy(Constants.ZERO);
                    //业务状态信息 0 =待处理;1=已同意/已处理;2=已拒绝/已退回;3=已转交;4=已撤销
                    notices.setParam1(JSONObject.toJSONString(data));
                    notices.setParam2(Constants.ZERO+"");
                    notices.setTitle(data.getMsgTitle());
                    if(notices.getCreateDate() == null){
                        notices.setCreateDate(date);
                    }
                    notices.setIsdeleted(Constants.ZERO);
                    notices.setParam4(data.getMsgCreateTimeIso());
                    notices.setParam3(data.getMsgCreateTime());
                    notices.setParam5(JSONObject.toJSONString(param));
                    notices.setType(Constants.noticesObjectType.hknotice);
                    list.add(notices);
                }
                 if(list.size()>0){
                    noticesJoinMapper.insert(list);//批量插入记录
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            Constants.DEALING_HK_NOTICE_LIST =false;
        }
        return  null;
    }
 
    private Member getFromUserList(String userId, List<Member> userList) {
        if(userList!=null ){
            for(Member u :userList){
                if(StringUtils.equals(u.getPhone(),userId)){
                    return  u;
                }
            }
        }
        return null;
    }
 
 
}