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
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
package com.doumee.service.business.impl.hksync.fhk;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.doumee.biz.system.SystemDictDataBiz;
import com.doumee.config.DataSyncConfig;
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.model.LoginUserInfo;
import com.doumee.core.utils.Constants;
import com.doumee.core.utils.DESUtil;
import com.doumee.core.utils.DateUtil;
import com.doumee.dao.business.dao.CompanyMapper;
import com.doumee.dao.business.dao.MemberMapper;
import com.doumee.dao.business.model.*;
import com.doumee.dao.business.model.Member;
import com.doumee.service.business.impl.hksync.HkSyncBaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.*;
 
/**
 * 海康组织人员通过步Service实现
 * @author 江蹄蹄
 * @date 2023/11/30 15:33
 */
@Service
@Slf4j
public class HkSyncOrgUserFromHKServiceImpl extends HkSyncBaseServiceImpl {
    @Autowired
    private CompanyMapper companyMapper;
    @Autowired
    private MemberMapper memberMapper;
 
    @Autowired
    private DataSyncConfig dataSyncConfig;
    @Autowired
    private SystemDictDataBiz systemDictDataBiz;
 
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
 
    /**
     * 同步海康组织信息到业务系统
     */
    @Override
    @Transactional
    public   void syncOrgData(LoginUserInfo user){
        if(Constants.DEALING_HK_ORG){
            return ;
        }
        Constants.DEALING_HK_ORG =true;
        try {
            if( Constants.formatIntegerNum(dataSyncConfig.getOrgUserDataOrigin()) != DataSyncConfig.origin.hk){
                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,当前不支持组织同步操作~");
            }
            TimeRangeListRequest param = new TimeRangeListRequest();
            //获取ERP组织信息(全量同步)
            boolean hasNext = true;
            int curTotal = 0;
            int curPage = 1;
 
            List<Company>  allHkList = new ArrayList<>();
            while (hasNext){
                //分页遍历循环查询所有门禁设备数据
                param = new TimeRangeListRequest();
                param.setPageNo(curPage);
                param.setPageSize(100);
                BaseResponse<BaseListPageResponse<OrgInfoResponse>> response = HKService.orgAllList(param);
                if(response == null || !StringUtils.equals(response.getCode(), HKConstants.RESPONSE_SUCCEE)){
                    throw  new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "对不起,海康同步数据失败~");
                }
                if(response.getData() == null || response.getData().getTotal() ==0){
                    throw  new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"未同步到任何信息!");
                }
                BaseListPageResponse<OrgInfoResponse> r = response.getData();
                curTotal += 100;
                if(curTotal >= r.getTotal()){
                    hasNext = false;
                }
                if(r.getList() == null || r.getList().size()==0){
                    hasNext =false;
                }else{
                    allHkList.addAll(getNewOrgModelBYList(r.getList(),user.getId()));
                }
                curPage++;
            }
            if(allHkList .size() == 0){
                throw  new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"未同步到任何组织信息!");
            }
            companyMapper.delete(new UpdateWrapper<>());//清空原有数据
            companyMapper.insert(allHkList);//插入新数据
        }catch (BusinessException e){
            throw  e;
        }catch (Exception e){
            throw  new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "对不起,海康同步数据失败~");
        }finally {
            Constants.DEALING_HK_ORG =false;
        }
    }
 
    private List<Company> getNewOrgModelBYList(List<OrgInfoResponse> list, Integer userid) {
        List<Company> newList = new ArrayList<>();
        if(list == null || list.size()==0){
            return  newList;
        }
        for(OrgInfoResponse model :list){
            Company c = new Company();
            c.setHkId(model.getOrgIndexCode());
            c.setCode(model.getOrgNo());
            c.setName(model.getOrgName());
            c.setHkStatus(Constants.ONE);
            c.setHkDate(new Date());
            c.setIsdeleted(Constants.ZERO);
            c.setCreateDate(c.getHkDate());
            c.setEditDate(DateUtil.getISO8601DateByStr(model.getUpdateTime()));
            c.setCreator(userid);
            c.setStatus(Constants.ZERO);
            c.setType(Constants.ONE);
            if(!StringUtils.equals(model.getOrgIndexCode(),systemDictDataBiz.queryByCode(Constants.HK_PARAM,Constants.HK_ROOTORG_CODE).getCode())){
                c.setHkParentId(model.getParentOrgIndexCode());
            }
            newList.add(c);
        }
        return newList;
    }
 
    /**
     * 同步海康人员信息
     * @return
     */
    @Override
    public   void syncUserData(LoginUserInfo user){
        if(Constants.DEALING_HK_USER){
            return   ;
        }
        Constants.DEALING_HK_USER =true;
        try {
            if( Constants.formatIntegerNum(dataSyncConfig.getOrgUserDataOrigin()) != DataSyncConfig.origin.hk){
                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,当前不支持组织同步操作~");
            }
            List<Company> companies = companyMapper.selectList(new QueryWrapper<>());
            if(companies == null || companies.size() == 0){
                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "对不起,请先同步企业组织信息~");
            }
            TimeRangeListRequest param = new TimeRangeListRequest();
            //获取ERP组织信息(全量同步)
            boolean hasNext = true;
            int curTotal = 0;
            int curPage = 1;
            List<Member>  allHkList = new ArrayList<>();
            while (hasNext){
                //分页遍历循环查询所有门禁设备数据
                param = new TimeRangeListRequest();
                param.setPageNo(curPage);
                param.setPageSize(100);
                BaseResponse<BaseListPageResponse<UserInfoResponse>> response = HKService.userAllList(param);
                if(response == null || !StringUtils.equals(response.getCode(), HKConstants.RESPONSE_SUCCEE)){
                    throw  new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "对不起,海康同步数据失败~");
                }
                if(response.getData() == null || response.getData().getTotal() ==0){
                    throw  new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"未同步到任何信息!");
                }
                BaseListPageResponse<UserInfoResponse> r = response.getData();
                curTotal += 100;
                if(curTotal >= r.getTotal()){
                    hasNext = false;
                }
                if(r.getList() == null || r.getList().size()==0){
                    hasNext =false;
                }else{
                    allHkList.addAll(getNewUserModelBYList(r.getList(),companies,user.getId()));
                }
                curPage++;
            }
            if(allHkList .size() == 0){
                throw  new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"未同步到任何组织信息!");
            }
            memberMapper.delete(new UpdateWrapper<>());//清空原有数据
            memberMapper.insert(allHkList);//插入新数据
        }catch (BusinessException e){
            throw  e;
        }catch (Exception e){
            throw  new BusinessException(ResponseStatus.SERVER_ERROR.getCode(), "对不起,海康同步数据失败~");
        }finally {
            Constants.DEALING_HK_USER =false;
        }
    }
 
 
    private List<Member> getNewUserModelBYList(List<UserInfoResponse> list, List<Company> companies,Integer userid) {
        List<Member> newList = new ArrayList<>();
        if(list == null || list.size()==0){
            return  newList;
        }
        for(UserInfoResponse model :list){
            Member c = new Member();
            String encryptIdNo = DESUtil.encrypt(Constants.EDS_PWD,model.getCertificateNo());
            c.setIdcardNo(encryptIdNo);
            c.setIdcardDecode(Constants.getTuominStr(model.getCertificateNo()));
            c.setIdcardType(getIdcardTypeByHk(model.getCertificateType()));
            c.setCode(model.getJobNo());
            c.setPhone(model.getPhoneNo());
            c.setHkId(model.getPersonId());
            c.setName(model.getPersonName());
            c.setHkStatus(Constants.ONE);
            c.setHkDate(new Date());
            c.setCanVisit(Constants.ZERO);
            c.setHighCheckor(Constants.ZERO);
            c.setSex(model.getGender());
            c.setIsdeleted(Constants.ZERO);
            c.setCreateDate(c.getHkDate());
            c.setEditDate(DateUtil.getISO8601DateByStr(model.getUpdateTime()));
            c.setCreator(userid);
            c.setStatus(Constants.ZERO);
            c.setType(Constants.TWO);
            c.setCompanyId(getCompanyId(companies,model.getOrgIndexCode()));
            if(model.getPersonPhoto()!=null && model.getPersonPhoto().size()>0){
                c.setFaceId(model.getPersonPhoto().get(0).getPersonPhotoIndexCode());
                c.setFaceImg(HKConstants.IMG_INDEX+model.getPersonPhoto().get(0).getPicUri());
                c.setFaceServerIndexCode(model.getPersonPhoto().get(0).getServerIndexCode());
            }
            newList.add(c);
        }
        return newList;
    }
 
    private Integer getCompanyId(List<Company> companies, String orgIndexCode) {
        if(companies == null){
            return  null;
        }
        for(Company c : companies){
            if(StringUtils.equals(c.getHkId(),orgIndexCode)){
                return c.getId();
            }
        }
        return null;
    }
 
    /**
     *   //证件类型111:身份证414:护照113:户口簿335:驾驶证131:工作证133:学生证990:其他 ;
     *  // 证件类型 0身份证 1港澳证件 2护照
     * @param certificateType
     * @return
     */
    private Integer getIdcardTypeByHk(Integer certificateType) {
        if(Constants.equalsInteger(certificateType,HKConstants.CertificateType.SHENFENZHENG.getKey())){
            return 0;
        }else if(Constants.equalsInteger(certificateType,HKConstants.CertificateType.HUZHAO.getKey())){
            return 2;
        }else if(Constants.equalsInteger(certificateType,HKConstants.CertificateType.JIASHIZHENG.getKey())){
            return 3;
        }else if(Constants.equalsInteger(certificateType,HKConstants.CertificateType.XUESHENGZHENG.getKey())){
            return 4;
        }else if(Constants.equalsInteger(certificateType,HKConstants.CertificateType.GONGXUOZHENG.getKey())){
            return 5;
        }else if(Constants.equalsInteger(certificateType,HKConstants.CertificateType.GONGXUOZHENG.getKey())){
            return 6;
        }else if(Constants.equalsInteger(certificateType,HKConstants.CertificateType.HUKOUBEN.getKey())){
            return 7;
        }else if(Constants.equalsInteger(certificateType,HKConstants.CertificateType.QITA.getKey())){
            return 8;
        }
        return  null;
    }
 
}