提交 e9ae1796 authored 作者: 吴德鹏's avatar 吴德鹏

update activity

上级 1374b5ae
package com.example.afrishop_v3.controllers;
import com.example.afrishop_v3.base.Result;
import com.example.afrishop_v3.enums.ResultCodeEnum;
import com.example.afrishop_v3.models.Activity;
import com.example.afrishop_v3.models.TbCfStationItem;
import com.example.afrishop_v3.repository.ActivityRepository;
import com.example.afrishop_v3.repository.TbCfStationItemRepository;
import org.checkerframework.checker.units.qual.A;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
/**
......@@ -22,9 +30,11 @@ import java.util.Optional;
public class ActivityController {
private final ActivityRepository activityRepository;
private final TbCfStationItemRepository itemRepository;
public ActivityController(ActivityRepository activityRepository) {
public ActivityController(ActivityRepository activityRepository, TbCfStationItemRepository itemRepository) {
this.activityRepository = activityRepository;
this.itemRepository = itemRepository;
}
@GetMapping("/getAllActivity")
......@@ -40,5 +50,76 @@ public class ActivityController {
return new Result(activityRepository.findById(id));
}
//查询商品活动
@GetMapping("item/{itemId}")
public Result getItemActivity(@PathVariable("itemId") String itemId) throws ParseException {
Result result = new Result();
Activity activity = null;
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//全场活动
if (activityRepository.existsByUseType(1)) {
Optional<Activity> fullAct = activityRepository.findFirstByUseType(1);
if (fullAct.isPresent()) {
activity = fullAct.get();
}
}
//商品分类活动
if (activityRepository.existsByUseType(2)) {
Optional<TbCfStationItem> byId = itemRepository.findById(itemId);
if (byId.isPresent()) {
TbCfStationItem item = byId.get();
String category1 = item.getItemCategory();
String category2 = item.getItemCategorytwo();
String category3 = item.getItemDescritionId();
String con2 = category1 + "," + category2;
String con3 = category1 + "," + category2 + "," + category3;
List<Activity> activityList = activityRepository.findAllByUseType(2);
for (Activity act2 : activityList) {
boolean condition = category1.equals(act2.getCategoryId()) || con2.equals(act2.getCategoryId()) || con3.equals(act2.getCategoryId());
if (condition)
activity = act2;
break;
}
}
}
//特定商品
if (activityRepository.existsByUseType(3)) {
List<Activity> list = activityRepository.findAllByUseType(3);
for (Activity act3 : list) {
String[] itemIdArr = act3.getItemIds().split(",");
List<String> idList = Arrays.asList(itemIdArr);
if (idList.contains(itemId))
activity = act3;
break;
}
}
if (activity != null) {
Date endTime = df.parse(activity.getEndTime().toString());
Date now = df.parse(df.format(new Date()));
long l = endTime.getTime() - now.getTime();
long day = l / (24 * 60 * 60 * 1000);
long hour = (l / (60 * 60 * 1000) - day * 24);
long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
activity.setDeadline(day + ":" + getTime(hour) + ":" + getTime(min) + ":" + getTime(s));
result.setData(activity);
return result;
}
result.setCode(ResultCodeEnum.QUERY_ERROR.getCode());
result.setMessage("No online activity");
return result;
}
public String getTime(long time) {
String t = String.valueOf(time);
if (time < 10) {
t = "0" + time;
}
return t;
}
}
......@@ -100,7 +100,8 @@ public class OrderController extends Controller {
@RequestParam("itemNum") Integer itemNum,
@RequestParam(value = "itemSku", defaultValue = "") String itemSku,
@RequestParam(value = "itemSkuId", required = false) String itemSkuId,
@RequestParam(value = "toitableId", required = false) String toitableId) {
@RequestParam(value = "toitableId", required = false) String toitableId,
@RequestParam(value = "open", required = false) boolean open) {
TbCfOrder order = new TbCfOrder();
......@@ -140,7 +141,7 @@ public class OrderController extends Controller {
order.setCoupon(coupon);
}
}
order.setOpen(open);
order.setCouponMap(map);
order.getItemOrderListFromCartList(list, itemRepository,activityRepository);
......@@ -148,7 +149,8 @@ public class OrderController extends Controller {
}
@PostMapping("/settle")
public Result<TbCfOrder> settleAccount(@RequestBody String[] ids, @RequestParam(value = "toitableId", required = false) String toitableId) {
public Result<TbCfOrder> settleAccount(@RequestBody String[] ids, @RequestParam(value = "toitableId", required = false) String toitableId,
@RequestParam(value = "open", required = false) boolean open) {
//String userId = user.userId();
List<TbCfCartRecordR> allByUserId = cartRepository.findAllByCartRecordIdIn(ids);
BigDecimal orderPrice = BigDecimal.ZERO;
......@@ -180,7 +182,7 @@ public class OrderController extends Controller {
}
}
order.setOpen(open);
order.getItemOrderListFromCartList(allByUserId, itemRepository,activityRepository);
......@@ -202,7 +204,8 @@ public class OrderController extends Controller {
@RequestParam(value = "itemNum", required = false) Integer itemNum,
@RequestParam(value = "itemSku", required = false) String itemSku,
@RequestParam(value = "itemSkuId", required = false) String itemSkuId,
@RequestParam(value = "orderSource", required = false) Integer orderSource
@RequestParam(value = "orderSource", required = false) Integer orderSource,
@RequestParam(value = "open", required = false) boolean open
) throws IOException, URISyntaxException, ExecutionException, InterruptedException, TimeoutException {
TbCfUserInfo user = this.user.user();
String userId = user.getUserId();
......@@ -322,7 +325,7 @@ public class OrderController extends Controller {
order.setOrderSource(orderSource);
}
order.setOpen(open);
order.getItemOrderListFromCartList(allByUserId, itemRepository,activityRepository);
TbCfOrder save = repository.save(order);
......
......@@ -84,4 +84,9 @@ public class Activity implements Serializable {
*/
private Date updateTime;
/**
* 距离活动截至时间
*/
@Transient
private String deadline;
}
......@@ -167,6 +167,8 @@ public class TbCfOrder {
private Map<String, Object> couponMap;
@Transient
private boolean open = false;
public Map<String, Object> getCouponMap() {
return couponMap;
......@@ -335,12 +337,13 @@ public class TbCfOrder {
expressCost = expressCost.add(express.getExpressFee().add(express.getContinuationFee().multiply(new BigDecimal(extralNum))));
}
}
System.out.println("活动是否开启:" + this.open);
/**
* 活动模块:活动类型 1:满减 2:满折 3:满件打折(type)
* 使用类型 1:全场 2:分类商品 3:特定商品(use_type)
* 使用类型 1:全场 2:分类商品 3:特定商品(use_type)
*/
boolean fullAct = activityRepository.existsByUseType(1);
if (fullAct) {
if (this.open && fullAct) {
Optional<Activity> allAct = activityRepository.findFirstByUseType(1);
if (allAct.isPresent()) {
Activity activity = allAct.get();
......
......@@ -7,6 +7,7 @@ import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.relational.core.sql.In;
import javax.annotation.Nullable;
import java.util.List;
import java.util.Optional;
......@@ -15,13 +16,18 @@ import java.util.Optional;
* @Date: 2020/12/02
* @Description:
*/
public interface ActivityRepository extends JpaRepository<Activity,String> {
public interface ActivityRepository extends JpaRepository<Activity, String> {
@Query(value = "select a from Activity a where CURRENT_TIMESTAMP between startTime and endTime and status=1 order by a.createTime desc")
Page<Activity> getAllActivity(Pageable pageable);
boolean existsByUseType(Integer useType);
@Query(value = "select * from Activity a where CURRENT_TIMESTAMP between a.start_time and a.end_time and a.status=1 order by a.create_time desc limit 1",nativeQuery = true)
@Query(value = "select * from Activity a where CURRENT_TIMESTAMP between a.start_time and a.end_time and a.status=1 and use_type=:useType order by a.create_time desc limit 1", nativeQuery = true)
Optional<Activity> findFirstByUseType(Integer useType);
@Query(value = "select * from Activity a where CURRENT_TIMESTAMP between a.start_time and a.end_time and a.status=1 and use_type=:useType", nativeQuery = true)
List<Activity> findAllByUseType(Integer useType);
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论