|  | @@ -6,22 +6,26 @@ import java.util.stream.Collectors;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  import com.alibaba.fastjson.JSON;
 | 
	
		
			
				|  |  |  import com.alibaba.fastjson.JSONObject;
 | 
	
		
			
				|  |  | +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 | 
	
		
			
				|  |  | +import com.ym.mec.biz.dal.dao.*;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.dal.entity.*;
 | 
	
		
			
				|  |  | +import com.ym.mec.biz.dal.enums.PaymentChannelEnum;
 | 
	
		
			
				|  |  | +import com.ym.mec.biz.dal.enums.TenantOrderRecordEnum;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.dal.page.CloudTeacherOrderQueryInfo;
 | 
	
		
			
				|  |  | -import com.ym.mec.biz.service.TenantConfigService;
 | 
	
		
			
				|  |  | +import com.ym.mec.biz.service.*;
 | 
	
		
			
				|  |  |  import com.ym.mec.common.page.PageInfo;
 | 
	
		
			
				|  |  | +import com.ym.mec.common.service.IdGeneratorService;
 | 
	
		
			
				|  |  | +import com.ym.mec.thirdparty.yqpay.Msg;
 | 
	
		
			
				|  |  |  import com.ym.mec.util.collection.MapUtil;
 | 
	
		
			
				|  |  | +import org.joda.time.LocalDateTime;
 | 
	
		
			
				|  |  | +import org.slf4j.Logger;
 | 
	
		
			
				|  |  | +import org.slf4j.LoggerFactory;
 | 
	
		
			
				|  |  |  import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  |  import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  |  import org.springframework.transaction.annotation.Transactional;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -import com.ym.mec.biz.dal.dao.CloudTeacherDao;
 | 
	
		
			
				|  |  | -import com.ym.mec.biz.dal.dao.CloudTeacherOrderDao;
 | 
	
		
			
				|  |  | -import com.ym.mec.biz.dal.dao.CourseScheduleDao;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.dal.dto.CloudTeacherOrderDto;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.dal.enums.PeriodEnum;
 | 
	
		
			
				|  |  | -import com.ym.mec.biz.service.CloudTeacherOrderService;
 | 
	
		
			
				|  |  | -import com.ym.mec.biz.service.StudentService;
 | 
	
		
			
				|  |  |  import com.ym.mec.common.dal.BaseDAO;
 | 
	
		
			
				|  |  |  import com.ym.mec.common.exception.BizException;
 | 
	
		
			
				|  |  |  import com.ym.mec.common.service.impl.BaseServiceImpl;
 | 
	
	
		
			
				|  | @@ -29,7 +33,7 @@ import com.ym.mec.util.date.DateUtil;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  @Service
 | 
	
		
			
				|  |  |  public class CloudTeacherOrderServiceImpl extends BaseServiceImpl<Long, CloudTeacherOrder> implements CloudTeacherOrderService {
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | +    private static final Logger log = LoggerFactory.getLogger(TenantInfoServiceImpl.class);
 | 
	
		
			
				|  |  |      @Autowired
 | 
	
		
			
				|  |  |      private CloudTeacherOrderDao cloudTeacherOrderDao;
 | 
	
		
			
				|  |  |      @Autowired
 | 
	
	
		
			
				|  | @@ -39,9 +43,16 @@ public class CloudTeacherOrderServiceImpl extends BaseServiceImpl<Long, CloudTea
 | 
	
		
			
				|  |  |      
 | 
	
		
			
				|  |  |      @Autowired
 | 
	
		
			
				|  |  |      private StudentService studentService;
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | +    @Autowired
 | 
	
		
			
				|  |  | +    private SysConfigDao sysConfigDao;
 | 
	
		
			
				|  |  |      @Autowired
 | 
	
		
			
				|  |  |      private TenantConfigService tenantConfigService;
 | 
	
		
			
				|  |  | +    @Autowired
 | 
	
		
			
				|  |  | +    private PayService payService;
 | 
	
		
			
				|  |  | +    @Autowired
 | 
	
		
			
				|  |  | +    private IdGeneratorService idGenerator;
 | 
	
		
			
				|  |  | +    @Autowired
 | 
	
		
			
				|  |  | +    private CloudTeacherOrderRecordService cloudTeacherOrderRecordService;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      @Override
 | 
	
		
			
				|  |  |      public BaseDAO<Long, CloudTeacherOrder> getDAO() {
 | 
	
	
		
			
				|  | @@ -50,8 +61,8 @@ public class CloudTeacherOrderServiceImpl extends BaseServiceImpl<Long, CloudTea
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      @Override
 | 
	
		
			
				|  |  |      public PageInfo<CloudTeacherStudent> queryInactive(CloudTeacherOrderQueryInfo queryInfo) {
 | 
	
		
			
				|  |  | -        PageInfo<CloudTeacherStudent> pageInfo = new PageInfo<CloudTeacherStudent>(queryInfo.getPage(), queryInfo.getRows());
 | 
	
		
			
				|  |  | -        Map<String, Object> params = new HashMap<String, Object>();
 | 
	
		
			
				|  |  | +        PageInfo<CloudTeacherStudent> pageInfo = new PageInfo<>(queryInfo.getPage(), queryInfo.getRows());
 | 
	
		
			
				|  |  | +        Map<String, Object> params = new HashMap<>();
 | 
	
		
			
				|  |  |          MapUtil.populateMap(params, queryInfo);
 | 
	
		
			
				|  |  |          List<CloudTeacherStudent> dataList = null;
 | 
	
		
			
				|  |  |          int count = cloudTeacherOrderDao.findInactiveCount(params);
 | 
	
	
		
			
				|  | @@ -61,39 +72,161 @@ public class CloudTeacherOrderServiceImpl extends BaseServiceImpl<Long, CloudTea
 | 
	
		
			
				|  |  |              dataList = cloudTeacherOrderDao.queryInactive(params);
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |          if (count == 0) {
 | 
	
		
			
				|  |  | -            dataList = new ArrayList<CloudTeacherStudent>();
 | 
	
		
			
				|  |  | +            dataList = new ArrayList<>();
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |          pageInfo.setRows(dataList);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          // List<TenantConfig> tenCfg = tenantConfigService.list(new Wrapper<TenantConfig>());
 | 
	
		
			
				|  |  | +        assert dataList != null;
 | 
	
		
			
				|  |  |          for (CloudTeacherStudent cst: dataList) {
 | 
	
		
			
				|  |  |              if (cst.getTime() == null) continue;
 | 
	
		
			
				|  |  |              TenantConfig tenantConfig = tenantConfigService.getById(cst.getTenantId());
 | 
	
		
			
				|  |  | +            if (tenantConfig == null) continue;
 | 
	
		
			
				|  |  |              JSONObject cfg = JSON.parseObject(tenantConfig.getConfig());
 | 
	
		
			
				|  |  |              JSONObject memberConfig = (JSONObject) cfg.get("member_config");
 | 
	
		
			
				|  |  |              if (cst.getType() == 1) {
 | 
	
		
			
				|  |  |                  double divide = memberConfig.getDouble("month_divide");
 | 
	
		
			
				|  |  | -                cst.setPrice(divide * cst.getTime());
 | 
	
		
			
				|  |  | +                cst.setAmount(divide * cst.getTime());
 | 
	
		
			
				|  |  |              } else if (cst.getType() == 2) { // 月
 | 
	
		
			
				|  |  |                  double divide = memberConfig.getDouble("month_divide");
 | 
	
		
			
				|  |  | -                cst.setPrice(divide * cst.getTime());
 | 
	
		
			
				|  |  | +                cst.setAmount(divide * cst.getTime());
 | 
	
		
			
				|  |  |              } else if (cst.getType() == 3) {
 | 
	
		
			
				|  |  |                  double divide = memberConfig.getDouble("quarter_divide");
 | 
	
		
			
				|  |  | -                cst.setPrice(divide * cst.getTime());
 | 
	
		
			
				|  |  | +                cst.setAmount(divide * cst.getTime());
 | 
	
		
			
				|  |  |              } else if (cst.getType() == 4) {
 | 
	
		
			
				|  |  |                  double divide = memberConfig.getDouble("half_year_divide");
 | 
	
		
			
				|  |  | -                cst.setPrice(divide * cst.getTime());
 | 
	
		
			
				|  |  | +                cst.setAmount(divide * cst.getTime());
 | 
	
		
			
				|  |  |              } else if (cst.getType() == 5) {
 | 
	
		
			
				|  |  |                  double divide = memberConfig.getDouble("year_divide");
 | 
	
		
			
				|  |  | -                cst.setPrice(divide * cst.getTime());
 | 
	
		
			
				|  |  | +                cst.setAmount(divide * cst.getTime());
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |          return pageInfo;
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      @Override
 | 
	
		
			
				|  |  | -    public Boolean pay(List<CloudTeacherStudent> orders) {
 | 
	
		
			
				|  |  | -        return true;
 | 
	
		
			
				|  |  | +    public Map<String, Object> pay(List<CloudTeacherStudent> cloudTeacherStudents) throws Exception {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        BigDecimal sumAmount = new BigDecimal(0);
 | 
	
		
			
				|  |  | +        Integer tenantId = null;
 | 
	
		
			
				|  |  | +        StringBuilder payStudentIds = new StringBuilder();
 | 
	
		
			
				|  |  | +        StringBuilder payStudentIdsZero = new StringBuilder();
 | 
	
		
			
				|  |  | +        for (CloudTeacherStudent cts: cloudTeacherStudents) {
 | 
	
		
			
				|  |  | +            if (cts.getAmount() == null) {
 | 
	
		
			
				|  |  | +                throw new Exception("金额错误在 " + cts.getStudentId().toString() );
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            if (tenantId == null) {
 | 
	
		
			
				|  |  | +                tenantId = cts.getTenantId();
 | 
	
		
			
				|  |  | +            } else {
 | 
	
		
			
				|  |  | +                if (!tenantId.equals(cts.getTenantId())) {
 | 
	
		
			
				|  |  | +                    throw new Exception("分部Id 错误 " + cts.getStudentId().toString() );
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            if (cts.getAmount() == 0) {
 | 
	
		
			
				|  |  | +                payStudentIdsZero.append(cts.getStudentId());
 | 
	
		
			
				|  |  | +                continue;
 | 
	
		
			
				|  |  | +            } else {
 | 
	
		
			
				|  |  | +                sumAmount.add(BigDecimal.valueOf(cts.getAmount()));
 | 
	
		
			
				|  |  | +                payStudentIds.append(cts.getStudentId().toString()).append(",");
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        // 金额为 0 的
 | 
	
		
			
				|  |  | +        if (payStudentIdsZero.length() > 0) {
 | 
	
		
			
				|  |  | +            this.updateCloudTeacherOrder(payStudentIdsZero.toString());
 | 
	
		
			
				|  |  | +            createOrderRecord(tenantId, new BigDecimal(0), "", TenantOrderRecordEnum.CLOUD_TEACHER, 1, payStudentIdsZero.toString());
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        return this.payExec(sumAmount, tenantId, payStudentIds.toString());
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 团练宝支付
 | 
	
		
			
				|  |  | +     * @param payAmount 金额
 | 
	
		
			
				|  |  | +     * @param tenantId 分部
 | 
	
		
			
				|  |  | +     * @return
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    private Map<String, Object> payExec(BigDecimal payAmount, Integer tenantId, String payStudentIds) throws Exception {
 | 
	
		
			
				|  |  | +        //生成订单编号
 | 
	
		
			
				|  |  | +        String orderNo = idGenerator.generatorId("payment") + "";
 | 
	
		
			
				|  |  | +        String baseApiUrl = sysConfigDao.findConfigValue("base_api_url");
 | 
	
		
			
				|  |  | +        TenantOrderRecordEnum tenantEnum = TenantOrderRecordEnum.CLOUD_TEACHER;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        Map<String, Object> payMap = new HashMap<>();
 | 
	
		
			
				|  |  | +        int orderState = 0;
 | 
	
		
			
				|  |  | +        //消费大于0元则拉起支付
 | 
	
		
			
				|  |  | +        if (payAmount.compareTo(BigDecimal.ZERO) > 0) {
 | 
	
		
			
				|  |  | +            payMap = payService.getPayMap(
 | 
	
		
			
				|  |  | +                    payAmount,
 | 
	
		
			
				|  |  | +                    BigDecimal.ZERO,
 | 
	
		
			
				|  |  | +                    orderNo,
 | 
	
		
			
				|  |  | +                    baseApiUrl + "/api-web/cloudTeacherOrder/payNotify",
 | 
	
		
			
				|  |  | +                    baseApiUrl + "/api-student/studentOrder/paymentResult?orderNo=" + orderNo,
 | 
	
		
			
				|  |  | +                    tenantEnum.getMsg(),
 | 
	
		
			
				|  |  | +                    tenantEnum.getMsg(),
 | 
	
		
			
				|  |  | +                    1,//临时写死
 | 
	
		
			
				|  |  | +                    tenantEnum.getCode()
 | 
	
		
			
				|  |  | +            );
 | 
	
		
			
				|  |  | +            createOrderRecord(tenantId, payAmount, orderNo, tenantEnum, orderState, payStudentIds);
 | 
	
		
			
				|  |  | +            log.info("tenant pay >>>>> " + JSON.toJSONString(payMap));
 | 
	
		
			
				|  |  | +        } else {
 | 
	
		
			
				|  |  | +            //已支付 直接激活
 | 
	
		
			
				|  |  | +            orderState = 1;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        return payMap;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private Integer updateCloudTeacherOrder(String payStudentIds) {
 | 
	
		
			
				|  |  | +        return cloudTeacherOrderDao.updateCloudTeacherOrder(payStudentIds);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    public Msg payNotify(Msg msg) {
 | 
	
		
			
				|  |  | +        log.info("tenant orderNotify >>>>> " + msg.toString());
 | 
	
		
			
				|  |  | +        msg.setMsg("fail");
 | 
	
		
			
				|  |  | +        Map<String, String> notifyMap = JSON.parseObject(msg.getResponseParameters(), Map.class);
 | 
	
		
			
				|  |  | +        CloudTeacherOrderRecord record = cloudTeacherOrderRecordService.getOne(
 | 
	
		
			
				|  |  | +                new QueryWrapper<CloudTeacherOrderRecord>().eq("order_no_", notifyMap.get("orderNo")));
 | 
	
		
			
				|  |  | +        record.setOrderNo(notifyMap.get("merMerOrderNo"));
 | 
	
		
			
				|  |  | +        record.setTransNo(notifyMap.get("orderNo"));
 | 
	
		
			
				|  |  | +        String channelType = notifyMap.get("channelType").equals("1") ? "WXPay" : (notifyMap.get("channelType").equals("2") ? "Alipay" : "quickPay");
 | 
	
		
			
				|  |  | +        record.setPayChannel(channelType);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        //支付中订单存在,更新状态
 | 
	
		
			
				|  |  | +        if (msg.getResponseType().equals("1") && notifyMap.size() > 0 && msg.getCode().equals("88")) {
 | 
	
		
			
				|  |  | +            //tradeState 88就是正确的 其他错误的
 | 
	
		
			
				|  |  | +            //            DealStatusEnum
 | 
	
		
			
				|  |  | +            //            notifyMap.put("totalMoney", notifyMap.get("payAmount"));
 | 
	
		
			
				|  |  | +            if (record.getPayStudentIds() != null) {
 | 
	
		
			
				|  |  | +                this.updateCloudTeacherOrder(record.getPayStudentIds());
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            record.setOrderState(1);
 | 
	
		
			
				|  |  | +            msg.setCode("000000");
 | 
	
		
			
				|  |  | +            msg.setMsg("success");
 | 
	
		
			
				|  |  | +        } else {
 | 
	
		
			
				|  |  | +            //支付失败
 | 
	
		
			
				|  |  | +            record.setOrderState(2);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        cloudTeacherOrderRecordService.update(record, new QueryWrapper<CloudTeacherOrderRecord>()
 | 
	
		
			
				|  |  | +                .eq("order_no_", record.getOrderNo()));
 | 
	
		
			
				|  |  | +        log.info("tenant orderNotify return>>>>> " + msg.toString());
 | 
	
		
			
				|  |  | +        return msg;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private void createOrderRecord(Integer tenantId, BigDecimal payAmount, String orderNo, TenantOrderRecordEnum tenantEnum, Integer orderState, String payStudentIds) {
 | 
	
		
			
				|  |  | +        LocalDateTime now = LocalDateTime.now();
 | 
	
		
			
				|  |  | +        CloudTeacherOrderRecord record = new CloudTeacherOrderRecord();
 | 
	
		
			
				|  |  | +        record.setTenantId(tenantId);
 | 
	
		
			
				|  |  | +        record.setOrderNo(orderNo);
 | 
	
		
			
				|  |  | +        record.setOrderType(tenantEnum.getCode());
 | 
	
		
			
				|  |  | +        record.setPaymentChannel(PaymentChannelEnum.ADAPAY.getCode());
 | 
	
		
			
				|  |  | +        record.setExpectAmount(payAmount);
 | 
	
		
			
				|  |  | +        record.setActualAmount(payAmount);
 | 
	
		
			
				|  |  | +        record.setOrderState(orderState);
 | 
	
		
			
				|  |  | +        record.setPayDate(now.toLocalDate().toDate());
 | 
	
		
			
				|  |  | +        record.setPayTime(now.toDate());
 | 
	
		
			
				|  |  | +        record.setCreatedTime(now.toDate());
 | 
	
		
			
				|  |  | +        record.setPayStudentIds(payStudentIds);
 | 
	
		
			
				|  |  | +        cloudTeacherOrderRecordService.save(record);
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      @Override
 |