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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.InfoDto;
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;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 信息表
 *
 * @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);
    }

    /** 
    * @Description:  分页查询报给我的信息
    * @Param: [page, receiverId]
    * @return: java.util.List<com.elephant.framework.galaxy.aroad.module.info.dto.InfoDto> 
    * @Author: songwenjie 
    * @Date: 2019/5/5 
    */ 
    public IPage<InfoDto> getReportedToMe (Page page, Integer receiverId){
        IPage<InfoDto> infos = baseMapper.getReportedToMe(page,receiverId);
        return infos;
    }

    /** 
    * @Description:  分页查询我上报的信息
    * @Param: [page, reporterId] 
    * @return: com.baomidou.mybatisplus.core.metadata.IPage<com.elephant.framework.galaxy.aroad.module.info.dto.InfoDto> 
    * @Author: songwenjie 
    * @Date: 2019/5/6 
    */ 
    public IPage<InfoDto> getIReported (Page page, Integer reporterId){
        IPage<InfoDto> infos = baseMapper.getIReported(page,reporterId);
        infos.getRecords().forEach(i->i.setReceiverName(i.getReceiverName().replaceAll(",","，")));
        return infos;
    }
}
