# ScheduleEmailTargetServiceImpl.java

    package cloud.wconnect.workperformance.service.schedule.impl;

    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.commonlib.cons.EmailConfigCons;
    import cloud.wconnect.workperformance.entity.schedule.ScheduleEmailTarget;
    import cloud.wconnect.workperformance.mapper.schedule.ScheduleEmailTargetMapper;
    import cloud.wconnect.workperformance.pojo.dto.schedule.ScheduleDataPermissionEmailConfigDTO;
    import cloud.wconnect.workperformance.service.schedule.IScheduleEmailTargetService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import org.apache.commons.collections.CollectionUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

    import java.util.ArrayList;
    import java.util.List;

    /**
     * <p>
     * schedule 收件人、抄送人配置 服务实现类
     * </p>
     *
     * @author yyt
     * @since 2023-06-28
     */
    @Service
    @DS("work_performance")
    public class ScheduleEmailTargetServiceImpl extends ServiceImpl<ScheduleEmailTargetMapper, ScheduleEmailTarget> implements IScheduleEmailTargetService {

        @Autowired
        private BeanMapper beanMapper;

        /**
         * 获取邮件配置收件人、抄送人
         *
         * @param configIds 邮件配置 id
         * @return 收件人、抄送人
         * @author yyt
         * @date 2023/6/30
         */
        @Override
        public List<ScheduleEmailTarget> getEmailTarget(List<Integer> configIds) {
            return list(new QueryWrapper<ScheduleEmailTarget>().lambda().in(ScheduleEmailTarget::getConfigId, configIds));
        }

        /**
         * 保存邮件通知人
         *
         * @param propertyId 资产 id
         * @param configId   邮件配置 id
         * @param configDTO      通知人
         * @author yyt
         * @date 2023/7/3
         */
        @Override
        @Transactional(rollbackFor = Exception.class)
        public void saveBatch(Integer propertyId, Integer configId, ScheduleDataPermissionEmailConfigDTO configDTO) {
            List<ScheduleEmailTarget> targets = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(configDTO.getToList())) {
                targets.addAll(configDTO.getToList().stream().map(item -> beanMapper.map(item, ScheduleEmailTarget.class,
                        target -> {
                            target.setTargetType(EmailConfigCons.TARGET_TYPE_TO);
                            target.setPropertyId(propertyId);
                            target.setConfigId(configId);
                        })).toList());
            }
            if (CollectionUtils.isNotEmpty(configDTO.getCcList())) {
                targets.addAll(configDTO.getCcList().stream().map(item -> beanMapper.map(item, ScheduleEmailTarget.class,
                        target -> {
                            target.setTargetType(EmailConfigCons.TARGET_TYPE_CC);
                            target.setPropertyId(propertyId);
                            target.setConfigId(configId);
                        })).toList());
            }
            if (CollectionUtils.isNotEmpty(targets)) {
                saveBatch(targets);
            }
        }

        /**
         * 清除邮件通知人
         *
         * @param config 邮件配置 id
         * @author yyt
         * @date 2023/7/3
         */
        @Override
        public void clearTarget(Integer config) {
            remove(new QueryWrapper<ScheduleEmailTarget>().lambda().eq(ScheduleEmailTarget::getConfigId, config));
        }

        /**
         * 查询通知配置下的用户
         *
         * @param id 通知配置 id
         * @return 用户
         * @author yyt
         * @date 2024/11/21
         **/
        @Override
        public List<ScheduleEmailTarget> list(Integer id) {
            return list(new QueryWrapper<ScheduleEmailTarget>().lambda().eq(ScheduleEmailTarget::getConfigId, id));
        }

    }