package com.stylefeng.guns.modular.smallLogistics.server.impl; 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.smallLogistics.dao.OrderLogisticsMapper; import com.stylefeng.guns.modular.smallLogistics.model.OrderLogistics; import com.stylefeng.guns.modular.smallLogistics.model.OrderLogisticsSpread; import com.stylefeng.guns.modular.smallLogistics.server.IOrderLogisticsService; import com.stylefeng.guns.modular.smallLogistics.server.IOrderLogisticsSpreadService; import com.stylefeng.guns.modular.system.dao.RegionMapper; import com.stylefeng.guns.modular.system.model.Company; import com.stylefeng.guns.modular.system.model.Driver; import com.stylefeng.guns.modular.system.model.Region; import com.stylefeng.guns.modular.system.service.ICompanyService; import com.stylefeng.guns.modular.system.service.IDriverService; import com.stylefeng.guns.modular.system.service.IIncomeService; import com.stylefeng.guns.modular.system.service.ISystemNoticeService; import com.stylefeng.guns.modular.system.util.*; import com.stylefeng.guns.modular.taxi.model.OrderTaxi; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.math.BigDecimal; import java.util.*; @Service public class OrderLogisticsServiceImpl extends ServiceImpl implements IOrderLogisticsService { @Resource private OrderLogisticsMapper orderLogisticsMapper; @Autowired private IDriverService driverService; @Autowired private GDFalconUtil gdFalconUtil; @Autowired private GDMapGeocodingUtil gdMapGeocodingUtil; @Resource private RegionMapper regionMapper; @Autowired private ChinaMobileUtil chinaMobileUtil; @Autowired private PushUtil pushUtil; @Autowired private ISystemNoticeService systemNoticeService; @Autowired private IOrderLogisticsSpreadService orderLogisticsSpreadService; @Autowired private RedisUtil redisUtil; @Autowired private ICompanyService companyService; @Autowired private IIncomeService incomeService; @Autowired private ALiSendSms aLiSendSms; /** * 获取司机抢单界面详情 * @param orderId * @return * @throws Exception */ @Override public Map queryPushOrder(Integer orderId) throws Exception { return orderLogisticsMapper.queryPushOrder(orderId); } /** * 司机抢单操作 * @param orderId * @param uid * @return * @throws Exception */ @Override public synchronized ResultUtil grabOrder(Integer orderId, Integer uid) throws Exception { OrderLogistics orderLogistics = this.selectById(orderId); if(orderLogistics.getState() == 10){ return ResultUtil.error("订单已取消"); } if(orderLogistics.getState() != 1){ return ResultUtil.error("手速有点慢哦,订单已被抢啦!"); } Driver driver = driverService.selectById(uid); orderLogistics.setDriverId(uid); orderLogistics.setCarId(driver.getCarId()); orderLogistics.setCompanyId(driver.getFranchiseeId() != null && driver.getFranchiseeId() != 0 ? driver.getFranchiseeId() : ( driver.getCompanyId() != null && driver.getCompanyId() != 0 ? driver.getCompanyId() : 1)); orderLogistics.setState(2); orderLogistics.setSnatchOrderTime(new Date()); //调用高德创建轨迹 String s = gdFalconUtil.selectTerminal(driver.getPhone()); String track = gdFalconUtil.createTrack(s); orderLogistics.setTrackId(track); //调用移动的小号接口 Map geocode = gdMapGeocodingUtil.geocode(orderLogistics.getStartLon().toString(), orderLogistics.getStartLat().toString()); Region region = regionMapper.query(geocode.get("districtCode")); Map map = chinaMobileUtil.midAxbBindSend(orderLogistics.getRecipientPhone(), driver.getPhone(), Integer.valueOf(region.getCitycode().substring(1))); if(String.valueOf(map.get("code")).equals("200")){ orderLogistics.setTelX(map.get("telX")); orderLogistics.setBindId(map.get("bindId")); } this.updateById(orderLogistics); //添加已收入明细 Company company = companyService.selectById(driver.getCompanyId()); Double speMoney = orderLogistics.getType() == 4 ? company.getSameLogisticsMoney() : company.getCrossLogisticsMoney(); BigDecimal d = null;//企业收入 BigDecimal c = null;//司机收入 if(company.getIsSpeFixedOrProportional() == 2){//固定 d = new BigDecimal(speMoney); c = new BigDecimal(orderLogistics.getOrderMoney()).subtract(d);//只有出行金额参与抽成,其余归属司机 } if(company.getIsSpeFixedOrProportional() == 1){//比例 Double price = orderLogistics.getTravelMoney(); d = new BigDecimal(price).multiply(new BigDecimal(speMoney).divide(new BigDecimal(100))).setScale(2, BigDecimal.ROUND_HALF_EVEN); c = new BigDecimal(orderLogistics.getOrderMoney()).subtract(d).setScale(2, BigDecimal.ROUND_HALF_EVEN); } incomeService.saveData(1, orderLogistics.getCompanyId(), 2, orderLogistics.getId(), orderLogistics.getType(), d.doubleValue()); incomeService.saveData(2, orderLogistics.getDriverId(), 2, orderLogistics.getId(), orderLogistics.getType(), c.doubleValue()); driver.setBusinessMoney(new BigDecimal(null != driver.getBusinessMoney() ? driver.getBusinessMoney() : 0).add(c).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue()); driver.setLaveBusinessMoney(new BigDecimal(null != driver.getLaveBusinessMoney() ? driver.getLaveBusinessMoney() : 0).add(c).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue()); driver.setBalance(new BigDecimal(null != driver.getBalance() ? driver.getBalance() : 0).add(c).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue()); driverService.updateById(driver); //推送相关代码------------------start---------------- new Thread(new Runnable() { @Override public void run() { pushUtil.pushOrderState(1, orderLogistics.getUserId(), orderLogistics.getId(), orderLogistics.getType(), orderLogistics.getState()); pushUtil.pushOrderState(2, orderLogistics.getDriverId(), orderLogistics.getId(), orderLogistics.getType(), orderLogistics.getState()); } }).start(); new Thread(new Runnable() { @Override public void run() { pushUtil.pushDriverPosition(orderId, orderLogistics.getType()); } }).start(); systemNoticeService.addSystemNotice(2, "您已成功抢得小件物流订单,请及时联系客户!", orderLogistics.getDriverId()); systemNoticeService.addSystemNotice(1, "您的订单已指派给" + driver.getName().substring(0, 1) + "师傅,请保持电话畅通!", orderLogistics.getUserId()); return ResultUtil.success(); } /** * 获取订单详情页(服务中的页面) * @param orderId * @return * @throws Exception */ @Override public Map queryOrderInfo(Integer orderId) throws Exception { return orderLogisticsMapper.queryOrderInfo(orderId); } /** * 走订单流程 * @param orderId * @param state * @param lon * @param lat * @param address * @return * @throws Exception */ @Override public ResultUtil process(Integer orderId, Integer state, Double lon, Double lat, String address) throws Exception { OrderLogistics orderLogistics = this.selectById(orderId); switch (state){ case 3://出发前往预约点 orderLogistics.setState(3); orderLogistics.setSetOutTime(new Date()); systemNoticeService.addSystemNotice(1, "司机已出发,请耐心等待", orderLogistics.getUserId()); break; case 4://到达预约点,等待客户上车 orderLogistics.setState(4); orderLogistics.setArriveTime(new Date()); systemNoticeService.addSystemNotice(1, "司机已到达您设置的预约地点。", orderLogistics.getUserId()); break; case 5://开始服务 orderLogistics.setBoardingLon(lon); orderLogistics.setBoardingLat(lat); orderLogistics.setBoardingAddress(address); orderLogistics.setBoardingTime(new Date()); orderLogistics.setState(5); orderLogistics.setStartServiceTime(new Date()); pushUtil.pushDriverPosition(orderLogistics.getId(), orderLogistics.getType());//主动推送司机定位 break; case 6://结束服务(专车可以返回继续服务)不修改状态 orderLogistics.setGetoffLon(lon); orderLogistics.setGetoffLat(lat); orderLogistics.setGetoffAddress(address); orderLogistics.setGetoffTime(new Date()); orderLogistics.setEndServiceTime(new Date()); orderLogistics.setState(6); break; } this.updateById(orderLogistics); // TODO: 2020/6/5 推送状态 new Thread(new Runnable() { @Override public void run() { pushUtil.pushOrderState(1, orderLogistics.getUserId(), orderLogistics.getId(), orderLogistics.getType(), orderLogistics.getState()); pushUtil.pushOrderState(2, orderLogistics.getDriverId(), orderLogistics.getId(), orderLogistics.getType(), orderLogistics.getState()); } }).start(); return ResultUtil.success(); } /** * 验证取件码(小件物流) * @param orderId * @param pickUpCode * @return * @throws Exception */ @Override public ResultUtil fillInPickUpCode(Integer orderId, String pickUpCode) throws Exception { OrderLogistics orderLogistics = this.selectById(orderId); if(!orderLogistics.getPickUpCode().equals(pickUpCode)){ return ResultUtil.error("验证失败"); } orderLogistics.setState(9); this.updateById(orderLogistics); return ResultUtil.success(); } /** * 司机设置补差价金额 * @param orderId * @param difference * @return * @throws Exception */ @Override public ResultUtil makeUpTheDifference(Integer orderId, Double difference) throws Exception { OrderLogisticsSpread query = orderLogisticsSpreadService.query(orderId); OrderLogistics orderLogistics = this.selectById(orderId); if(orderLogistics.getState() != 4 && orderLogistics.getState() != 8){ return ResultUtil.error("不允许设置差价"); } if(query != null){ query.setPrice(difference); orderLogisticsSpreadService.updateById(query); return ResultUtil.success(); } OrderLogisticsSpread orderLogisticsSpread = new OrderLogisticsSpread(); orderLogisticsSpread.setOrderLogisticsId(orderId); orderLogisticsSpread.setPrice(difference); orderLogisticsSpreadService.insert(orderLogisticsSpread); orderLogistics.setState(8); this.updateById(orderLogistics); new Thread(new Runnable() { @Override public void run() { pushUtil.pushPayDifference(1, orderLogistics.getUserId(), orderId, orderLogistics.getType(), difference); } }).start(); return ResultUtil.success(); } @Override public List> queryOrderList(Integer driverId) throws Exception { return orderLogisticsMapper.queryOrderList(driverId); } /** * 获取我的订单列表 * @param state 1=全部,2=待支付,3=已取消 * @param uid * @return * @throws Exception */ @Override public List> queryMyAllOrder(Integer state, Integer uid) throws Exception { return orderLogisticsMapper.queryMyAllOrder(state, uid); } /** * 抢单操作(车载端) * @param orderId * @param uid * @return * @throws Exception */ @Override public synchronized ResultUtil grabOrder_(Integer orderId, Integer uid) throws Exception { OrderLogistics orderLogistics = this.selectById(orderId); if(orderLogistics.getState() == 10){ return ResultUtil.error("订单已取消"); } if(orderLogistics.getState() != 1){ return ResultUtil.error("手速有点慢哦,订单已被抢啦!"); } Driver driver = driverService.selectById(uid); orderLogistics.setDriverId(uid); orderLogistics.setCarId(driver.getCarId()); orderLogistics.setCompanyId(driver.getFranchiseeId() != null && driver.getFranchiseeId() != 0 ? driver.getFranchiseeId() : ( driver.getCompanyId() != null && driver.getCompanyId() != 0 ? driver.getCompanyId() : 1)); orderLogistics.setState(2); Date date = new Date(); orderLogistics.setSnatchOrderTime(date); orderLogistics.setSetOutTime(date); orderLogistics.setArriveTime(date); orderLogistics.setStartServiceTime(date); orderLogistics.setBoardingTime(date); String value = redisUtil.getValue("DRIVER" + uid); if(ToolUtil.isNotEmpty(value)){ String[] split = value.split(","); Map geocode1 = gdMapGeocodingUtil.geocode(split[0], split[1]); orderLogistics.setBoardingAddress(geocode1.get("address")); orderLogistics.setBoardingLon(Double.valueOf(split[0])); orderLogistics.setBoardingLat(Double.valueOf(split[1])); } //调用高德创建轨迹 String s = gdFalconUtil.selectTerminal(driver.getPhone()); String track = gdFalconUtil.createTrack(s); orderLogistics.setTrackId(track); //调用移动的小号接口 TODO 车载端使用真实号码 // Map geocode = gdMapGeocodingUtil.geocode(orderTaxi.getStartLon().toString(), orderTaxi.getStartLat().toString()); // Region region = regionMapper.query(geocode.get("districtCode")); // Map map = chinaMobileUtil.midAxbBindSend(orderTaxi.getPassengersPhone(), driver.getPhone(), Integer.valueOf(region.getCitycode().substring(1))); //// if(String.valueOf(map.get("code")).equals("200")){ //// orderTaxi.setTelX(map.get("telX")); //// orderTaxi.setBindId(map.get("bindId")); //// } this.updateById(orderLogistics); //推送相关代码------------------start---------------- new Thread(new Runnable() { @Override public void run() { pushUtil.pushOrderState(1, orderLogistics.getUserId(), orderLogistics.getId(), orderLogistics.getType(), orderLogistics.getState()); pushUtil.pushOrderState(2, orderLogistics.getDriverId(), orderLogistics.getId(), orderLogistics.getType(), orderLogistics.getState()); } }).start(); new Thread(new Runnable() { @Override public void run() { pushUtil.pushDriverPosition(orderId, orderLogistics.getType()); } }).start(); systemNoticeService.addSystemNotice(2, "您已成功抢得出租车订单,请及时联系客户!", orderLogistics.getDriverId()); systemNoticeService.addSystemNotice(1, "您的订单已指派给" + driver.getName().substring(0, 1) + "师傅,请保持电话畅通!", orderLogistics.getUserId()); return ResultUtil.success(); } @Override public List query(Integer driverId, Integer... state) throws Exception { return orderLogisticsMapper.query(Arrays.asList(state), driverId); } @Override public void sendVerificationCode(Integer orderId) throws Exception { OrderLogistics orderLogistics = this.selectById(orderId); String random = ""; for(int i = 0; i < 6; i++){ random += Double.valueOf(Math.random() * 10).intValue(); } orderLogistics.setPickUpCode(random); this.updateById(orderLogistics); //发送短信 // HuaWeiSMSUtil.sendSms("[\"" + random + "\"]", orderLogistics.getRecipientPhone(), "8822072510762", "3f8a36fe5265433783655dcb6fb51465"); aLiSendSms.sendSms(orderLogistics.getRecipientPhone(), "SMS_317840272", "{\"code\":\"" + random + "\"}"); } @Override public List queryMaturity() throws Exception { return orderLogisticsMapper.queryMaturity(); } }