|
@@ -0,0 +1,329 @@
|
|
|
+package com.ym.mec.biz.service.impl;
|
|
|
+
|
|
|
+import com.ym.mec.auth.api.client.SysUserFeignService;
|
|
|
+import com.ym.mec.auth.api.entity.SysUser;
|
|
|
+import com.ym.mec.biz.dal.dao.CooperationOrganDao;
|
|
|
+import com.ym.mec.biz.dal.dao.ReplacementInstrumentDao;
|
|
|
+import com.ym.mec.biz.dal.dao.SysConfigDao;
|
|
|
+import com.ym.mec.biz.dal.dto.ReplacementInstrumentActivityStatDto;
|
|
|
+import com.ym.mec.biz.dal.dto.ReplacementInstrumentActivityStatHead;
|
|
|
+import com.ym.mec.biz.dal.dto.ReplacementPayDto;
|
|
|
+import com.ym.mec.biz.dal.entity.*;
|
|
|
+import com.ym.mec.biz.dal.enums.*;
|
|
|
+import com.ym.mec.biz.dal.page.ReplacementInstrumentActivityQueryInfo;
|
|
|
+import com.ym.mec.biz.dal.page.ReplacementInstrumentQueryInfo;
|
|
|
+import com.ym.mec.biz.service.*;
|
|
|
+import com.ym.mec.common.dal.BaseDAO;
|
|
|
+import com.ym.mec.common.exception.BizException;
|
|
|
+import com.ym.mec.common.page.PageInfo;
|
|
|
+import com.ym.mec.common.page.QueryInfo;
|
|
|
+import com.ym.mec.common.service.IdGeneratorService;
|
|
|
+import com.ym.mec.common.service.impl.BaseServiceImpl;
|
|
|
+import com.ym.mec.thirdparty.message.MessageSenderPluginContext;
|
|
|
+import com.ym.mec.util.collection.MapUtil;
|
|
|
+import com.ym.mec.util.date.DateUtil;
|
|
|
+
|
|
|
+import org.apache.poi.ss.formula.functions.T;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+
|
|
|
+import com.ym.mec.biz.dal.dao.ReplacementInstrumentActivityDao;
|
|
|
+
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class ReplacementInstrumentActivityServiceImpl extends BaseServiceImpl<Integer, ReplacementInstrumentActivity> implements ReplacementInstrumentActivityService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ReplacementInstrumentActivityDao replacementInstrumentActivityDao;
|
|
|
+ @Autowired
|
|
|
+ private SysUserFeignService sysUserFeignService;
|
|
|
+ @Autowired
|
|
|
+ private CooperationOrganDao cooperationOrganDao;
|
|
|
+ @Autowired
|
|
|
+ private ReplacementInstrumentDao replacementInstrumentDao;
|
|
|
+ @Autowired
|
|
|
+ private SysUserCashAccountService sysUserCashAccountService;
|
|
|
+ @Autowired
|
|
|
+ private IdGeneratorService idGeneratorService;
|
|
|
+ @Autowired
|
|
|
+ private StudentPaymentOrderService studentPaymentOrderService;
|
|
|
+ @Autowired
|
|
|
+ private StudentPaymentRouteOrderService studentPaymentRouteOrderService;
|
|
|
+ @Autowired
|
|
|
+ private SysConfigDao sysConfigDao;
|
|
|
+ @Autowired
|
|
|
+ private PayService payService;
|
|
|
+ @Autowired
|
|
|
+ private ContractService contractService;
|
|
|
+ @Autowired
|
|
|
+ private SysUserCashAccountDetailService sysUserCashAccountDetailService;
|
|
|
+ @Autowired
|
|
|
+ private SysMessageService sysMessageService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private CooperationOrganService cooperationOrganService;
|
|
|
+
|
|
|
+ private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public BaseDAO<Integer, ReplacementInstrumentActivity> getDAO() {
|
|
|
+ return replacementInstrumentActivityDao;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public Object add(ReplacementInstrumentActivity replacementInstrumentActivity) {
|
|
|
+ SysUser sysUser = sysUserFeignService.queryUserInfo();
|
|
|
+ if (sysUser == null) {
|
|
|
+ throw new BizException("用户信息获取失败,请重新登陆");
|
|
|
+ }
|
|
|
+ replacementInstrumentActivity.setUserId(sysUser.getId());
|
|
|
+ //如果用户是43分部,那么修改用户信息
|
|
|
+ if(sysUser.getOrganId() == 43){
|
|
|
+ CooperationOrgan cooperationOrgan = cooperationOrganService.get(replacementInstrumentActivity.getCooperationOrganId());
|
|
|
+ if (cooperationOrgan != null) {
|
|
|
+ sysUser.setOrganId(cooperationOrgan.getOrganId());
|
|
|
+ sysUser.setUpdateTime(new Date());
|
|
|
+ sysUserFeignService.updateSysUser(sysUser);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //如果提交过调查问卷,那么覆盖之前的记录
|
|
|
+ ReplacementInstrumentActivity activity = replacementInstrumentActivityDao.findByUserId(replacementInstrumentActivity.getCooperationOrganId(), sysUser.getId());
|
|
|
+ if (activity != null) {
|
|
|
+ //覆盖之前的数据
|
|
|
+ replacementInstrumentActivity.setId(activity.getId());
|
|
|
+ replacementInstrumentActivityDao.update(replacementInstrumentActivity);
|
|
|
+ } else {
|
|
|
+ replacementInstrumentActivityDao.insert(replacementInstrumentActivity);
|
|
|
+ }
|
|
|
+ return replacementInstrumentActivity;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ReplacementInstrumentActivity findByUserId(Integer cooperationOrganId, Integer userId) {
|
|
|
+ return replacementInstrumentActivityDao.findByUserId(cooperationOrganId, userId);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public PageInfo<ReplacementInstrumentActivityStatDto> queryReplacementsStat(ReplacementInstrumentActivityQueryInfo queryInfo) {
|
|
|
+ PageInfo<ReplacementInstrumentActivityStatDto> pageInfo = new PageInfo<>(queryInfo.getPage(), queryInfo.getRows());
|
|
|
+ Map<String, Object> params = new HashMap<String, Object>();
|
|
|
+ MapUtil.populateMap(params, queryInfo);
|
|
|
+
|
|
|
+ if (Objects.isNull(queryInfo.getCooperationOrganId())) {
|
|
|
+ throw new BizException("请指定合作单位");
|
|
|
+ }
|
|
|
+
|
|
|
+ int count = replacementInstrumentActivityDao.countReplacements(params);
|
|
|
+
|
|
|
+ if (queryInfo.getPage() == 1) {
|
|
|
+ ReplacementInstrumentActivityStatHead head = replacementInstrumentActivityDao.countReplacementsInfo(queryInfo.getCooperationOrganId());
|
|
|
+ CooperationOrgan cooperationOrgan = cooperationOrganDao.get(queryInfo.getCooperationOrganId());
|
|
|
+ head.setCooperationOrganName(cooperationOrgan.getName());
|
|
|
+ pageInfo.setStatInfo(head);
|
|
|
+ }
|
|
|
+
|
|
|
+ pageInfo.setTotal(count);
|
|
|
+ params.put("offset", pageInfo.getOffset());
|
|
|
+ List<ReplacementInstrumentActivityStatDto> dataList = replacementInstrumentActivityDao.queryReplacements(params);
|
|
|
+ if (!CollectionUtils.isEmpty(dataList)) {
|
|
|
+
|
|
|
+ }
|
|
|
+ pageInfo.setRows(dataList);
|
|
|
+ return pageInfo;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map pay(ReplacementPayDto replacementPayDto) throws Exception {
|
|
|
+ ReplacementInstrumentActivity replacementInstrumentActivity = replacementInstrumentActivityDao.getLock(replacementPayDto.getId());
|
|
|
+ if (replacementInstrumentActivity == null) {
|
|
|
+ throw new BizException("乐器置换不存在,请核查");
|
|
|
+ }
|
|
|
+ if (replacementInstrumentActivity.getInstrumentsId() == null || replacementInstrumentActivity.getInstrumentsId() == 0) {
|
|
|
+ throw new BizException("没有置换的乐器,请核查");
|
|
|
+ }
|
|
|
+ Integer userId = replacementInstrumentActivity.getUserId();
|
|
|
+ ReplacementInstrument replacementInstrument = replacementInstrumentDao.get(replacementInstrumentActivity.getInstrumentsId());
|
|
|
+ BigDecimal orderAmount = replacementInstrument.getSalePrice();
|
|
|
+ BigDecimal amount = replacementPayDto.getAmount(); //扣除余额之前的金额
|
|
|
+ if (amount.compareTo(replacementInstrument.getSalePrice()) != 0) {
|
|
|
+ throw new BizException("商品价格不符");
|
|
|
+ }
|
|
|
+
|
|
|
+ String channelType = "";
|
|
|
+ Date nowDate = new Date();
|
|
|
+
|
|
|
+ BigDecimal balance = BigDecimal.ZERO;
|
|
|
+ if (replacementPayDto.getUseBalance() && amount.compareTo(BigDecimal.ZERO) > 0) {
|
|
|
+ SysUserCashAccount userCashAccount = sysUserCashAccountService.getLocked(userId);
|
|
|
+ if (userCashAccount == null) {
|
|
|
+ throw new BizException("用户账户找不到");
|
|
|
+ }
|
|
|
+ if (userCashAccount.getBalance() != null && userCashAccount.getBalance().compareTo(BigDecimal.ZERO) > 0) {
|
|
|
+ balance = amount.compareTo(userCashAccount.getBalance()) >= 0 ? userCashAccount.getBalance() : amount;
|
|
|
+ amount = amount.subtract(balance);
|
|
|
+ sysUserCashAccountService.updateBalance(userId, balance.negate(), PlatformCashAccountDetailTypeEnum.PAY_FEE, "乐保购买");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ CooperationOrgan cooperationOrgan = cooperationOrganDao.get(replacementInstrumentActivity.getCooperationOrganId());
|
|
|
+
|
|
|
+ String orderNo = idGeneratorService.generatorId("payment") + "";
|
|
|
+ StudentPaymentOrder studentPaymentOrder = new StudentPaymentOrder();
|
|
|
+
|
|
|
+ studentPaymentOrder.setPaymentChannel("BALANCE");
|
|
|
+ studentPaymentOrder.setUserId(userId);
|
|
|
+ studentPaymentOrder.setGroupType(GroupType.REPLACEMENT);
|
|
|
+ studentPaymentOrder.setOrderNo(orderNo);
|
|
|
+ studentPaymentOrder.setType(OrderTypeEnum.REPLACEMENT);
|
|
|
+ studentPaymentOrder.setExpectAmount(orderAmount);
|
|
|
+ studentPaymentOrder.setActualAmount(amount);
|
|
|
+ studentPaymentOrder.setBalancePaymentAmount(balance);
|
|
|
+ studentPaymentOrder.setStatus(DealStatusEnum.ING);
|
|
|
+ studentPaymentOrder.setMusicGroupId(replacementInstrumentActivity.getId().toString());
|
|
|
+ studentPaymentOrder.setRemitFee(BigDecimal.ZERO);
|
|
|
+ studentPaymentOrder.setCourseRemitFee(BigDecimal.ZERO);
|
|
|
+ studentPaymentOrder.setOrganId(cooperationOrgan.getOrganId());
|
|
|
+ studentPaymentOrder.setRoutingOrganId(cooperationOrgan.getOrganId());
|
|
|
+ studentPaymentOrder.setCreateTime(nowDate);
|
|
|
+ studentPaymentOrder.setUpdateTime(nowDate);
|
|
|
+ studentPaymentOrderService.insert(studentPaymentOrder);
|
|
|
+ //修改支付状态
|
|
|
+ replacementInstrumentActivity.setPayStatus(1);
|
|
|
+ replacementInstrumentActivity.setUpdateTime(nowDate);
|
|
|
+ replacementInstrumentActivityDao.update(replacementInstrumentActivity);
|
|
|
+
|
|
|
+ studentPaymentOrder.setVersion(0);
|
|
|
+ if (amount.compareTo(BigDecimal.ZERO) == 0) {
|
|
|
+ studentPaymentRouteOrderService.addRouteOrder(orderNo, cooperationOrgan.getOrganId(), balance);
|
|
|
+ Map<String, String> notifyMap = new HashMap<>();
|
|
|
+ notifyMap.put("tradeState", "1");
|
|
|
+ notifyMap.put("merOrderNo", studentPaymentOrder.getOrderNo());
|
|
|
+ notifyMap.put("channelType", channelType);
|
|
|
+ notifyMap.put("orderNo", "");
|
|
|
+ studentPaymentOrderService.updateOrder(notifyMap);
|
|
|
+ return notifyMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ String orderSubject = "乐器置换";
|
|
|
+ String receiver = "replacement";
|
|
|
+
|
|
|
+ String baseApiUrl = sysConfigDao.findConfigValue("base_api_url");
|
|
|
+
|
|
|
+ Map payMap = payService.getPayMap(
|
|
|
+ amount,
|
|
|
+ balance,
|
|
|
+ orderNo,
|
|
|
+ baseApiUrl + "/api-student/studentOrder/notify",
|
|
|
+ baseApiUrl + "/api-student/studentOrder/paymentResult?orderNo=" + orderNo,
|
|
|
+ orderSubject,
|
|
|
+ orderSubject,
|
|
|
+ cooperationOrgan.getOrganId(),
|
|
|
+ receiver
|
|
|
+ );
|
|
|
+
|
|
|
+ studentPaymentOrder.setOrganId(cooperationOrgan.getOrganId());
|
|
|
+ studentPaymentOrder.setMerNos((String) payMap.get("routingMerNos"));
|
|
|
+ studentPaymentOrder.setPaymentChannel((String) payMap.get("type"));
|
|
|
+ studentPaymentOrder.setUpdateTime(nowDate);
|
|
|
+ studentPaymentOrderService.update(studentPaymentOrder);
|
|
|
+ return payMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public Boolean orderCallback(StudentPaymentOrder studentPaymentOrder) {
|
|
|
+ Date nowDate = new Date();
|
|
|
+ //更新订单信息
|
|
|
+ studentPaymentOrder.setUpdateTime(nowDate);
|
|
|
+ int updateCount = studentPaymentOrderService.update(studentPaymentOrder);
|
|
|
+ if (updateCount <= 0) {
|
|
|
+ throw new BizException("订单更新失败");
|
|
|
+ }
|
|
|
+ //更新维修单信息
|
|
|
+ ReplacementInstrumentActivity replacementInstrumentActivity = replacementInstrumentActivityDao.getLock(Integer.parseInt(studentPaymentOrder.getMusicGroupId()));
|
|
|
+ if (replacementInstrumentActivity == null) {
|
|
|
+ throw new BizException("置换信息不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ Integer userId = studentPaymentOrder.getUserId();
|
|
|
+
|
|
|
+ Map<Integer, String> map = new HashMap<>();
|
|
|
+ map.put(userId, userId.toString());
|
|
|
+
|
|
|
+ //支付成功
|
|
|
+ if (studentPaymentOrder.getStatus() == DealStatusEnum.SUCCESS) {
|
|
|
+ replacementInstrumentActivity.setPayStatus(2);
|
|
|
+ replacementInstrumentActivity.setUpdateTime(nowDate);
|
|
|
+ if (this.update(replacementInstrumentActivity) <= 0) {
|
|
|
+ throw new BizException("置换信息更新失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ //插入交易明细
|
|
|
+ BigDecimal amount = studentPaymentOrder.getActualAmount();
|
|
|
+ if (amount.compareTo(BigDecimal.ZERO) > 0) {
|
|
|
+ SysUserCashAccount cashAccount = sysUserCashAccountService.get(userId);
|
|
|
+ //充值
|
|
|
+ SysUserCashAccountDetail rechargeDetail = new SysUserCashAccountDetail();
|
|
|
+ rechargeDetail.setAmount(amount);
|
|
|
+ rechargeDetail.setBalance(cashAccount.getBalance().add(amount));
|
|
|
+ rechargeDetail.setComment("缴费前充值");
|
|
|
+ rechargeDetail.setCreateTime(nowDate);
|
|
|
+ rechargeDetail.setStatus(DealStatusEnum.SUCCESS);
|
|
|
+ rechargeDetail.setTransNo(studentPaymentOrder.getTransNo());
|
|
|
+ rechargeDetail.setType(PlatformCashAccountDetailTypeEnum.RECHARGE);
|
|
|
+ rechargeDetail.setUpdateTime(nowDate);
|
|
|
+ rechargeDetail.setUserId(userId);
|
|
|
+ rechargeDetail.setChannel(studentPaymentOrder.getPaymentChannel());
|
|
|
+ rechargeDetail.setComAmount(studentPaymentOrder.getComAmount());
|
|
|
+ rechargeDetail.setPerAmount(studentPaymentOrder.getPerAmount());
|
|
|
+ sysUserCashAccountDetailService.insert(rechargeDetail);
|
|
|
+
|
|
|
+ //缴费
|
|
|
+ SysUserCashAccountDetail paymentDetail = new SysUserCashAccountDetail();
|
|
|
+ paymentDetail.setAmount(amount.negate());
|
|
|
+ paymentDetail.setBalance(cashAccount.getBalance());
|
|
|
+ paymentDetail.setComment("乐器置换");
|
|
|
+ paymentDetail.setCreateTime(nowDate);
|
|
|
+ paymentDetail.setStatus(DealStatusEnum.SUCCESS);
|
|
|
+ paymentDetail.setTransNo(studentPaymentOrder.getTransNo());
|
|
|
+ paymentDetail.setType(PlatformCashAccountDetailTypeEnum.PAY_FEE);
|
|
|
+ paymentDetail.setUpdateTime(nowDate);
|
|
|
+ paymentDetail.setUserId(userId);
|
|
|
+ rechargeDetail.setChannel(studentPaymentOrder.getPaymentChannel());
|
|
|
+ sysUserCashAccountDetailService.insert(paymentDetail);
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ contractService.transferProduceContract(userId, null);
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("产品协议生成失败", e);
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (studentPaymentOrder.getStatus() == DealStatusEnum.CLOSE || studentPaymentOrder.getStatus() == DealStatusEnum.FAILED) {
|
|
|
+
|
|
|
+ replacementInstrumentActivity.setPayStatus(0);
|
|
|
+ replacementInstrumentActivity.setUpdateTime(nowDate);
|
|
|
+ if (this.update(replacementInstrumentActivity) <= 0) {
|
|
|
+ throw new BizException("置换信息更新失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (studentPaymentOrder.getBalancePaymentAmount() != null && studentPaymentOrder.getBalancePaymentAmount().compareTo(BigDecimal.ZERO) > 0) {
|
|
|
+ sysUserCashAccountService.updateBalance(studentPaymentOrder.getUserId(), studentPaymentOrder.getBalancePaymentAmount(), PlatformCashAccountDetailTypeEnum.REFUNDS, "乐保购买支付失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ sysMessageService.batchSendMessage(MessageSenderPluginContext.MessageSender.JIGUANG, MessageTypeEnum.SMS_SPORADIC_PAYMENT_FAILED, map, null, 0, null, "STUDENT",
|
|
|
+ studentPaymentOrder.getActualAmount(), "乐器置换");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+}
|