/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.base.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.base.entity.App;
import com.tofly.base.entity.Department;
import com.tofly.base.entity.User;
import com.tofly.base.mapper.DepartmentMapper;
import com.tofly.base.service.DepartmentService;
import com.tofly.base.service.SourcedicService;
import com.tofly.common.core.util.io.ExcelUtil;
import com.tofly.common.oauth.exception.ToflyDeniedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * 部门表
 *
 * @author admin
 * @date Thu Oct 10 00:00:00 CST 2019
 */
@Service("departmentService")
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {
    
    @Autowired
    private SourcedicService sourcedicService;
    
    @Override
    public List<Map> listSourceByDept(Long departmentId) {
        List<Map<String,Object>> sourcedicList=sourcedicService.listMaps();
        List<Map> reList=new ArrayList<>();
        Map m=new HashMap(),map;
        m.put("departmentId",departmentId);
        for(Map map1:sourcedicList){
            m.put("pid",map1.get("id"));
            map1.put("funs",baseMapper.listSourceByDept(m));
            reList.add(map1);
        };
        
        return reList;
    }

    @Override
    public List<Department> subList(Long id, String returnSelf) {
        return baseMapper.subList(id,returnSelf);
    }

    @Override
    public Object uploadBatchDepartment(MultipartFile file) throws Exception {
        if (file == null) {
            throw new ToflyDeniedException("上传文件为空");
        }
        InputStream inputStream = file.getInputStream();
        ExcelUtil userExcel = new ExcelUtil(Department.class);
        List<Department> appList =  userExcel.importExcel(inputStream);
        List<Department> listAllApp = this.list();
        Map<String,Department> collect =  listAllApp.stream().collect(Collectors.toMap(Department::getName ,m->m));
        StringBuffer sb = new StringBuffer();
        List<String> collect1 = appList.stream().filter(k -> collect.get(k.getName()) !=null)
                .map(Department::getName).collect(Collectors.toList());
        if (collect1.size() > 0) {
            throw new ToflyDeniedException("数据库已经存部门名称，不能导入" + JSONArray.toJSONString(collect1));
        }

        appList.stream().forEach(department -> {
            this.save(department);
        });
       return  "成功导入" + appList.size() + "条数据";
    }

    @Override
    public List<Long> getRelatedDeptIds(Long deptId) {
        return this.baseMapper.getRelatedDeptIds(deptId);
    }

}
