package com.zcloud.primeport.command.query;

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

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


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

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    public ClosedAreaCO queryById(Long id) {
        return closedAreaCoConvertor.converDOToCO(closedAreaRepository.getById(id));
    }

    /**
     * 分页
     *
     * @param closedAreaPageQry
     * @return
     */
    public PageResponse<ClosedAreaCO> execute(ClosedAreaPageQry closedAreaPageQry) {
        Map<String, Object> params = PageQueryHelper.toHashMap(closedAreaPageQry);
        PageResponse<ClosedAreaDO> pageResponse = closedAreaRepository.listPage(params);
        List<ClosedAreaCO> examCenterCOS = closedAreaCoConvertor.converDOsToCOs(pageResponse.getData());
        return PageResponse.of(examCenterCOS, pageResponse.getTotalCount(), pageResponse.getPageSize(), pageResponse.getPageIndex());
    }

    public List<ClosedAreaCO> getCountByHgAuthArea() {
        List<ClosedAreaDO> dataResList = closedAreaRepository.getCountByHgAuthArea();
        List<ClosedAreaCO> examCenterCOS = closedAreaCoConvertor.converDOsToCOs(dataResList);
        return examCenterCOS;
    }

    public List<ClosedAreaCO> listAllByJurisdictionalCorpId(Long id) {
        QueryWrapper<ClosedAreaDO> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("jurisdictional_corp_id", id);
        List<ClosedAreaDO> list = closedAreaRepository.list(objectQueryWrapper);
        return closedAreaCoConvertor.converDOsToCOs(list);
    }

    public MultiResponse<ClosedAreaCO> listTree(ClosedAreaPageQry qry) {
        QueryWrapper<ClosedAreaDO> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq(Tools.notEmpty(qry.getHgAuthArea()), "hg_auth_area", qry.getHgAuthArea());
        objectQueryWrapper.eq(qry.getParentId() != null, "parent_id", qry.getParentId());
        objectQueryWrapper.eq(qry.getJurisdictionalCorpId() != null, "jurisdictional_corp_id", qry.getJurisdictionalCorpId());
        List<ClosedAreaDO> list = closedAreaRepository.list(objectQueryWrapper);
        ArrayList<ClosedAreaCO> closedAreaDOS = new ArrayList<>();
        for (ClosedAreaDO closedAreaDO : list) {
            if (closedAreaDO.getParentId().equals(0L)) {
                closedAreaDOS.add(closedAreaCoConvertor.converDOToCO(closedAreaDO));
            }
        }
        for (ClosedAreaCO closedAreaCO : closedAreaDOS) {
            closedAreaCO.setChildren(treeChildren(closedAreaCO.getId(), list));
        }
        return MultiResponse.of(closedAreaDOS);
    }

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

    public MultiResponse<ClosedAreaCO> listAllByhgAuthArea(String hgAuthArea) {
        QueryWrapper<ClosedAreaDO> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("hg_auth_area",hgAuthArea);
        List<ClosedAreaDO> list = closedAreaRepository.list(objectQueryWrapper);
        List<ClosedAreaCO> closedAreaCOS = closedAreaCoConvertor.converDOsToCOs(list);
        return MultiResponse.of(closedAreaCOS);
    }
}

