package com.elephant.framework.galaxy.aroad.module.info.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.elephant.framework.galaxy.aroad.module.info.dto.InfoInformationsDto;
import com.elephant.framework.galaxy.aroad.module.info.entity.InfoInformations;
import com.elephant.framework.galaxy.aroad.module.info.entity.InfoReceivers;
import com.elephant.framework.galaxy.aroad.module.info.mapper.InfoInformationsMapper;
import com.elephant.framework.galaxy.aroad.module.info.service.InfoInformationsService;
import com.elephant.framework.galaxy.aroad.module.info.service.InfoReceiversService;
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;

/**
 * 信息表
 *
 * @author songwenjie
 * @date 2019-04-28 10:34:46
 */
@Service
public class InfoInformationsServiceImpl extends ServiceImpl<InfoInformationsMapper, InfoInformations> implements InfoInformationsService {

    @Autowired
    private InfoReceiversService infoReceiversService;

    /** 
    * @Description: 新增上报信息
    * @Param: [info] 
    * @return: java.lang.Boolean 
    * @Author: songwenjie 
    * @Date: 2019/5/5 
    */ 
    @Transactional
    public Boolean saveInformations(InfoInformationsDto info){
        InfoInformations information = InfoInformations.builder()
                .templateId(info.getTemplateId())
                .infoTitle(info.getInfoTitle())
                .infoContent(info.getInfoContent())
                .infoRemark(info.getInfoRemark())
                .reporterId(info.getReporterId())
                .reportingTime(info.getReportingTime())
                .build();
        baseMapper.saveInformations(information);

        List<InfoReceivers> receivers = new ArrayList<>();
        info.getReceiverIds().forEach(r->{
            receivers.add(InfoReceivers.builder()
                    .infoId(information.getInfoId())
                    .deptId(info.getDeptId())
                    .receiverId(r)
                    .build());
        });
        return infoReceiversService.saveBatch(receivers);
    }
}
