package com.zcxk.rmcp.web.service.impl;

import com.zcxk.core.common.pojo.ProgramItem;
import com.zcxk.core.oauth2.pojo.LoginUser;
import com.zcxk.core.oauth2.util.UserUtil;
import com.zcxk.rmcp.api.vo.OrgTreeVo;
import com.zcxk.rmcp.web.service.OrgService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.zcxk.rmcp.core.dao.OrgMapper;
import com.zcxk.rmcp.core.entity.Org;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static com.google.common.collect.Lists.newArrayList;

/**
 * 
 * @author linqingwei
 * @date 2021-07-22 10:26
 */
@Service
public class OrgServiceImpl implements OrgService {

    @Resource
    private OrgMapper orgMapper;

    @Override
    public int insertSelective(Org record) {
        return orgMapper.insertSelective(record);
    }

    @Override
    public int updateByPrimaryKeySelective(Org record) {
        return orgMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public List<OrgTreeVo> getUserTrees(Org org) {
        LoginUser currentUser = UserUtil.getCurrentUser();
        org.setTenantId(currentUser.getTenantId());
        Integer id=org.getId();
        org.setId(null);
        Set<Integer> orgIds=new HashSet<>();
        List<ProgramItem> programItemList = currentUser.getProgramItemList();
        if(programItemList!=null){
            programItemList.forEach(programItem -> {
                orgIds.add(programItem.getOrgId());
            });
        }

        List<OrgTreeVo> trees = new ArrayList<>();
        if("company".equals(org.getOrgType())){

            if(currentUser.getDepartmentId()==null&&currentUser.getPermissonType()==2){
                trees=orgMapper.selectTrees(org);
                trees=getOrgTreeByOrg(trees,currentUser.getCompanyId(),orgIds);
            }else{
                org.setId(currentUser.getCompanyId());
                trees=orgMapper.selectTrees(org);
            }
        }else{
            if(currentUser.getDepartmentId()!=null&&currentUser.getPermissonType()==4){
                org.setOrgType("department");
                trees=orgMapper.selectTrees(org);
                trees=getOrgTreeByOrg(trees,currentUser.getDepartmentId(),orgIds);
            }
            if(currentUser.getDepartmentId()!=null&&currentUser.getPermissonType()==3){
                org.setId(currentUser.getDepartmentId());
                trees=orgMapper.selectTrees(org);
            }else{
                trees=orgMapper.selectTrees(org);
                trees=getOrgDepartmentTree(trees,id);

            }

        }

        return trees;
    }

    List<OrgTreeVo> getOrgTreeByOrg(List<OrgTreeVo> trees,int id, Set<Integer>orgIds){
        OrgTreeVo fatherOrgTree=new OrgTreeVo();
        for (OrgTreeVo tree : trees) {
            if(tree.getId()==id){
                fatherOrgTree=tree;
                break;
            }

        }

        trees=getOrgTree(trees,id,1,orgIds);
        fatherOrgTree.setOrgs(trees);
        List<OrgTreeVo> result=new ArrayList<>();
        result.add(fatherOrgTree);
        trees=result;
        return trees;
    }
    public  List<OrgTreeVo> getOrgTree(List<OrgTreeVo> list, Integer id, Integer level, Set<Integer>orgIds) {
        List<OrgTreeVo> temList = newArrayList();
        if (list != null) {
            for (OrgTreeVo orgTree : list) {

                if (id.equals(orgTree.getParentOrgId())) {

                    orgTree.setDisabled(false);

                    List<OrgTreeVo> chidren = getOrgTree(list, orgTree.getId(),level,orgIds);
                    OrgTreeVo temPermission = new OrgTreeVo();
                    BeanUtils.copyProperties(orgTree, temPermission);
                    temPermission.setOrgs(chidren);
                    temList.add(temPermission);
                    level--;
                }

            }
        }
        if (temList.size() > 0) {
            return temList;
        } else {
            return null;
        }
    }
    public  List<OrgTreeVo> getOrgDepartmentTree(List<OrgTreeVo> list, Integer id) {
        List<OrgTreeVo> temList = newArrayList();
        if (list != null) {
            for (OrgTreeVo orgTree : list) {

                if (id.equals(orgTree.getParentOrgId())
                        &&"department".equals(orgTree.getOrgType())) {

                    List<OrgTreeVo> chidren = getOrgDepartmentTree(list, orgTree.getId());
                    OrgTreeVo temPermission = new OrgTreeVo();
                    BeanUtils.copyProperties(orgTree, temPermission);
                    temPermission.setOrgs(chidren);
                    temList.add(temPermission);
                    ;
                }

            }
        }
        if (temList.size() > 0) {
            return temList;
        } else {
            return null;
        }
    }

    @Override
    public Org findByName(String tenantId, String name) {
        return orgMapper.findByName(tenantId,name);
    }

    @Override
    public Org findByParentIdAndName(Integer parentId, String name) {
        return orgMapper.findByParentIdAndName(parentId,name);
    }
}
