Просмотр исходного кода

提交司机管理初始化角色

chenxiaofei 2 месяцев назад
Родитель
Сommit
7831f9480f

+ 22 - 8
sckw-modules/sckw-fleet/src/main/java/com/sckw/fleet/service/KwfDriverService.java

@@ -8,7 +8,6 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.sckw.contract.api.RemoteContractService;
 import com.sckw.contract.api.model.vo.KwcContractLogisticsDto;
-import com.google.common.collect.Maps;
 import com.sckw.core.common.enums.enums.DictEnum;
 import com.sckw.core.common.enums.enums.DictTypeEnum;
 import com.sckw.core.common.enums.enums.ErrorCodeEnum;
@@ -45,16 +44,17 @@ import com.sckw.transport.api.dubbo.TransportRemoteService;
 import com.sckw.transport.api.model.param.CapacityTruckParam;
 import com.sckw.transport.api.model.vo.RWaybillOrderVo;
 import com.sckw.transport.api.model.vo.WaybillOrderTaskVO;
+import io.seata.spring.annotation.GlobalTransactional;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.dubbo.config.annotation.DubboReference;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Propagation;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.multipart.MultipartFile;
 
 import java.io.InputStream;
 import java.util.*;
-import java.util.function.Function;
 import java.util.stream.Collectors;
 
 /**
@@ -467,10 +467,12 @@ public class KwfDriverService {
 
     /**
      * @param params 参数
-     * @desc 新增司机
+     * @desc 新增司机(Seata 全局事务:车队库 + 远程 system 用户/角色库一致提交或回滚,需 Seata TC 与各服务数据源代理已就绪)
      * @author zk
      * @date 2023/7/6
      **/
+    @GlobalTransactional(name = "default_tx_group", rollbackFor = Exception.class)
+    @Transactional(rollbackFor = Exception.class)
     public HttpResult add(KwfDriverDto params) {
         /**司机信息**/
         KwfDriver driver = new KwfDriver();
@@ -492,30 +494,42 @@ public class KwfDriverService {
         KwfDriverCardDto driverCardDto = params.getDriverCard();
         if (driverCardDto != null) {
             driverCardDto.setDriverId(driver.getId());
-            driverCardEdit(driverCardDto);
+            requireBizSuccess(driverCardEdit(driverCardDto), "身份证信息");
         }
 
         /**司机驾驶证信息**/
         KwfDriverLicenseDto driverLicenseDto = params.getDriverLicense();
         if (driverLicenseDto != null) {
             driverLicenseDto.setDriverId(driver.getId());
-            driverLicenseEdit(driverLicenseDto);
+            requireBizSuccess(driverLicenseEdit(driverLicenseDto), "驾驶证信息");
         }
 
         /**司机从业资格证**/
         KwfDriverQualificationDto driverQualificationDto = params.getDriverQualification();
         if (driverQualificationDto != null) {
             driverQualificationDto.setDriverId(driver.getId());
-            driverQualificationEdit(driverQualificationDto);
+            requireBizSuccess(driverQualificationEdit(driverQualificationDto), "从业资格证信息");
         }
 
         /**车队班组绑定**/
-        driverFleetEdit(driver.getId(), params.getFleetId());
-        //同步信息到员工列表
+        requireBizSuccess(driverFleetEdit(driver.getId(), params.getFleetId()), "车队班组绑定");
+        //同步信息到员工列表(system 服务,参与同一全局事务)
         userEdit(params);
         return HttpResult.ok(result.getMsg(), driver);
     }
 
+    /**
+     * 全局事务下子步骤若仅用 {@link HttpResult} 表示失败而不抛异常,TM 仍会提交;失败时必须抛异常以驱动回滚。
+     */
+    private void requireBizSuccess(HttpResult result, String stepName) {
+        if (result == null || result.getCode() != HttpStatus.SUCCESS_CODE) {
+            String detail = result != null ? result.getMsg() : "未返回结果";
+            throw new SystemException(
+                    result != null ? result.getCode() : HttpStatus.CRUD_FAIL_CODE,
+                    stepName + "失败:" + detail);
+        }
+    }
+
     private void userEdit(KwfDriverDto params) {
         KwsUserReqDto kwsUserReqDto = new KwsUserReqDto();
         //密码为空时,以登录名作为密码

+ 73 - 0
sckw-modules/sckw-system/src/main/java/com/sckw/system/dubbo/RemoteUserServiceImpl.java

@@ -7,6 +7,7 @@ import com.github.pagehelper.PageInfo;
 import com.sckw.core.exception.SystemException;
 import com.sckw.core.model.base.BaseModel;
 import com.sckw.core.model.constant.Global;
+import com.sckw.core.utils.IdWorker;
 import com.sckw.core.model.page.PageHelperUtil;
 import com.sckw.core.model.page.PageResult;
 import com.sckw.core.utils.BeanUtils;
@@ -28,6 +29,7 @@ import com.sckw.system.model.vo.res.KwsUserSystemTypeVo;
 import com.sckw.system.service.*;
 import jakarta.annotation.Resource;
 import org.apache.dubbo.config.annotation.DubboService;
+import org.springframework.transaction.annotation.Transactional;
 
 import java.util.*;
 import java.util.stream.Collectors;
@@ -41,6 +43,8 @@ import java.util.stream.Collectors;
 @DubboService(group = "design", version = "1.0.0")
 public class RemoteUserServiceImpl implements RemoteUserService {
 
+    private static final String DRIVER_ROLE_NAME = "司机";
+
     @Resource
     private RemoteBaseService remoteBaseService;
 
@@ -74,6 +78,9 @@ public class RemoteUserServiceImpl implements RemoteUserService {
     @Resource
     private KwsUserDao userDao;
 
+    @Resource
+    private KwsRoleDao kwsRoleDao;
+
     @Resource
     private CommonService commonService;
 
@@ -471,6 +478,7 @@ public class RemoteUserServiceImpl implements RemoteUserService {
     }
 
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public void saveUser(KwsUserReqDto kwsUserReqDto) {
         KwsUser kwsUser = new KwsUser();
         BeanUtils.copyProperties(kwsUserReqDto, kwsUser);
@@ -485,6 +493,71 @@ public class RemoteUserServiceImpl implements RemoteUserService {
             kwsUser.setCreateTime(null);
             userDao.updateById(kwsUser);
         }
+        bindDriverUserRole(kwsUser, kwsUserReqDto);
+    }
+
+    /**
+     * 司机用户在 {@link KwsUser} 保存后关联企业下「司机」角色;若该企业尚不存在该角色则先初始化。
+     */
+    private void bindDriverUserRole(KwsUser kwsUser, KwsUserReqDto kwsUserReqDto) {
+        if (kwsUser.getId() == null || kwsUserReqDto.getEntId() == null) {
+            return;
+        }
+        KwsRole driverRole = kwsRoleDao.selectOne(Wrappers.<KwsRole>lambdaQuery()
+                .eq(KwsRole::getEntId, kwsUserReqDto.getEntId())
+                .eq(KwsRole::getName, DRIVER_ROLE_NAME)
+                .eq(KwsRole::getDelFlag, Global.NO)
+                .last("limit 1"));
+        if (driverRole == null) {
+            driverRole = initDriverRole(kwsUserReqDto);
+        }
+        List<KwsUserRole> userRoles = userRoleDao.findAllByUserId(kwsUser.getId());
+        if (!CollectionUtils.isEmpty(userRoles)) {
+            KwsRole finalDriverRole = driverRole;
+            boolean alreadyBound = userRoles.stream()
+                    .anyMatch(ur -> Objects.equals(ur.getRoleId(), finalDriverRole.getId()));
+            if (alreadyBound) {
+                return;
+            }
+        }
+        Date now = new Date();
+        KwsUserRole userRole = new KwsUserRole();
+        userRole.setId(new IdWorker(1L).nextId());
+        userRole.setUserId(kwsUser.getId());
+        userRole.setRoleId(driverRole.getId());
+        userRole.setRemark(kwsUserReqDto.getRemark());
+        userRole.setStatus(Global.NO);
+        userRole.setDelFlag(Global.NO);
+        userRole.setCreateBy(kwsUserReqDto.getCreateBy() != null ? kwsUserReqDto.getCreateBy() : -1L);
+        userRole.setCreateTime(kwsUserReqDto.getCreateTime() != null ? kwsUserReqDto.getCreateTime() : now);
+        userRole.setUpdateBy(kwsUserReqDto.getUpdateBy() != null ? kwsUserReqDto.getUpdateBy() : userRole.getCreateBy());
+        userRole.setUpdateTime(kwsUserReqDto.getUpdateTime() != null ? kwsUserReqDto.getUpdateTime() : now);
+        userRoleDao.insert(userRole);
+    }
+
+    /**
+     * 在当前企业下创建「司机」角色(admin_flag=2 非管理员),不绑定菜单; 相比为司机同步场景的最小初始化。
+     */
+    private KwsRole initDriverRole(KwsUserReqDto kwsUserReqDto) {
+        Date now = new Date();
+        KwsRole role = new KwsRole();
+        long roleId = new IdWorker(1L).nextId();
+        role.setId(roleId);
+        role.setEntId(kwsUserReqDto.getEntId());
+        role.setName(DRIVER_ROLE_NAME);
+        role.setAdminFlag(2);
+        role.setRemark("司机用户默认角色");
+        role.setStatus(Global.NO);
+        role.setDelFlag(Global.NO);
+        Long operator = kwsUserReqDto.getCreateBy() != null ? kwsUserReqDto.getCreateBy() : -1L;
+        role.setCreateBy(operator);
+        role.setCreateTime(kwsUserReqDto.getCreateTime() != null ? kwsUserReqDto.getCreateTime() : now);
+        role.setUpdateBy(kwsUserReqDto.getUpdateBy() != null ? kwsUserReqDto.getUpdateBy() : operator);
+        role.setUpdateTime(kwsUserReqDto.getUpdateTime() != null ? kwsUserReqDto.getUpdateTime() : now);
+        if (kwsRoleDao.insert(role) <= 0) {
+            throw new SystemException(HttpStatus.CRUD_FAIL_CODE, HttpStatus.INSERT_FAIL);
+        }
+        return role;
     }
 
     /**