PinanbaoContactService.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package com.care.client.service;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import cn.hutool.core.collection.CollUtil;
  4. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  5. import com.care.client.vo.DeviceVO;
  6. import com.care.client.vo.HouseContactVO;
  7. import com.care.common.entity.*;
  8. import com.care.common.enums.OrderStatusEnum;
  9. import com.care.common.enums.RelationTypeEnum;
  10. import com.care.common.exception.BDException;
  11. import com.care.common.service.*;
  12. import lombok.extern.slf4j.Slf4j;
  13. import org.apache.commons.collections4.CollectionUtils;
  14. import org.apache.commons.compress.utils.Lists;
  15. import org.apache.commons.lang3.StringUtils;
  16. import org.springframework.stereotype.Service;
  17. import org.springframework.transaction.annotation.Transactional;
  18. import javax.annotation.Resource;
  19. import java.util.Date;
  20. import java.util.List;
  21. import java.util.concurrent.locks.Lock;
  22. import java.util.concurrent.locks.ReentrantLock;
  23. /**
  24. * @Author: lilt
  25. * @Date: 2021/6/7
  26. * @Desc:
  27. */
  28. @Slf4j
  29. @Service
  30. public class PinanbaoContactService {
  31. @Resource
  32. private CareHouseContactService careHouseContactService;
  33. @Resource
  34. private CareHouseContactRelService careHouseContactRelService;
  35. @Resource
  36. private PinanbaoDeviceService pinanbaoDeviceService;
  37. private final Lock lock = new ReentrantLock();
  38. /**
  39. * 查询紧急联系人
  40. * @param memberId
  41. * @return
  42. */
  43. public List<HouseContactVO> queryContactByMemberId(Long memberId){
  44. List<HouseContactVO> vos = Lists.newArrayList();
  45. QueryWrapper<CareHouseContact> queryWrapper = new QueryWrapper<>();
  46. queryWrapper.lambda().eq(CareHouseContact::getMemberId, memberId)
  47. .orderByAsc(CareHouseContact::getContactLevel);
  48. List<CareHouseContact> contacts = this.careHouseContactService.list(queryWrapper);
  49. if (CollUtil.isNotEmpty(contacts)){
  50. contacts.forEach(item -> {
  51. HouseContactVO vo = new HouseContactVO();
  52. BeanUtil.copyProperties(item,vo);
  53. vo.setRelationTypeDesc(RelationTypeEnum.getCodeToName(vo.getRelationType()));
  54. vos.add(vo);
  55. });
  56. }
  57. return vos;
  58. }
  59. /**
  60. * 新增紧急联系人
  61. * @param memberId
  62. * @return
  63. */
  64. @Transactional(rollbackFor = Exception.class)
  65. public Boolean createMyContact(Long memberId,HouseContactVO vo){
  66. //保存被监护人
  67. CareHouseContact careContact = new CareHouseContact();
  68. BeanUtil.copyProperties(vo,careContact);
  69. careContact.setMemberId(memberId);
  70. careContact.setCreateTime(new Date());
  71. return this.careHouseContactService.save(careContact);
  72. }
  73. /**
  74. * 查询紧急联系人
  75. * @return
  76. */
  77. public HouseContactVO getMyContact(Long memberId, String phone, String openId) {
  78. if (memberId == null) return null;
  79. if (StringUtils.isBlank(phone) && StringUtils.isBlank(openId)) return null;
  80. QueryWrapper<CareHouseContact> queryWrapper = new QueryWrapper<>();
  81. queryWrapper.lambda().eq(CareHouseContact::getMemberId, memberId).and(wrapper->
  82. wrapper.eq(StringUtils.isNotBlank(phone), CareHouseContact::getContactPhone, phone)
  83. .or().eq(StringUtils.isNotBlank(openId), CareHouseContact::getOpenId, openId));
  84. List<CareHouseContact> contactList = careHouseContactService.list(queryWrapper);
  85. if (CollectionUtils.isNotEmpty(contactList)) {
  86. HouseContactVO vo = new HouseContactVO();
  87. BeanUtil.copyProperties(contactList.get(0), vo);
  88. vo.setRelationTypeDesc(RelationTypeEnum.getCodeToName(vo.getRelationType()));
  89. return vo;
  90. }
  91. return null;
  92. }
  93. /**
  94. * 新增修改紧急联系人
  95. * @param vo
  96. * @return
  97. */
  98. @Transactional(rollbackFor = Exception.class)
  99. public Boolean createOrUpdateMyContact(Long devId, HouseContactVO vo) {
  100. lock.lock();
  101. try {
  102. QueryWrapper<CareHouseContact> queryWrapper = new QueryWrapper<>();
  103. queryWrapper.lambda().eq(CareHouseContact::getMemberId, vo.getMemberId()).and(wrapper->
  104. wrapper.eq(StringUtils.isNotBlank(vo.getContactPhone()), CareHouseContact::getContactPhone, vo.getContactPhone())
  105. .or().eq(StringUtils.isNotBlank(vo.getOpenId()), CareHouseContact::getOpenId, vo.getOpenId()));
  106. List<CareHouseContact> contactList = careHouseContactService.list(queryWrapper);
  107. if (CollectionUtils.isEmpty(contactList)) {
  108. //保存被监护人
  109. CareHouseContact careContact = new CareHouseContact();
  110. BeanUtil.copyProperties(vo,careContact);
  111. careContact.setMemberId(vo.getMemberId());
  112. careContact.setCreateTime(new Date());
  113. boolean saved = this.careHouseContactService.save(careContact);
  114. if (saved) {
  115. boolean childSaved = pinanbaoDeviceService.bindHouseContact(devId, careContact.getId());
  116. if (childSaved == false) {
  117. throw new BDException("该设备绑定联系人出错");
  118. }
  119. }
  120. return saved;
  121. } else {
  122. //修改被监护人
  123. CareHouseContact careContact = contactList.get(0);
  124. careContact.setContactName(vo.getContactName());
  125. careContact.setContactPhone(vo.getContactPhone());
  126. careContact.setAddr(vo.getAddr());
  127. careContact.setLatitude(vo.getLatitude());
  128. careContact.setLongitude(vo.getLongitude());
  129. careContact.setRelationType(vo.getRelationType());
  130. boolean updated = this.careHouseContactService.updateById(careContact);
  131. if (devId != null) {
  132. List<CareHouseContactRel> relVos = pinanbaoDeviceService.queryMyContactRelListByDeviceId(devId);
  133. if (updated && (relVos == null || relVos.stream().filter(v -> careContact.getId() == v.getContactId()).count() <= 0)) {
  134. boolean childSaved = pinanbaoDeviceService.bindHouseContact(devId, careContact.getId());
  135. if (childSaved == false) {
  136. throw new BDException("修改紧急联系人, 对该设备绑定联系人出错");
  137. }
  138. }
  139. }
  140. return updated;
  141. }
  142. } catch (Exception e) {
  143. throw new BDException(e.getMessage());
  144. } finally {
  145. lock.unlock();
  146. }
  147. }
  148. /**
  149. * 查询紧急联系人详情
  150. * @param id
  151. * @return
  152. */
  153. public HouseContactVO getContactInfo(Long id){
  154. CareHouseContact careContact = this.careHouseContactService.getById(id);
  155. HouseContactVO vo = new HouseContactVO();
  156. BeanUtil.copyProperties(careContact,vo);
  157. vo.setRelationTypeDesc(RelationTypeEnum.getCodeToName(vo.getRelationType()));
  158. return vo;
  159. }
  160. /**
  161. * 修改紧急联系人
  162. * @param vo
  163. */
  164. @Transactional(rollbackFor = Exception.class)
  165. public Boolean updateMyContact(HouseContactVO vo){
  166. //修改紧急联系人
  167. CareHouseContact careContact = this.careHouseContactService.getById(vo.getId());
  168. BeanUtil.copyProperties(vo,careContact);
  169. careContact.setModifyTime(new Date());
  170. return this.careHouseContactService.updateById(careContact);
  171. }
  172. /**
  173. * 删除紧急联系人
  174. * @param id
  175. * @return
  176. */
  177. public boolean deleteById(Long id){
  178. QueryWrapper<CareHouseContactRel> queryWrapper = new QueryWrapper<>();
  179. queryWrapper.lambda().eq(CareHouseContactRel::getContactId,id);
  180. careHouseContactRelService.remove(queryWrapper);
  181. return this.careHouseContactService.removeById(id);
  182. }
  183. }