| | |
| | | package cn.iocoder.yudao.module.ums.service.member; |
| | | |
| | | import cn.hutool.core.util.StrUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
| | | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| | | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| | | import cn.iocoder.yudao.module.archive.api.document.ArchiveDocumentApi; |
| | | import cn.iocoder.yudao.module.archive.api.document.dto.ArchiveDocumentCreateReqDTO; |
| | | import cn.iocoder.yudao.module.archive.api.document.dto.ArchiveDocumentRespDTO; |
| | | import cn.iocoder.yudao.module.system.api.storage.StorageAttachmentApi; |
| | | import cn.iocoder.yudao.module.system.api.storage.dto.StorageBlobRespDTO; |
| | | import cn.iocoder.yudao.module.system.api.user.AdminUserApi; |
| | | import cn.iocoder.yudao.module.system.api.user.dto.AdminUserCreateReqDTO; |
| | | import cn.iocoder.yudao.module.system.service.social.SocialUserService; |
| | | import cn.iocoder.yudao.module.system.service.permission.RoleService; |
| | | import cn.iocoder.yudao.module.system.dal.dataobject.permission.RoleDO; |
| | | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| | | import cn.iocoder.yudao.module.ums.controller.admin.member.vo.UmsMemberPageReqVO; |
| | | import cn.iocoder.yudao.module.ums.controller.admin.member.vo.UmsMemberSaveReqVO; |
| | | import cn.iocoder.yudao.module.ums.dal.dataobject.application.UmsApplicationDO; |
| | | import cn.iocoder.yudao.module.ums.dal.dataobject.member.UmsMemberDO; |
| | | import cn.iocoder.yudao.module.ums.dal.mysql.application.UmsApplicationMapper; |
| | | import cn.iocoder.yudao.module.ums.dal.mysql.member.UmsMemberMapper; |
| | | import cn.iocoder.yudao.module.ums.dal.redis.UmsNoRedisDAO; |
| | | import jakarta.annotation.Resource; |
| | |
| | | |
| | | import java.time.LocalDate; |
| | | import java.util.Collections; |
| | | import java.util.List; |
| | | import java.util.Set; |
| | | import java.util.UUID; |
| | | import java.util.stream.Collectors; |
| | | |
| | | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | import static cn.iocoder.yudao.module.ums.enums.ErrorCodeConstants.*; |
| | |
| | | */ |
| | | private static final Long MEMBER_DEPT_ID = 129L; |
| | | |
| | | /** |
| | | * 企业微信扫码用户角色编码;按编码查询,避免不同环境角色编号不一致。 |
| | | */ |
| | | private static final String MEMBER_ROLE_CODE = "wxcp_user"; |
| | | |
| | | public static final String MEMBER_RECORD_TYPE = "ums_member"; |
| | | |
| | | /** 会员状态:正常(可归档) */ |
| | | private static final Integer MEMBER_STATUS_NORMAL = 1; |
| | | |
| | | /** 归档来源类型:3-会员(对应 archive 模块 source_type) */ |
| | | private static final Integer ARCHIVE_SOURCE_TYPE_MEMBER = 3; |
| | | |
| | | /** 归档密级:2-内部 */ |
| | | private static final Integer ARCHIVE_SECRET_LEVEL_INTERNAL = 2; |
| | | |
| | | /** 会员档案分类编码(对应 archive_category.code) */ |
| | | private static final String ARCHIVE_CATEGORY_CODE_MEMBER = "member"; |
| | | |
| | | @Resource |
| | | private UmsMemberMapper memberMapper; |
| | | @Resource |
| | | private UmsApplicationMapper applicationMapper; |
| | | @Resource |
| | | private SocialUserService socialUserService; |
| | | @Resource |
| | | private RoleService roleService; |
| | | |
| | | @Resource |
| | | private UmsNoRedisDAO noRedisDAO; |
| | |
| | | @Resource |
| | | private StorageAttachmentApi storageAttachmentApi; |
| | | |
| | | @Resource |
| | | private ArchiveDocumentApi archiveDocumentApi; |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public UmsMemberDO createMemberFromApplication(UmsApplicationDO application) { |
| | | UmsMemberDO existing = memberMapper.selectByApplicationId(application.getId()); |
| | | if (existing != null) return existing; |
| | | // 1. 生成会员编号 |
| | | String no = noRedisDAO.generate(UmsNoRedisDAO.MEMBER_NO_PREFIX); |
| | | if (memberMapper.selectByNo(no) != null) { |
| | |
| | | |
| | | // 2. 创建系统用户账号 |
| | | Long userId = createSystemUser(application); |
| | | if (StrUtil.isNotBlank(application.getWxcpOpenid())) { |
| | | socialUserService.bindApprovedWxcpUser(application.getWxcpOpenid(), userId); |
| | | } |
| | | |
| | | // 3. 创建会员记录 |
| | | UmsMemberDO member = UmsMemberDO.builder() |
| | |
| | | .applicationId(application.getId()) |
| | | .memberType(application.getMemberType()) |
| | | .memberGrade(application.getMemberGrade()) |
| | | .feePaid(application.getFeePaid()) |
| | | .passwordSetupRequired(StrUtil.isBlank(application.getContactPhone())) |
| | | .name(application.getApplicantName()) |
| | | .contactPhone(application.getContactPhone()) |
| | | .contactEmail(application.getContactEmail()) |
| | |
| | | |
| | | // 会员等级与类型匹配校验 |
| | | validateMemberGrade(createReqVO.getMemberType(), createReqVO.getMemberGrade()); |
| | | validateFeePaid(createReqVO); |
| | | |
| | | UmsMemberDO member = BeanUtils.toBean(createReqVO, UmsMemberDO.class, in -> in |
| | | .setNo(no) |
| | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void updateMember(UmsMemberSaveReqVO updateReqVO) { |
| | | validateMemberExists(updateReqVO.getId()); |
| | | UmsMemberDO existingMember = validateMemberExists(updateReqVO.getId()); |
| | | validateMemberGrade(updateReqVO.getMemberType(), updateReqVO.getMemberGrade()); |
| | | validateFeePaid(updateReqVO); |
| | | |
| | | // 取消「已缴纳会费」时需要把缴费年度真正清空(updateById 会忽略 null 字段) |
| | | boolean clearFeeYear = Boolean.FALSE.equals(updateReqVO.getFeePaid()); |
| | | if (clearFeeYear) { |
| | | updateReqVO.setFeeYear(null); |
| | | } |
| | | |
| | | UmsMemberDO updateObj = BeanUtils.toBean(updateReqVO, UmsMemberDO.class); |
| | | memberMapper.updateById(updateObj); |
| | | if (existingMember.getApplicationId() != null && updateReqVO.getFeePaid() != null) { |
| | | applicationMapper.updateById(new UmsApplicationDO() |
| | | .setId(existingMember.getApplicationId()).setFeePaid(updateReqVO.getFeePaid())); |
| | | } |
| | | if (clearFeeYear) { |
| | | memberMapper.update(null, new LambdaUpdateWrapper<UmsMemberDO>() |
| | | .eq(UmsMemberDO::getId, updateReqVO.getId()) |
| | | .set(UmsMemberDO::getFeeYear, null)); |
| | | } |
| | | |
| | | storageAttachmentApi.updateAttachments("file", MEMBER_RECORD_TYPE, |
| | | updateReqVO.getId(), updateReqVO.getBlobIds()); |
| | |
| | | return memberMapper.selectPage(pageReqVO); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public String archiveMember(Long id) { |
| | | // 1.1 校验会员存在 |
| | | UmsMemberDO member = validateMemberExists(id); |
| | | // 1.2 只有「正常」状态的会员才能归档 |
| | | if (!MEMBER_STATUS_NORMAL.equals(member.getStatus())) { |
| | | throw exception(MEMBER_ARCHIVE_FAIL_STATUS, member.getNo(), memberStatusName(member.getStatus())); |
| | | } |
| | | // 1.3 重复归档校验 |
| | | if (member.getArchiveDocumentId() != null) { |
| | | throw exception(MEMBER_ALREADY_ARCHIVED, member.getNo(), |
| | | StrUtil.blankToDefault(archiveNo(id), "未知")); |
| | | } |
| | | |
| | | // 2. 带入会员材料附件,按来源单据归档 |
| | | List<Long> blobIds = storageAttachmentApi.listAttachments(MEMBER_RECORD_TYPE, id).stream() |
| | | .map(StorageBlobRespDTO::getId).collect(Collectors.toList()); |
| | | Long archiveDocumentId = archiveDocumentApi.archiveBySource(new ArchiveDocumentCreateReqDTO() |
| | | .setSourceType(ARCHIVE_SOURCE_TYPE_MEMBER) |
| | | .setSourceId(id) |
| | | .setSourceNo(member.getNo()) |
| | | .setSourceTitle(member.getName()) |
| | | .setCategoryCode(ARCHIVE_CATEGORY_CODE_MEMBER) |
| | | .setSecretLevel(ARCHIVE_SECRET_LEVEL_INTERNAL) |
| | | .setSummary("会员材料归档") |
| | | .setBlobIds(blobIds)).getCheckedData(); |
| | | |
| | | // 3. 回写归档档案 ID(作为已归档硬标记) |
| | | memberMapper.updateById(new UmsMemberDO().setId(id).setArchiveDocumentId(archiveDocumentId)); |
| | | return StrUtil.blankToDefault(archiveNo(id), "未知"); |
| | | } |
| | | |
| | | // =================== 内部方法 =================== |
| | | |
| | | private Long createSystemUser(UmsApplicationDO application) { |
| | | // 手机号可作为用户名/密码(参考 HRM 员工模式) |
| | | String mobile = StrUtil.blankToDefault(application.getContactPhone(), "unknown"); |
| | | String username = mobile; |
| | | String nickname = application.getApplicantName(); |
| | | String mobile = StrUtil.trim(application.getContactPhone()); |
| | | if ((StrUtil.isBlank(mobile) && StrUtil.isBlank(application.getWxcpOpenid())) |
| | | || (StrUtil.isNotBlank(mobile) && !mobile.matches("^1[3-9]\\d{9}$"))) { |
| | | throw exception(APPLICATION_WXCP_INFO_REQUIRED); |
| | | } |
| | | RoleDO role = roleService.getRoleByCode(MEMBER_ROLE_CODE); |
| | | if (role == null || CommonStatusEnum.isDisable(role.getStatus())) { |
| | | throw exception(MEMBER_WXCP_ROLE_NOT_AVAILABLE); |
| | | } |
| | | boolean hasMobile = StrUtil.isNotBlank(mobile); |
| | | String username = hasMobile ? mobile : "wx" + UUID.randomUUID().toString().replace("-", "").substring(0, 16); |
| | | String password = hasMobile ? mobile : UUID.randomUUID().toString().replace("-", "").substring(0, 16); |
| | | String nickname = StrUtil.maxLength(application.getApplicantName(), 30); |
| | | |
| | | try { |
| | | return adminUserApi.createUser(new AdminUserCreateReqDTO() |
| | |
| | | .setNickname(nickname) |
| | | .setDeptId(MEMBER_DEPT_ID) |
| | | .setPostIds(Collections.emptySet()) |
| | | .setMobile(mobile) |
| | | .setPassword(mobile) // 默认密码为手机号 |
| | | .setRoleIds(Collections.singleton(role.getId())) |
| | | .setMobile(hasMobile ? mobile : null) |
| | | .setPassword(password) |
| | | .setEmail(application.getContactEmail()) |
| | | .setRemark("由入会申请自动创建:" + application.getNo())); |
| | | } catch (Exception e) { |
| | |
| | | /** |
| | | * 校验会员等级与类型匹配:单位会员(type=2)不可选个人专有等级「5-理事长」 |
| | | */ |
| | | /** 已缴纳会费时必须填写缴费年度 */ |
| | | private void validateFeePaid(UmsMemberSaveReqVO reqVO) { |
| | | if (Boolean.TRUE.equals(reqVO.getFeePaid()) && reqVO.getFeeYear() == null) { |
| | | throw exception(MEMBER_FEE_YEAR_REQUIRED); |
| | | } |
| | | } |
| | | |
| | | private void validateMemberGrade(Integer memberType, String memberGrade) { |
| | | if (memberType == null || StrUtil.isBlank(memberGrade)) { |
| | | return; |
| | |
| | | return member; |
| | | } |
| | | |
| | | } |
| | | /** 会员状态中文名:1-正常 0-暂停(用于归档失败提示) */ |
| | | private String memberStatusName(Integer status) { |
| | | if (status == null) { |
| | | return "未知"; |
| | | } |
| | | return MEMBER_STATUS_NORMAL.equals(status) ? "正常" : "暂停"; |
| | | } |
| | | |
| | | /** 查询来源单据已生成的档案编号(未归档时返回 null) */ |
| | | private String archiveNo(Long memberId) { |
| | | ArchiveDocumentRespDTO archive = archiveDocumentApi |
| | | .getBySource(ARCHIVE_SOURCE_TYPE_MEMBER, memberId).getCheckedData(); |
| | | return archive != null ? archive.getNo() : null; |
| | | } |
| | | |
| | | } |