package org.enfi.modules.system.service.impl;

import org.enfi.common.system.vo.SysDepartModel;
import org.enfi.common.util.ThreadUtil;
import org.enfi.modules.system.entity.DepartHierarchy;
import org.enfi.modules.system.entity.SysDepart;
import org.enfi.modules.system.mapper.IOaEmployeeVMapper;
import org.enfi.modules.system.service.IDepartDataService;
import org.enfi.modules.system.service.ISysDepartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

/**
 * @Author zbl
 * @Date: 2020/08/11/ 16:46
 * @Description
 */

@Service
public class DepartDataServiceImpl implements IDepartDataService {

    @Autowired
    private ISysDepartService sysDepartService;

    @Autowired
    private IOaEmployeeVMapper baseMapper;

    @Override
    public List<DepartHierarchy> findErpDepAll() {
        return baseMapper.findErpDepAll();
    }

    @Override
    public boolean saveAllSysDepart(List<SysDepartModel> sysDepartModelList) {
        List<SysDepart> departList = new ArrayList<SysDepart>();
        ThreadPoolExecutor theadPool = ThreadUtil.getTheadPool(10);
        for (SysDepartModel departModel : sysDepartModelList) {
            SysDepart depart = new SysDepart();
            BeanUtils.copyProperties(departModel, depart);
            depart.setCreateTime(new Date());
            depart.setUpdateTime(new Date());
            depart.setUpdateBy("admin");
            depart.setDelFlag("0");
            depart.setCreateBy("admin");  //默认创建者为系统用户
            departList.add(depart);
            depart.insertOrUpdate();
            //sysDepartService.saveOrUpdate(depart);
        }
        ThreadUtil.waitThreadPoolCompleted(theadPool, "ERP部门信息同步完成");
        //储存部门信息 sysDepartService.saveOrUpdateBatch(departList)
        return true;
    }

    @Override
    public void importDep() {

        List<DepartHierarchy> departHierarchyList = this.findErpDepAll();

        List<SysDepartModel> departModelList = new ArrayList<SysDepartModel>();

        /**=================================层级遍历添加各个部门=================================*/

        //获取根结点
        List<DepartHierarchy> root = departHierarchyList.stream().filter(s -> s.getOrganizationIdParent() == 0).collect(Collectors.toList());

        Queue<DepartHierarchy> queue = new ArrayDeque<>();
        root.forEach(s -> {
            s.setOrgCode("Top" + s.getChildOrgId().toString());
            queue.add(s);
        });

        while (queue.size() > 0) {

            SysDepartModel departModel = new SysDepartModel();
            //出一个进n个
            DepartHierarchy node = queue.poll();
            departModel.setId(node.getChildOrgId().toString());
            departModel.setOrgCategory("1");
            if (node.getOrganizationIdParent() != 0) {
                departModel.setParentId(node.getOrganizationIdParent().toString());
                departModel.setOrgType("2");
            } else {
                departModel.setOrgType("1");

            }
            departModel.setDepartName(node.getChildOrgName());

            departModel.setOrgCode(node.getOrgCode());
            //存下该部门信息
            departModelList.add(departModel);

//          List<DepartHierarchy> collect = departHierarchyList.stream().filter(s -> s.getOraganizationIdParent().toString().equals(node.getChildOrgId().toString())).collect(Collectors.toList());


            //   在判断ChildOrgId和OraganizationIdParent是否相等时用== 在矿山院548处会返回false，所以改用toString后使用equals方法进行判断
            departHierarchyList.stream().filter(s -> s.getOrganizationIdParent().toString().equals(node.getChildOrgId().toString())).collect(Collectors.toList()).forEach(d -> {
                d.setOrgCode(node.getOrgCode() + d.getOrgCode());
                queue.add(d);
            });

        }
        //存入数据库
        this.saveAllSysDepart(departModelList);
        System.gc();
    }
}
