|
| 1 | +package com.java110.user.listener; |
| 2 | + |
| 3 | +import com.alibaba.fastjson.JSONArray; |
| 4 | +import com.alibaba.fastjson.JSONObject; |
| 5 | +import com.java110.common.constant.ResponseConstant; |
| 6 | +import com.java110.common.constant.ServiceCodeConstant; |
| 7 | +import com.java110.common.constant.StatusConstant; |
| 8 | +import com.java110.common.exception.ListenerExecuteException; |
| 9 | +import com.java110.common.log.LoggerEngine; |
| 10 | +import com.java110.common.util.Assert; |
| 11 | +import com.java110.core.context.DataFlowContext; |
| 12 | +import com.java110.core.factory.DataTransactionFactory; |
| 13 | +import com.java110.core.factory.GenerateCodeFactory; |
| 14 | +import com.java110.entity.center.Business; |
| 15 | +import com.java110.event.service.BusinessServiceDataFlowEvent; |
| 16 | +import com.java110.event.service.BusinessServiceDataFlowListener; |
| 17 | +import com.java110.user.dao.IUserServiceDao; |
| 18 | +import org.slf4j.Logger; |
| 19 | +import org.slf4j.LoggerFactory; |
| 20 | +import org.springframework.beans.factory.annotation.Autowired; |
| 21 | +import org.springframework.stereotype.Service; |
| 22 | +import org.springframework.transaction.annotation.Transactional; |
| 23 | + |
| 24 | +import java.util.HashMap; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Map; |
| 27 | + |
| 28 | +/** |
| 29 | + * 保存 用户信息 侦听 |
| 30 | + * Created by wuxw on 2018/5/18. |
| 31 | + */ |
| 32 | +@Service("saveUserAddress") |
| 33 | +@Transactional |
| 34 | +public class SaveUserAddressListener implements BusinessServiceDataFlowListener{ |
| 35 | + |
| 36 | + private final static Logger logger = LoggerFactory.getLogger(SaveUserAddressListener.class); |
| 37 | + |
| 38 | + @Autowired |
| 39 | + IUserServiceDao userServiceDaoImpl; |
| 40 | + |
| 41 | + @Override |
| 42 | + public int getOrder() { |
| 43 | + return 1; |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public String getServiceCode() { |
| 48 | + return ServiceCodeConstant.SERVICE_CODE_SAVE_USER_ADDRESS; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public void soService(BusinessServiceDataFlowEvent event) { |
| 53 | + //这里处理业务逻辑数据 |
| 54 | + DataFlowContext dataFlowContext = event.getDataFlowContext(); |
| 55 | + doSaveUserAddress(dataFlowContext); |
| 56 | + } |
| 57 | + |
| 58 | + private void doSaveUserAddress(DataFlowContext dataFlowContext){ |
| 59 | + String businessType = dataFlowContext.getOrder().getBusinessType(); |
| 60 | + Business business = dataFlowContext.getCurrentBusiness(); |
| 61 | + //Assert.hasLength(business.getbId(),"bId 不能为空"); |
| 62 | + // Instance 过程 |
| 63 | + if(StatusConstant.REQUEST_BUSINESS_TYPE_INSTANCE.equals(businessType)){ |
| 64 | + //doComplateUserInfo(business); |
| 65 | + doSaveInstanceUserAddress(dataFlowContext,business); |
| 66 | + }else if(StatusConstant.REQUEST_BUSINESS_TYPE_BUSINESS.equals(businessType)){ // Business过程 |
| 67 | + doSaveBusinessUserAddress(dataFlowContext,business); |
| 68 | + }else if(StatusConstant.REQUEST_BUSINESS_TYPE_DELETE.equals(businessType)){ //撤单过程 |
| 69 | + doDeleteInstanceUserAddress(dataFlowContext,business); |
| 70 | + } |
| 71 | + |
| 72 | + dataFlowContext.setResJson(DataTransactionFactory.createBusinessResponseJson(dataFlowContext,ResponseConstant.RESULT_CODE_SUCCESS,"成功", |
| 73 | + dataFlowContext.getParamOut())); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * 撤单 |
| 78 | + * @param business |
| 79 | + */ |
| 80 | + private void doDeleteInstanceUserAddress(DataFlowContext dataFlowContext, Business business) { |
| 81 | + |
| 82 | + String bId = business.getbId(); |
| 83 | + //Assert.hasLength(bId,"请求报文中没有包含 bId"); |
| 84 | + Map info = new HashMap(); |
| 85 | + info.put("bId",bId); |
| 86 | + Map userAddress = userServiceDaoImpl.queryBusinessUserAddress(info); |
| 87 | + if(userAddress != null && !userAddress.isEmpty()){ |
| 88 | + info.put("bId",bId); |
| 89 | + info.put("userId",userAddress.get("user_id").toString()); |
| 90 | + info.put("addressId",userAddress.get("address_id").toString()); |
| 91 | + info.put("statusCd",StatusConstant.STATUS_CD_INVALID); |
| 92 | + userServiceDaoImpl.updateUserAddressInstance(userAddress); |
| 93 | + dataFlowContext.addParamOut("userId",userAddress.get("user_id")); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * instance过程 |
| 99 | + * @param business |
| 100 | + */ |
| 101 | + private void doSaveInstanceUserAddress(DataFlowContext dataFlowContext, Business business) { |
| 102 | + |
| 103 | + JSONObject data = business.getDatas(); |
| 104 | + |
| 105 | + Map info = new HashMap(); |
| 106 | + info.put("bId",business.getbId()); |
| 107 | + info.put("operate",StatusConstant.OPERATE_ADD); |
| 108 | + Map businessUserAddress = userServiceDaoImpl.queryBusinessUserAddress(info); |
| 109 | + if( businessUserAddress != null && !businessUserAddress.isEmpty()) { |
| 110 | + userServiceDaoImpl.saveUserAddressInstance(businessUserAddress); |
| 111 | + dataFlowContext.addParamOut("userId",businessUserAddress.get("user_id")); |
| 112 | + return ; |
| 113 | + } |
| 114 | + |
| 115 | + throw new ListenerExecuteException(ResponseConstant.RESULT_CODE_ERROR,"当前数据未找到business 数据"+info); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * 处理用户地址信息 |
| 120 | + * @param business 业务信息 |
| 121 | + */ |
| 122 | + private void doSaveBusinessUserAddress(DataFlowContext dataFlowContext, Business business) { |
| 123 | + |
| 124 | + JSONObject data = business.getDatas(); |
| 125 | + |
| 126 | + Assert.notEmpty(data,"没有datas 节点,或没有子节点需要处理"); |
| 127 | + |
| 128 | + Assert.jsonObjectHaveKey(data,"businessUserAddress","datas 节点下没有包含 businessUser 节点"); |
| 129 | + |
| 130 | + JSONObject businessUser = data.getJSONObject("businessUserAddress"); |
| 131 | + |
| 132 | + Assert.jsonObjectHaveKey(businessUser,"userId","businessUser 节点下没有包含 userId 节点"); |
| 133 | + |
| 134 | + if(businessUser.getLong("userId") <= 0){ |
| 135 | + throw new ListenerExecuteException(ResponseConstant.RESULT_PARAM_ERROR,"用户地址(saveUserAddress)保存失败,userId 不正确"+businessUser.getInteger("userId")); |
| 136 | + } |
| 137 | + dataFlowContext.addParamOut("userId",businessUser.getString("userId")); |
| 138 | + businessUser.put("bId",business.getbId()); |
| 139 | + businessUser.put("operate", StatusConstant.OPERATE_ADD); |
| 140 | + //保存用户信息 |
| 141 | + userServiceDaoImpl.saveBusinessUserAddress(businessUser); |
| 142 | + |
| 143 | + } |
| 144 | + |
| 145 | + public IUserServiceDao getUserServiceDaoImpl() { |
| 146 | + return userServiceDaoImpl; |
| 147 | + } |
| 148 | + |
| 149 | + public void setUserServiceDaoImpl(IUserServiceDao userServiceDaoImpl) { |
| 150 | + this.userServiceDaoImpl = userServiceDaoImpl; |
| 151 | + } |
| 152 | +} |
0 commit comments