package com.zcloud.modules.sys.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zcloud.common.utils.PageUtils;
import com.zcloud.common.utils.Query;
import com.zcloud.common.utils.Tools;
import com.zcloud.datasource.annotation.DataSource;
import com.zcloud.modules.sys.dao.SysDictionariesDao;
import com.zcloud.modules.sys.entity.SysDictionariesEntity;
import com.zcloud.modules.sys.service.SysDictionariesService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.*;
import java.util.stream.Collectors;


@Service("sysDictionariesService")
public class SysDictionariesServiceImpl extends ServiceImpl<SysDictionariesDao, SysDictionariesEntity> implements SysDictionariesService {

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		params.put("sidx","order_by");
		params.put("order","asc");
		String parentId = (String)params.get("parentId");

		IPage<SysDictionariesEntity> page = this.page(
				new Query<SysDictionariesEntity>().getPage(params),
				new QueryWrapper<SysDictionariesEntity>().eq("parent_id", parentId)
		);

		return new PageUtils(page);
	}
	@Override
	public List<SysDictionariesEntity> listTreeCte(String parentId) {
		List<SysDictionariesEntity> dictionariesList = listAllCte(parentId);
		return Tools.buildEntityTree(dictionariesList, "dictionariesId", "parentId", "list", parentId);
	}


	@Override
	public List<SysDictionariesEntity> listAllCte(String parentId) {
		Map<String,Object> params = new HashMap<>();
		params.put("parentId", parentId);
		List<SysDictionariesEntity> dictionariesList = baseMapper.listTreeCte(params);
		return dictionariesList;
	}


	@Override
	public void delete(String dictionariesId){
		//删除菜单
		this.removeById(dictionariesId);
	}

	@Override
	public SysDictionariesEntity queryByBianma(String bianma){
		return  this.baseMapper.selectOne(new QueryWrapper<SysDictionariesEntity>()
				.eq("bianma",bianma));
	}

	@Override
	public String getRecurrenceIds(String id){
		return  this.baseMapper.getRecurrenceIds(id);
	}


	@Override
	@Transactional(rollbackFor = Exception.class)
	public void importAreaFromJson(){
		try {
			BufferedReader br = new BufferedReader(new FileReader("D:\\educationWorkSpace\\untitled\\index_2022__level_5.json"));
			String buf = br.readLine();
			StringBuffer json = new StringBuffer();
			while (buf != null) {
				json.append(buf);
				buf = br.readLine();
			}
			List<JSONObject> jsonList = (List<JSONObject>) JSON.parse(json.toString());
			int porder = 0;
			for (JSONObject provinces : jsonList) {
				SysDictionariesEntity ppd = new SysDictionariesEntity();
				ppd.setDictionariesId(get32UUID());	//主键
				ppd.setName(provinces.getString("label"));
				ppd.setBianma(provinces.getString("value").substring(0,9));
				ppd.setOrderBy(porder);
				ppd.setParentId("e725d2a91b8248f4b8f49889038df7de");
				save(ppd);
				porder++;

				int ciorder = 0;
				for (JSONObject city : (List<JSONObject>)provinces.get("children")){
					SysDictionariesEntity cipd = new SysDictionariesEntity();
					cipd.setDictionariesId(get32UUID());	//主键
					cipd.setName(city.getString("label"));
					cipd.setBianma(city.getString("value").substring(0,9));
					cipd.setOrderBy(ciorder);
					cipd.setParentId(ppd.getDictionariesId());
					save(cipd);
					ciorder++;

					int corder = 0;
					for (JSONObject county : (List<JSONObject>)city.get("children")){
						SysDictionariesEntity cpd = new SysDictionariesEntity();
						cpd.setDictionariesId(get32UUID());	//主键
						cpd.setName(county.getString("label"));
						cpd.setBianma(county.getString("value").substring(0,9));
						cpd.setOrderBy(corder);
						cpd.setParentId(cipd.getDictionariesId());
						save(cpd);

						int vorder = 0;
						for (JSONObject village : (List<JSONObject>)county.get("children")){
							SysDictionariesEntity vpd = new SysDictionariesEntity();
							vpd.setDictionariesId(get32UUID());	//主键
							vpd.setName(village.getString("label"));
							vpd.setBianma(village.getString("value").substring(0,9));
							vpd.setOrderBy(vorder);
							vpd.setParentId(cpd.getDictionariesId());
							save(vpd);
							vorder++;

							int order = 0;
							for (JSONObject street : (List<JSONObject>)village.get("children")){
								SysDictionariesEntity spd = new SysDictionariesEntity();
								spd.setDictionariesId(get32UUID());	//主键
								spd.setName(street.getString("label"));
								spd.setBianma(street.getString("value").substring(0,9));
								spd.setOrderBy(vorder);
								spd.setParentId(vpd.getDictionariesId());
								save(spd);
								order ++;
							}
						}
					}
				}
			}
			System.out.println("=====================导入完成===================");
		}catch (Exception e){
			e.printStackTrace();
		}
	}

	public static String get32UUID() {
		String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
		return uuid;
	}
}
