use of com.guhanjie.model.User 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());
}
use of com.guhanjie.model.User 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 "";
}
use of com.guhanjie.model.User in project weixin-boot by guhanjie.
the class TestUserMapper method testCRUD.
@Test
public void testCRUD() {
// Create
logger.debug("Create one record to table[{}]...\n", tableName);
User model = new User();
model.setName("guhanjie");
model.setSex("m");
long insertCount = mapper.insertSelective(model);
assertEquals(insertCount, 1L);
// Retrieve
logger.debug("Select one record from table[{}]...\n", tableName);
model = mapper.selectByPrimaryKey(model.getId());
logger.debug(JSON.toJSONString(model, true));
// Update
logger.debug("Update one record in table[{}]...\n", tableName);
model.setSex("f");
long updateCount = mapper.updateByPrimaryKeySelective(model);
logger.debug("Update [{}] record(s) in table[{}]...\n", updateCount, tableName);
// Delete
logger.debug("Delete one record from table[{}]...\n", tableName);
long deleteCount = mapper.deleteByPrimaryKey(model.getId());
assertEquals(deleteCount, 1L);
}
use of com.guhanjie.model.User in project weixin-boot by guhanjie.
the class TestPropertyUtils method main.
/**
* Method Name: main<br/>
* Description: [description]
* @author guhanjie
* @time 2016年9月4日 下午12:33:35
* @param args
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws IllegalAccessException
*/
public static void main(String[] args) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
final User user1 = new User();
System.out.println(JSON.toJSONString(user1));
User user2 = new User();
user2.setCity("jfds");
user2.setId(12421);
user2.setNickname("sdfsdf");
PropertyUtils.copyProperties(user1, user2);
// BeanUtils.copyProperties(user1, user2);
System.out.println(JSON.toJSONString(user1));
}
use of com.guhanjie.model.User in project weixin-boot by guhanjie.
the class OAuthIntercepter method preHandle.
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HttpSession session = request.getSession();
Object user = session.getAttribute(AppConstants.SESSION_KEY_USER);
if (user != null && user instanceof User) {
LOGGER.debug("user[{}] login...", JSON.toJSONString(user));
return true;
}
String openid = (String) request.getSession().getAttribute(AppConstants.SESSION_KEY_OPEN_ID);
if (openid == null) {
// 用户未登录,需要网页授权获取用户信息
LOGGER.info("user non login, redirect to weixin oauth2.0...");
String lasturl = request.getRequestURI();
session.setAttribute(AppConstants.SESSION_KEY_RETURN_URL, lasturl);
String state = String.valueOf(new Random().nextInt(100));
session.setAttribute(AppConstants.SESSION_KEY_OAUTH_STATE, state);
LOGGER.info("cache return url[{}] and oauth state[{}] into user session", lasturl, state);
String url = WeixinConstants.OAUTH2_AUTHORIZE;
url = url.replaceAll("APPID", weixinContants.APPID);
url = url.replaceAll("REDIRECT_URI", weixinContants.OAUTH2_REDIRECT_URI);
url = url.replaceAll("SCOPE", weixinContants.OAUTH2_SCOPE_SNSAPI_USERINFO);
url = url.replaceAll("STATE", state);
response.sendRedirect(url);
return false;
}
LOGGER.debug("user[{}] login...", openid);
return true;
// LOGGER.debug("intercept request, getScheme: [{}]", request.getScheme());
// LOGGER.debug("intercept request, getProtocol: [{}]", request.getProtocol());
// LOGGER.debug("intercept request, getRequestURI: [{}]", request.getRequestURI());
// LOGGER.debug("intercept request, getRequestURL: [{}]", request.getRequestURL());
// LOGGER.debug("intercept request, getContextPath: [{}]", request.getContextPath());
// LOGGER.debug("intercept request, getServletPath: [{}]", request.getServletPath());
// LOGGER.debug("intercept request, getPathInfo: [{}]", request.getPathInfo());
// LOGGER.debug("intercept request, getQueryString: [{}]", request.getQueryString());
// LOGGER.debug("intercept request, getParameterMap: [{}]", request.getParameterMap()==null?"":request.getParameterMap().toString());
// LOGGER.debug("intercept request, getMethod: [{}]", request.getMethod());
// LOGGER.debug("intercept request, getContentType: [{}]", request.getContentType());
// LOGGER.debug("intercept request, getRemoteAddr: [{}]", request.getRemoteAddr());
// LOGGER.debug("intercept request, getRemoteHost: [{}]", request.getRemoteHost());
// LOGGER.debug("intercept request, getRemotePort: [{}]", request.getRemotePort());
// LOGGER.debug("intercept request, getRemoteUser: [{}]", request.getRemoteUser());
// LOGGER.debug("intercept request, getLocalAddr: [{}]", request.getLocalAddr());
// LOGGER.debug("intercept request, getLocalName: [{}]", request.getLocalName());
// LOGGER.debug("intercept request, getLocalPort: [{}]", request.getLocalPort());
// LOGGER.debug("intercept request, getServerName: [{}]", request.getServerName());
// LOGGER.debug("intercept request, getServerPort: [{}]", request.getServerPort());
// LOGGER.debug("intercept request, getAuthType: [{}]", request.getAuthType());
// LOGGER.debug("intercept request, getCharacterEncoding: [{}]", request.getCharacterEncoding());
// LOGGER.debug("intercept request, getContentLength: [{}]", request.getContentLength());
// LOGGER.debug("intercept request, getCookies: [{}]", request.getCookies()==null?"":request.getCookies().toString());
// LOGGER.debug("intercept request, getPathTranslated: [{}]", request.getPathTranslated());
// LOGGER.debug("intercept request, getRequestedSessionId: [{}]", request.getRequestedSessionId());
// return true;
}
Aggregations