package com.enterprise.manage.modules.asset.service.impl;

import com.enterprise.manage.common.base.BaseServiceImpl;
import com.enterprise.manage.modules.asset.domain.Enterprise;
import com.enterprise.manage.modules.asset.mapper.EnterpriseMapper;
import com.enterprise.manage.modules.asset.service.IEnterpriseService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * 企业机构 Service 业务层处理
 *
 * @author QingLi Wang
 * 2023-11-28 09:24
 */
@Service
public class EnterpriseServiceImpl extends BaseServiceImpl<EnterpriseMapper, Enterprise> implements IEnterpriseService {

    @Override
    public boolean enterpriseUniqueCheck(String creditCode,Long id) {
        return baseMapper.enterpriseUniqueCheck(creditCode,id) > 0;
    }
    @Override
    public List<Enterprise> querySelectTree() {
        List<Enterprise> list = new ArrayList<>();
        // 添加根菜单
        Enterprise enterprise = new Enterprise();
        enterprise.setId(0L);
        enterprise.setName("企业单位");
        enterprise.setChildren(getChildren(enterprise.getId(), baseMapper.selectTreeAll()));
        list.add(enterprise);
        return list;
    }
    /*
     * 递归获取企业 tree
     */
    @Override
    public List<Enterprise> getChildren(Long parentId, List<Enterprise> Enterprises) {
        List<Enterprise> list = new ArrayList<>();
        for (Enterprise menu : Enterprises) {
            if (parentId.equals(menu.getParentId())) {
                menu.setChildren(getChildren(menu.getId(), Enterprises));
                list.add(menu);
            }
        }
        return list;
    }
    
//    @Override
//    public boolean updateEnable(Long id, boolean enable) {
//        return baseMapper.updateEnable(id, enable) > 0;
//    }
}