From 263f4cdc0eb96f924d8967bb77f953477022774e Mon Sep 17 00:00:00 2001 From: Burce Date: Tue, 8 Oct 2019 18:11:16 +0800 Subject: [PATCH] =?UTF-8?q?[=E6=8B=9B=E5=95=86]add=20RentContract=20Event?= =?UTF-8?q?=20logic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/iformall/domain/vo/RentEventInfo.java | 18 ++++++ .../service/WxRentContractService.java | 2 + .../service/impl/WxFlowServiceImpl.java | 1 + .../service/impl/WxMerchantServiceImpl.java | 3 + .../impl/WxRentContractServiceImpl.java | 38 +++++++++++-- .../service/invest/event/InvestEvent.java | 14 +++++ .../service/invest/event/InvestListener.java | 56 +++++++++++++++++++ .../java/com/iformall/utils/EventUtil.java | 24 ++++++++ .../iformall/utils/SpringContextUtils.java | 5 ++ 9 files changed, 157 insertions(+), 4 deletions(-) create mode 100644 mallinkService/src/main/java/com/iformall/domain/vo/RentEventInfo.java create mode 100644 mallinkService/src/main/java/com/iformall/service/invest/event/InvestEvent.java create mode 100644 mallinkService/src/main/java/com/iformall/service/invest/event/InvestListener.java create mode 100644 mallinkService/src/main/java/com/iformall/utils/EventUtil.java diff --git a/mallinkService/src/main/java/com/iformall/domain/vo/RentEventInfo.java b/mallinkService/src/main/java/com/iformall/domain/vo/RentEventInfo.java new file mode 100644 index 000000000..c252a63d7 --- /dev/null +++ b/mallinkService/src/main/java/com/iformall/domain/vo/RentEventInfo.java @@ -0,0 +1,18 @@ +package com.iformall.domain.vo; + +import com.iformall.enums.EnumRentContractStatus; +import lombok.Data; + +import java.util.List; + +/** + * 合同签约信息 + * 商铺 -> 合同 : 1 -> 1 + * 商铺 -> 合同 : n -> 1 + */ +@Data(staticConstructor = "of") +public class RentEventInfo { + private final Long rentId ; + private final List targetIds ; + private final EnumRentContractStatus type ; +} diff --git a/mallinkService/src/main/java/com/iformall/service/WxRentContractService.java b/mallinkService/src/main/java/com/iformall/service/WxRentContractService.java index 3e5288fdc..db4c718bc 100644 --- a/mallinkService/src/main/java/com/iformall/service/WxRentContractService.java +++ b/mallinkService/src/main/java/com/iformall/service/WxRentContractService.java @@ -70,6 +70,8 @@ public interface WxRentContractService { ResultData updateRentContractStatus(Long id); + List getShopIds(WxRentContract record) ; + void updateApplyStatus(WxRentContract wxRentContract); List buildRent(WxMerchant wxMerchant, Long userId,WxRentContract rentContract,Integer isPreview,boolean saveDb); diff --git a/mallinkService/src/main/java/com/iformall/service/impl/WxFlowServiceImpl.java b/mallinkService/src/main/java/com/iformall/service/impl/WxFlowServiceImpl.java index 04c4a85f6..b8bbf0041 100644 --- a/mallinkService/src/main/java/com/iformall/service/impl/WxFlowServiceImpl.java +++ b/mallinkService/src/main/java/com/iformall/service/impl/WxFlowServiceImpl.java @@ -18,6 +18,7 @@ import com.iformall.mapper.*; import com.iformall.mq.MqBaseProducer; import com.iformall.service.*; import com.iformall.utils.Constant; +import com.iformall.utils.EventUtil; import com.iformall.utils.JsonUtil; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.map.HashedMap; diff --git a/mallinkService/src/main/java/com/iformall/service/impl/WxMerchantServiceImpl.java b/mallinkService/src/main/java/com/iformall/service/impl/WxMerchantServiceImpl.java index 7468228e3..a7f2809e1 100644 --- a/mallinkService/src/main/java/com/iformall/service/impl/WxMerchantServiceImpl.java +++ b/mallinkService/src/main/java/com/iformall/service/impl/WxMerchantServiceImpl.java @@ -21,6 +21,7 @@ import com.iformall.service.*; import com.iformall.utils.BeanMapping; import com.iformall.utils.DataUtil; import com.iformall.utils.DateUtils; +import com.iformall.utils.EventUtil; import me.chanjar.weixin.common.util.DataUtils; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; @@ -390,6 +391,8 @@ public class WxMerchantServiceImpl implements WxMerchantService { wxRentContract.setStatus(EnumRentContractStatus.SIGNED_RENT_UNPAID.getCode()); } wxRentContractMapper.updateById(wxRentContract); + + EventUtil.publistRentEvent(wxRentContractService,wxRentContract); //预览账单改为正式,并写入商户id(租赁合同需要绑定商户才变成已签约) WxBillRent billRent = new WxBillRent(); billRent.setRentContractId(wxMerchant.getRentContractId()); diff --git a/mallinkService/src/main/java/com/iformall/service/impl/WxRentContractServiceImpl.java b/mallinkService/src/main/java/com/iformall/service/impl/WxRentContractServiceImpl.java index 3e153fc96..3610410dc 100644 --- a/mallinkService/src/main/java/com/iformall/service/impl/WxRentContractServiceImpl.java +++ b/mallinkService/src/main/java/com/iformall/service/impl/WxRentContractServiceImpl.java @@ -12,15 +12,15 @@ import com.iformall.common.ResultData; import com.iformall.domain.po.*; import com.iformall.domain.vo.BillTimeVo; import com.iformall.domain.vo.RatioVo; +import com.iformall.domain.vo.RentEventInfo; import com.iformall.enums.*; import com.iformall.exception.MallinkException; import com.iformall.mapper.*; import com.iformall.service.*; -import com.iformall.utils.DateUtils; -import com.iformall.utils.DownFileUtil; -import com.iformall.utils.PriceUtil; -import com.iformall.utils.WordUtil; +import com.iformall.service.invest.event.InvestEvent; +import com.iformall.utils.*; import org.apache.commons.collections.map.HashedMap; +import org.apache.commons.compress.utils.Lists; import org.apache.commons.lang3.StringUtils; import org.apache.shiro.SecurityUtils; import org.flowable.engine.RuntimeService; @@ -428,6 +428,7 @@ public class WxRentContractServiceImpl implements WxRentContractService { record.setCreatetime(date); record.setUpdatetime(date); + List shopList = Lists.newArrayList(); if (record.getRentShopType().equals(EnumRentShopType.SHOP.getCode())) { String rentInfo = record.getRentInfo(); JSONArray rentInfoArray = JSONArray.parseArray(rentInfo); @@ -466,6 +467,7 @@ public class WxRentContractServiceImpl implements WxRentContractService { return new ResultData(ErrorCode.SHOP_IS_RENT.getCode(), "店铺 " + wxShop.getShopNumber() + " 已出租"); } } + shopList.add(shopId); } } else { WxRentContract wxRentContract = new WxRentContract(); @@ -478,6 +480,7 @@ public class WxRentContractServiceImpl implements WxRentContractService { if (count > 0) { return new ResultData(ErrorCode.RENT_CONTRACT_WITH_SHOP_IS_FOUND); } + shopList.add(record.getShopId()); } try { @@ -527,9 +530,35 @@ public class WxRentContractServiceImpl implements WxRentContractService { List resultList = buildRent(new WxMerchant(), null, record, EnumIsPreview.YES.getCode(),true); record.setPreviewBillRentList(resultList); } + + EventUtil.publistRentEvent(this, record); return new ResultData(Result.SUCCESS, "保存租赁合同信息成功", record); } + @Override + public List getShopIds(WxRentContract record) { + List shopList = Lists.newArrayList(); + if (record.getRentShopType().equals(EnumRentShopType.SHOP.getCode())) { + String rentInfo = record.getRentInfo(); + JSONArray rentInfoArray = JSONArray.parseArray(rentInfo); + int size = rentInfoArray.size(); + //查询rent_info 包括 shopId + for (int i = 0; i < size; i++) { + JSONObject rentInfoObject = rentInfoArray.getJSONObject(i); + WxRentContract wxRentContract = new WxRentContract(); + wxRentContract.setTenantId(record.getTenantId()); + Long shopId = rentInfoObject.getLong("shopId"); + shopList.add(shopId); + } + } else { + WxRentContract wxRentContract = new WxRentContract(); + wxRentContract.setTenantId(record.getTenantId()); + wxRentContract.setShopId(record.getShopId()); + shopList.add(record.getShopId()); + } + return shopList; + } + @Transactional(rollbackFor = {Exception.class}) @Override public ResultData update(WxRentContract record, Long userId,String userName,int from,Date oldRentStartDate) { @@ -1685,6 +1714,7 @@ public class WxRentContractServiceImpl implements WxRentContractService { wxRentContract.setStatus(EnumRentContractStatus.SIGNED_RENT_UNPAID.getCode()); } wxRentContractMapper.updateById(wxRentContract); + EventUtil.publistRentEvent(this, wxRentContract); //建立账单 if (record != null && record.getMerchantId() != null && diff --git a/mallinkService/src/main/java/com/iformall/service/invest/event/InvestEvent.java b/mallinkService/src/main/java/com/iformall/service/invest/event/InvestEvent.java new file mode 100644 index 000000000..34731363f --- /dev/null +++ b/mallinkService/src/main/java/com/iformall/service/invest/event/InvestEvent.java @@ -0,0 +1,14 @@ +package com.iformall.service.invest.event; + +import org.springframework.context.ApplicationEvent; + +public class InvestEvent extends ApplicationEvent { + /** + * Create a new ApplicationEvent. + * + * @param source the object on which the event initially occurred (never {@code null}) + */ + public InvestEvent(Object source) { + super(source); + } +} diff --git a/mallinkService/src/main/java/com/iformall/service/invest/event/InvestListener.java b/mallinkService/src/main/java/com/iformall/service/invest/event/InvestListener.java new file mode 100644 index 000000000..2e46b2265 --- /dev/null +++ b/mallinkService/src/main/java/com/iformall/service/invest/event/InvestListener.java @@ -0,0 +1,56 @@ +package com.iformall.service.invest.event; + +import com.alibaba.fastjson.JSON; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.iformall.domain.po.InvestTaskEntity; +import com.iformall.domain.vo.RentEventInfo; +import com.iformall.enums.EnumInvestType; +import com.iformall.enums.EnumRentContractStatus; +import com.iformall.enums.EnumTaskStatus; +import com.iformall.service.invest.InvestTaskService; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.collections.CollectionUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationListener; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.Objects; + +@Slf4j +@Component +public class InvestListener implements ApplicationListener { + + @Autowired + private InvestTaskService taskService; + + @Override + public void onApplicationEvent(InvestEvent event) { + RentEventInfo rentEventInfo = (RentEventInfo) event.getSource(); + log.debug("receive InvestEvent , info : {}", JSON.toJSONString(rentEventInfo)); + + LambdaQueryWrapper query = new LambdaQueryWrapper<>(); + query.in(InvestTaskEntity::getTargetId, rentEventInfo.getTargetIds()); + query.eq(InvestTaskEntity::getTargetType, EnumInvestType.SHOP); + List tasks = taskService.list(query); + if (CollectionUtils.isEmpty(tasks)) { + log.warn("receive InvestEvent , tasks is empty : {}", tasks); + return; + } + + EnumTaskStatus status = EnumTaskStatus.CREATED; + EnumRentContractStatus rentContractStatus = rentEventInfo.getType(); + if (Objects.equals(EnumRentContractStatus.INTENTION, rentContractStatus)) { + status = EnumTaskStatus.INTENTION; + } else if (Objects.equals(EnumRentContractStatus.SIGNED_RENT_UNPAID, rentContractStatus)) { + status = EnumTaskStatus.FINISH; + } else if (Objects.equals(EnumRentContractStatus.RENT_PAID, rentContractStatus)) { + status = EnumTaskStatus.FINISH; + } + + for (InvestTaskEntity task : tasks) { + task.setStatus(status); + } + taskService.updateBatchById(tasks); + } +} diff --git a/mallinkService/src/main/java/com/iformall/utils/EventUtil.java b/mallinkService/src/main/java/com/iformall/utils/EventUtil.java new file mode 100644 index 000000000..5b703f9ff --- /dev/null +++ b/mallinkService/src/main/java/com/iformall/utils/EventUtil.java @@ -0,0 +1,24 @@ +package com.iformall.utils; + +import com.iformall.domain.po.WxRentContract; +import com.iformall.domain.vo.RentEventInfo; +import com.iformall.enums.EnumRentContractStatus; +import com.iformall.service.WxRentContractService; +import com.iformall.service.invest.event.InvestEvent; +import lombok.extern.slf4j.Slf4j; + + +@Slf4j +public class EventUtil { + + public static void publistRentEvent(WxRentContractService rentContractService, WxRentContract rentContract) { + try { + RentEventInfo rentEventInfo = RentEventInfo + .of(rentContract.getId(), rentContractService.getShopIds(rentContract), EnumRentContractStatus.getEnum(rentContract.getStatus())); + InvestEvent event = new InvestEvent(rentEventInfo); + SpringContextUtils.pulish(event); + } catch (Exception e) { + log.error("publistRentEvent error", e); + } + } +} diff --git a/mallinkService/src/main/java/com/iformall/utils/SpringContextUtils.java b/mallinkService/src/main/java/com/iformall/utils/SpringContextUtils.java index e5f566f90..c56a3128e 100644 --- a/mallinkService/src/main/java/com/iformall/utils/SpringContextUtils.java +++ b/mallinkService/src/main/java/com/iformall/utils/SpringContextUtils.java @@ -3,6 +3,7 @@ package com.iformall.utils; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; +import org.springframework.context.ApplicationEvent; import org.springframework.stereotype.Component; /** @@ -46,4 +47,8 @@ public class SpringContextUtils implements ApplicationContextAware { return applicationContext.getBean(clazz); } + public static void pulish(ApplicationEvent event) { + applicationContext.publishEvent(event); + } + } \ No newline at end of file