package com.doumee.service.business.impl; import cn.hutool.core.util.IdcardUtil; import com.doumee.biz.system.SystemDictDataBiz; import com.doumee.core.constants.ResponseStatus; import com.doumee.core.exception.BusinessException; import com.doumee.core.model.PageData; import com.doumee.core.model.PageWrap; import com.doumee.core.utils.Constants; import com.doumee.core.utils.DESUtil; import com.doumee.core.utils.Utils; import com.doumee.dao.business.DeviceRoleMapper; import com.doumee.dao.business.MemberMapper; import com.doumee.dao.business.ProblemLogMapper; import com.doumee.dao.business.VisitsMapper; import com.doumee.dao.business.model.DeviceRole; import com.doumee.dao.business.model.Member; import com.doumee.dao.business.model.ProblemLog; import com.doumee.dao.business.model.Visits; import com.doumee.service.business.VisitsService; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import org.springframework.util.DigestUtils; import java.util.Arrays; import java.util.Date; import java.util.List; /** * 访客申请信息表Service实现 * @author 江蹄蹄 * @date 2023/11/30 15:33 */ @Service public class VisitsServiceImpl implements VisitsService { @Autowired private VisitsMapper visitsMapper; @Autowired private DeviceRoleMapper deviceRoleMapper; @Autowired private ProblemLogMapper problemLogMapper ; @Autowired private MemberMapper memberMapper ; @Autowired private SystemDictDataBiz systemDictDataBiz; @Override public Integer create(Visits visits) { visitsMapper.insert(visits); return visits.getId(); } /** * 普通访客申请 * @param visits * @return */ @Override public Integer createFk(Visits visits) { if(visits.getStarttime() == null || visits.getEndtime() == null || StringUtils.isBlank(visits.getReason()) || StringUtils.isBlank( visits.getName()) || StringUtils.isBlank( visits.getPhone()) || visits.getIdcardType() == null || StringUtils.isBlank( visits.getFaceImg() ) || StringUtils.isBlank( visits.getIdcardNo() ) || visits.getReceptMemberId() == null){ throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,信息填写不正确!"); } //检查是否必须答题,并且符合答题要求 ProblemLog problemLog = isValidProblemLog(visits); //获取申请的海康访问门禁组信息 String[] hkIds = getHkDeviceRoles(visits); //检验拜访人是否合法 isValideVisitedUser(visits.getReceptMemberId()); //检查随访人员是否合法,生成随访人员新增集合 List withUsers = isValideWithUsers(visits.getWithUserList()); if(Constants.equalsInteger(Constants.ZERO, visits.getIdcardType()) &&!IdcardUtil.isValidCard(visits.getIdcardNo())){ throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,身份证号码有误,请核实后重试!"); } Date date = new Date(); visits.setBirthday(Constants.getBirthdyByCardNo(visits.getIdcardNo())); //身份证号存储密文 visits.setIdcardNo(DESUtil.encrypt(Constants.EDS_PWD, visits.getIdcardNo()));//身份证号加密 visits.setIdcardDecode(Constants.getTuominStr(visits.getIdcardNo()));//脱敏手机号 visits.setCreateDate(date); //访客信息(使用身份证号查询是否已存在改普通访客,没有则新增,存在则更新openid等信息) Member member = getMemberByIdcardno(visits); visitsMapper.insert(visits); updateProblemLog(visits,problemLog,member); return visits.getId(); } private Member getMemberByIdcardno(Visits idcardNo) { Member member = memberMapper.selectOne(new QueryWrapper().lambda() // .eq(Member::getType,Constants.MEMBER_TYPE) .eq(Member::getIdcardNo,idcardNo.getIdcardNo()) ); return null; } private List isValideWithUsers(List withUserList) { if(withUserList!= null && withUserList .size()>0){ for(Visits model:withUserList){ } } return null; } /** * 如果有答题记录,更新答题记录关联数据 * @param visits * @param problemLog * @param member */ private void updateProblemLog(Visits visits, ProblemLog problemLog,Member member) { if(problemLog!=null){ problemLog.setEditDate(visits.getCreateDate()); problemLog.setName(visits.getName()); problemLog.setUserId(member.getId()); problemLog.setPhone(member.getPhone()); problemLog.setLogId(visits.getId()); problemLogMapper.updateById(problemLog); } } /** * 检查访人是否合法 * @param receptMemberId */ private Member isValideVisitedUser(Integer receptMemberId) { Member member = memberMapper.selectById(receptMemberId); if(member == null || Constants.equalsInteger(Constants.ZERO,member.getIsdeleted())){ throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,该拜访人暂时不能接受您的拜访申请!"); } if(!Constants.equalsInteger(member.getStatus(), Constants.ZERO)){ throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,该拜访人暂时不能接受您的拜访申请."); } if(!Constants.equalsInteger(Constants.ONE, member.getCanVisit())){ throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,该拜访人暂时不能接受您的拜访申请~"); } return member; } private ProblemLog isValidProblemLog(Visits visits) { String required = systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.PROBLEM_VISIT_REQUIRED).getCode(); if(StringUtils.equals(required,Constants.ONE+"")){ //如果必须答题,查找答题记录 if(visits.getUserAnswerId() == null){ throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,请先按要求进行安全知识答题!"); } ProblemLog log = problemLogMapper.selectById(visits.getUserAnswerId()); if(log == null){ throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,请先按要求进行安全知识答题."); } if(log.getLogId() !=null){ throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,请先按要求进行安全知识答题~"); } return log; } return null; } /** * 获取申请的海康访问门禁组信息 * @param visits * @return */ private String[] getHkDeviceRoles(Visits visits) { String mustSelectDoors = systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.SELECT_DOORS_VISIT_REQUIRED).getCode(); List roles = null; if(StringUtils.equals(mustSelectDoors,Constants.ZERO+"")){ //如果必须选择门禁,但未选择 if(StringUtils.isBlank(visits.getDoors())){ throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,请选择访问门禁!"); } //校验门禁组信息是否正确 String[] dIds = visits.getDoors().split(","); roles = deviceRoleMapper.selectList(new QueryWrapper().lambda() .eq(DeviceRole::getIsdeleted,Constants.ZERO) .eq(DeviceRole::getType,Constants.DeviceRoleType.fk) .in(DeviceRole::getId, Arrays.asList(dIds))); if(roles == null || roles.size()==0 || roles.size() != dIds.length){ throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,请选择有效访问门禁!"); } }else{ //如果设置不选择门禁,则提供默认门禁组信息 String[] dIds = visits.getDoors().split(","); roles = deviceRoleMapper.selectList(new QueryWrapper().lambda() .eq(DeviceRole::getIsdeleted,Constants.ZERO) .eq(DeviceRole::getIsDefault,Constants.ZERO) .eq(DeviceRole::getType,Constants.DeviceRoleType.fk)); if(roles.size() ==0){ throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), "对不起,暂不存在可访问的访客门禁,无法进行申请操作!"); } } String[] ids =new String[roles.size()]; for (int i = 0; i < roles.size(); i++) { ids[i] = roles.get(i).getHkId(); } return ids; } @Override public void deleteById(Integer id) { visitsMapper.deleteById(id); } @Override public void delete(Visits visits) { UpdateWrapper deleteWrapper = new UpdateWrapper<>(visits); visitsMapper.delete(deleteWrapper); } @Override public void deleteByIdInBatch(List ids) { if (CollectionUtils.isEmpty(ids)) { return; } visitsMapper.deleteBatchIds(ids); } @Override public void updateById(Visits visits) { visitsMapper.updateById(visits); } @Override public void updateByIdInBatch(List visitss) { if (CollectionUtils.isEmpty(visitss)) { return; } for (Visits visits: visitss) { this.updateById(visits); } } @Override public Visits findById(Integer id) { return visitsMapper.selectById(id); } @Override public Visits findOne(Visits visits) { QueryWrapper wrapper = new QueryWrapper<>(visits); return visitsMapper.selectOne(wrapper); } @Override public List findList(Visits visits) { QueryWrapper wrapper = new QueryWrapper<>(visits); return visitsMapper.selectList(wrapper); } @Override public PageData findPage(PageWrap pageWrap) { IPage page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity()); QueryWrapper queryWrapper = new QueryWrapper<>(); Utils.MP.blankToNull(pageWrap.getModel()); if (pageWrap.getModel().getId() != null) { queryWrapper.lambda().eq(Visits::getId, pageWrap.getModel().getId()); } if (pageWrap.getModel().getCreator() != null) { queryWrapper.lambda().eq(Visits::getCreator, pageWrap.getModel().getCreator()); } if (pageWrap.getModel().getCreateDate() != null) { queryWrapper.lambda().ge(Visits::getCreateDate, Utils.Date.getStart(pageWrap.getModel().getCreateDate())); queryWrapper.lambda().le(Visits::getCreateDate, Utils.Date.getEnd(pageWrap.getModel().getCreateDate())); } if (pageWrap.getModel().getEditor() != null) { queryWrapper.lambda().eq(Visits::getEditor, pageWrap.getModel().getEditor()); } if (pageWrap.getModel().getEditDate() != null) { queryWrapper.lambda().ge(Visits::getEditDate, Utils.Date.getStart(pageWrap.getModel().getEditDate())); queryWrapper.lambda().le(Visits::getEditDate, Utils.Date.getEnd(pageWrap.getModel().getEditDate())); } if (pageWrap.getModel().getIsdeleted() != null) { queryWrapper.lambda().eq(Visits::getIsdeleted, pageWrap.getModel().getIsdeleted()); } if (pageWrap.getModel().getRemark() != null) { queryWrapper.lambda().eq(Visits::getRemark, pageWrap.getModel().getRemark()); } if (pageWrap.getModel().getMemberId() != null) { queryWrapper.lambda().eq(Visits::getMemberId, pageWrap.getModel().getMemberId()); } if (pageWrap.getModel().getCompanyId() != null) { queryWrapper.lambda().eq(Visits::getCompanyId, pageWrap.getModel().getCompanyId()); } if (pageWrap.getModel().getFaceImg() != null) { queryWrapper.lambda().eq(Visits::getFaceImg, pageWrap.getModel().getFaceImg()); } if (pageWrap.getModel().getImgurl() != null) { queryWrapper.lambda().eq(Visits::getImgurl, pageWrap.getModel().getImgurl()); } if (pageWrap.getModel().getType() != null) { queryWrapper.lambda().eq(Visits::getType, pageWrap.getModel().getType()); } if (pageWrap.getModel().getName() != null) { queryWrapper.lambda().eq(Visits::getName, pageWrap.getModel().getName()); } if (pageWrap.getModel().getBirthday() != null) { queryWrapper.lambda().ge(Visits::getBirthday, Utils.Date.getStart(pageWrap.getModel().getBirthday())); queryWrapper.lambda().le(Visits::getBirthday, Utils.Date.getEnd(pageWrap.getModel().getBirthday())); } if (pageWrap.getModel().getPhone() != null) { queryWrapper.lambda().eq(Visits::getPhone, pageWrap.getModel().getPhone()); } if (pageWrap.getModel().getCompanyName() != null) { queryWrapper.lambda().eq(Visits::getCompanyName, pageWrap.getModel().getCompanyName()); } if (pageWrap.getModel().getIdcardNo() != null) { queryWrapper.lambda().eq(Visits::getIdcardNo, pageWrap.getModel().getIdcardNo()); } if (pageWrap.getModel().getCode() != null) { queryWrapper.lambda().eq(Visits::getCode, pageWrap.getModel().getCode()); } if (pageWrap.getModel().getClasses() != null) { queryWrapper.lambda().eq(Visits::getClasses, pageWrap.getModel().getClasses()); } if (pageWrap.getModel().getMemberNum() != null) { queryWrapper.lambda().eq(Visits::getMemberNum, pageWrap.getModel().getMemberNum()); } if (pageWrap.getModel().getStarttime() != null) { queryWrapper.lambda().ge(Visits::getStarttime, Utils.Date.getStart(pageWrap.getModel().getStarttime())); queryWrapper.lambda().le(Visits::getStarttime, Utils.Date.getEnd(pageWrap.getModel().getStarttime())); } if (pageWrap.getModel().getEndtime() != null) { queryWrapper.lambda().ge(Visits::getEndtime, Utils.Date.getStart(pageWrap.getModel().getEndtime())); queryWrapper.lambda().le(Visits::getEndtime, Utils.Date.getEnd(pageWrap.getModel().getEndtime())); } if (pageWrap.getModel().getReason() != null) { queryWrapper.lambda().eq(Visits::getReason, pageWrap.getModel().getReason()); } if (pageWrap.getModel().getDoorSelect() != null) { queryWrapper.lambda().eq(Visits::getDoorSelect, pageWrap.getModel().getDoorSelect()); } if (pageWrap.getModel().getDoors() != null) { queryWrapper.lambda().eq(Visits::getDoors, pageWrap.getModel().getDoors()); } if (pageWrap.getModel().getReceptMemberId() != null) { queryWrapper.lambda().eq(Visits::getReceptMemberId, pageWrap.getModel().getReceptMemberId()); } if (pageWrap.getModel().getStatus() != null) { queryWrapper.lambda().eq(Visits::getStatus, pageWrap.getModel().getStatus()); } if (pageWrap.getModel().getCheckorId() != null) { queryWrapper.lambda().eq(Visits::getCheckorId, pageWrap.getModel().getCheckorId()); } if (pageWrap.getModel().getCheckDate() != null) { queryWrapper.lambda().ge(Visits::getCheckDate, Utils.Date.getStart(pageWrap.getModel().getCheckDate())); queryWrapper.lambda().le(Visits::getCheckDate, Utils.Date.getEnd(pageWrap.getModel().getCheckDate())); } if (pageWrap.getModel().getCheckInfo() != null) { queryWrapper.lambda().eq(Visits::getCheckInfo, pageWrap.getModel().getCheckInfo()); } if (pageWrap.getModel().getEndCheckorId() != null) { queryWrapper.lambda().eq(Visits::getEndCheckorId, pageWrap.getModel().getEndCheckorId()); } if (pageWrap.getModel().getEndCheckDate() != null) { queryWrapper.lambda().ge(Visits::getEndCheckDate, Utils.Date.getStart(pageWrap.getModel().getEndCheckDate())); queryWrapper.lambda().le(Visits::getEndCheckDate, Utils.Date.getEnd(pageWrap.getModel().getEndCheckDate())); } if (pageWrap.getModel().getEndCheckInfo() != null) { queryWrapper.lambda().eq(Visits::getEndCheckInfo, pageWrap.getModel().getEndCheckInfo()); } if (pageWrap.getModel().getIdcardType() != null) { queryWrapper.lambda().eq(Visits::getIdcardType, pageWrap.getModel().getIdcardType()); } if (pageWrap.getModel().getCarNos() != null) { queryWrapper.lambda().eq(Visits::getCarNos, pageWrap.getModel().getCarNos()); } if (pageWrap.getModel().getParentId() != null) { queryWrapper.lambda().eq(Visits::getParentId, pageWrap.getModel().getParentId()); } if (pageWrap.getModel().getUserAnswerId() != null) { queryWrapper.lambda().eq(Visits::getUserAnswerId, pageWrap.getModel().getUserAnswerId()); } if (pageWrap.getModel().getHkId() != null) { queryWrapper.lambda().eq(Visits::getHkId, pageWrap.getModel().getHkId()); } if (pageWrap.getModel().getHkStatus() != null) { queryWrapper.lambda().eq(Visits::getHkStatus, pageWrap.getModel().getHkStatus()); } if (pageWrap.getModel().getHkDate() != null) { queryWrapper.lambda().ge(Visits::getHkDate, Utils.Date.getStart(pageWrap.getModel().getHkDate())); queryWrapper.lambda().le(Visits::getHkDate, Utils.Date.getEnd(pageWrap.getModel().getHkDate())); } if (pageWrap.getModel().getErpId() != null) { queryWrapper.lambda().eq(Visits::getErpId, pageWrap.getModel().getErpId()); } for(PageWrap.SortData sortData: pageWrap.getSorts()) { if (sortData.getDirection().equalsIgnoreCase(PageWrap.DESC)) { queryWrapper.orderByDesc(sortData.getProperty()); } else { queryWrapper.orderByAsc(sortData.getProperty()); } } return PageData.from(visitsMapper.selectPage(page, queryWrapper)); } @Override public long count(Visits visits) { QueryWrapper wrapper = new QueryWrapper<>(visits); return visitsMapper.selectCount(wrapper); } }