package com.hzya.frame.sys.thirdparty.product.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzya.frame.sys.thirdparty.api.certification.dao.ISysThirdpartyapiCertificationMapper;
import com.hzya.frame.sys.thirdparty.api.dao.ISysThirdpartyapiMapper;
import com.hzya.frame.sys.thirdparty.api.entity.SysThirdpartyapi;
import com.hzya.frame.sys.thirdparty.api.parameter.dao.ISysThirdpartyapiParameterMapper;
import com.hzya.frame.sys.thirdparty.api.test.dao.ISysThirdpartyapiTestMapper;
import com.hzya.frame.sys.thirdparty.api.testparameter.dao.ISysThirdpartyapiTestparameterMapper;
import com.hzya.frame.web.entity.BaseResult;
import org.springframework.stereotype.Service;
import com.hzya.frame.serviceUtil.ServiceUtil;
import com.hzya.frame.sys.thirdparty.product.entity.SysThirdpartyProduct;
import com.hzya.frame.sys.thirdparty.product.service.ISysThirdpartyProductService;
import com.hzya.frame.sys.thirdparty.product.dao.ISysThirdpartyProductMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.hzya.frame.web.entity.JsonResultEntity;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * @description: 第三方应用分类表  service
 * @tableName:   sys_thirdparty_product
 * @entityName:  SysThirdpartyProduct
 * @author: 	 gjh
 * @history:	 1.0
 */
@Service(value="systhirdpartyproductService")
public class  SysThirdpartyProductServiceImpl extends ServiceUtil implements ISysThirdpartyProductService{
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Resource
	protected ISysThirdpartyProductMapper  systhirdpartyproductMapper;
	@Resource
	protected ISysThirdpartyapiMapper sysThirdpartyapiMapper;
	@Resource
	protected ISysThirdpartyapiParameterMapper sysThirdpartyapiParameterMapper;
	@Resource
	protected ISysThirdpartyapiCertificationMapper sysThirdpartyapiCertificationMapper;
	@Resource
	protected ISysThirdpartyapiTestMapper sysThirdpartyapiTestMapper;
	@Resource
	protected ISysThirdpartyapiTestparameterMapper sysThirdpartyapiTestparameterMapper;

	/**
	 * @Author lvleigang
	 * @Description  查询第三方应用分类树
	 * @Date 11:15 上午 2023/3/6
	 * @param jsonObject
	 * @return com.hzya.frame.web.entity.JsonResultEntity
	 **/
	@Override
	public JsonResultEntity queryThirdpartyProductTree(JSONObject jsonObject) {
		//先查询所有的
		List<SysThirdpartyProduct> list = new ArrayList<>();
		SysThirdpartyProduct sysThirdpartyProduct = new SysThirdpartyProduct();
		sysThirdpartyProduct.setSts("Y");
		List<SysThirdpartyProduct> sysThirdpartyProductList = systhirdpartyproductMapper.entity_list_like(sysThirdpartyProduct);
		//根据上级id递归设置
		sysThirdpartyProduct.setId("0");
		sysThirdpartyProduct.setLvl(0L);
		sysThirdpartyProduct.setProductName("接口分类");
		recursiveQueryTreeListId(sysThirdpartyProduct,sysThirdpartyProductList);
		list.add(sysThirdpartyProduct);
		return BaseResult.getSuccessMessageEntity("获取树列表成功", list);
	}

	/**
	 * @Author lvleigang
	 * @Description  新增树节点
	 * @Date 2:27 下午 2023/3/15
	 * @param jsonObject
	 * @return com.hzya.frame.web.entity.JsonResultEntity
	 **/
	@Override
	public JsonResultEntity addThirdpartyProduct(JSONObject jsonObject) {
		SysThirdpartyProduct sysThirdpartyProduct = getData("jsonStr",jsonObject,SysThirdpartyProduct.class);
		if(sysThirdpartyProduct == null){
			return BaseResult.getFailureMessageEntity("请求参数不存在");
		}
		//判断编号是否重复
		SysThirdpartyProduct checkEntity = new SysThirdpartyProduct();
		checkEntity.setProductCode(sysThirdpartyProduct.getProductCode());
		checkEntity.setSts("Y");
		Integer count = systhirdpartyproductMapper.entity_count(checkEntity);
		if(count != null && count> 0){
			return BaseResult.getFailureMessageEntity("编号重复请修改");
		}
		sysThirdpartyProduct.setCreate();
		systhirdpartyproductMapper.entity_insert(sysThirdpartyProduct);
		return BaseResult.getSuccessMessageEntity("新增接口分类成功");
	}

	/**
	 * @Author lvleigang
	 * @Description  修改树节点
	 * @Date 2:50 下午 2023/3/15
	 * @param jsonObject
	 * @return com.hzya.frame.web.entity.JsonResultEntity
	 **/
	@Override
	public JsonResultEntity updateThirdpartyProduct(JSONObject jsonObject) {
		SysThirdpartyProduct sysThirdpartyProduct = getData("jsonStr",jsonObject,SysThirdpartyProduct.class);
		if(sysThirdpartyProduct == null ||  sysThirdpartyProduct.getId() == null || "".equals(sysThirdpartyProduct.getId()) ){
			return BaseResult.getFailureMessageEntity("请求参数不存在");
		}
		sysThirdpartyProduct.setUpdate();
		systhirdpartyproductMapper.entity_update(sysThirdpartyProduct);
		return BaseResult.getSuccessMessageEntity("修改接口分类成功");
	}

	/**
	 * @Author lvleigang
	 * @Description  删除树节点
	 * @Date 2:54 下午 2023/3/15
	 * @param jsonObject
	 * @return com.hzya.frame.web.entity.JsonResultEntity
	 **/
	@Override
	public JsonResultEntity delectThirdpartyProduct(JSONObject jsonObject) {
		//根据当前节点id，删除下级分类，以及下级接口
		SysThirdpartyProduct sysThirdpartyProduct = getData("jsonStr",jsonObject,SysThirdpartyProduct.class);
		if(sysThirdpartyProduct == null ||  sysThirdpartyProduct.getId() == null || "".equals(sysThirdpartyProduct.getId()) ){
			return BaseResult.getFailureMessageEntity("请求参数不存在");
		}
		//先查询所有的
		SysThirdpartyProduct queryall = new SysThirdpartyProduct();
		queryall.setSts("Y");
		List<SysThirdpartyProduct> sysThirdpartyProductList = systhirdpartyproductMapper.entity_list_like(queryall);

		//递归查询第三方应用表id
		List<String> ids = new ArrayList<>();
		recursiveQueryIds(ids,sysThirdpartyProduct.getId(),sysThirdpartyProductList);
 		// 根据第三方应用表id查询接口
		List<String> interviceids = sysThirdpartyapiMapper.entity_getID_by_thirdpartyProductId(ids);
		if(interviceids != null && interviceids.size() > 0){
			//根据接口id删除第三方应用接口参数表
			sysThirdpartyapiParameterMapper.delect_by_ThirdpartyApiId(interviceids);
			//根据接口id删除第三方应用接口认证信息表
			sysThirdpartyapiCertificationMapper.delect_by_ThirdpartyApiId(interviceids);
			//根据接口id删除第三方应用接口测试表
			sysThirdpartyapiTestMapper.delect_by_ThirdpartyApiId(interviceids);
			//根据接口id删除第三方应用接口测试参数表
			sysThirdpartyapiTestparameterMapper.delect_by_ThirdpartyApiId(interviceids);
			// 根据接口id删除接口
			sysThirdpartyapiMapper.delect_by_Id(interviceids);
		}
		// 应用表id 删除第三方应用表
		systhirdpartyproductMapper.delect_by_Id(ids);
		return BaseResult.getSuccessMessageEntity("删除接口分类成功");
	}

	/**
	 * @Author lvleigang
	 * @Description  根据id获取单个信息
	 * @Date 4:11 下午 2023/3/15
	 * @param jsonObject
	 * @return com.hzya.frame.web.entity.JsonResultEntity
	 **/
	@Override
	public JsonResultEntity getThirdpartyProduct(JSONObject jsonObject) {
		//根据当前节点id，删除下级分类，以及下级接口
		SysThirdpartyProduct sysThirdpartyProduct = getData("jsonStr",jsonObject,SysThirdpartyProduct.class);
		if(sysThirdpartyProduct == null ||  sysThirdpartyProduct.getId() == null || "".equals(sysThirdpartyProduct.getId()) ){
			return BaseResult.getFailureMessageEntity("请求参数不存在");
		}
		sysThirdpartyProduct = systhirdpartyproductMapper.entity_get(sysThirdpartyProduct.getId());
		return BaseResult.getSuccessMessageEntity("获取接口分类成功",sysThirdpartyProduct);
	}
	/**
	 * @Author lvleigang
	 * @Description  根据树节点ID，获取所有的接口
	 * @Date 4:13 下午 2023/3/15
	 * @param jsonObject
	 * @return com.hzya.frame.web.entity.JsonResultEntity
	 **/
	@Override
	public JsonResultEntity getThirdpartyapiByThirdpartyProductId(JSONObject jsonObject) {
		//根据当前节点id，删除下级分类，以及下级接口
		SysThirdpartyapi sysThirdpartyapi = getData("jsonStr",jsonObject,SysThirdpartyapi.class);
		if(sysThirdpartyapi == null ||  sysThirdpartyapi.getThirdpartyProductId() == null || "".equals(sysThirdpartyapi.getThirdpartyProductId()) ){
			return BaseResult.getFailureMessageEntity("请求参数不存在");
		}
		//先查询所有的分类
		List<String> ids = new ArrayList<>();

		if(!"0".equals(sysThirdpartyapi.getThirdpartyProductId())){
			SysThirdpartyProduct queryall = new SysThirdpartyProduct();
			queryall.setSts("Y");
			List<SysThirdpartyProduct> sysThirdpartyProductList = systhirdpartyproductMapper.entity_list_like(queryall);

			//递归查询第三方应用表id
			recursiveQueryIds(ids,sysThirdpartyapi.getThirdpartyProductId(),sysThirdpartyProductList);
		}
		//查询产品
		PageHelper.startPage(sysThirdpartyapi.getPageNum() != null? sysThirdpartyapi.getPageNum() : 1, sysThirdpartyapi.getPageSize() != null? sysThirdpartyapi.getPageSize() : 10);
		SysThirdpartyapi queryAPage  = new SysThirdpartyapi();
		queryAPage.setSts("Y");
		queryAPage.setIds(ids);
		queryAPage.setName(sysThirdpartyapi.getName());
		queryAPage.setProductId(sysThirdpartyapi.getProductId());
		queryAPage.setApiType(sysThirdpartyapi.getApiType());
		queryAPage.setSketch(sysThirdpartyapi.getSketch());
		List<SysThirdpartyapi> interviceids = sysThirdpartyapiMapper.entity_list_like(queryAPage);
		PageInfo pageInfo = new PageInfo(interviceids);
		return BaseResult.getSuccessMessageEntity("获取接口成功", pageInfo);
	}

	/**
	 * @Author lvleigang
	 * @Description  递归设置id
	 * @Date 3:20 下午 2023/3/15
	 * @param ids
	 * @param id
	 * @param sysThirdpartyProductList
	 * @return void
	 **/
	private void recursiveQueryIds(List<String> ids ,String id, List<SysThirdpartyProduct> sysThirdpartyProductList) {
		if(id != null && sysThirdpartyProductList != null && sysThirdpartyProductList.size() > 0){
			ids.add(id);
			for (int i = 0; i < sysThirdpartyProductList.size(); i++) {
				//等于上级id的时候
				if(id.equals(sysThirdpartyProductList.get(i).getUpId())){
					recursiveQueryIds(ids,sysThirdpartyProductList.get(i).getId(),sysThirdpartyProductList);
				}
			}
		}
	}


	/**
	 * @Author lvleigang
	 * @Description  根据上级编号设置数据
	 * @Date 11:59 上午 2023/3/14
	 * @param sysThirdpartyProduct
	 * @param sysThirdpartyProductList
	 * @return void
	 **/
	private void recursiveQueryTreeListId(SysThirdpartyProduct sysThirdpartyProduct, List<SysThirdpartyProduct> sysThirdpartyProductList) {
		if(sysThirdpartyProduct != null && sysThirdpartyProductList != null && sysThirdpartyProductList.size() > 0){
			List<SysThirdpartyProduct> chiledList = new ArrayList<>();
			for (int i = 0; i < sysThirdpartyProductList.size(); i++) {
				//等于上级id的时候
				if(sysThirdpartyProduct.getId().equals(sysThirdpartyProductList.get(i).getUpId())){
					recursiveQueryTreeListId(sysThirdpartyProductList.get(i),sysThirdpartyProductList);
					chiledList.add(sysThirdpartyProductList.get(i));
				}
			}
			sysThirdpartyProduct.setChiledList(chiledList.size() > 0 ? chiledList : null);
		}
	}
}
