guohongjin
2024-04-24 a242c960b790e72fd6a73f35111824e21dbd350c
添加分布式锁
3个文件已修改
80 ■■■■ 已修改文件
rest/pom.xml 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
rest/src/main/java/cn/stylefeng/rest/config/ObjectRedisAutoConfiguration.java 24 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
rest/src/main/java/cn/stylefeng/rest/modular/user/controller/CounsellingInfoController.java 51 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
rest/pom.xml
@@ -295,6 +295,11 @@
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
        <dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson-spring-boot-starter</artifactId>
            <version>3.14.0</version>
        </dependency>
    </dependencies>
rest/src/main/java/cn/stylefeng/rest/config/ObjectRedisAutoConfiguration.java
@@ -3,6 +3,10 @@
import cn.stylefeng.rest.config.cache.ObjectRedisCache;
import cn.stylefeng.roses.kernel.cache.api.CacheOperatorApi;
import cn.stylefeng.roses.kernel.cache.redis.util.CreateRedisTemplateUtil;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -22,4 +26,24 @@
        return new ObjectRedisCache(redisTemplate);
    }
    @Value("${spring.redis.host}")
    private String host;
    @Value("${spring.redis.port}")
    private int port;
    @Value("${spring.redis.password}")
    private String password;
    @Value("${spring.redis.database}")
    private int database;
    @Bean
    public RedissonClient redissonClient(){
        Config config = new Config();
        config.useSingleServer().setAddress("redis://"+host+":"+port)
                .setPassword(password)
                .setDatabase(database);
        return Redisson.create(config);
    }
}
rest/src/main/java/cn/stylefeng/rest/modular/user/controller/CounsellingInfoController.java
@@ -48,6 +48,8 @@
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
@@ -60,6 +62,7 @@
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;
@@ -125,6 +128,9 @@
    private String refundAlipayUrl;
    @Value("${refund.wxpay-url}")
    private String refundWxpayUrl;
    @Resource
    private RedissonClient redissonClient;
@@ -582,6 +588,7 @@
    @ApiOperation("咨询订单预约")
    @PostResource(name = "咨询订单预约", path = "/counsellingOrderReservation/saveCounsellingOrderReservation")
    public ResponseData<CounsellingOrderReservation> saveCounsellingOrderReservation(@RequestBody CounsellingReservationRequest counsellingReservationRequest) throws ParseException {
        //查询咨询师当天是否取消预约
        CounsellingSpecialTimeConfig specialTimeConfig = this.counsellingSpecialTimeConfigService.getOne(new LambdaQueryWrapper<CounsellingSpecialTimeConfig>().eq(CounsellingSpecialTimeConfig::getCounsellingInfoId,counsellingReservationRequest.getCounsellingId())
                .eq(CounsellingSpecialTimeConfig::getSpecialDay,counsellingReservationRequest.getDayTime()).eq(CounsellingSpecialTimeConfig::getIsCancelDay,1));
@@ -617,6 +624,13 @@
        if (counsellingOrder == null){
            throw new ServiceException("没有在咨询的订单,无法进行预约!");
        }
        RLock lock = redissonClient.getLock("counsel:" + counsellingReservationRequest.getCounsellingId()+"_"+counsellingReservationRequest.getDayTime()+"_"+counsellingReservationRequest.getTimePoint());
        boolean tryLock = false;
        try {
            tryLock = lock.tryLock(10, TimeUnit.SECONDS);
            if (!tryLock) {
                throw new ServiceException("当前时间段已预约,请选择其他时间段!");
            }
        //验证未支付的首次咨询订单
        long counselllingCount = this.counsellingOrderService.count(new LambdaQueryWrapper<CounsellingOrder>().eq(CounsellingOrder::getFirstAppointmentDate,counsellingReservationRequest.getDayTime())
@@ -674,10 +688,7 @@
            this.counsellingOrderService.updateById(counsellingOrder);
            this.counsellingUserService.updateById(counsellingUserOld);
            CounsellingInfo counsellingInfo = this.counsellingInfoService.getById(counsellingOrder.getCounsellingInfoId());
            //将此条消息加入到可聊天的表中t_mental_appointment
            MentalAppointment mentalAppointment = MentalAppointment.builder()
@@ -770,24 +781,32 @@
            customerService.updateCustomerRemoveCache(customer);
        }
        try {
            try {
        CustomerUpdateRequest customerUpdateRequest = counsellingReservationRequest.getCustomerUpdateRequest();
        Customer customer = new Customer();
        BeanUtil.copyProperties(customerUpdateRequest,customer);
            LoginUser loginUser = LoginContext.me().getLoginUser();
            customer.setCustomerId(loginUser.getUserId());
        customerService.updateById(customer);
        }catch (Exception e){
            e.printStackTrace();
            log.info("编辑用户报错");
            CustomerUpdateRequest customerUpdateRequest = counsellingReservationRequest.getCustomerUpdateRequest();
            Customer customer = new Customer();
            BeanUtil.copyProperties(customerUpdateRequest,customer);
                LoginUser loginUser = LoginContext.me().getLoginUser();
                customer.setCustomerId(loginUser.getUserId());
            customerService.updateById(customer);
            }catch (Exception e){
                e.printStackTrace();
                log.info("编辑用户报错");
            }
            return new SuccessResponseData<>(counsellingOrderReservation);
        }catch (Exception ex){
            log.error("咨询预约服务异常",ex.getStackTrace());
            throw new ServiceException("服务异常,请稍后再试");
        }finally {
            if(tryLock){
                lock.unlock();
            }
        }
        return new SuccessResponseData<>(counsellingOrderReservation);
    }
  }
    @ApiOperation("根据咨询订单id查询咨询信息")
   @ApiOperation("根据咨询订单id查询咨询信息")
    @GetResource(name = "根据咨询订单id查询咨询信息", path = {"/counsellingOrder/getCounsellingOrderInfoById","/worker/counsellingOrder/getCounsellingOrderInfoById"})
    public ResponseData<CounsellingOrderResponseDTO> getCounsellingOrderInfoById(Long counsellingOrderId){
        CounsellingOrder counsellingOrder = this.counsellingOrderService.getById(counsellingOrderId);