package com.zcloud.primeport.command.query;

import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.PageResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jjb.saas.framework.auth.utils.AuthContext;
import com.zcloud.gbscommon.utils.PageQueryHelper;
import com.zcloud.gbscommon.utils.Tools;
import com.zcloud.primeport.command.convertor.ClosedAreaCoConvertor;
import com.zcloud.primeport.command.convertor.ClosedAreaCountCoConvertor;
import com.zcloud.primeport.dto.ClosedAreaCountCmd;
import com.zcloud.primeport.dto.ClosedAreaPageQry;
import com.zcloud.primeport.dto.clientobject.ClosedAreaCO;
import com.zcloud.primeport.dto.clientobject.ClosedAreaCountCO;
import com.zcloud.primeport.persistence.dataobject.ClosedAreaCountDO;
import com.zcloud.primeport.persistence.dataobject.ClosedAreaDO;
import com.zcloud.primeport.persistence.repository.ClosedAreaRepository;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * web-app
 *
 * @Author dearLin
 * @Date 2026-03-19 10:27:51
 */
@Component
@AllArgsConstructor
public class ClosedAreaCountExe {
    private final ClosedAreaRepository closedAreaRepository;
    private final ClosedAreaCountCoConvertor closedAreaCountCoConvertor;


    /**
     * 分页
     *
     * @param cmd
     * @return
     */
    public MultiResponse<ClosedAreaCountCO> execute(ClosedAreaCountCmd cmd) {

        Map<String, Object> params = PageQueryHelper.toHashMap(cmd);
//        if (cmd.getCorpId() != null) {
//            params.put("corpId", cmd.getCorpId());
//        }else {
//            params.put("corpId", AuthContext.getTenantId());
//        }
        List<ClosedAreaCountDO> pageResponse = closedAreaRepository.listCount(params);

        List<ClosedAreaCountCO> examCenterCOS = closedAreaCountCoConvertor.converDOsToCOs(pageResponse);
        for (ClosedAreaCountCO closedAreaCountCO : examCenterCOS) {
            closedAreaCountCO.setChildren(treeChildren(closedAreaCountCO.getId(), pageResponse));
        }
        // 递归汇总子节点的人员数和车辆数到父节点
        for (ClosedAreaCountCO closedAreaCountCO : examCenterCOS) {
            sumChildrenCount(closedAreaCountCO);
        }
        return MultiResponse.of(examCenterCOS);
    }

    private List<ClosedAreaCountCO> treeChildren(Long parentId, List<ClosedAreaCountDO> closedAreaDOS) {
        List<ClosedAreaCountCO> childList = new ArrayList<>();
        for (ClosedAreaCountDO closedAreaCountDO : closedAreaDOS) {
            //遍历所有节点，与上级传过来的父节点id比较
            if (parentId.equals(closedAreaCountDO.getParentId())) {
                childList.add(closedAreaCountCoConvertor.converDOToCO(closedAreaCountDO));
            }
        }
        //递归
        for (ClosedAreaCountCO route : childList) {
            route.setChildren(treeChildren(route.getId(), closedAreaDOS));
        }
        return childList.isEmpty() ? null : childList;
    }

    /**
     * 递归汇总子节点的人员数和车辆数到父节点
     * 父节点自身可能有直接关联的申请，加上所有子节点的数量
     */
    private void sumChildrenCount(ClosedAreaCountCO node) {
        List<ClosedAreaCountCO> children = node.getChildren();
        if (children == null || children.isEmpty()) {
            return;
        }
        int personSum = 0;
        int carSum = 0;
        for (ClosedAreaCountCO child : children) {
            sumChildrenCount(child);
            personSum += (child.getPersonCount() != null ? child.getPersonCount() : 0);
            carSum += (child.getCarCount() != null ? child.getCarCount() : 0);
        }
        // 父节点自身直接关联的数量 + 所有子节点的汇总
        node.setPersonCount((node.getPersonCount() != null ? node.getPersonCount() : 0) + personSum);
        node.setCarCount((node.getCarCount() != null ? node.getCarCount() : 0) + carSum);
    }
}

