liujie
9 天以前 a8895e0ce4d03c9a50ad0196bb4ddc6dd642cd34
DriverQYTTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/service/impl/ReassignServiceImpl.java
@@ -1,24 +1,18 @@
package com.stylefeng.guns.modular.system.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.account.controller.AppOrderController;
import com.stylefeng.guns.modular.account.model.TEnterpriseWithdrawal;
import com.stylefeng.guns.modular.account.service.ITEnterpriseWithdrawalService;
import com.stylefeng.guns.modular.account.service.UserWithdrawalService;
import com.stylefeng.guns.modular.account.unionpay.GetOpenBodySig;
import com.stylefeng.guns.modular.account.unionpay.QrCodeConfiguration;
import com.stylefeng.guns.modular.account.unionpay.Util;
import com.stylefeng.guns.modular.account.util.Base64Util;
import com.stylefeng.guns.modular.cloudPayment.example.AllocationExample;
import com.stylefeng.guns.modular.cloudPayment.req.AllocationReq;
import com.stylefeng.guns.modular.crossCity.model.OrderCrossCity;
import com.stylefeng.guns.modular.crossCity.server.IOrderCrossCityService;
import com.stylefeng.guns.modular.smallLogistics.model.OrderLogistics;
import com.stylefeng.guns.modular.smallLogistics.server.IOrderLogisticsService;
import com.stylefeng.guns.modular.smallLogistics.server.IOrderLogisticsSpreadService;
import com.stylefeng.guns.modular.specialTrain.model.OrderPrivateCar;
import com.stylefeng.guns.modular.specialTrain.server.IOrderPrivateCarService;
import com.stylefeng.guns.modular.system.dao.DispatchMapper;
@@ -29,10 +23,11 @@
import com.stylefeng.guns.modular.system.util.*;
import com.stylefeng.guns.modular.taxi.model.OrderTaxi;
import com.stylefeng.guns.modular.taxi.service.IOrderTaxiService;
import com.unionpay.upyzt.resp.AllocationResp;
import com.stylefeng.guns.modular.taxi.service.impl.OrderTaxiServiceImpl;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
@@ -41,6 +36,7 @@
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
@Service
@@ -196,6 +192,8 @@
                        //添加已收入明细
                        incomeService.saveData(1, orderPrivateCar.getCompanyId(), 4, orderPrivateCar.getId(), 1, reassign.getMoney());
                        // qyt 改派直接推送
                        pushOrderPrivate(orderPrivateCar,uid);
                        break;
                    case 2:
                        OrderTaxi orderTaxi = orderTaxiService.selectById(reassign.getOrderId());
@@ -206,6 +204,9 @@
                        //添加已收入明细
                        incomeService.saveData(1, orderTaxi.getCompanyId(), 4, orderTaxi.getId(), 2, reassign.getMoney());
                        // qyt 改派直接推送
                        pushOrderTaxi(orderTaxi,uid);
                        break;
                    case 4:
                        OrderLogistics orderLogistics = orderLogisticsService.selectById(reassign.getOrderId());
@@ -234,16 +235,14 @@
                Map<String, String> map = new HashMap<>();
                map.put("orderId", reassign.getOrderId().toString());
                map.put("orderType", reassign.getOrderType().toString());
                List<Dispatch> dispatches = dispatchMapper.queryDispatchs(driver.getFranchiseeId() == null ? driver.getCompanyId() : driver.getFranchiseeId(), 1);
                for(Dispatch dispatch : dispatches){
                    jgPushUtil.push(2,"有新的改派订单需要处理,请及时处理!", "订单改派", map, "DISPATCH" + dispatch.getId());
                }
//                List<Dispatch> dispatches = dispatchMapper.queryDispatchs(driver.getFranchiseeId() == null ? driver.getCompanyId() : driver.getFranchiseeId(), 1);
//                for(Dispatch dispatch : dispatches){
//                    jgPushUtil.push(2,"有新的改派订单需要处理,请及时处理!", "订单改派", map, "DISPATCH" + dispatch.getId());
//                }
                // TODO 司机云闪付 改派余额支付,司机电子账簿转账到平台
                if(BigDecimal.ZERO.compareTo(new BigDecimal(aDouble)) < 0){
                    cloudPay(reassign.getOriginalDriverId(),aDouble,Util.getMerOrderId(qrCodeConfiguration.getMsgSrcId()));
                }
            }
            if(reassign.getPayType() == 4){
                // TODO 司机云闪付 改派调起云闪付
@@ -255,44 +254,6 @@
        return resultUtil;
    }
    private void cloudPay(Integer uid,Double total_fee,String transaction_id) {
        System.out.println(uid+"======"+total_fee+"====="+transaction_id);
        Driver driver = driverService.selectById(uid);
        if(Objects.isNull(driver)){
            throw new RuntimeException("该司机不存在!"+uid);
        }
        // 查询平台账户
        Company company = companyService.selectOne(new EntityWrapper<Company>()
                .eq("type", 1)
                .last("LIMIT 1"));
        System.out.println("查询平台账户==============="+company);
        // 查询平台开户信息
        TEnterpriseWithdrawal enterpriseWithdrawal = enterpriseWithdrawalService.selectOne(new EntityWrapper<TEnterpriseWithdrawal>()
                .eq("companyId", company.getId())); //  替换为公司id
        System.out.println("查询平台开户信息==============="+enterpriseWithdrawal);
        // TODO 司机进件信息
        UserWithdrawal driverWith = userWithdrawalService.selectOne(new EntityWrapper<UserWithdrawal>()
                .eq("phone", driver.getPhone())
                .last("LIMIT 1"));
        try {
            AllocationReq allocationReq = new AllocationReq();
            allocationReq.setOutOrderNo(transaction_id);
            allocationReq.setPayBalanceAcctId(driverWith.getBalanceAcctId()); // 发送方
            allocationReq.setRecvBalanceAcctId(enterpriseWithdrawal.getBalanceAcctId()); // 接收方
            BigDecimal multiply = new BigDecimal(100).multiply(new BigDecimal(total_fee));
            allocationReq.setAmount(multiply.intValue()); // 金额
            allocationReq.setPassword(Base64Util.decode(enterpriseWithdrawal.getTransactionAuthorizationCode())); // 密码
            allocationReq.setOrderNo(transaction_id);
            allocationReq.setOrderAmount(multiply.longValue());
            allocationReq.setProductName("订单改派");
            allocationReq.setProductCount(1);
            AllocationResp allocationResp = AllocationExample.create(allocationReq);
            System.err.println("司机分账信息:"+allocationResp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private ResultUtil placeAnOrder(BigDecimal amount, Integer type,Integer orderType,Integer reassignId) throws Exception {
        Reassign reassign = reassignMapper.selectById(reassignId);
@@ -543,6 +504,13 @@
    }
    public static List<Integer> orderIds = new ArrayList<>();
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    /**
     * 改派支付完成后的操作
     * @param id            改派单id
@@ -598,6 +566,12 @@
                    }
                    systemNoticeService.addSystemNotice(2, "您已使用" + (type == 1 ? "微信" : (type == 2?"支付宝":"云闪付")) + "成功支付改派申请费用!", reassigns.get(0).getOriginalDriverId(), 1);
                    System.out.println("==============跨城修改改派通知状态完毕===================");
                    // qyt 新增改派 重新推送
                }else{
                    System.err.println("预支付数据异常(orderId = "  + id + ")");
                }
@@ -687,4 +661,156 @@
    }
    @Autowired
    private ICompanyCityService companyCityService;
    @Autowired
    private IPushOrderService pushOrderService;
    @Autowired
    private PushUtil pushUtil;
    /**
     * 推送订单给司机抢单
     * @param orderTaxi
     * @throws Exception
     */
    public void pushOrderTaxi(OrderTaxi orderTaxi, Integer uid) throws Exception{
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    orderIds.add(orderTaxi.getId());//添加记录,防止调用接口重复提醒无人接单
                    String vehicle = (String) redisTemplate.opsForValue().get("VEHICLE");
                    List<Integer> integers = new ArrayList<>();
                    if (ToolUtil.isNotEmpty(vehicle)) {
                        integers = JSON.parseArray(vehicle).toJavaList(Integer.class);
                    }
                    Company query = companyCityService.query(String.valueOf(orderTaxi.getStartLon()), String.valueOf(orderTaxi.getStartLat()));//获取起点所属分公司
                    List<PushOrder> querys = pushOrderService.querys(null, 2, query.getId());//获取需要推送的次数
                    for (int i = 1; i <=6; i++) {
                        PushOrder pushOrder = pushOrderService.querys(i, 2, query.getId()).get(0);
                        System.out.println("pushOrder:" + pushOrder);
                        //获取空闲司机
                        List<Driver> list = driverService.queryIdleDriver(2, orderTaxi.getStartLon(), orderTaxi.getStartLat(), pushOrder.getPushDistance(), null);//所有附近空闲司机
                        list = list.stream().filter(e->e.getId()!=uid).collect(Collectors.toList());
                        if (list.size() > 0) {
                            double driverProportion = pushOrder.getDriverProportion() / 100;//推送占比计算成小数
                            int lastIndex = Double.valueOf(list.size() * driverProportion).intValue();//计算占比转成整数(下标截取)
                            lastIndex = lastIndex == 0 ? list.size() : lastIndex;
                            list = list.subList(0, lastIndex);//获取空闲司机中占比数据
                            for (Driver driver : list) {//开始进行推送
                                //查询是否在限制推单范围内
                                boolean bo = false;
                                for (Integer integer : integers) {
                                    if (integer.compareTo(driver.getId()) == 0) {
                                        bo = true;
                                        break;
                                    }
                                }
                                if (bo) {
                                    continue;
                                }
                                pushUtil.pushOrderState(2, driver.getId(), orderTaxi.getId(), 2, orderTaxi.getState(), pushOrder.getPushTime());
                            }
                        }
                        Thread.sleep(pushOrder.getPushTime() * 1000);//设置等待时间
                        OrderTaxi orderTaxi1 = orderTaxiService.selectById(orderTaxi.getId());
                        Integer state = orderTaxi1.getState();
                        if (state != 1) {
                            break;
                        }
                        if (i == querys.size() && state == 1) {
                            pushUtil.pushEndPush(1, orderTaxi.getUserId(), orderTaxi.getId(), 2);
                            orderTaxi1.setState(10);
                            orderTaxiService.updateById(orderTaxi1);
                            orderIds.remove(orderTaxi.getId());
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    /**
     * 推送抢单数据
     * @param orderPrivateCar
     * @throws Exception
     */
    public void pushOrderPrivate(OrderPrivateCar orderPrivateCar,Integer uid) throws Exception{
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Integer id = orderPrivateCar.getId();
                    Integer orderType = orderPrivateCar.getRideType()==1?1:orderPrivateCar.getRideType()==2?9:8;
                    orderIds.add(orderPrivateCar.getId());//添加记录,防止调用接口重复提醒无人接单
                    String vehicle = (String) redisTemplate.opsForValue().get("VEHICLE");
                    List<Integer> integers = new ArrayList<>();
                    if(ToolUtil.isNotEmpty(vehicle)){
                        integers = JSON.parseArray(vehicle).toJavaList(Integer.class);
                    }
                    Company query = companyCityService.query(String.valueOf(orderPrivateCar.getStartLon()), String.valueOf(orderPrivateCar.getStartLat()));//获取起点所属分公司
                    List<PushOrder> querys = pushOrderService.querys(null, 1, query.getId());//获取需要推送的次数
                    for(int i = 1; i <= 6; i++){
                        OrderPrivateCar orderPrivateCar = orderPrivateCarService.selectById(id);
                        if (null == orderPrivateCar || orderPrivateCar.getState() > 1) {
                            break;
                        }
                        PushOrder pushOrder = pushOrderService.querys(i, 1, query.getId()).get(0);
                        System.out.println("pushOrder:"+pushOrder);
                        int i2 = pushOrder.getPushTime() / 10;
                        if(i2==0){
                            i2=1;
                        }
                        for (int i1 = 0; i1 < i2 ; i1++) {
                            if (orderPrivateCar.getState() > 1) {
                                break;
                            }
                            //获取空闲司机
                            List<Driver> list = driverService.queryIdleDriver(orderType, orderPrivateCar.getServerCarModelId(), orderPrivateCar.getStartLon(), orderPrivateCar.getStartLat(), pushOrder.getPushDistance(), null);//所有附近空闲司机
                            list = list.stream().filter(e->e.getId()!=uid).collect(Collectors.toList());
                            if (list.size() > 0) {
                                double driverProportion = pushOrder.getDriverProportion() / 100;//推送占比计算成小数
                                int lastIndex = Double.valueOf(list.size() * driverProportion).intValue();//计算占比转成整数(下标截取)
                                lastIndex = lastIndex == 0 ? list.size() : lastIndex;
                                list = list.subList(0, lastIndex);//获取空闲司机中占比数据
                                for (Driver driver : list) {//开始进行推送
                                    boolean bo = false;
                                    for (Integer integer : integers) {
                                        if (integer.compareTo(driver.getId()) == 0) {
                                            bo = true;
                                            break;
                                        }
                                    }
                                    if (bo) {
                                        continue;
                                    }
                                    pushUtil.pushOrderState(2, driver.getId(), orderPrivateCar.getId(), 1, orderPrivateCar.getState(), pushOrder.getPushTime());
                                }
                            }
                            Thread.sleep(10000);//设置等待时间
                            Integer state = orderPrivateCar.getState();
                            if (state > 1) {
                                orderIds.remove(orderPrivateCar.getId());
                                return;
                            }
                            if (i == querys.size() && state == 1) {
                                pushUtil.pushEndPush(1, orderPrivateCar.getUserId(), orderPrivateCar.getId(), 1);
                                orderPrivateCar.setState(10);
                                orderPrivateCarService.updateById(orderPrivateCar);
                                orderIds.remove(orderPrivateCar.getId());
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}