package com.stylefeng.guns.modular.taxi.controller;
|
|
import cn.hutool.json.JSONObject;
|
import com.baomidou.mybatisplus.mapper.EntityWrapper;
|
import com.stylefeng.guns.modular.system.model.*;
|
import com.stylefeng.guns.modular.system.service.*;
|
import com.stylefeng.guns.modular.system.util.PushUtil;
|
import com.stylefeng.guns.modular.system.util.ResultUtil;
|
import com.stylefeng.guns.modular.system.util.SignVerificationUtil;
|
import com.stylefeng.guns.modular.taxi.dao.OrderTaxiMapper;
|
import com.stylefeng.guns.modular.taxi.model.OrderTaxi;
|
import com.stylefeng.guns.modular.taxi.service.IOrderTaxiService;
|
import io.swagger.annotations.ApiOperation;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.util.StringUtils;
|
import org.springframework.web.bind.annotation.*;
|
|
import javax.annotation.Resource;
|
import javax.servlet.http.HttpServletRequest;
|
import java.util.ArrayList;
|
import java.util.Date;
|
import java.util.List;
|
import java.util.Objects;
|
import java.util.stream.Collectors;
|
|
/**
|
* 支付回调控制器
|
*/
|
@RestController
|
@RequestMapping("/base")
|
public class TaxiCallbackController {
|
|
@Autowired
|
private ICompanyService companyService;
|
|
@Autowired
|
private IPushOrderService pushOrderService;
|
@Autowired
|
private IDriverService driverService;
|
@Autowired
|
private PushUtil pushUtil;
|
@Resource
|
private OrderTaxiMapper orderTaxiMapper;
|
@Autowired
|
private IUserInfoService userInfoService;
|
@Autowired
|
private ICallingAreaService callingAreaService;
|
@Autowired
|
private ICompanyCityService companyCityService;
|
@ResponseBody
|
@PostMapping("/callback")
|
@ApiOperation(value = "AI回调", tags = {"AI回调"}, notes = "")
|
public ResultUtil callback(@RequestBody JSONObject req, HttpServletRequest request) throws Exception {
|
System.err.println("回调信息==========》"+req);
|
|
String appKey = request.getHeader("appKey");
|
String ts = request.getHeader("ts");
|
String signature = request.getHeader("signature");
|
|
// 验签
|
Boolean flag = SignVerificationUtil.signVerification(appKey, ts, signature, req);
|
if (!flag) {
|
System.err.println("验签失败");
|
return ResultUtil.error("验签失败");
|
}
|
|
// 遍历列表以获取每个结果的地址信息
|
OrderTaxi orderTaxi = new OrderTaxi();
|
|
String phone = req.getStr("phone");
|
JSONObject outputParam = req.getJSONObject("outputParam");
|
String area = outputParam.getStr("area");
|
|
System.err.println("phone++++"+phone);
|
|
String downloadUrl = ""
|
;
|
for (Object ivrNodeAudio : req.getJSONArray("ivrNodeAudioList")) {
|
JSONObject jsonObject = (JSONObject) ivrNodeAudio;
|
downloadUrl = jsonObject.getStr("downloadUrl");
|
if(StringUtils.hasLength(downloadUrl)){
|
break;
|
}
|
}
|
|
//判断该号码是否为黑名单用户
|
try {
|
UserInfo userInfo = userInfoService.queryByPhone(phone);
|
// orderTaxi.setUserId(userInfo.getId());
|
if (userInfo!=null&&userInfo.getIsBlack()==1){
|
return ResultUtil.success();
|
}
|
} catch (Exception e) {
|
throw new RuntimeException(e);
|
}
|
// 查询电召区域
|
CallingArea callingArea = callingAreaService.selectOne(new EntityWrapper<CallingArea>().eq("areaCode", area));
|
if(Objects.nonNull(callingArea)){
|
// 打印或处理地址信息
|
System.out.println("地址信息:" + callingArea.getArea());
|
orderTaxi.setStartAddress(callingArea.getArea());
|
}else {
|
return ResultUtil.error("电召未开通该区域城市");
|
}
|
//创建订单
|
orderTaxi.setAudioLinkUrl(downloadUrl);
|
orderTaxi.setInsertTime(new Date());
|
orderTaxi.setIsReassign(1);
|
orderTaxi.setOrderSource(6);
|
orderTaxi.setState(1);//待接单
|
orderTaxi.setOrderNum(getOrderNum());
|
orderTaxi.setPassengersPhone(phone);
|
orderTaxi.setTravelTime(new Date());
|
orderTaxi.setOrderType(1);
|
orderTaxiMapper.insert(orderTaxi);
|
new Thread(new Runnable() {
|
@Override
|
public void run() {
|
if(orderTaxi.getState() == 2){
|
//推送司机订单状态
|
pushUtil.pushOrderState(2, orderTaxi.getDriverId(), orderTaxi.getId(), 2, orderTaxi.getState(), 0);
|
}
|
}
|
}).start();
|
if(orderTaxi.getState() == 1){
|
//推送司机抢单
|
try {
|
this.pushOrder(orderTaxi,callingArea);
|
}catch (Exception e){
|
}
|
}
|
|
return ResultUtil.success();
|
}
|
@Autowired
|
private IOrderTaxiService orderTaxiService;
|
public synchronized String getOrderNum() throws Exception{
|
int size = orderTaxiService.selectCount(null);
|
return "TAXI" + String.valueOf(1000000 + size + 1).substring(1);
|
}
|
public static List<Integer> orderIds = new ArrayList<>();
|
|
public void pushOrder(OrderTaxi orderTaxi,CallingArea callingArea) throws Exception{
|
new Thread(new Runnable() {
|
@Override
|
public void run() {
|
try {
|
orderIds.add(orderTaxi.getId());//添加记录,防止调用接口重复提醒无人接单
|
Company query = companyService.selectById(1);//获取起点所属分公司
|
List<PushOrder> querys = pushOrderService.querys(null, 2, query.getId());//获取需要推送的次数
|
// 优先查询区域
|
List<Company> companys = companyCityService.queryCall(callingArea.getAreaCode());
|
System.err.println("根据区域查询出来的分公司======"+companys);
|
List<Integer> companyIds = companys.stream().map(Company::getId).collect(Collectors.toList());
|
for(int i = 1; i <= querys.size(); i++){
|
PushOrder pushOrder = pushOrderService.querys(i, 2, query.getId()).get(0);
|
//获取空闲司机
|
List<Driver> list = driverService.queryIdleDriverAllCall(2, orderTaxi.getStartLon(), orderTaxi.getStartLat(), pushOrder.getPushDistance(), companyIds);//所有附近空闲司机
|
// List<Driver> list = driverService.queryIdleDriverAll(2, orderTaxi.getStartLon(), orderTaxi.getStartLat(), pushOrder.getPushDistance(), null);//所有附近空闲司机
|
if(list.size() > 0){
|
double driverProportion = pushOrder.getDriverProportion() / 100;//推送占比计算成小数
|
int lastIndex = Double.valueOf(list.size() * driverProportion).intValue();//计算占比转成整数(下标截取)
|
list = list.subList(0, lastIndex);//获取空闲司机中占比数据
|
for(Driver driver : list){//开始进行推送
|
pushUtil.pushOrderStateVedio(2, driver.getId(), orderTaxi.getId(), 2, orderTaxi.getState(), pushOrder.getPushTime(),orderTaxi.getAudioLinkUrl());
|
}
|
}
|
Thread.sleep(pushOrder.getPushTime() * 1000);//设置等待时间
|
Integer state = orderTaxiMapper.selectById(orderTaxi.getId()).getState();
|
if(state > 1){
|
break;
|
}
|
if(i == querys.size() && state == 1){
|
orderIds.remove(orderTaxi.getId());
|
}
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
}).start();
|
}
|
|
|
|
|
}
|