/**
 * BladeX Commercial License Agreement
 * Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
 * <p>
 * Use of this software is governed by the Commercial License Agreement
 * obtained after purchasing a license from BladeX.
 * <p>
 * 1. This software is for development use only under a valid license
 * from BladeX.
 * <p>
 * 2. Redistribution of this software's source code to any third party
 * without a commercial license is strictly prohibited.
 * <p>
 * 3. Licensees may copyright their own code but cannot use segments
 * from this software for such purposes. Copyright of this software
 * remains with BladeX.
 * <p>
 * Using this software signifies agreement to this License, and the software
 * must not be used for illegal purposes.
 * <p>
 * THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
 * not liable for any claims arising from secondary or illegal development.
 * <p>
 * Author: Chill Zhuang (bladejava@qq.com)
 */
package org.springblade.system.feign;

import io.swagger.v3.oas.annotations.Hidden;
import lombok.AllArgsConstructor;
import org.springblade.core.tenant.annotation.NonDS;
import org.springblade.core.tool.api.R;
import org.springblade.system.pojo.entity.DictBiz;
import org.springblade.system.pojo.vo.*;
import org.springblade.system.service.IDeptService;
import org.springblade.system.service.IDictBizService;
import org.springblade.system.service.IRegionService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 字典服务Feign实现类
 *
 * @author Chill
 */
@NonDS
@Hidden
@RestController
@AllArgsConstructor
public class DictBizClient implements IDictBizClient {

	private final IDictBizService service;

	private final IDeptService deptService;

	private final IRegionService regionService;

	@Override
	@GetMapping(GET_BY_ID)
	public R<DictBiz> getById(Long id) {
		return R.data(service.getById(id));
	}

	@Override
	@GetMapping(GET_VALUE)
	public R<String> getValue(String code, String dictKey) {
		return R.data(service.getValue(code, dictKey));
	}

	@Override
	@GetMapping(GET_LIST)
	public R<List<DictBiz>> getList(String code) {
		return R.data(service.getList(code));
	}

	@Override
	@PostMapping(API_PREFIX + "/getTreeByCode")
	public R<List<DictBizVO>> getTreeByCode(Map<String, Object> params) {
		List<DictBizVO> list1 = service.getTreeByCode(params.get("code").toString(), 0L);
		if (null != list1 && list1.size() > 0) {
			for (DictBizVO dictBizVO1 : list1) {
				List<DictBizVO> list2 = service.getTreeByCode(params.get("code").toString(), dictBizVO1.getId());
				if (null != list2 && list2.size() > 0) {
					for (DictBizVO dictBizVO2 : list2) {
						List<DictBizVO> list3 = service.getTreeByCode(params.get("code").toString(), dictBizVO2.getId());
						dictBizVO2.setChildren(list3);
					}
				}
				return R.data(list2);
			}
		}
		return null;
	}

	@Override
	@PostMapping(API_PREFIX + "/getDictionary")
	@Cacheable(cacheNames = "blade:getZd")
	public R<ZdAllVO> getDictionary() {
		ZdAllVO zdAllVO = new ZdAllVO();
		String code = "";

		code = "wdhlx"; // 未电话联系
		List<ZdVO> wdhlx = service.getDictList(code);
		zdAllVO.setWdhlx(wdhlx);

		code = "ydhlx"; // 已电话联系
		List<ZdVO> ydhlx = service.getDictList(code);
		zdAllVO.setYdhlx(ydhlx);

		code = "fkjg"; // 反馈结果
		List<ZdVO> fkjg = service.getDictList(code);
		zdAllVO.setFkjg(fkjg);

		code = "sfbj"; // 是否报警
		List<ZdVO> sfbj = service.getDictList(code);
		zdAllVO.setSfbj(sfbj);

		code = "zplx"; // 诈骗类型
		List<ZdVO> zplx = service.getDictList(code);
		zdAllVO.setZplx(zplx);

		code = "sjzpxx"; // 收集涉诈信息
		List<ZdVO> sjzpxx = service.getDictList(code);
		zdAllVO.setSjzpxx(sjzpxx);

		code = "jmjg"; // 见面结果
		List<ZdVO> jmjg = service.getDictList(code);
		zdAllVO.setJmjg(jmjg);

		// 省内移交
		List<DeptVO> sjList = deptService.getSjfj();
		zdAllVO.setSjfj(sjList);

		// 省外移交行政区划
		List<XzqhVO> provinceList = regionService.getRegionByParentCode("00");
		for (XzqhVO province : provinceList) {
			List<XzqhVO> cityList = regionService.getRegionByParentCode(province.getCode());
			province.setChildren(cityList);
		}
		zdAllVO.setXzqh(provinceList);

		// 市、区、街道
		List<XzqhVO> shiList = service.getDqzdb("220000000000");
		for (XzqhVO shi : shiList) {
			List<XzqhVO> qvList = service.getDqzdb(shi.getCode());
			for (XzqhVO qv : qvList) {
				List<XzqhVO> jdList = service.getDqzdb(qv.getCode());
				qv.setChildren(jdList);
			}
			shi.setChildren(qvList);
		}
		zdAllVO.setSqjd(shiList);

		return R.data(zdAllVO);
	}
}
