| | |
| | | package com.dsh.activity.service.impl; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import com.alipay.api.AlipayApiException; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.dsh.activity.entity.THuiminAgreement; |
| | | import com.dsh.activity.entity.HuiminPayQuery; |
| | | import com.dsh.activity.entity.THuiminRecord; |
| | | import com.dsh.activity.entity.TPayHuimin; |
| | | import com.dsh.activity.mapper.HuiminAgreementMapper; |
| | | import com.dsh.activity.entity.TStudent; |
| | | import com.dsh.activity.feignclient.account.AppUserClient; |
| | | import com.dsh.activity.feignclient.account.StudentClient; |
| | | import com.dsh.activity.feignclient.account.model.AppUser; |
| | | import com.dsh.activity.mapper.PayHuiminMapper; |
| | | import com.dsh.activity.service.HuiminAgreementService; |
| | | import com.dsh.activity.model.response.SalesDetailVO; |
| | | import com.dsh.activity.service.HuiminRecordService; |
| | | import com.dsh.activity.service.PayHuiminService; |
| | | import com.dsh.activity.util.DateUtil; |
| | | import com.dsh.activity.util.PayMoneyUtil; |
| | | import com.dsh.activity.util.ResultUtil; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.math.BigDecimal; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Objects; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * @author zhibing.pu |
| | |
| | | */ |
| | | @Service |
| | | public class PayHuiminServiceImpl extends ServiceImpl<PayHuiminMapper, TPayHuimin> implements PayHuiminService { |
| | | @Autowired |
| | | private AppUserClient appUserClient; |
| | | @Autowired |
| | | private StudentClient studentClient; |
| | | @Autowired |
| | | private HuiminRecordService huiminRecordService; |
| | | @Resource |
| | | private PayMoneyUtil payMoneyUtil; |
| | | @Override |
| | | public Page<SalesDetailVO> selectPage(HuiminPayQuery query) { |
| | | Page<SalesDetailVO> salesDetailVOPage = baseMapper.selectSalesDetailPage(new Page<>(query.getCurrent(), query.getSize()), query); |
| | | List<SalesDetailVO> records = salesDetailVOPage.getRecords(); |
| | | for (SalesDetailVO record : records) { |
| | | List<THuiminRecord> list = huiminRecordService.list(new LambdaQueryWrapper<THuiminRecord>() |
| | | .eq(THuiminRecord::getPayId, record.getId())); |
| | | record.setUseTimes(list.size()); |
| | | } |
| | | if (CollUtil.isNotEmpty(records)){ |
| | | List<Integer> appUserIdList = records.stream().map(SalesDetailVO::getAppUserId).collect(Collectors.toList()); |
| | | List<AppUser> appUserList = appUserClient.queryAppUserBatch(appUserIdList); |
| | | Map<Integer, AppUser> appUserMap = appUserList.stream().collect(Collectors.toMap(AppUser::getId, appUser -> appUser)); |
| | | records.forEach(item->{ |
| | | AppUser appUser = appUserMap.get(item.getAppUserId()); |
| | | if (Objects.nonNull(appUser)){ |
| | | item.setUserName(appUser.getName()); |
| | | item.setPhone(appUser.getPhone()); |
| | | } |
| | | List<TStudent> studentList = studentClient.getStudentByIds(item.getStudentId()); |
| | | if (CollUtil.isNotEmpty(studentList)){ |
| | | item.setStudentName(studentList.stream().map(TStudent::getName).collect(Collectors.joining(","))); |
| | | } |
| | | }); |
| | | } |
| | | return salesDetailVOPage; |
| | | } |
| | | |
| | | @Override |
| | | public Map<String, BigDecimal> getStaticsData(HuiminPayQuery query) { |
| | | return baseMapper.getStaticsData(query); |
| | | } |
| | | |
| | | @Override |
| | | public ResultUtil<?> refund(Integer id) throws AlipayApiException { |
| | | TPayHuimin payHuimin = this.getById(id); |
| | | if (Objects.nonNull(payHuimin)) { |
| | | Integer count = huiminRecordService.lambdaQuery() |
| | | .eq(THuiminRecord::getPayId, payHuimin.getId()) |
| | | .count(); |
| | | if (count > 0 || new Date().after(DateUtil.addDay(payHuimin.getPaymentTime(),7))) { |
| | | //超过七日或者七日内有使用记录的退款:更改状态,实际退款线下操作。 |
| | | payHuimin.setRefundStatus(3); |
| | | } else { |
| | | if (payHuimin.getPaymentType() == 1) { |
| | | Map<String, String> map = payMoneyUtil.wxRefund(payHuimin.getOrderNumber(), payHuimin.getCode(), |
| | | payHuimin.getSalesMoney().toString(), payHuimin.getSalesMoney().toString(), "/base/huimin/callBack/wxRefundHuiminCallback"); |
| | | if (!"SUCCESS".equals(map.get("return_code"))) { |
| | | System.err.println("-------------微信退款失败---------"); |
| | | System.err.println(map.get("return_msg")); |
| | | return ResultUtil.error("微信退款失败"); |
| | | } |
| | | } else { |
| | | Map<String, String> map = payMoneyUtil.aliRefund(payHuimin.getOrderNumber(), payHuimin.getSalesMoney().toString()); |
| | | String return_code = map.get("code"); |
| | | if (!"10000".equals(return_code)) { |
| | | return ResultUtil.error(map.get("msg")); |
| | | } |
| | | String refund_id = map.get("trade_no"); |
| | | payHuimin.setRefundNumber(refund_id); |
| | | } |
| | | payHuimin.setRefundStatus(2); |
| | | } |
| | | payHuimin.setStatus(3); |
| | | payHuimin.setRefundTime(new Date()); |
| | | this.updateById(payHuimin); |
| | | } |
| | | return ResultUtil.success(); |
| | | } |
| | | |
| | | /** |
| | | * 导出数据 |
| | | * @param query |
| | | * @return |
| | | */ |
| | | @Override |
| | | public List<SalesDetailVO> exportData(HuiminPayQuery query) { |
| | | List<SalesDetailVO> records = baseMapper.exportData(query); |
| | | if (CollUtil.isNotEmpty(records)){ |
| | | List<Integer> appUserIdList = records.stream().map(SalesDetailVO::getAppUserId).collect(Collectors.toList()); |
| | | List<AppUser> appUserList = appUserClient.queryAppUserBatch(appUserIdList); |
| | | Map<Integer, AppUser> appUserMap = appUserList.stream().collect(Collectors.toMap(AppUser::getId, appUser -> appUser)); |
| | | records.forEach(item->{ |
| | | AppUser appUser = appUserMap.get(item.getAppUserId()); |
| | | if (Objects.nonNull(appUser)){ |
| | | item.setUserName(appUser.getName()); |
| | | item.setPhone(appUser.getPhone()); |
| | | } |
| | | List<TStudent> studentList = studentClient.getStudentByIds(item.getStudentId()); |
| | | if (CollUtil.isNotEmpty(studentList)){ |
| | | item.setStudentName(studentList.stream().map(TStudent::getName).collect(Collectors.joining(","))); |
| | | } |
| | | }); |
| | | } |
| | | return records; |
| | | } |
| | | } |