# GmwrEmailConfigController.java

    package cloud.wconnect.workperformance.controller.gmwr;

    import cloud.wconnect.commonlib.annotation.ApiId;
    import cloud.wconnect.commonlib.component.SessionContext;
    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.cons.CommonErrorI18nConstant;
    import cloud.wconnect.commonlib.cons.EmailConfigCons;
    import cloud.wconnect.commonlib.exception.BadRequestException;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.pojo.dto.UserDTO;
    import cloud.wconnect.commonlib.pojo.dto.UsergroupDTO;
    import cloud.wconnect.commonlib.redis.PropertyI18nRedisService;
    import cloud.wconnect.commonlib.service.UserInternalService;
    import cloud.wconnect.commonlib.service.UsergroupInternalService;
    import cloud.wconnect.commonlib.util.I18nMessageUtils;
    import cloud.wconnect.workperformance.cons.gmwr.GmwrNotifyTypeEnum;
    import cloud.wconnect.workperformance.pojo.dto.gmwr.email.GmwrEmailConfigDTO;
    import cloud.wconnect.workperformance.pojo.dto.gmwr.email.GmwrEmailTargetDTO;
    import cloud.wconnect.workperformance.service.gmwr.IGmwrEmailConfigService;
    import com.google.common.collect.ImmutableMap;
    import com.google.common.collect.Lists;
    import com.google.common.collect.Sets;
    import jakarta.annotation.Resource;
    import jakarta.validation.Valid;
    import org.apache.commons.collections.CollectionUtils;
    import org.springframework.web.bind.annotation.*;

    import java.text.MessageFormat;
    import java.util.Collections;
    import java.util.List;
    import java.util.Map;
    import java.util.stream.Collectors;

    /**
     * <p>
     * Weekly GM Report 功能的邮件配置表 前端控制器
     * </p>
     *
     * @author zc
     * @since 2025-01-24
     */
    @RestController
    @RequestMapping("/gm-weekly-report/email-config")
    public class GmwrEmailConfigController {
        @Resource
        private IGmwrEmailConfigService emailConfigService;
        @Resource
        private UserInternalService userInternalService;
        @Resource
        private UsergroupInternalService usergroupInternalService;
        @Resource
        private SessionContext sessionContext;
        @Resource
        private PropertyI18nRedisService propertyI18nRedisService;
        @Resource
        private I18nMessageUtils i18nMessageUtils;

        /**
         * 获取邮件配置
         *
         * @param property 资产标识
         * @param type     通知类型
         * @return 邮件配置
         * @author zc
         * @date 2025/1/24
         */
        @ApiId(1332)
        @GetMapping
        public GmwrEmailConfigDTO get(@RequestParam String property, @RequestParam int type) {
            // 校验支持的通知类型
            checkNotifyType(type);
            int propertyId = sessionContext.getPropertyId(property);
            PropertyDTO propertyDTO = propertyI18nRedisService.get(propertyId);
            return emailConfigService.getEmailConfigDTO(propertyDTO, type);
        }

        /**
         * 保存邮件配置
         *
         * @param property       资产标识
         * @param emailConfigDTO 邮件配置信息
         * @return 邮件配置 id
         * @author zc
         * @date 2025/1/24
         */
        @ApiId(1333)
        @PostMapping
        public Map<String, Integer> save(@RequestParam String property, @RequestBody @Valid GmwrEmailConfigDTO emailConfigDTO) {
            int tenantId = sessionContext.getTenantSymbolBO().getId();
            int propertyId = sessionContext.getPropertyId(property);
            // 校验支持的通知类型
            checkNotifyType(emailConfigDTO.getType());
            // 校验是否不可关闭
            checkStatusClosed(emailConfigDTO.getType(), emailConfigDTO.getStatus());
            // 校验通知对象是否必须为空
            checkEmailTargetMustEmpty(emailConfigDTO);
            // 校验通知对象有效性
            checkEmailTarget(tenantId, propertyId, emailConfigDTO);
            return ImmutableMap.of("id", emailConfigService.save(propertyId, sessionContext.getLoginUserId(), emailConfigDTO));
        }

        /**
         * 校验支持的通知类型
         *
         * @param notifyType 报告类型
         * @author zc
         * @date 2026/3/6
         */
        private void checkNotifyType(int notifyType) {
            List<Integer> effectNotifyTypeList = List.of(GmwrNotifyTypeEnum.DRAFT_DATA_PROCESS.getType());
            if (!effectNotifyTypeList.contains(notifyType)) {
                throw new BadRequestException(CommonErrorI18nConstant.NOTIFY_TYPE_NOT_SUPPORTED,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        MessageFormat.format("The currently selected notify type is not supported, " +
                                "notifyType must be in {0}", effectNotifyTypeList));
            }
        }

        /**
         * 校验是否不可关闭
         *
         * @param emailType 邮件类型
         * @param status    状态：true：开启，false：关闭
         * @author zc
         * @date 2025/1/24
         */
        private void checkStatusClosed(int emailType, boolean status) {
            List<Integer> cannotCloseTypeList = Lists.newArrayList(GmwrNotifyTypeEnum.DRAFT_DATA_PROCESS.getType());
            if (!status && cannotCloseTypeList.contains(emailType)) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_CANNOT_CLOSE,
                        i18nMessageUtils.getErrorParamMapNotNeedTranslate("Draft data process"),
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The draft data process type cannot be closed");
            }
        }

        /**
         * 校验通知对象是否必须为空
         *
         * @param emailConfigDTO 邮件配置DTO
         * @author zc
         * @date 2025/1/24
         */
        private void checkEmailTargetMustEmpty(GmwrEmailConfigDTO emailConfigDTO) {
            List<Integer> targetMustEmptyTypeList = Lists.newArrayList(GmwrNotifyTypeEnum.DRAFT_DATA_PROCESS.getType());
            if (targetMustEmptyTypeList.contains(emailConfigDTO.getType()) &&
                    (CollectionUtils.isNotEmpty(emailConfigDTO.getToList()) || CollectionUtils.isNotEmpty(emailConfigDTO.getCcList()))) {
                throw new BadRequestException(CommonErrorI18nConstant.MUST_EMPTY,
                        i18nMessageUtils.getErrorParamMapNotNeedTranslate("emailTarget"),
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The email target must empty");
            }
        }

        /**
         * 校验通知对象有效性
         *
         * @param tenantId       租户 id
         * @param propertyId     资产 id
         * @param emailConfigDTO 邮件配置DTO
         * @author zc
         * @date 2025/1/24
         */
        private void checkEmailTarget(int tenantId, int propertyId, GmwrEmailConfigDTO emailConfigDTO) {
            List<GmwrEmailTargetDTO> allNotifyList = Lists.newArrayList();
            if (CollectionUtils.isNotEmpty(emailConfigDTO.getToList())) {
                allNotifyList.addAll(emailConfigDTO.getToList());
            }
            if (CollectionUtils.isNotEmpty(emailConfigDTO.getCcList())) {
                allNotifyList.addAll(emailConfigDTO.getCcList());
            }
            if (CollectionUtils.isEmpty(allNotifyList)) {
                return;
            }

            // 按照用户来源分组
            Map<Integer, List<Integer>> source2IdentityIdList = allNotifyList.stream()
                    .collect(Collectors.groupingBy(GmwrEmailTargetDTO::getSource,
                            Collectors.mapping(GmwrEmailTargetDTO::getIdentityId, Collectors.toList())));

            // 校验是否重复
            List<Integer> userIds = source2IdentityIdList.getOrDefault(EmailConfigCons.SOURCE_USER, Collections.emptyList());
            if (userIds.size() != Sets.newHashSet(userIds).size()) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_CANNOT_REPEAT,
                        i18nMessageUtils.getErrorParamMapNotNeedTranslate("userIds"),
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The user cannot repeat");
            }
            List<Integer> userGroupIds = source2IdentityIdList.getOrDefault(EmailConfigCons.SOURCE_USERGROUP, Collections.emptyList());
            if (userGroupIds.size() != Sets.newHashSet(userGroupIds).size()) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_CANNOT_REPEAT,
                        i18nMessageUtils.getErrorParamMapNotNeedTranslate("userGroupIds"),
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The user group cannot repeat");
            }
            List<String> manualEmails = allNotifyList.stream().filter(item -> item.getSource() == EmailConfigCons.SOURCE_MANUAL)
                    .map(GmwrEmailTargetDTO::getEmail).collect(Collectors.toList());
            if (manualEmails.size() != Sets.newHashSet(manualEmails).size()) {
                throw new BadRequestException(CommonErrorI18nConstant.DATA_CANNOT_REPEAT,
                        i18nMessageUtils.getErrorParamMapNotNeedTranslate("manualEmails"),
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The manual email cannot repeat");
            }
            // 用户集合
            if (CollectionUtils.isNotEmpty(userIds)) {
                List<UserDTO> allUsers = userInternalService.getUsers(tenantId, userIds);
                if (userIds.size() != allUsers.size()) {
                    throw new BadRequestException(CommonErrorI18nConstant.DATA_NOT_EXIST,
                            i18nMessageUtils.getErrorParamMapNotNeedTranslate("userId"),
                            BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                            "The user not exist");
                }
            }
            // 校验用户组是否存在
            if (CollectionUtils.isNotEmpty(userGroupIds)) {
                List<UsergroupDTO> userGroups = usergroupInternalService.getUsergroups(tenantId, propertyId, userGroupIds);
                if (userGroupIds.size() != userGroups.size()) {
                    throw new BadRequestException(CommonErrorI18nConstant.DATA_NOT_EXIST,
                            i18nMessageUtils.getErrorParamMapNotNeedTranslate("userGroupId"),
                            BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                            "The user group not exist");
                }
            }
        }
    }