package com.zjrs.zwnw.jbxxgl.blo.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang.StringUtils;
import com.zjrs.zwnw.jbxxgl.blo.JbxxglCommonBLO;
import com.zjrs.zwnw.jbxxgl.dto.AreaDTO;
import com.zjrs.zwnw.jbxxgl.entity.VAaa3Mapper;
import com.zjrs.zwnw.jbxxgl.entity.VSSgAreaMapper;
import com.zjrs.zwnw.jbxxgl.po.VAaa3PO;
import com.zjrs.zwnw.jbxxgl.po.VSSgAreaPO;
import com.zjrs.zwnw.portal.dto.common.CommonDictDTO;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class JbxxglCommonBLOimpl implements JbxxglCommonBLO {
    @Resource
    private VSSgAreaMapper vsSgAreaMapper;

    @Resource
    private VAaa3Mapper vAaa3Mapper;

    @Override
    public List<Object> getXzqhTree(AreaDTO dto) {
        List<Object> orgList = new ArrayList<Object>();
        if (StringUtils.isNotBlank(dto.getParentid())) {
            //查询该节点的父节点
            QueryWrapper<VSSgAreaPO> queryWrapper = new QueryWrapper<VSSgAreaPO>();

            List<VSSgAreaPO> itemList = vsSgAreaMapper.selectList(queryWrapper);
            for (int i = 0; i < itemList.size(); i++) {
                Map<String, Object> orgmap = new HashMap<String, Object>();
                VSSgAreaPO po = itemList.get(i);
                // 拼全称，最多三级
                String alltitle = po.getAreaname() == null ? "" : po.getAreaname().trim();
                if (StringUtils.isNotBlank(po.getParentid())) {
                    VSSgAreaPO popr = vsSgAreaMapper.selectOne(new QueryWrapper<VSSgAreaPO>()
                            .eq("area_code", po.getParentid()));
                    String prname = popr.getAreaname() == null ? "" : popr.getAreaname().trim();
                    alltitle = prname + alltitle;
                    if (StringUtils.isNotBlank(popr.getParentid())){
                        VSSgAreaPO poprpr = vsSgAreaMapper.selectOne(new QueryWrapper<VSSgAreaPO>()
                                .eq("area_code", popr.getParentid()));
                        String prprname = poprpr.getAreaname() == null ? "" : poprpr.getAreaname().trim();
                        alltitle = prprname + alltitle;
                    }
                }
                orgmap.put("alltitle", alltitle);
                if (po.getIsleaf() > 0) {
                    orgmap.put("isLeaf", false);
                } else {
                    orgmap.put("isLeaf", true);
                }
                orgList.add(orgmap);
            }
        } else {
            QueryWrapper<VSSgAreaPO> queryWrapper = new QueryWrapper<VSSgAreaPO>();
            queryWrapper.select("id", "parent_id", "area_code", "area_name", "isleaf")
                    .isNull("parent_id").orderByAsc("area_code");
            List<VSSgAreaPO> itemList = vsSgAreaMapper.selectList(queryWrapper);
            for (int i = 0; i < itemList.size(); i++) {
                Map<String, Object> orgmap = new HashMap<String, Object>();
                VSSgAreaPO po = itemList.get(i);
                orgmap.put("alltitle", po.getAreaname());
                if (po.getIsleaf() > 0) {
                    orgmap.put("isLeaf", false);
                } else {
                    orgmap.put("isLeaf", true);
                }
                orgList.add(orgmap);
            }
        }
        return orgList;
    }

    @Override
    public Map<String, Object> getJbxxDicts(CommonDictDTO dto) {
        String[] arr = dto.getDictArr();
        Map<String, Object> dictMap = new HashMap<>();
        Map<String, List> codedetailDictMap = new HashMap<>();
        Map<String, List> valuelabelDictMap = new HashMap<>();
        for (int i = 0; i < arr.length; i++) {
            List codedetailList = new ArrayList();
            List valuelabelList = new ArrayList();
            QueryWrapper<VAaa3PO> queryWrapper = new QueryWrapper<VAaa3PO>();
            queryWrapper.eq("aaa100", arr[i]).eq("aae100", "1").orderByAsc("aaa102");
            List<VAaa3PO> itemList = vAaa3Mapper.selectList(queryWrapper);
            for (int j = 0; j < itemList.size(); j++) {
                Map<String, Object> keyVlueMap = new HashMap<>();
                Map<String, Object> valuelabelMap = new HashMap<>();
                VAaa3PO po = itemList.get(j);
                codedetailList.add(keyVlueMap);
                valuelabelList.add(valuelabelMap);
            }
            codedetailDictMap.put(arr[i], codedetailList);
            valuelabelDictMap.put(arr[i], valuelabelList);
        }
        return dictMap;
    }

    @Override
    public List<Object> getSshyTree(AreaDTO dto) {
        List<Object> orgList = new ArrayList<Object>();
        if (StringUtils.isNotBlank(dto.getParentid())){
            //截取字符串
            String str = dto.getParentid().trim().substring(0,2);
            List<Map<String,Object>> itemList = vAaa3Mapper.getSshyCList(str);
            for (int i = 0; i < itemList.size(); i++) {
                Map<String, Object> orgmap = new HashMap<String, Object>();
                Map Cmap = itemList.get(i);
                orgmap.put("title", Cmap.get("aaa103"));
                orgmap.put("isLeaf", true);
                orgList.add(orgmap);
            }
        } else {
            List<Map<String,Object>> itemList = vAaa3Mapper.getSshyPList();
            for (int i = 0; i < itemList.size(); i++) {
                Map<String, Object> orgmap = new HashMap<String, Object>();
                Map Pmap = itemList.get(i);
                orgmap.put("title", Pmap.get("aaa103"));
                orgmap.put("isLeaf", false);
                orgList.add(orgmap);
            }
        }
        return orgList;
    }
}
