master
jia 2024-03-03 10:02:00 +08:00
parent 934e15d2f4
commit 5b56774d44
21 changed files with 404 additions and 36 deletions

View File

@ -3,4 +3,5 @@ package com.bwie.common.constant;
public class ServerNameConstants {
public final static String SYSTEM_NAME="bwie-system";
public final static String Ord_NAME="bwie-ord";
}

View File

@ -3,22 +3,47 @@ package com.bwie.common.domain;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@TableName(value = "ord")
public class Ord {
@TableId(value = "ord_id",type = IdType.AUTO)
private Long ordId;
@TableField(value = "ord_hao")
private String ordHao;
@TableField("car_id")
private Long carId;
private Long goodsId;
private String goodsHao;
private String goodsName;
private String goodsBrand;
@TableField("ord_money")
private Integer ordMoney;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss" ,timezone ="GMT+8")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@TableField("ord_time")
private Date ordTime;
@TableField("ord_status")
private Integer ordStatus;
@TableField("ord_tui")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss" ,timezone ="GMT+8")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date ordTui;

View File

@ -0,0 +1,16 @@
package com.bwie.common.domain.request;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Pay {
private List<String> carIds;
private Integer total;
private String ordHao;
}

View File

@ -0,0 +1,32 @@
package com.bwie.common.domain.response;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class OrdRes {
private Long carId;
private String ordHao;
private Long goodsId;
private Integer goodsNum;
private Integer ordMoney;
private String goodsHao;
private String goodsName;
private String goodsBrand;
private Integer goodsXin;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss" ,timezone ="GMT+8")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date ordTime;
private Date ordTui;
}

View File

@ -0,0 +1,20 @@
package com.bwie.common.remote.ord;
import com.bwie.common.constant.ServerNameConstants;
import com.bwie.common.domain.request.Pay;
import com.bwie.common.remote.ord.factory.OrdRemoteFactory;
import com.bwie.common.result.Result;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
@FeignClient(
name = ServerNameConstants.Ord_NAME,
fallbackFactory = OrdRemoteFactory.class
)
public interface OrdRemoteService{
@PostMapping("addord")
public Result addord(@RequestBody Pay pay);
}

View File

@ -0,0 +1,24 @@
package com.bwie.common.remote.ord.factory;
import com.bwie.common.domain.request.Pay;
import com.bwie.common.remote.ord.OrdRemoteService;
import com.bwie.common.result.Result;
import lombok.extern.java.Log;
import lombok.extern.log4j.Log4j2;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;
@Component
@Log4j2
public class OrdRemoteFactory implements FallbackFactory<OrdRemoteService> {
@Override
public OrdRemoteService create(Throwable cause) {
return new OrdRemoteService() {
@Override
public Result addord(Pay pay) {
log.error("[{}-{}]添加订单远程调用错误",pay,cause.getMessage(),cause);
return Result.error();
}
};
}
}

View File

@ -3,4 +3,5 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.bwie.common.handler.GlobalExceptionHandle,\
com.bwie.common.config.RedisConfig,\
com.bwie.common.redis.RedisCache,\
com.bwie.common.remote.user.factory.UserRemoteFactory
com.bwie.common.remote.user.factory.UserRemoteFactory,\
com.bwie.common.remote.ord.factory.OrdRemoteFactory

View File

@ -54,7 +54,7 @@ public class AuthFilter implements GlobalFilter, Ordered {
if(null==claims){
return GatewayUtils.errorResponse(exchange,"token格式不正确");
}
String userKey = JwtUtils.getUserKey(claims);
String userKey = JwtUtils.getUserKey(first);
if(!redisCache.hasKey(TokenConstants.LOGIN_TOKEN_KEY+userKey)){
return GatewayUtils.errorResponse(exchange,"token过期");
}

View File

@ -4,10 +4,12 @@ import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@MapperScan("com.bwie.car.mapper")
@EnableFeignClients(basePackages = "com.bwie.**")
public class CarApp {
public static void main(String[] args) {
SpringApplication.run(CarApp.class);

View File

@ -6,6 +6,7 @@ import com.bwie.common.constant.TokenConstants;
import com.bwie.common.domain.Car;
import com.bwie.common.domain.Ord;
import com.bwie.common.domain.User;
import com.bwie.common.domain.request.Pay;
import com.bwie.common.domain.response.CarShow;
import com.bwie.common.redis.RedisCache;
import com.bwie.common.result.Result;
@ -23,6 +24,7 @@ public class CarController {
private final CarMapper carMapper;
private final HttpServletRequest request;
public CarController(CarService carService, RedisCache redisCache, CarMapper carMapper, HttpServletRequest request) {
this.carService = carService;
this.redisCache = redisCache;
@ -31,34 +33,47 @@ public class CarController {
}
@PostMapping("addcar")
public Result addcar(@RequestParam Long goodsId){
public Result addcar(@RequestParam Long goodsId) {
Car findgoods = carService.findgoods(goodsId, info().getUserId());
if(findgoods==null){
if (findgoods == null) {
Car build = Car.builder()
.userId(info().getUserId())
.goodsId(goodsId)
.goodsNum(1)
.build();
carService.save(build);
}else {
findgoods.setGoodsNum(findgoods.getGoodsNum()+1);
} else {
findgoods.setGoodsNum(findgoods.getGoodsNum() + 1);
carMapper.updateById(findgoods);
}
return Result.success();
}
@GetMapping("show")
public Result show(){
public Result show() {
List<CarShow> show = carService.show();
return Result.success(show);
}
@PostMapping("delcar")
public Result delcar(@RequestParam Long carId){
public Result delcar(@RequestParam Long carId) {
carService.delcar(carId);
return Result.success();
}
@PostMapping("updnum")
public Result updnum(@RequestBody CarShow carShow) {
carService.addnum(carShow);
return Result.success();
}
@PostMapping("pay")
public Result pay(@RequestBody Pay pay){
carService.pay(pay);
return Result.success();
}
public User info() {
String header = request.getHeader(TokenConstants.TOKEN);
String userKey = JwtUtils.getUserKey(header);
@ -66,9 +81,6 @@ public class CarController {
return cacheObject;
}
@GetMapping("showord")
public Result showord(){
List<Ord> show = carService.showord();
return Result.success(show);
}
}

View File

@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.bwie.car.CarApp;
import com.bwie.common.domain.Car;
import com.bwie.common.domain.Ord;
import com.bwie.common.domain.request.Pay;
import com.bwie.common.domain.response.CarShow;
import java.util.List;
@ -12,7 +13,8 @@ public interface CarMapper extends BaseMapper<Car> {
List<CarShow> show(Long userId);
List<Ord> showord(Long userId);
void updnum(CarShow carShow);
}

View File

@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.extension.service.IService;
import com.bwie.common.domain.Car;
import com.bwie.common.domain.Goods;
import com.bwie.common.domain.Ord;
import com.bwie.common.domain.request.Pay;
import com.bwie.common.domain.response.CarShow;
import java.util.List;
@ -14,5 +15,9 @@ public interface CarService extends IService<Car> {
List<CarShow> show();
void delcar(Long carId);
List<Ord> showord();
void addnum(CarShow carShow);
void pay(Pay pay);
}

View File

@ -7,13 +7,17 @@ import com.bwie.common.constant.TokenConstants;
import com.bwie.common.domain.Car;
import com.bwie.common.domain.Ord;
import com.bwie.common.domain.User;
import com.bwie.common.domain.request.Pay;
import com.bwie.common.domain.response.CarShow;
import com.bwie.common.redis.RedisCache;
import com.bwie.common.remote.ord.OrdRemoteService;
import com.bwie.common.utils.JwtUtils;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class CarServiceImpl extends ServiceImpl<CarMapper, Car> implements CarService {
@ -21,11 +25,13 @@ public class CarServiceImpl extends ServiceImpl<CarMapper, Car> implements CarSe
private final RedisCache redisCache;
private final CarMapper carMapper;
private final HttpServletRequest request;
private final OrdRemoteService ordRemoteService;
public CarServiceImpl(RedisCache redisCache, CarMapper carMapper, HttpServletRequest request) {
public CarServiceImpl(RedisCache redisCache, CarMapper carMapper, HttpServletRequest request, OrdRemoteService ordRemoteService) {
this.redisCache = redisCache;
this.carMapper = carMapper;
this.request = request;
this.ordRemoteService = ordRemoteService;
}
@Override
@ -48,9 +54,21 @@ public class CarServiceImpl extends ServiceImpl<CarMapper, Car> implements CarSe
carMapper.deleteById(carId);
}
@Override
public List<Ord> showord() {
return carMapper.showord(info().getUserId());
public void addnum(CarShow carShow) {
carMapper.updnum(carShow);
}
@Override
public void pay(Pay pay) {
ordRemoteService.addord(pay);
List<Integer> collect = pay.getCarIds().stream().map(c -> {
String[] split = c.split("-");
return Integer.valueOf(split[3]);
}).collect(Collectors.toList());
carMapper.deleteBatchIds(collect);
}
public User info() {

View File

@ -2,17 +2,16 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bwie.car.mapper.CarMapper">
<update id="updnum">
update car set goods_num=#{goodsNum} where car_id=#{carId}
</update>
<select id="show" resultType="com.bwie.common.domain.response.CarShow">
select c.*,g.* from car c
left join goods g on g.goods_id=c.goods_id
left join user u on u.user_id=c.user_id
where u.user_id=#{userId}
</select>
<select id="showord" resultType="com.bwie.common.domain.Ord">
select * from ord o
left join car c on c.car_id=o.car_id
select c.car_id,c.goods_num,c.user_id ,g.* from car c
left join goods g on g.goods_id=c.goods_id
left join user u on u.user_id=c.user_id
where u.user_id=#{userId}
</select>
</mapper>

View File

@ -0,0 +1,15 @@
package com.bwie.ord;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@MapperScan("com.bwie.ord.mapper")
@EnableDiscoveryClient
public class OrdApp {
public static void main(String[] args) {
SpringApplication.run(OrdApp.class);
}
}

View File

@ -0,0 +1,20 @@
package com.bwie.ord;
import java.util.Calendar;
import java.util.Date;
public class Test {
public static void main(String[] args) {
Date date = new Date();
System.out.println("date = " + date);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
// 增加两天
calendar.add(Calendar.DAY_OF_MONTH, 2);
// 获取增加后的日期
Date newDate = calendar.getTime();
System.out.println("date = " + newDate);
}
}

View File

@ -1,4 +1,48 @@
package com.bwie.ord.controller;
import com.bwie.common.domain.Ord;
import com.bwie.common.domain.request.Pay;
import com.bwie.common.domain.response.OrdRes;
import com.bwie.common.result.Result;
import com.bwie.ord.service.OrdService;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;
@RestController
@ResponseBody
public class OrdController {
private final OrdService ordService;
public OrdController(OrdService ordService) {
this.ordService = ordService;
}
@PostMapping("addord")
public Result addord(@RequestBody Pay pay){
ordService.addord(pay);
return Result.success();
}
@GetMapping("show")
public Result showord(){
List<OrdRes> show = ordService.showord();
return Result.success(show);
}
@PostMapping("tui")
public Result tui(@RequestParam Long ordId,Integer goodsXin){
ordService.tui(ordId,goodsXin);
return Result.success();
}
@GetMapping("showtui")
public Result showtui(){
List<Ord> show = ordService.showtui();
return Result.success(show);
}
}

View File

@ -1,4 +1,21 @@
package com.bwie.ord.mapper;
public interface OrdMapper {
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.bwie.common.domain.Ord;
import com.bwie.common.domain.request.Pay;
import com.bwie.common.domain.response.OrdRes;
import org.apache.ibatis.annotations.Param;
import java.util.Date;
import java.util.List;
public interface OrdMapper extends BaseMapper<Ord> {
void addord(@Param("pay") Pay pay, @Param("id") Integer id, @Param("price") Integer price, @Param("userId") Long userId, @Param("goodsId") Long goodsId);
List<Ord> showtui(Long userId);
List<OrdRes> showord(Long userId);
void tui(Long ordId, Date date);
}

View File

@ -1,4 +1,16 @@
package com.bwie.ord.service;
public interface OrdService {
import com.baomidou.mybatisplus.extension.service.IService;
import com.bwie.common.domain.Ord;
import com.bwie.common.domain.request.Pay;
import com.bwie.common.domain.response.OrdRes;
import java.util.List;
public interface OrdService extends IService<Ord> {
void addord(Pay pay);
List<OrdRes> showord();
List<Ord> showtui();
void tui(Long ordId, Integer goodsXin);
}

View File

@ -1,4 +1,78 @@
package com.bwie.ord.service;
public class OrdServiceImpl {
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.common.constant.TokenConstants;
import com.bwie.common.domain.Ord;
import com.bwie.common.domain.User;
import com.bwie.common.domain.request.Pay;
import com.bwie.common.domain.response.OrdRes;
import com.bwie.common.redis.RedisCache;
import com.bwie.common.utils.JwtUtils;
import com.bwie.ord.mapper.OrdMapper;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
@Service
public class OrdServiceImpl extends ServiceImpl<OrdMapper, Ord> implements OrdService{
private final OrdMapper ordMapper;
private final RedisCache redisCache;
private final HttpServletRequest request;
public OrdServiceImpl(OrdMapper ordMapper, RedisCache redisCache, HttpServletRequest request) {
this.ordMapper = ordMapper;
this.redisCache = redisCache;
this.request = request;
}
@Override
public void addord(Pay pay) {
String s = RandomUtil.randomNumbers(6);
pay.setOrdHao(s);
pay.getCarIds().stream().forEach(c->{
String[] split = c.split("-");
Integer id= Integer.valueOf(split[0]);
Integer price= Integer.valueOf(split[1]);
Long goodsId= Long.valueOf(split[2]);
ordMapper.addord(pay,id,price,info().getUserId(),goodsId);
});
}
@Override
public List<OrdRes> showord() {
return ordMapper.showord(info().getUserId());
}
@Override
public List<Ord> showtui() {
return ordMapper.showtui(info().getUserId());
}
@Override
public void tui(Long ordId, Integer goodsXin) {
Date date = new Date();
if(goodsXin==2){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
// 增加两天
calendar.add(Calendar.DAY_OF_MONTH, 2);
// 获取增加后的日期
date = calendar.getTime();
}
ordMapper.tui(ordId,date);
}
public User info() {
String header = request.getHeader(TokenConstants.TOKEN);
String userKey = JwtUtils.getUserKey(header);
User cacheObject = redisCache.getCacheObject(TokenConstants.LOGIN_TOKEN_KEY + userKey);
return cacheObject;
}
}

View File

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bwie.ord.mapper.OrdMapper">
<insert id="addord">
INSERT INTO `test_week1`.`ord`
(`ord_hao`, `goods_num`,`goods_sum`, `ord_money`, `ord_time`, `ord_status`, `ord_tui`,`goods_id`,`user_id`)
VALUES
(#{pay.ordHao}, #{id},#{price}, #{pay.total}, now(), 1, now(),#{goodsId},#{userId})
</insert>
<update id="tui">
update ord set ord_status=2 where ord_id=#{ordId},ord_tui=#{data}
</update>
<select id="showord" resultType="com.bwie.common.domain.response.OrdRes">
select * from ord o
left join goods g on g.goods_id=o.goods_id
where o.user_id=#{userId}
</select>
<select id="showtui" resultType="com.bwie.common.domain.Ord">
select * from ord o
left join goods g on g.goods_id=o.goods_id
where o.user_id=#{userId} and o.ord_status=2
</select>
</mapper>