package com.unis.crk.basic.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.unis.crk.basic.entity.OrgInfo;
import com.unis.crk.basic.mapper.OrgInfoMapper;
import com.unis.crk.basic.service.IOrgInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 组织机构信息表 服务实现类
 * </p>
 *
 * @author ghh
 * @since 2021-09-26
 */
@Service
public class OrgInfoServiceImpl extends ServiceImpl<OrgInfoMapper, OrgInfo> implements IOrgInfoService {
    @Resource
    private  OrgInfoMapper orgInfoMapper;
    @Override
    public OrgInfo getDepotInfo(OrgInfo orgInfo) {
        //组织类别为5318粮食企业，直接返回单位信息
        if (StringUtils.equals(orgInfo.getParentId(),"5318")) {
            return orgInfo;
        } else {
            if (StringUtils.isBlank(orgInfo.getParentId()) || StringUtils.equals(orgInfo.getParentId(),"0")) {
                QueryWrapper<OrgInfo> basicQueryWrapper = new QueryWrapper<>();
                basicQueryWrapper.eq("org_id",orgInfo.getParentId());
                OrgInfo parentOrg = orgInfoMapper.selectOne(basicQueryWrapper);
                return getDepotInfo(parentOrg);
            }
        }
        return null;
    }

    @Override
    public List<OrgInfo> getTree(String orgName) {
        HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("orgName",orgName);
        List<OrgInfo> toplist=null;
        List<OrgInfo> tree = orgInfoMapper.getTree(objectObjectHashMap);
        if(tree!=null && tree.size()>0){
            OrgInfo orgInfo = tree.get(0);
            toplist = this.getChildren(tree, orgInfo.getOrgId().toString());
        }

        this.fillTree(toplist, tree);
        return toplist;
    }

    @Override
    public void updateOrgLevel(OrgInfo orgInfo) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("parentId", orgInfo.getOrgId());
        map.put("parentLevel", "14");
        orgInfoMapper.updateOrgLevel(map);
    }

    @Override
    public List<OrgInfo> getAllChildren(String orgId, String barnName) {
        HashMap hashMap = new HashMap();

        QueryWrapper<OrgInfo> orgInfoVoQueryWrapper = new QueryWrapper<>();
        //传输过来的库点名称
        if(barnName!=""&&barnName!=null){
            String[] barn = barnName.split(":");
            if(StrUtil.isNotBlank(barn[0]) && barn[0].compareTo("null")!=0){
                hashMap.put("orgName",barn[0]);
            }
            hashMap.put("orgClassId",Integer.parseInt(barn[1]));

        }
        List<OrgInfo> orgInfoVos = orgInfoMapper.getAllChildren(hashMap);
        hashMap.put("orgId",orgId);
        OrgInfo orgInfoVo = orgInfoMapper.selectOrgInfoVo(hashMap);
        List<OrgInfo> toplist = this.getChildren(orgInfoVos, orgId);
        List<OrgInfo> resultList = new ArrayList<>();
        resultList.add(orgInfoVo);
        this.addChildren(orgInfoVos, toplist, resultList);
        return resultList;
    }

    @Override
    public List<OrgInfo> getallOrgInfo() {
        return orgInfoMapper.getallOrgInfo();
    }

    @Override
    public int getCountChildren(String orgId) {
        return orgInfoMapper.getCountChildren(orgId);
    }


    private void addChildren(List<OrgInfo> allList, List<OrgInfo> childList, List<OrgInfo> resultList) {
        resultList.addAll(childList);
        for(OrgInfo org : childList) {
            List<OrgInfo> childList1 = this.getChildren(allList, org.getOrgId().toString());
            if(childList1.size()>0){
                addChildren(allList, childList1, resultList);
            }
        }
    }


    /**
     * 递归填充树形结构
     * @param allList
     * @param childList
     */
    public void fillTree(List<OrgInfo> allList, List<OrgInfo> childList) {
        if(childList!=null){
            for(OrgInfo org : childList) {
                List<OrgInfo> childList1 = this.getChildren(childList, org.getOrgId().toString());
                fillTree(allList, childList1);
                org.setChildren(childList1);
            }
        }
    }
    /**
     * 根据编号获取所有子节点
     * @param list
     * @param parentId
     * @return
     */
    private List<OrgInfo> getChildren(List<OrgInfo> list, String parentId) {
        List<OrgInfo> children = new ArrayList<OrgInfo>();
        for(OrgInfo org: list){
            if (org.getParentId().equals(parentId)) {
                children.add(org);
            }
        }
        return children;
    }
}
