Commit 770c75ff by weijiguang

下单时校验订单中sku数量

parent 0e138be9
......@@ -19,6 +19,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.List;
import java.util.Map;
......@@ -102,6 +103,33 @@ public class WeixinController {
return weixinService.wxNotifyTest(orderNo);
}
@RequestMapping("/v2/getShop")
public AjaxResult getShopV2(HttpServletRequest request, @RequestParam(required = false) String lng, @RequestParam(required = false) String lat) throws ParseException {
LoginUser loginUser = tokenService.getLoginUser(request);
boolean testFlag = false;
if (loginUser != null) {
Customer customer = customerService.selectCustById(loginUser.getOpenId());
if(customer!=null) {
testFlag = customer.getTestFlag();
}
}
Shop shop = appService.getShop(loginUser, lng, lat, testFlag);
return AjaxResult.success(shop);
}
@RequestMapping("/v2/getLocationShops")
public AjaxResult getSameLocationShops(HttpServletRequest request, @RequestParam(required = false) String lng, @RequestParam(required = false) String lat) throws ParseException {
LoginUser loginUser = tokenService.getLoginUser(request);
boolean testFlag = false;
if (loginUser != null) {
Customer customer = customerService.selectCustById(loginUser.getOpenId());
if(customer!=null) {
testFlag = customer.getTestFlag();
}
}
Shop shop = appService.getShop(loginUser, lng, lat, testFlag);
return AjaxResult.success(shop);
}
@RequestMapping("/getShop")
public AjaxResult getShop(HttpServletRequest request, @RequestParam(required = false) String lng, @RequestParam(required = false) String lat) {
......
import java.util.Calendar;
import com.soss.common.utils.DateUtils;
import com.soss.system.domain.Shop;
import java.text.ParseException;
public class JavaTest {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
System.out.println(calendar.get(Calendar.DAY_OF_WEEK) - 1);
public static void main(String[] args) throws ParseException {
Shop shop = new Shop();
shop.setStartTime("14:21");
shop.setEndTime("18:00");
System.out.println(DateUtils.isEffectiveDate(shop.getStartTime(), shop.getEndTime()));
// Shop shop = new Shop();
// shop.setLat("39.91571");
// shop.setLng("116.463485");
// String lat = "39.789916";
// String lng = "116.531469";
// double realDistance = DistanceUtil.getRealDistance(Double.parseDouble(lng), Double.parseDouble(lat), Double.parseDouble(shop.getLng()), Double.parseDouble(shop.getLat()));
// System.out.println(realDistance);
// Calendar calendar = Calendar.getInstance();
// System.out.println(calendar.get(Calendar.DAY_OF_WEEK) - 1);
}
}
......@@ -6,6 +6,7 @@ import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.util.Calendar;
import java.util.Date;
/**
......@@ -29,6 +30,38 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
"yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
public static boolean isEffectiveDate(String shopStartTime, String shopEndTime) throws ParseException {
String format = "HH:mm";
SimpleDateFormat sf = new SimpleDateFormat("HH:mm");
String now = sf.format(new Date());
Date nowTime = new SimpleDateFormat(format).parse(now);
Date startTime = new SimpleDateFormat(format).parse(shopStartTime);
Date endTime = new SimpleDateFormat(format).parse(shopEndTime);
return DateUtils.isEffectiveDate(nowTime, startTime, endTime);
}
public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
if (nowTime.getTime() == startTime.getTime()
|| nowTime.getTime() == endTime.getTime()) {
return true;
}
Calendar date = Calendar.getInstance();
date.setTime(nowTime);
Calendar begin = Calendar.getInstance();
begin.setTime(startTime);
Calendar end = Calendar.getInstance();
end.setTime(endTime);
if (date.after(begin) && date.before(end)) {
return true;
} else {
return false;
}
}
/**
* 获取当前Date型日期
*
......
......@@ -14,13 +14,12 @@ import java.util.List;
/**
* 店铺对象 shop
*
*
* @author zxq
* @date 2022-04-28
*/
@Data
public class Shop extends BaseEntity
{
public class Shop extends BaseEntity {
private static final long serialVersionUID = 1L;
/** 主键 */
......@@ -135,6 +134,15 @@ public class Shop extends BaseEntity
private Integer salesVolume;
/**
* 是否允许下单, 1-可以下单,0-不可以下单
*/
private Integer orderState;
/**
* 不允许下单原因
*/
private String stateDesc;
public Shop() {
super();
}
......@@ -154,23 +162,23 @@ public class Shop extends BaseEntity
@Override
public String toString() {
return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
.append("id", getId())
.append("name", getName())
.append("code", getCode())
.append("lng", getLng())
.append("lat", getLat())
.append("remarks", getRemarks())
.append("startTime", getStartTime())
.append("endTime", getEndTime())
.append("address", getAddress())
.append("province", getProvince())
.append("city", getCity())
.append("zone", getZone())
.append("state", getState())
.append("createdAt", getCreatedAt())
.append("updatedAt", getUpdatedAt())
.append("isDefault", getIsDefault())
.toString();
return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
.append("id", getId())
.append("name", getName())
.append("code", getCode())
.append("lng", getLng())
.append("lat", getLat())
.append("remarks", getRemarks())
.append("startTime", getStartTime())
.append("endTime", getEndTime())
.append("address", getAddress())
.append("province", getProvince())
.append("city", getCity())
.append("zone", getZone())
.append("state", getState())
.append("createdAt", getCreatedAt())
.append("updatedAt", getUpdatedAt())
.append("isDefault", getIsDefault())
.toString();
}
}
......@@ -76,4 +76,6 @@ public interface ShopMapper {
public int deleteShopByIds(String[] ids);
void updateNoDefault();
List<Shop> selectEnableShops();
}
......@@ -2,6 +2,8 @@ package com.soss.system.service.impl;
import com.soss.common.core.domain.model.LoginUser;
import com.soss.common.enums.ShopState;
import com.soss.common.utils.DateUtils;
import com.soss.common.utils.StringUtils;
import com.soss.system.domain.Order;
import com.soss.system.domain.Shop;
import com.soss.system.mapper.ShopMapper;
......@@ -11,6 +13,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
......@@ -24,6 +27,87 @@ public class AppServiceImpl {
@Autowired
private OrderServiceImpl orderService;
public Shop getShop(LoginUser loginUser, String lng, String lat, Boolean testFlag) throws ParseException {
Shop shop = null;
if (StringUtils.isEmpty(lng) || StringUtils.isEmpty(lat)) {
// 未授权位置
if (loginUser == null) {
// 未登录,返回默认店铺
shop = getDefaultShop();
} else {
// 已登录
List<Order> myOrder = orderService.getMyOrder(loginUser.getOpenId());
if (CollectionUtils.isEmpty(myOrder)) {
// 未下过单返回默认店铺
shop = getDefaultShop();
} else {
// 返回最后下单店铺
Order order = myOrder.get(0);
shop = shopService.selectShopById(order.getShopId());
}
}
} else {
// 授权了位置
List<Shop> shops = shopMapper.selectEnableShops(); // 获取所有非关闭的店铺
if (CollectionUtils.isEmpty(shops)) {
return null;
}
for (Shop s : shops) { // 获取所有店铺距离
double realDistance = DistanceUtil.getRealDistance(Double.parseDouble(lng), Double.parseDouble(lat), Double.parseDouble(s.getLng()), Double.parseDouble(s.getLat()));
s.setRealDistance(realDistance);
}
shops.sort(Comparator.comparing(Shop::getRealDistance)); // 按距离排序
for (Shop s : shops) {
if (perfectOrderState(s, lng, lat)) { // 是否能下单
shop = s;
}
}
if (shop == null) { // 没有能下单店铺选择最近的店铺
shop = shops.get(0);
}
}
if (shop.getOrderState() == null) {
perfectOrderState(shop, lng, lat);
}
return shop;
}
private boolean perfectOrderState(Shop shop, String lng, String lat) throws ParseException {
if (shop == null) {
return false;
}
// 状态已停止营业
if (shop.getState() != 1) {
shop.setOrderState(0);
shop.setStateDesc("门店已暂停营业");
return false;
}
// 不在营业时间范围内
if (!DateUtils.isEffectiveDate(shop.getStartTime(), shop.getEndTime())) {
shop.setOrderState(0);
shop.setStateDesc("门店已休息");
return false;
}
// 超出最大配送距离10KM 或 超出店铺设置最大距离
double realDistance = DistanceUtil.getRealDistance(Double.parseDouble(lng), Double.parseDouble(lat), Double.parseDouble(shop.getLng()), Double.parseDouble(shop.getLat()));
double realKm = realDistance / 1000;
double shopLimit = shop.getDistanceLimit() == null ? 0 : shop.getDistanceLimit();
if (realKm > 10 || realKm > shopLimit) {
shop.setOrderState(0);
shop.setStateDesc("距离较远,门店暂停接单");
return false;
}
shop.setOrderState(1);
return true;
}
public Shop getShop(LoginUser loginUser) {
if (loginUser == null) {
// 未登录,返回默认店铺
......
......@@ -32,6 +32,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
select id, name, code, province, city, zone from shop
</select>
<select id="selectEnableShops" resultMap="ShopResult">
select * from shop where `state` != 2
</select>
<select id="selectShopList" parameterType="Shop" resultMap="ShopResult">
select * from shop s left join machine m on s.id =m.shop_id
<where>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment