package com.mortals.xhx.module.site.service.impl;
import org.springframework.stereotype.Service;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.xhx.common.code.SatusEnum;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.xhx.module.site.dao.SiteBusinessDao;
import com.mortals.xhx.module.site.model.SiteBusinessEntity;
import com.mortals.xhx.module.site.model.SiteBusinessQuery;
import com.mortals.xhx.module.site.service.SiteBusinessService;
import com.mortals.xhx.module.site.model.SiteBusinessTreeSelect;
import org.springframework.util.ObjectUtils;
import java.util.*;
import java.util.stream.Collectors;
/**
* SiteBusinessService
* 站点事项 service实现
*
* @author zxfei
* @date 2022-01-13
*/
@Service("siteBusinessService")
public class SiteBusinessServiceImpl extends AbstractCRUDServiceImpl<SiteBusinessDao, SiteBusinessEntity, Long> implements SiteBusinessService {

    @Override
    protected void saveBefore(SiteBusinessEntity entity, Context context) throws AppException {

    }

    @Override
    protected void updateBefore(SiteBusinessEntity entity, Context context) throws AppException {

        super.updateBefore(entity, context);
    }


    @Override
    protected void updateAfter(SiteBusinessEntity entity, Context context) throws AppException {

        super.updateAfter(entity, context);
    }

    @Override
    protected void removeBefore(Long[] ids, Context context) throws AppException {
        Long[] childIds = Arrays.asList(ids).stream()
                .flatMap(item -> {
                    SiteBusinessEntity siteBusinessEntity = this.get(item);
                    return this.find(new SiteBusinessQuery().parentId(siteBusinessEntity.getBusinessId())).stream();
                }).map(item -> item.getId()).toArray(Long[]::new);

        this.getDao().delete(childIds);
    }



    @Override
    public boolean hasChildBySiteBusinessId(Long siteBusinessId) {
        List<SiteBusinessEntity> list = this.find(new SiteBusinessQuery().parentId(siteBusinessId));
        return list.size() > 0 ? true : false;
    }

    @Override
    public List<SiteBusinessEntity> selectSiteBusinessList(SiteBusinessEntity siteBusiness) {
        return this.find(new SiteBusinessQuery());
    }

    @Override
    public List<SiteBusinessTreeSelect> buildSiteBusinessTreeSelect(List<SiteBusinessEntity> list) {
        List<SiteBusinessEntity> returnList = new ArrayList<>();
        List<Long> tempList = list.stream().map(SiteBusinessEntity::getId).collect(Collectors.toList());

        for (Iterator<SiteBusinessEntity> iterator = list.iterator(); iterator.hasNext(); ) {
            SiteBusinessEntity siteBusinessEntity = iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(siteBusinessEntity.getParentId())) {
                recursionFn(list, siteBusinessEntity);
                returnList.add(siteBusinessEntity);
            }
            }
            if (returnList.isEmpty()) {
            returnList = list;
        }

        return returnList.stream().map(SiteBusinessTreeSelect::new).collect(Collectors.toList());
    }


    /**
    * 递归列表
    */
    private void recursionFn(List<SiteBusinessEntity> list, SiteBusinessEntity t) {
        // 得到子节点列表
        List<SiteBusinessEntity> childList = getChildList(list, t);
        t.setChildren(childList);
        for (SiteBusinessEntity tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }

    /**
    * 判断是否有子节点
    */
    private boolean hasChild(List<SiteBusinessEntity> list, SiteBusinessEntity t) {
        return getChildList(list, t).size() > 0 ? true : false;
    }

    /**
    * 得到子节点列表
    */
    private List<SiteBusinessEntity> getChildList(List<SiteBusinessEntity> list, SiteBusinessEntity t) {
        return list.stream().map(item->{
            if (!ObjectUtils.isEmpty(item.getParentId()) && item.getParentId() == t.getId()) {
                return item;
            }
            return null;
            }).filter(f->f!=null).collect(Collectors.toList());
    }

}