| | |
| | | import com.java110.po.chargeMachinePort.ChargeMachinePortPo; |
| | | import com.java110.po.couponPropertyUser.CouponPropertyUserPo; |
| | | import com.java110.utils.exception.CmdException; |
| | | import com.java110.utils.lock.DistributedLock; |
| | | import com.java110.utils.util.Assert; |
| | | import com.java110.utils.util.DateUtil; |
| | | import com.java110.utils.util.StringUtil; |
| | |
| | | } |
| | | resultVo.setData(orderId); |
| | | |
| | | double couponDurationHours = 0.0; |
| | | //扣款 |
| | | if (!reqJson.containsKey("couponIds") || StringUtil.isEmpty(reqJson.getString("couponIds"))) { |
| | | // todo 3.0 账户扣款 |
| | | withholdAccount(reqJson, chargeMachineDtos, orderId); |
| | | context.setResponseEntity(ResultVo.createResponseEntity(resultVo)); |
| | | return; |
| | | if (reqJson.containsKey("couponIds") && !StringUtil.isEmpty(reqJson.getString("couponIds"))) { |
| | | //todo 优惠券抵扣 |
| | | couponDurationHours = withholdCoupon(reqJson, chargeMachineDtos, orderId); |
| | | } |
| | | |
| | | //todo 优惠券抵扣 |
| | | withholdCoupon(reqJson, chargeMachineDtos, orderId); |
| | | if(durationHours - couponDurationHours >0) { |
| | | // todo 3.0 账户扣款 |
| | | withholdAccount(reqJson, chargeMachineDtos, orderId,durationHours,couponDurationHours); |
| | | } |
| | | |
| | | context.setResponseEntity(ResultVo.createResponseEntity(resultVo)); |
| | | } |
| | |
| | | * @param chargeMachineDtos |
| | | * @param orderId |
| | | */ |
| | | private void withholdCoupon(JSONObject reqJson, List<ChargeMachineDto> chargeMachineDtos, String orderId) { |
| | | private double withholdCoupon(JSONObject reqJson, List<ChargeMachineDto> chargeMachineDtos, String orderId) { |
| | | int flag; |
| | | double hours = 0; |
| | | String couponNames = ""; |
| | | for (String couponId : reqJson.getString("couponIds").split(",")) { |
| | | String requestId = DistributedLock.getLockUUID(); |
| | | String key = this.getClass().getSimpleName() + reqJson.getString("couponId"); |
| | | try { |
| | | CouponPropertyUserDto couponPropertyUserDto = new CouponPropertyUserDto(); |
| | | couponPropertyUserDto.setCouponId(couponId); |
| | | couponPropertyUserDto.setToType(CouponPropertyUserDto.TO_TYPE_CHARGE); |
| | | couponPropertyUserDto.setState(CouponPropertyUserDto.STATE_WAIT); |
| | | |
| | | CouponPropertyUserDto couponPropertyUserDto = new CouponPropertyUserDto(); |
| | | couponPropertyUserDto.setCouponId(couponId); |
| | | couponPropertyUserDto.setToType(CouponPropertyUserDto.TO_TYPE_CHARGE); |
| | | couponPropertyUserDto.setState(CouponPropertyUserDto.STATE_WAIT); |
| | | List<CouponPropertyUserDto> couponPropertyUserDtos = couponPropertyUserV1InnerServiceSMOImpl.queryCouponPropertyUsers(couponPropertyUserDto); |
| | | int stock = Integer.parseInt(couponPropertyUserDtos.get(0).getStock()); |
| | | CouponPropertyUserPo couponPropertyUserPo = new CouponPropertyUserPo(); |
| | | couponPropertyUserPo.setCouponId(couponPropertyUserDtos.get(0).getCouponId()); |
| | | couponPropertyUserPo.setCommunityId(couponPropertyUserDtos.get(0).getCommunityId()); |
| | | couponPropertyUserPo.setStock((stock - 1) + ""); |
| | | if (stock == 1) { |
| | | couponPropertyUserPo.setState(CouponPropertyUserDto.STATE_FINISH); |
| | | } |
| | | flag = couponPropertyUserV1InnerServiceSMOImpl.updateCouponPropertyUser(couponPropertyUserPo); |
| | | if (flag < 1) { |
| | | throw new CmdException("核销失败"); |
| | | } |
| | | |
| | | List<CouponPropertyUserDto> couponPropertyUserDtos = couponPropertyUserV1InnerServiceSMOImpl.queryCouponPropertyUsers(couponPropertyUserDto); |
| | | couponNames += ("优惠券名称:" + couponPropertyUserDtos.get(0).getCouponName() + ",优惠券编号:" + couponId + ";"); |
| | | |
| | | CouponPropertyUserPo couponPropertyUserPo = new CouponPropertyUserPo(); |
| | | couponPropertyUserPo.setCouponId(couponPropertyUserDtos.get(0).getCouponId()); |
| | | couponPropertyUserPo.setCommunityId(couponPropertyUserDtos.get(0).getCommunityId()); |
| | | couponPropertyUserPo.setState(CouponPropertyUserDto.STATE_FINISH); |
| | | flag = couponPropertyUserV1InnerServiceSMOImpl.updateCouponPropertyUser(couponPropertyUserPo); |
| | | if (flag < 1) { |
| | | throw new CmdException("核销失败"); |
| | | CouponPropertyPoolConfigDto couponPropertyPoolConfigDto = new CouponPropertyPoolConfigDto(); |
| | | couponPropertyPoolConfigDto.setCouponId(couponPropertyUserDtos.get(0).getCppId()); |
| | | couponPropertyPoolConfigDto.setColumnKey("hours"); |
| | | List<CouponPropertyPoolConfigDto> couponPropertyPoolConfigDtos = couponPropertyPoolConfigV1InnerServiceSMOImpl.queryCouponPropertyPoolConfigs(couponPropertyPoolConfigDto); |
| | | |
| | | Assert.listOnlyOne(couponPropertyPoolConfigDtos, "未包含优惠券配置信息"); |
| | | |
| | | double value = Double.parseDouble(couponPropertyPoolConfigDtos.get(0).getColumnValue()); |
| | | hours += value; |
| | | } finally { |
| | | DistributedLock.releaseDistributedLock(requestId, key); |
| | | } |
| | | |
| | | couponNames += ("优惠券名称:" + couponPropertyUserDtos.get(0).getCouponName() + ",优惠券编号:" + couponId + ";"); |
| | | |
| | | CouponPropertyPoolConfigDto couponPropertyPoolConfigDto = new CouponPropertyPoolConfigDto(); |
| | | couponPropertyPoolConfigDto.setCouponId(couponPropertyUserDtos.get(0).getCppId()); |
| | | couponPropertyPoolConfigDto.setColumnKey("hours"); |
| | | List<CouponPropertyPoolConfigDto> couponPropertyPoolConfigDtos = couponPropertyPoolConfigV1InnerServiceSMOImpl.queryCouponPropertyPoolConfigs(couponPropertyPoolConfigDto); |
| | | |
| | | Assert.listOnlyOne(couponPropertyPoolConfigDtos, "未包含优惠券配置信息"); |
| | | |
| | | double value = Double.parseDouble(couponPropertyPoolConfigDtos.get(0).getColumnValue()); |
| | | hours += value; |
| | | } |
| | | |
| | | hours = Math.ceil(hours); |
| | |
| | | chargeMachineOrderAcctPo.setEnergy("0"); |
| | | |
| | | chargeMachineOrderAcctV1InnerServiceSMOImpl.saveChargeMachineOrderAcct(chargeMachineOrderAcctPo); |
| | | |
| | | return hours; |
| | | } |
| | | |
| | | /** |
| | |
| | | * @param chargeMachineDtos |
| | | * @param orderId |
| | | */ |
| | | private void withholdAccount(JSONObject reqJson, List<ChargeMachineDto> chargeMachineDtos, String orderId) { |
| | | private void withholdAccount(JSONObject reqJson, List<ChargeMachineDto> chargeMachineDtos, String orderId ,double durationHours,double couponDurationHours) { |
| | | AccountDto accountDto = new AccountDto(); |
| | | accountDto.setAcctId(reqJson.getString("acctId")); |
| | | List<AccountDto> accountDtos = accountInnerServiceSMOImpl.queryAccounts(accountDto); |
| | | |
| | | double amount = 0.0; |
| | | BigDecimal durationPrice = new BigDecimal(Double.parseDouble(chargeMachineDtos.get(0).getDurationPrice())); |
| | | if(couponDurationHours >0) { |
| | | durationPrice = durationPrice.multiply(new BigDecimal(durationHours - couponDurationHours)).setScale(2,BigDecimal.ROUND_HALF_UP); |
| | | amount = durationPrice.doubleValue(); |
| | | }else{ |
| | | durationPrice = durationPrice.multiply(new BigDecimal(durationHours)).setScale(2,BigDecimal.ROUND_HALF_UP); |
| | | amount = durationPrice.doubleValue(); |
| | | } |
| | | |
| | | AccountDetailPo accountDetailPo = new AccountDetailPo(); |
| | | accountDetailPo.setAcctId(accountDtos.get(0).getAcctId()); |
| | | accountDetailPo.setObjId(accountDtos.get(0).getObjId()); |
| | | accountDetailPo.setObjType(accountDtos.get(0).getObjType()); |
| | | accountDetailPo.setAmount(chargeMachineDtos.get(0).getDurationPrice()); |
| | | accountDetailPo.setAmount(amount+""); |
| | | accountDetailPo.setDetailId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_detailId)); |
| | | accountInnerServiceSMOImpl.withholdAccount(accountDetailPo); |
| | | |
| | | ChargeMachineOrderAcctPo chargeMachineOrderAcctPo = new ChargeMachineOrderAcctPo(); |
| | | chargeMachineOrderAcctPo.setAcctDetailId(accountDetailPo.getDetailId()); |
| | | chargeMachineOrderAcctPo.setAmount(chargeMachineDtos.get(0).getDurationPrice()); |
| | | chargeMachineOrderAcctPo.setAmount(amount+""); |
| | | |
| | | chargeMachineOrderAcctPo.setCmoaId(GenerateCodeFactory.getGeneratorId("11")); |
| | | chargeMachineOrderAcctPo.setOrderId(orderId); |
| | | chargeMachineOrderAcctPo.setAcctId(accountDtos.get(0).getAcctId()); |
| | | chargeMachineOrderAcctPo.setStartTime(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_A)); |
| | | chargeMachineOrderAcctPo.setEndTime(DateUtil.getAddHoursStringA(DateUtil.getCurrentDate(), 1)); |
| | | chargeMachineOrderAcctPo.setRemark("一小时定时扣款"); |
| | | if(couponDurationHours >0) { |
| | | chargeMachineOrderAcctPo.setStartTime(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_A)); |
| | | }else{ |
| | | chargeMachineOrderAcctPo.setStartTime(DateUtil.getAddHoursStringA(DateUtil.getCurrentDate(), new Double(Math.ceil(couponDurationHours)).intValue())); |
| | | } |
| | | chargeMachineOrderAcctPo.setEndTime(DateUtil.getAddHoursStringA(DateUtil.getCurrentDate(), new Double(Math.ceil(durationHours)).intValue())); |
| | | chargeMachineOrderAcctPo.setRemark("账户扣款"); |
| | | chargeMachineOrderAcctPo.setCommunityId(chargeMachineDtos.get(0).getCommunityId()); |
| | | chargeMachineOrderAcctPo.setEnergy("0"); |
| | | |