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.attendance.dto.MemberDto;
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 com.elephant.framework.galaxy.aroad.module.system.service.UserService;
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;

    @Autowired
    private UserService userService;

    /** 
    * @Description: 新增上报信息
    * @Param: [info] 
    * @return: java.lang.Boolean 
    * @Author: songwenjie 
    * @Date: 2019/5/5 
    */ 
    @Transactional
    public Boolean saveInformations(InfoInformationsDto info){
        List<MemberDto> users = userService.getUsers(info.getReceiverIds());
        if(users == null || users.size() <= 0){
            return false;
        }
        InfoInformations information = InfoInformations.builder()
                .templateId(info.getTemplateId())
                .infoTitle(info.getInfoTitle())
                .infoContent(info.getInfoContent())
                .infoRemark(info.getInfoRemark())
                .reporterId(info.getReporterId())
                .reporter(info.getReporter())
                .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)
                    .receiverName(users.stream()
                            .filter((s) -> s.getId().equals(r))
                            .findFirst()
                            .get()
                            .getName())
                    .build());
        });
        return infoReceiversService.saveBatch(receivers);
    }
}
