phpcjl
2024-12-02 3a2f3beb47ba2ffc6862c9d089e35e1dda9040ff
ruoyi-service/ruoyi-other/src/main/java/com/ruoyi/other/service/impl/TechnicianSubscribeServiceImpl.java
@@ -1,18 +1,18 @@
package com.ruoyi.other.service.impl;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.redis.annotation.DistributedLock;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.other.api.domain.Technician;
import com.ruoyi.other.api.domain.TechnicianSubscribe;
import com.ruoyi.other.enums.TechnicianErrorCode;
import com.ruoyi.other.enums.TechnicianStatus;
import com.ruoyi.other.mapper.TechnicianMapper;
import com.ruoyi.other.mapper.TechnicianSubscribeMapper;
import com.ruoyi.other.service.TechnicianSubscribeService;
import com.ruoyi.other.vo.TechnicianSubscribeVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.time.LocalDateTime;
@@ -43,25 +43,22 @@
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void subscribe(TechnicianSubscribe subscribe) {
    @DistributedLock(lockNamePre = "#technician_subscribe_lock", lockNamePost = "#technicianId")
    public void subscribe(TechnicianSubscribe subscribe, Long technicianId) {
        Long count = technicianSubscribeMapper.selectCount(new LambdaQueryWrapper<TechnicianSubscribe>()
                .eq(TechnicianSubscribe::getTechnicianId, technicianId)
                .eq(TechnicianSubscribe::getSubscribeTime, subscribe.getSubscribeTime())
                .eq(TechnicianSubscribe::getStatus, TechnicianStatus.UNSUBSCRIBE.getCode()));
        if (count > 0) {
            throw new ServiceException("当前时间段已被预约", TechnicianErrorCode.TECHNICIAN_ALREADY_SUBSCRIBED.getCode());
        }
        // 创建技师预约单
        Long technicianId = subscribe.getTechnicianId();
        Long userId = SecurityUtils.getUserId();
        subscribe.setAppUserId(userId);
        subscribe.setStatus(TechnicianStatus.UNSUBSCRIBE.getCode());
        subscribe.setDelFlag(0);
        subscribe.setCreateTime(LocalDateTime.now());
        technicianSubscribeMapper.insert(subscribe);
        // 更新技师状态
        UpdateWrapper<Technician> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", technicianId);
        updateWrapper.eq("subscribe_status", 1);
        updateWrapper.eq("status", 2);
        updateWrapper.set("subscribe_status", 2);
        updateWrapper.set("create_time", LocalDateTime.now());
        int update = technicianMapper.update(null, updateWrapper);
        if (update == 0){
            throw new ServiceException("该技师已被预约", TechnicianErrorCode.TECHNICIAN_ALREADY_SUBSCRIBED.getCode());
        }
    }
}