123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- package com.care.client.service;
- import cn.hutool.core.bean.BeanUtil;
- import cn.hutool.core.collection.CollUtil;
- import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
- import com.care.client.vo.DeviceVO;
- import com.care.client.vo.HouseContactVO;
- import com.care.common.entity.*;
- import com.care.common.enums.OrderStatusEnum;
- import com.care.common.enums.RelationTypeEnum;
- import com.care.common.exception.BDException;
- import com.care.common.service.*;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.collections4.CollectionUtils;
- import org.apache.commons.compress.utils.Lists;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import javax.annotation.Resource;
- import java.util.Date;
- import java.util.List;
- import java.util.concurrent.locks.Lock;
- import java.util.concurrent.locks.ReentrantLock;
- /**
- * @Author: lilt
- * @Date: 2021/6/7
- * @Desc:
- */
- @Slf4j
- @Service
- public class PinanbaoContactService {
- @Resource
- private CareHouseContactService careHouseContactService;
- @Resource
- private CareHouseContactRelService careHouseContactRelService;
- @Resource
- private PinanbaoDeviceService pinanbaoDeviceService;
- private final Lock lock = new ReentrantLock();
- /**
- * 查询紧急联系人
- * @param memberId
- * @return
- */
- public List<HouseContactVO> queryContactByMemberId(Long memberId){
- List<HouseContactVO> vos = Lists.newArrayList();
- QueryWrapper<CareHouseContact> queryWrapper = new QueryWrapper<>();
- queryWrapper.lambda().eq(CareHouseContact::getMemberId, memberId)
- .orderByAsc(CareHouseContact::getContactLevel);
- List<CareHouseContact> contacts = this.careHouseContactService.list(queryWrapper);
- if (CollUtil.isNotEmpty(contacts)){
- contacts.forEach(item -> {
- HouseContactVO vo = new HouseContactVO();
- BeanUtil.copyProperties(item,vo);
- vo.setRelationTypeDesc(RelationTypeEnum.getCodeToName(vo.getRelationType()));
- vos.add(vo);
- });
- }
- return vos;
- }
- /**
- * 新增紧急联系人
- * @param memberId
- * @return
- */
- @Transactional(rollbackFor = Exception.class)
- public Boolean createMyContact(Long memberId,HouseContactVO vo){
- //保存被监护人
- CareHouseContact careContact = new CareHouseContact();
- BeanUtil.copyProperties(vo,careContact);
- careContact.setMemberId(memberId);
- careContact.setCreateTime(new Date());
- return this.careHouseContactService.save(careContact);
- }
- /**
- * 查询紧急联系人
- * @return
- */
- public HouseContactVO getMyContact(Long memberId, String phone, String openId) {
- if (memberId == null) return null;
- if (StringUtils.isBlank(phone) && StringUtils.isBlank(openId)) return null;
- QueryWrapper<CareHouseContact> queryWrapper = new QueryWrapper<>();
- queryWrapper.lambda().eq(CareHouseContact::getMemberId, memberId).and(wrapper->
- wrapper.eq(StringUtils.isNotBlank(phone), CareHouseContact::getContactPhone, phone)
- .or().eq(StringUtils.isNotBlank(openId), CareHouseContact::getOpenId, openId));
- List<CareHouseContact> contactList = careHouseContactService.list(queryWrapper);
- if (CollectionUtils.isNotEmpty(contactList)) {
- HouseContactVO vo = new HouseContactVO();
- BeanUtil.copyProperties(contactList.get(0), vo);
- vo.setRelationTypeDesc(RelationTypeEnum.getCodeToName(vo.getRelationType()));
- return vo;
- }
- return null;
- }
- /**
- * 新增修改紧急联系人
- * @param vo
- * @return
- */
- @Transactional(rollbackFor = Exception.class)
- public Boolean createOrUpdateMyContact(Long devId, HouseContactVO vo) {
- lock.lock();
- try {
- QueryWrapper<CareHouseContact> queryWrapper = new QueryWrapper<>();
- queryWrapper.lambda().eq(CareHouseContact::getMemberId, vo.getMemberId()).and(wrapper->
- wrapper.eq(StringUtils.isNotBlank(vo.getContactPhone()), CareHouseContact::getContactPhone, vo.getContactPhone())
- .or().eq(StringUtils.isNotBlank(vo.getOpenId()), CareHouseContact::getOpenId, vo.getOpenId()));
- List<CareHouseContact> contactList = careHouseContactService.list(queryWrapper);
- if (CollectionUtils.isEmpty(contactList)) {
- //保存被监护人
- CareHouseContact careContact = new CareHouseContact();
- BeanUtil.copyProperties(vo,careContact);
- careContact.setMemberId(vo.getMemberId());
- careContact.setCreateTime(new Date());
- boolean saved = this.careHouseContactService.save(careContact);
- if (saved) {
- boolean childSaved = pinanbaoDeviceService.bindHouseContact(devId, careContact.getId());
- if (childSaved == false) {
- throw new BDException("该设备绑定联系人出错");
- }
- }
- return saved;
- } else {
- //修改被监护人
- CareHouseContact careContact = contactList.get(0);
- careContact.setContactName(vo.getContactName());
- careContact.setContactPhone(vo.getContactPhone());
- careContact.setAddr(vo.getAddr());
- careContact.setLatitude(vo.getLatitude());
- careContact.setLongitude(vo.getLongitude());
- careContact.setRelationType(vo.getRelationType());
- boolean updated = this.careHouseContactService.updateById(careContact);
- if (devId != null) {
- List<CareHouseContactRel> relVos = pinanbaoDeviceService.queryMyContactRelListByDeviceId(devId);
- if (updated && (relVos == null || relVos.stream().filter(v -> careContact.getId() == v.getContactId()).count() <= 0)) {
- boolean childSaved = pinanbaoDeviceService.bindHouseContact(devId, careContact.getId());
- if (childSaved == false) {
- throw new BDException("修改紧急联系人, 对该设备绑定联系人出错");
- }
- }
- }
- return updated;
- }
- } catch (Exception e) {
- throw new BDException(e.getMessage());
- } finally {
- lock.unlock();
- }
- }
- /**
- * 查询紧急联系人详情
- * @param id
- * @return
- */
- public HouseContactVO getContactInfo(Long id){
- CareHouseContact careContact = this.careHouseContactService.getById(id);
- HouseContactVO vo = new HouseContactVO();
- BeanUtil.copyProperties(careContact,vo);
- vo.setRelationTypeDesc(RelationTypeEnum.getCodeToName(vo.getRelationType()));
- return vo;
- }
- /**
- * 修改紧急联系人
- * @param vo
- */
- @Transactional(rollbackFor = Exception.class)
- public Boolean updateMyContact(HouseContactVO vo){
- //修改紧急联系人
- CareHouseContact careContact = this.careHouseContactService.getById(vo.getId());
- BeanUtil.copyProperties(vo,careContact);
- careContact.setModifyTime(new Date());
- return this.careHouseContactService.updateById(careContact);
- }
- /**
- * 删除紧急联系人
- * @param id
- * @return
- */
- public boolean deleteById(Long id){
- QueryWrapper<CareHouseContactRel> queryWrapper = new QueryWrapper<>();
- queryWrapper.lambda().eq(CareHouseContactRel::getContactId,id);
- careHouseContactRelService.remove(queryWrapper);
- return this.careHouseContactService.removeById(id);
- }
- }
|