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

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzya.frame.sys.product.dao.ISysProductMapper;
import com.hzya.frame.sys.product.entity.SysProduct;
import com.hzya.frame.sys.product.service.ISysProductService;
import com.hzya.frame.sys.productVersion.dao.ISysProductVersionMapper;
import com.hzya.frame.sys.productVersion.entity.SysProductVersion;
import com.hzya.frame.sys.thirdparty.product.dao.ISysThirdpartyProductMapper;
import com.hzya.frame.sys.thirdparty.product.entity.SysThirdpartyProduct;
import com.hzya.frame.sys.versionParameter.dao.ISysVersionParameterMapper;
import com.hzya.frame.sys.versionParameter.entity.SysVersionParameter;
import com.hzya.frame.web.entity.BaseResult;
import com.hzya.frame.web.entity.JsonResultEntity;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @description: 产品表  service
 * @tableName:   sys_product
 * @entityName:  Sysproduct
 * @author: 	 llg
 * @history:	 1.0
 */
@Service(value = "sysProductService")
public class  SysProductServiceImpl implements ISysProductService {

	@Resource
	private ISysProductMapper sysProductMapper;
	@Resource
	private ISysProductVersionMapper sysProductVersionMapper;
	@Resource
	private ISysThirdpartyProductMapper sysThirdpartyProductMapper;
	@Resource
	private ISysVersionParameterMapper sysVersionParameterMapper;
	/**
	 * @Author lvleigang
	 * @Description  根据产品id查询
	 * @Date 9:44 上午 2023/3/1
	 * @return com.hzya.frame.web.entity.JsonResultEntity
	 **/
	@Override
	public JsonResultEntity queryProduct(JSONObject jsonObject) {
		SysProduct sysProduct = getData("jsonStr",jsonObject,SysProduct.class);
		if(sysProduct == null || sysProduct.getId() == null || "".equals(sysProduct.getId())){
			return BaseResult.getFailureMessageEntity("查询产品id不存在");
		}
		//查询产品
		SysProduct queryEntity = sysProductMapper.entity_get(sysProduct.getId());
		if(queryEntity == null || queryEntity.getId() == null || "".equals(queryEntity.getId())){
			return BaseResult.getFailureMessageEntity("查询产品不存在");
		}
		//查询产品版本
		SysProductVersion sysProductVersion = new SysProductVersion();
		sysProductVersion.setProductId(queryEntity.getId());
		sysProductVersion.setSts("Y");
		List<SysProductVersion> sysProductVersionList = sysProductVersionMapper.entity_list_base(sysProductVersion);
		if(sysProductVersionList != null && sysProductVersionList.size() > 0){
			queryEntity.setSysProductVersionList(sysProductVersionList);
			////查询产品版本参数
			//SysVersionParameter sysVersionParameter = new SysVersionParameter();
			//sysVersionParameter.setProductId(queryEntity.getId());
			//sysVersionParameter.setSts("Y");
			//List<SysVersionParameter> sysVersionParameterList = sysVersionParameterMapper.entity_list_base(sysVersionParameter);
			//queryEntity = assembleProduct(queryEntity,sysProductVersionList,sysVersionParameterList);
		}else {
			queryEntity.setSysProductVersionList(new ArrayList<>());
		}
		return BaseResult.getSuccessMessageEntity("获取产品成功", queryEntity);
	}



	/**
	 * @Author lvleigang
	 * @Description  组装产品数据
	 * @Date 9:58 上午 2023/3/1
	 * @param queryEntity
	 * @param sysProductVersionList
	 * @param sysVersionParameterList
	 * @return com.hzya.frame.sys.product.entity.SysProduct
	 **/
	private SysProduct assembleProduct(SysProduct queryEntity, List<SysProductVersion> sysProductVersionList, List<SysVersionParameter> sysVersionParameterList) {
		if(queryEntity != null){
			if(sysProductVersionList != null && sysProductVersionList.size() > 0){
				queryEntity.setSysProductVersionList(sysProductVersionList);
				if(sysVersionParameterList != null && sysVersionParameterList.size() > 0){
					for (int i = 0; i < sysProductVersionList.size(); i++) {
						List<SysVersionParameter> parameters = new ArrayList<>();
						for (int i1 = 0; i1 < sysVersionParameterList.size(); i1++) {
							if(sysProductVersionList.get(i).getId().equals(sysVersionParameterList.get(i1).getProductVersionId())){
								parameters.add(sysVersionParameterList.get(i1));
							}
						}
						sysProductVersionList.get(i).setSysVersionParameterList(parameters);
					}
				}
			}
		}
		return queryEntity;
	}

	/**
	 * @Author lvleigang
	 * @Description  根据key获取数据jsonObject中的数据，如果没有设置默认值
	 * @Date 3:41 下午 2023/2/14
	 * @param key 健
	 * @param jsonObject json数据
	 * @return java.lang.String
	 **/
	private <T> T getData( String key, JSONObject jsonObject, Class<T> clz) {
		if (checkStr(jsonObject.getString(key)) ) {
			return jsonObject.getJSONObject(key).toJavaObject(clz);
		}
		return null;
	}

	/**
	 * @param str
	 * @return void
	 * @Author lvleigang
	 * @Description 校验字符串
	 * @Date 11:41 上午 2022/12/7
	 **/
	private Boolean checkStr(String str) {
		Boolean flag = true;
		if (str == null || "".equals(str)) {
			flag = false;
		}
		return flag;
	}

	/**
	 * @Author lvleigang
	 * @Description  新增产品
	 * @Date 9:44 上午 2023/3/1
	 * @return com.hzya.frame.web.entity.JsonResultEntity
	 **/
	@Override
	public JsonResultEntity addProduct(JSONObject jsonObject) {
		SysProduct sysProduct = getData("jsonStr",jsonObject,SysProduct.class);
		if(sysProduct == null){
			return BaseResult.getFailureMessageEntity("请求参数不存在");
		}
		//判断产品编号是否重复
		SysProduct checkEntity = new SysProduct();
		checkEntity.setCode(sysProduct.getCode());
		checkEntity.setSts("Y");
		Integer count = sysProductMapper.entity_list_base_count(checkEntity);
		if(count != null && count> 0){
			return BaseResult.getFailureMessageEntity("产品编号重复请修改");
		}
		//新增产品
		//添加产品的id，代码生成
		sysProduct.setCreateProduct();
		//新增产品版本
		//添加产品版本的id，代码生成
		if(sysProduct.getSysProductVersionList() != null && sysProduct.getSysProductVersionList().size() > 0){
			for (int i = 0; i < sysProduct.getSysProductVersionList().size(); i++) {
				SysProductVersion sysProductVersion = sysProduct.getSysProductVersionList().get(i);
				sysProductVersion.setCreateProductVersion();
				sysProductVersion.setProductId(sysProduct.getId());
				if(sysProductVersion.getSysVersionParameterList() != null && sysProductVersion.getSysVersionParameterList().size() > 0) {
					for (int a = 0; a < sysProductVersion.getSysVersionParameterList().size(); a++) {
						//产品版本参数
						//添加产品版本参数的id，代码生成
						SysVersionParameter sysVersionParameter = sysProductVersion.getSysVersionParameterList().get(a);
						sysVersionParameter.setCreateVersionParameter();
						sysVersionParameter.setProductId(sysProduct.getId());
						sysVersionParameter.setProductVersionId(sysProductVersion.getId());
						sysVersionParameterMapper.entity_insert(sysVersionParameter);
					}
				}
				//添加到第三方应用分类表
				SysThirdpartyProduct sysThirdpartyProduct = new SysThirdpartyProduct();
				sysThirdpartyProduct.setCreate();
				sysThirdpartyProduct.setProductId(sysProduct.getId());
				sysThirdpartyProduct.setProductCode(sysProduct.getCode());
				sysThirdpartyProduct.setProductName(sysProduct.getName()+sysProductVersion.getVersionNumber());
				sysThirdpartyProduct.setProductVersionId(sysProductVersion.getId());
				sysThirdpartyProduct.setVersionNumber(sysProductVersion.getVersionNumber());
				sysThirdpartyProduct.setLvl(1L);
				sysThirdpartyProduct.setUpId("0");
				sysThirdpartyProduct.setSorts(sysProductVersion.getSorts());
				sysThirdpartyProductMapper.entity_insert(sysThirdpartyProduct);
				sysProductVersionMapper.entity_insert(sysProductVersion);
			}
		}
		sysProductMapper.entity_insert(sysProduct);
		return BaseResult.getSuccessMessageEntity("新增产品成功");
	}

	/**
	 * @Author lvleigang
	 * @Description  修改产品
	 * @Date 11:58 上午 2023/3/3
	 * @param jsonObject
	 * @return com.hzya.frame.web.entity.JsonResultEntity
	 **/
	@Override
	public JsonResultEntity updateProduct(JSONObject jsonObject) {
		SysProduct sysProduct = getData("jsonStr",jsonObject,SysProduct.class);
		if(sysProduct == null || sysProduct.getId() == null || "".equals(sysProduct.getId()) ){
			return BaseResult.getFailureMessageEntity("请求参数不存在");
		}
		//新增产品
		//添加产品的id，代码生成
		sysProduct.setUpdateProduct();
		//新增产品版本
		//添加产品版本的id，代码生成
		if(sysProduct.getSysProductVersionList() != null && sysProduct.getSysProductVersionList().size() > 0){
			//先删除原先的(逻辑删除)
			delectProductVersionByProductId(sysProduct.getId());
			//先删除原先的
			delectVersionParameterByProductId(sysProduct.getId());
			//先删除原先的
			delectThirdpartyProductByProductId(sysProduct.getId());
			for (int i = 0; i < sysProduct.getSysProductVersionList().size(); i++) {
				//新增或修改产品版本
				SysProductVersion sysProductVersion = sysProduct.getSysProductVersionList().get(i);
				addOrUpdateProductVersionById(sysProductVersion,sysProduct.getId());
				addOrUpdateThirdpartyProductById(sysProductVersion,sysProduct);
				if(sysProductVersion.getSysVersionParameterList() != null && sysProductVersion.getSysVersionParameterList().size() > 0) {
					for (int a = 0; a < sysProductVersion.getSysVersionParameterList().size(); a++) {
						//产品版本参数
						//添加产品版本参数的id，代码生成
						SysVersionParameter sysVersionParameter = sysProductVersion.getSysVersionParameterList().get(a);
						addOrUpdateVersionParameterById(sysVersionParameter,sysProduct.getId(),sysProductVersion.getId());
					}
				}
			}
		}
		sysProductMapper.entity_update(sysProduct);
		return BaseResult.getSuccessMessageEntity("修改产品成功");
	}

	/**
	 * @Author lvleigang
	 * @Description  	新增或修改第三方应用表
	 * @Date 3:05 下午 2023/3/14
	 * @param sysProductVersion
	 * @param sysProduct
	 * @return void
	 **/
	private void addOrUpdateThirdpartyProductById(SysProductVersion sysProductVersion, SysProduct sysProduct) {
		if(sysProductVersion != null && sysProduct.getId() != null && !"".equals(sysProduct.getId())){
			SysThirdpartyProduct sysThirdpartyProduct = new SysThirdpartyProduct();
			sysThirdpartyProduct.setProductVersionId(sysProductVersion.getId());
			sysThirdpartyProduct.setProductId(sysProduct.getId());
			sysThirdpartyProduct.setSts("N");
			sysThirdpartyProduct.setLvl(1L);
			sysThirdpartyProduct.setUpId("0");
			List<SysThirdpartyProduct> sysThirdpartyProductList = sysThirdpartyProductMapper.entity_list_base(sysThirdpartyProduct);
			if(sysThirdpartyProductList != null && sysThirdpartyProductList.size() == 1){
				sysThirdpartyProduct = sysThirdpartyProductList.get(0);
				sysThirdpartyProduct.setUpdate();
				sysThirdpartyProduct.setSts("Y");
				sysThirdpartyProduct.setProductCode(sysProduct.getCode());
				sysThirdpartyProduct.setProductName(sysProduct.getName()+sysProductVersion.getVersionNumber());
				sysThirdpartyProduct.setVersionNumber(sysProductVersion.getVersionNumber());
				sysThirdpartyProductMapper.entity_update(sysThirdpartyProduct);
			}else {
				//添加到第三方应用分类表
				sysThirdpartyProduct.setCreate();
				sysThirdpartyProduct.setProductId(sysProduct.getId());
				sysThirdpartyProduct.setProductCode(sysProduct.getCode());
				sysThirdpartyProduct.setProductName(sysProduct.getName()+sysProductVersion.getVersionNumber());
				sysThirdpartyProduct.setProductVersionId(sysProductVersion.getId());
				sysThirdpartyProduct.setVersionNumber(sysProductVersion.getVersionNumber());
				sysThirdpartyProduct.setLvl(1L);
				sysThirdpartyProduct.setUpId("0");
				sysThirdpartyProduct.setSorts(sysProductVersion.getSorts());
				sysThirdpartyProductMapper.entity_insert(sysThirdpartyProduct);
			}
		}

	}

	/**
	 * @Author lvleigang
	 * @Description  根据产品id删除第三方表
	 * @Date 3:02 下午 2023/3/14
	 * @param id
	 * @return void
	 **/
	private void delectThirdpartyProductByProductId(String id) {
		if(id != null && !"".equals(id)){
			SysThirdpartyProduct sysThirdpartyProduct = new SysThirdpartyProduct();
			sysThirdpartyProduct.setProductId(id);
			sysThirdpartyProduct.setSts("Y");
			sysThirdpartyProduct.setLvl(1L);
			sysThirdpartyProduct.setUpId("0");
			sysThirdpartyProduct.setUpdate();
			sysThirdpartyProductMapper.entity_logicDelete_Multi_Condition(sysThirdpartyProduct);
		}
	}

	/**
	 * @Author lvleigang
	 * @Description  删除产品
	 * @Date 4:36 下午 2023/3/3
	 * @param jsonObject
	 * @return com.hzya.frame.web.entity.JsonResultEntity
	 **/
	@Override
	public JsonResultEntity delectProduct(JSONObject jsonObject) {
		SysProduct sysProduct = getData("jsonStr",jsonObject,SysProduct.class);
		if(sysProduct == null || sysProduct.getId() == null || "".equals(sysProduct.getId())){
			return BaseResult.getFailureMessageEntity("请求参数不存在");
		}
		//删除原先的(逻辑删除)
		delectProductVersionByProductId(sysProduct.getId());
		//删除原先的
		delectVersionParameterByProductId(sysProduct.getId());

		//删除原先的
		delectProductByProductId(sysProduct.getId());
		//删除原先的
		delectThirdpartyProductByProductId(sysProduct.getId());

		return BaseResult.getSuccessMessageEntity("删除产品成功");
	}

	/**
	 * @Author lvleigang
	 * @Description  分页查询数据成功
	 * @Date 11:00 上午 2023/3/14
	 * @param jsonObject
	 * @return com.hzya.frame.web.entity.JsonResultEntity
	 **/
	@Override
	public JsonResultEntity queryPageProduct(JSONObject jsonObject) {

		SysProduct sysProduct = getData("jsonStr",jsonObject,SysProduct.class);
		if(sysProduct == null || sysProduct.getPageNum() == null || sysProduct.getPageSize() == null){
			return BaseResult.getFailureMessageEntity("分页查询参数不存在");
		}
		//查询产品
		PageHelper.startPage(sysProduct.getPageNum(), sysProduct.getPageSize());
		sysProduct.setSts("Y");
		List<SysProduct>  sysProductList = sysProductMapper.entity_list_like(sysProduct);
		PageInfo pageInfo = new PageInfo(sysProductList);
		return BaseResult.getSuccessMessageEntity("获取产品成功", pageInfo);
	}

	/**
	 * @Author lvleigang
	 * @Description  获取所有产品
	 * @Date 4:32 下午 2023/3/14
	 * @param jsonObject
	 * @return com.hzya.frame.web.entity.JsonResultEntity
	 **/
	@Override
	public JsonResultEntity queryProductAll(JSONObject jsonObject) {
		SysProduct sysProduct = getData("jsonStr",jsonObject,SysProduct.class);
		sysProduct.setSts("Y");
		//查询产品参数
		List<SysProduct> sysProductList = sysProductMapper.entity_list_base(sysProduct);
		return BaseResult.getSuccessMessageEntity("获取产品成功", sysProductList);
	}

	/**
	 * @Author lvleigang
	 * @Description  新增或修改产品版本参数
	 * @Date 3:35 下午 2023/3/3
	 * @param sysVersionParameter
	 * @param productid
	 * @param productVersionid
	 * @return void
	 **/
	private void addOrUpdateVersionParameterById(SysVersionParameter sysVersionParameter, String productid, String productVersionid) {
		if(sysVersionParameter != null && productid != null && !"".equals(productid) && productVersionid != null && !"".equals(productVersionid)) {
			sysVersionParameter.setProductId(productid);
			sysVersionParameter.setProductVersionId(productVersionid);
			if(sysVersionParameter.getId() == null || "".equals(sysVersionParameter.getId())){
				sysVersionParameter.setCreateVersionParameter();
				sysVersionParameterMapper.entity_insert(sysVersionParameter);
			} else {
				sysVersionParameter.setUpdateVersionParameter();
				sysVersionParameterMapper.entity_update(sysVersionParameter);
			}
		}
	}

	/**
	 * @Author lvleigang
	 * @Description  删除版本参数表
	 * @Date 3:32 下午 2023/3/3
	 * @param id
	 * @return void
	 **/
	private void delectVersionParameterByProductId(String id) {
		if(id != null && !"".equals(id)){
			SysVersionParameter delectVersionParameter = new SysVersionParameter();
			delectVersionParameter.setProductId(id);
			delectVersionParameter.setSts("Y");
			delectVersionParameter.setUpdateVersionParameter();
			sysVersionParameterMapper.logicDelete_Multi_Condition(delectVersionParameter);
		}

	}

	/**
	 * @Author lvleigang
	 * @Description  删除产品
	 * @Date 3:32 下午 2023/3/3
	 * @param id
	 * @return void
	 **/
	private void delectProductByProductId(String id) {
		if(id != null && !"".equals(id)){
			SysProduct sysProduct = new SysProduct();
			sysProduct.setId(id);
			sysProduct.setSts("Y");
			sysProduct.setUpdateProduct();
			sysProductMapper.logicDelete_Multi_Condition(sysProduct);
		}

	}


	/**
	 * @Author lvleigang
	 * @Description  新增或修改产品版本
	 * @Date 3:30 下午 2023/3/3
	 * @param sysProductVersion
	 * @param id
	 * @return void
	 **/
	private void addOrUpdateProductVersionById(SysProductVersion sysProductVersion , String id) {
		if(sysProductVersion != null && id != null && !"".equals(id)){
			sysProductVersion.setProductId(id);
			if(sysProductVersion.getId() == null || "".equals(sysProductVersion.getId())){
				sysProductVersion.setCreateProductVersion();
				sysProductVersionMapper.entity_insert(sysProductVersion);
			}else {
				sysProductVersion.setUpdateProductVersion();
				sysProductVersionMapper.entity_update(sysProductVersion);
			}
		}
	}

	/**
	 * @Author lvleigang
	 * @Description  根据id逻辑删除产品版本表数据
	 * @Date 3:21 下午 2023/3/3
	 * @param id
	 * @return void
	 **/
	private void delectProductVersionByProductId(String id) {
		if(id != null && !"".equals(id)){
			SysProductVersion delectProductVersion = new SysProductVersion();
			delectProductVersion.setProductId(id);
			delectProductVersion.setSts("Y");
			delectProductVersion.setUpdateProductVersion();
			sysProductVersionMapper.logicDelete_Multi_Condition(delectProductVersion);
		}
	}
}
