TestBizService.java 3.54 KB
package cn.fw.dalaran.server.controller.web;

import cn.fw.attendance.sdk.api.AttendanceApi;
import cn.fw.attendance.sdk.api.StaffStatusApi;
import cn.fw.attendance.sdk.api.dto.UsersScheduleDetailsDto;
import cn.fw.attendance.sdk.api.result.UserStatusVo;
import cn.fw.attendance.sdk.api.result.UsersScheduleDetailsVo;
import cn.fw.dalaran.domain.db.Account;
import cn.fw.dalaran.domain.enums.PlatformEnum;
import cn.fw.dalaran.service.data.AccountService;
import cn.fw.data.base.domain.common.Message;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author wmy3969
 * @version 1.0
 * @date 2022/10/12 0:23
 * @Description
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class TestBizService {

    /*
     * A(不+事务注解)中调用B(+事务注解):
     *      A无事务, B无事务
     *
     * A(+事务注解)中调用B(+事务注解):
     *      A有事务, B无事务
     *
     * 解决方案:
     *      1: bean中自己注入自己, 让A中调用B方法使用注入的自己去调用B方法(testBizService.methodB(), 而不是this.methodB())
     *      2: 配置类上声明@EnableAspectJAutoProxy(exposeProxy = true), 然后业务方法((TestBizService) AopContext.currentProxy()).methodB()
     * AopContext.currentProxy()其实就是获取的代理对象, bean中注入的自己也是代理对象
     */

    private TestBizService testBizService;
    private final StaffStatusApi staffStatusApi;
    private final AttendanceApi attendanceApi;

    @Autowired
    public void inject(TestBizService testBizService) {
        this.testBizService = testBizService;
    }

    private final AccountService accountService;

    @Transactional(rollbackFor = Exception.class)
    public void methodA() {
        Message<UserStatusVo> details = staffStatusApi.getUserStatusDetails(Arrays.asList(760L, 1545L, 1486L, 1342L), 1611975222000L, 1675047222937L);
        UsersScheduleDetailsDto dto = new UsersScheduleDetailsDto();
        dto.setGroupId(2L);
        dto.setUserIds(Arrays.asList(760L, 1545L, 1486L, 1342L));
        dto.setStartTime(new Date(1646818372000L));
        dto.setEndTime(new Date(1678354372000L));
        Message<List<UsersScheduleDetailsVo>> scheduleDetails = attendanceApi.getUsersScheduleDetails(dto);
        Account account = new Account();
        account.setAccount("methodA");
        account.setYn(Boolean.FALSE);
        account.setGroupId(2L);
        account.setType(PlatformEnum.DY);
        account.setUserId(1545L);
        account.setShopId(11L);
        accountService.save(account);
        try {
            //this.methodB();
            //testBizService.methodB();
            //testBizService1.methodB();
            ((TestBizService) AopContext.currentProxy()).methodB();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //int i = 1 / 0;
    }

    @Transactional(rollbackFor = Exception.class/*, propagation = Propagation.REQUIRES_NEW*/)
    public void methodB() {
        Account account = new Account();
        account.setAccount("methodB");
        account.setYn(Boolean.FALSE);
        account.setGroupId(2L);
        account.setType(PlatformEnum.DY);
        account.setUserId(1545L);
        account.setShopId(11L);
        accountService.save(account);
        int i = 1 / 0;
    }
}