# ItticketNotificationTargetServiceImpl.java

    package cloud.wconnect.it.service.itrequest.impl;

    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.commonlib.cons.EmailConfigCons;
    import cloud.wconnect.it.entity.itrequest.ItticketNotificationTarget;
    import cloud.wconnect.it.mapper.itrequest.ItticketNotificationTargetMapper;
    import cloud.wconnect.it.pojo.dto.itrequest.ItticketNotificationDTO;
    import cloud.wconnect.it.service.itrequest.IItticketNotificationTargetService;
    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>
     *  服务实现类
     * </p>
     *
     * @author yyt
     * @since 2023-03-25
     */
    @DS("it_manage")
    @Service
    public class ItticketNotificationTargetServiceImpl extends ServiceImpl<ItticketNotificationTargetMapper, ItticketNotificationTarget> implements IItticketNotificationTargetService {

        @Autowired
        private BeanMapper beanMapper;

        /**
         * 获取通知配置用户
         *
         * @param notificationId 通知 id
         * @return 配置用户
         * @author yyt
         * @date 2023/3/28
         */
        @Override
        public List<ItticketNotificationTarget> listByNotificationIds(Integer notificationId) {
            return baseMapper.selectList(new QueryWrapper<ItticketNotificationTarget>().lambda()
                    .eq(ItticketNotificationTarget::getNotificationId, notificationId));
        }

        /**
         * 批量添加
         *
         * @param id              notification id
         * @param propertyId      资产 id
         * @param notificationDTO 通知用户
         * @author yyt
         * @date 2023/3/28
         */
        @Override
        @Transactional(rollbackFor = Exception.class)
        public void saveBatch(Integer id, int propertyId, ItticketNotificationDTO notificationDTO) {
            List<ItticketNotificationTarget> targets = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(notificationDTO.getToList())) {
                targets.addAll(notificationDTO.getToList().stream().map(item -> beanMapper.map(item, ItticketNotificationTarget.class,
                        target -> {
                            target.setTargetType(EmailConfigCons.TARGET_TYPE_TO);
                            target.setPropertyId(propertyId);
                            target.setNotificationId(id);
                        })).toList());
            }
            if (CollectionUtils.isNotEmpty(notificationDTO.getCcList())) {
                targets.addAll(notificationDTO.getCcList().stream().map(item -> beanMapper.map(item, ItticketNotificationTarget.class,
                        target -> {
                            target.setTargetType(EmailConfigCons.TARGET_TYPE_CC);
                            target.setPropertyId(propertyId);
                            target.setNotificationId(id);
                        })).toList());
            }
            if (CollectionUtils.isNotEmpty(targets)) {
                saveBatch(targets);
            }
        }

        /**
         * 清除通知下配置的用户
         *
         * @param id notification id
         * @author yyt
         * @date 2023/3/28
         */
        @Override
        public void clearNotificationTarget(Integer id) {
            remove(new QueryWrapper<ItticketNotificationTarget>().lambda().eq(ItticketNotificationTarget::getNotificationId, id));
        }

    }