Search in sources :

Example 1 with Position

use of com.guhanjie.model.Position in project weixin-boot by guhanjie.

the class TestMessageKit method testSendKFMsg.

@Test
public void testSendKFMsg() {
    Order order = new Order();
    order.setAmount(new BigDecimal(307.0));
    order.setContactor("顾汉杰");
    order.setDistance(new BigDecimal(34.0));
    Position from = new Position();
    from.setAddress("莲泰苑");
    from.setDetail("4号602室");
    from.setFloor((short) 6);
    order.setFrom(from);
    Position to = new Position();
    to.setAddress("莲泰苑");
    to.setDetail("5号502室");
    to.setFloor((short) 5);
    order.setTo(to);
    order.setPhone("13052333613");
    order.setRemark("测试");
    order.setStartTime(new Date());
    order.setVehicle(VehicleEnum.JINBEI.code());
    StringBuffer sb = new StringBuffer("主人,您有新的订单:\n");
    sb.append("订单金额:").append(order.getAmount()).append("元\n");
    sb.append("路程:").append(order.getDistance()).append("公里\n");
    sb.append("联系人:").append(order.getContactor()).append("\n");
    sb.append("电话:").append(order.getPhone()).append("\n");
    sb.append("车型:").append(VehicleEnum.valueOf(order.getVehicle()).desc()).append("\n");
    sb.append("预订时间:").append(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(order.getStartTime())).append("\n");
    sb.append("起始地:").append(order.getFrom().getAddress()).append(" ").append(order.getFrom().getDetail()).append(" 第").append(order.getFrom().getFloor()).append("楼\n");
    sb.append("目的地:").append(order.getTo().getAddress()).append(" ").append(order.getTo().getDetail()).append(" 第").append(order.getTo().getFloor()).append("楼\n");
    sb.append("备注:").append(order.getRemark()).append("\n");
    MessageKit.sendKFMsg(weixinConstants.KF_OPENIDS, sb.toString());
}
Also used : Order(com.guhanjie.model.Order) Position(com.guhanjie.model.Position) SimpleDateFormat(java.text.SimpleDateFormat) BigDecimal(java.math.BigDecimal) Date(java.util.Date) Test(org.junit.Test)

Example 2 with Position

use of com.guhanjie.model.Position in project weixin-boot by guhanjie.

the class OrderService method putOrder.

public void putOrder(Order order) {
    if (order == null) {
        LOGGER.error("put order error, order is null");
        throw WebExceptionFactory.exception(WebExceptionEnum.PARAMETER_NULL);
    }
    // 1. 检查用户信息
    User user = order.getUser();
    if (user == null || user.getId() == null) {
        // 若用户不存在,首次进入添加user记录
        if (StringUtils.isBlank(order.getPhone())) {
            LOGGER.error("put order error, user not exist, order:[{}]", order.getId());
            throw WebExceptionFactory.exception(WebExceptionEnum.USER_NOT_EXIST);
        }
        user = userService.getUserByPhone(order.getPhone());
        if (user == null) {
            user = new User();
            user.setName(order.getContactor());
            user.setPhone(order.getPhone());
            user.setCreateTime(new Date());
            LOGGER.info("user first in while putting order, add an new user:[{}]", JSON.toJSONString(user));
            userService.addUser(user);
        }
    }
    if (StringUtils.isBlank(user.getPhone())) {
        // 用户信息默认不含手机号码,第一次用户填写信息时记录手机号码
        String phone = order.getPhone();
        if (StringUtils.isBlank(phone)) {
            throw WebExceptionFactory.exception(WebExceptionEnum.DATA_NOT_WELL, "缺失联系方式");
        }
        user.setPhone(phone);
        userService.updateUser(user);
    }
    order.setUserId(user.getId());
    order.setUser(user);
    if (StringUtils.isBlank(order.getContactor())) {
        String username = StringUtils.isBlank(user.getName()) ? user.getNickname() : user.getName();
        order.setContactor(username);
    }
    if (StringUtils.isBlank(order.getPhone())) {
        order.setContactor(user.getPhone());
    }
    // 2. 检查位置信息
    // 起始地信息
    Position from = order.getFrom();
    if (from == null) {
        LOGGER.warn("put order error, from info not exist, order:[{}]", order.getId());
        throw WebExceptionFactory.exception(WebExceptionEnum.DATA_NOT_WELL, "缺失起始地信息");
    }
    positionMapper.insertSelective(from);
    order.setFromId(from.getId());
    order.setFrom(from);
    // 目的地信息
    Position to = order.getTo();
    if (to == null) {
        LOGGER.warn("put order error, to info not exist, order:[{}]", order.getId());
        throw WebExceptionFactory.exception(WebExceptionEnum.DATA_NOT_WELL, "缺失目的地信息");
    }
    positionMapper.insertSelective(to);
    order.setToId(to.getId());
    order.setTo(to);
    // 途经点信息
    if (order.getWaypoints() != null) {
        StringBuilder sb = new StringBuilder();
        for (Position p : order.getWaypoints()) {
            positionMapper.insertSelective(p);
            sb.append(p.getId()).append(",");
        }
        if (sb.length() > 0 && sb.charAt(sb.length() - 1) == ',') {
            sb.deleteCharAt(sb.length() - 1);
        }
        order.setWaypointsIds(sb.toString());
    }
    // 3. 校验订单有效性
    double price = 0.0;
    double amount = order.getAmount().doubleValue();
    double distance = Math.ceil(order.getDistance().doubleValue());
    short fromFloor = from.getFloor();
    short toFloor = to.getFloor();
    int workers = order.getWorkers();
    Short vehicle = order.getVehicle();
    if (vehicle == null) {
        throw WebExceptionFactory.exception(WebExceptionEnum.DATA_NOT_WELL, "缺失车型信息");
    }
    if (VehicleEnum.XIAOMIAN.code() == vehicle) {
        // 小面车
        // 起步价208(10公里内),超出后每公里5元
        price += (distance < 10) ? 208.0 : (208.0 + (distance - 10) * 5.0);
        // 电梯搬运免费,楼梯3层以下加收15元/层的人工费,3层以上加收20元/层的人工费
        price += fromFloor * ((fromFloor > 3) ? 20.0 : 15.0);
        price += toFloor * ((toFloor > 3) ? 20.0 : 15.0);
        // 每增加一名搬家师傅,加收200元
        price += workers < 2 ? 0.0 : (workers - 1) * 200;
        if (order.getWaypoints() != null) {
            for (Position p : order.getWaypoints()) {
                // 途经点
                // 每增加一个点位装卸货,增加50元
                price += 50.0;
                price += p.getFloor() * ((p.getFloor() > 3) ? 20.0 : 15.0);
            }
        }
    } else if (VehicleEnum.JINBEI.code() == vehicle) {
        // 金杯车
        // 起步价288(10公里内),超出后每公里6元
        price += (distance < 10) ? 288.0 : (288.0 + (distance - 10) * 6.0);
        // 电梯搬运免费,楼梯3层以下加收15元/层的人工费,3层以上加收20元/层的人工费
        price += fromFloor * ((fromFloor > 3) ? 20.0 : 15.0);
        price += toFloor * ((toFloor > 3) ? 20.0 : 15.0);
        // 每增加一名搬家师傅,加收200元
        price += workers > 1 ? (workers - 1) * 200 : 0;
        if (order.getWaypoints() != null) {
            for (Position p : order.getWaypoints()) {
                // 途经点
                // 每增加一个点位装卸货,增加50元
                price += 50.0;
                price += p.getFloor() * ((p.getFloor() > 3) ? 20.0 : 15.0);
            }
        }
    } else if (VehicleEnum.QUANSHUN.code() == vehicle) {
        // 全顺/依维轲
        // 起步价388(10公里内),超出后每公里8元
        price += (distance < 10) ? 388.0 : (388.0 + (distance - 10) * 8.0);
        // 电梯搬运免费,楼梯3层以下加收20元/层的人工费,3层以上加收30元/层的人工费
        price += fromFloor * ((fromFloor > 3) ? 30.0 : 20.0);
        price += toFloor * ((toFloor > 3) ? 30.0 : 20.0);
        // 每增加一名搬家师傅,加收200元
        price += workers > 1 ? (workers - 1) * 200 : 0;
        if (order.getWaypoints() != null) {
            for (Position p : order.getWaypoints()) {
                // 途经点
                // 每增加一个点位装卸货,增加50元
                price += 50.0;
                price += p.getFloor() * ((p.getFloor() > 3) ? 30.0 : 20.0);
            }
        }
    } else if (VehicleEnum.XIANGHUO.code() == vehicle) {
        // 4.2米厢货车型
        // 起步价1288(10公里内),超出后每公里10元
        price += (distance < 10) ? 1288.0 : (1288.0 + (distance - 10) * 10.0);
        // 电梯搬运免费,楼梯3层以下加收30元/层的人工费,3层以上加收40元/层的人工费
        price += fromFloor * ((fromFloor > 3) ? 40.0 : 30.0);
        price += toFloor * ((toFloor > 3) ? 40.0 : 30.0);
        // 每增加一名搬家师傅,加收300元
        price += workers > 2 ? (workers - 2) * 300 : 0;
        if (order.getWaypoints() != null) {
            for (Position p : order.getWaypoints()) {
                // 途经点
                // 每增加一个点位装卸货,增加200元
                price += 200.0;
                price += p.getFloor() * ((p.getFloor() > 3) ? 40.0 : 30.0);
            }
        }
    } else {
        // 车型参数非法
        throw WebExceptionFactory.exception(WebExceptionEnum.DATA_NOT_WELL, "车型信息有误");
    }
    // 下单时间到服务时间小于4小时的价格翻1.5倍,下单时间到服务时间小于2小时的价格翻2倍
    long countdown = order.getStartTime().getTime() - System.currentTimeMillis();
    if (countdown < 4 * 60 * 60 * 1000L && countdown >= 2 * 60 * 60 * 1000L) {
        price = price * 1.5;
    } else if (countdown < 2 * 60 * 60 * 1000L) {
        price = price * 2.0;
    }
    // 校验金额(计算金额与前端展示金额误差在1.0以内)
    if (Math.abs(price - amount) > 1.0) {
        throw WebExceptionFactory.exception(WebExceptionEnum.VALIDATE_ERROR, "订单金额有误");
    }
    // 4. 生成订单
    order.setCreateTime(new Date());
    order.setStatus(OrderStatusEnum.NEW.code());
    orderMapper.insertSelective(order);
    // 5. 发送微信消息通知客服
    StringBuffer sb = new StringBuffer("主人,您有新的订单:\n");
    sb.append("金额:").append(order.getAmount()).append("元\n");
    sb.append("路程:").append(order.getDistance()).append("公里\n");
    sb.append("联系人:").append(order.getContactor()).append("\n");
    sb.append("电话:").append(order.getPhone()).append("\n");
    sb.append("车型:").append(VehicleEnum.valueOf(order.getVehicle()).desc()).append("\n");
    sb.append("搬家师傅:").append(order.getWorkers()).append("人\n");
    sb.append("服务时间:").append(DateTimeUtil.formatDate(order.getStartTime(), "yyyy-MM-dd HH:mm")).append("\n");
    sb.append("起始地:").append(order.getFrom().getAddress()).append(" ").append(order.getFrom().getDetail()).append(" 第").append(order.getFrom().getFloor()).append("楼\n");
    sb.append("目的地:").append(order.getTo().getAddress()).append(" ").append(order.getTo().getDetail()).append(" 第").append(order.getTo().getFloor()).append("楼\n");
    if (order.getWaypoints() != null) {
        for (Position p : order.getWaypoints()) {
            sb.append("途经:").append(p.getAddress()).append(" ").append(p.getDetail()).append(" 第").append(p.getFloor()).append("楼\n");
        }
    }
    sb.append("备注:").append(order.getRemark()).append("\n");
    MessageKit.sendKFMsg(weixinConstants.KF_OPENIDS, sb.toString());
}
Also used : User(com.guhanjie.model.User) Position(com.guhanjie.model.Position) Date(java.util.Date)

Example 3 with Position

use of com.guhanjie.model.Position in project weixin-boot by guhanjie.

the class OrderService method setWayPoints.

private void setWayPoints(Order order) {
    if (order != null && StringUtils.isNotBlank(order.getWaypointsIds())) {
        String wayPoints = order.getWaypointsIds();
        String[] pids = wayPoints.split(",");
        for (String pid : pids) {
            Position poi = positionMapper.selectByPrimaryKey(Integer.parseInt(pid));
            List<Position> ways = order.getWaypoints();
            if (ways == null) {
                ways = new ArrayList<Position>();
                order.setWaypoints(ways);
            }
            ways.add(poi);
        }
    }
}
Also used : Position(com.guhanjie.model.Position)

Example 4 with Position

use of com.guhanjie.model.Position in project weixin-boot by guhanjie.

the class OrderService method cancelOrder.

public boolean cancelOrder(Order order) {
    if (order == null) {
        LOGGER.warn("order can not be null.");
        return false;
    }
    LOGGER.info("cancelling order[{}]...", order.getId());
    long startTime = order.getStartTime().getTime();
    long now = System.currentTimeMillis();
    // 只有订单状态为新建,且距离服务时间4小时之前,才可取消订单
    if (order.getStatus() == OrderStatusEnum.NEW.code() && (startTime - now) > 4 * 60 * 60 * 1000) {
        order.setStatus(OrderStatusEnum.CANCEL.code());
        order.setUpdateTime(new Date());
        if (1 == orderMapper.updateByStatus(order, OrderStatusEnum.NEW.code())) {
            LOGGER.info("success to cancel order[{}]", order.getId());
            // 订单被取消,发送微信消息通知客服
            StringBuffer sb = new StringBuffer("主人,您有一笔订单已取消:\n");
            sb.append("订单标识:").append(order.getId()).append("\n");
            sb.append("联系人:").append(order.getContactor()).append("\n");
            sb.append("电话:").append(order.getPhone()).append("\n");
            sb.append("服务时间:").append(DateTimeUtil.formatDate(order.getStartTime())).append("\n");
            Position from = positionMapper.selectByPrimaryKey(order.getFromId());
            sb.append("起始地:").append(from.getAddress()).append("\n");
            Position to = positionMapper.selectByPrimaryKey(order.getToId());
            sb.append("目的地:").append(to.getAddress()).append("\n");
            MessageKit.sendKFMsg(weixinConstants.KF_OPENIDS, sb.toString());
            return true;
        }
    }
    throw WebExceptionFactory.exception(WebExceptionEnum.ORDER_CANCEL_ERROR, "当前订单状态无法取消");
}
Also used : Position(com.guhanjie.model.Position) Date(java.util.Date)

Example 5 with Position

use of com.guhanjie.model.Position in project weixin-boot by guhanjie.

the class MessageKit method handleLocationEvent.

private static String handleLocationEvent(Map<String, String> msgMap) throws IOException {
    LOGGER.info("starting to handle location event....");
    String openId = msgMap.get("FromUserName");
    String lat = msgMap.get("Latitude");
    String lng = msgMap.get("Longitude");
    // 获取用户上报的地址微信,并绑定到内存中
    User user = new User();
    user.setOpenId(openId);
    Position p = new Position();
    p.setGeoLat(lat);
    p.setGeoLng(lng);
    user.setPosition(p);
    UserService userService = SpringContextUtil.getBean(UserService.class);
    userService.updateToCache(user);
    return "";
}
Also used : User(com.guhanjie.model.User) Position(com.guhanjie.model.Position) UserService(com.guhanjie.service.UserService)

Aggregations

Position (com.guhanjie.model.Position)8 Date (java.util.Date)3 Test (org.junit.Test)3 Order (com.guhanjie.model.Order)2 User (com.guhanjie.model.User)2 UserService (com.guhanjie.service.UserService)1 BigDecimal (java.math.BigDecimal)1 SimpleDateFormat (java.text.SimpleDateFormat)1