| 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.*; | 
| import com.doumee.dao.business.dao.CompanyMapper; | 
| import com.doumee.dao.business.model.*; | 
| 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; | 
|     } | 
|   | 
| } |