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

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hzya.frame.sys.flow.entity.SysFlowNifiConstantEntity;
import com.hzya.frame.sys.flow.dao.ISysFlowNifiConstantDao;
import com.hzya.frame.sys.flow.service.ISysFlowNifiConstantService;
import com.hzya.frame.web.entity.BaseResult;
import com.hzya.frame.web.entity.JsonResultEntity;
import com.hzya.frame.web.exception.BaseSystemException;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import javax.annotation.Resource;
import com.hzya.frame.basedao.service.impl.BaseService;

import java.util.List;

/**
 * nifi常量(SysFlowNifiConstant)表服务实现类
 *
 * @author xiang2lin
 * @since 2025-04-29 10:16:27
 */
@Service(value = "sysFlowNifiConstantService")
public class SysFlowNifiConstantServiceImpl extends BaseService<SysFlowNifiConstantEntity, String> implements ISysFlowNifiConstantService {
     
    private ISysFlowNifiConstantDao sysFlowNifiConstantDao;
    
    @Autowired
	public void setSysFlowNifiConstantDao(ISysFlowNifiConstantDao dao) {
		this.sysFlowNifiConstantDao = dao;
		this.dao = dao;
	}

	/**
	 * 详情
	 *
	 * @param object
	 * @return
	 */
	@Override
	public JsonResultEntity getNifiConstant(JSONObject object) {
		SysFlowNifiConstantEntity sysFlowNifiConstantEntity = null;
		try {
			sysFlowNifiConstantEntity = preCheck(object,"get");
		}catch(Exception e){
			return BaseResult.getFailureMessageEntity(e.getMessage());
		}
		SysFlowNifiConstantEntity nifiConstant = sysFlowNifiConstantDao.queryOne(sysFlowNifiConstantEntity);
		return BaseResult.getSuccessMessageEntity("查询详情成功",nifiConstant);
	}

	/**
	 * 保存nifi常量
	 *
	 * @param object
	 * @return
	 */
	@Override
	public JsonResultEntity saveNifiConstant(JSONObject object) {
		SysFlowNifiConstantEntity sysFlowNifiConstantEntity = null;
		try {
			sysFlowNifiConstantEntity = preCheck(object,"save");
		}catch(Exception e){
			return BaseResult.getFailureMessageEntity(e.getMessage());
		}
		sysFlowNifiConstantDao.save(sysFlowNifiConstantEntity);
		return BaseResult.getSuccessMessageEntity("保存成功");
	}

	/**
	 * 更新nifi常量
	 *
	 * @param object
	 * @return
	 */
	@Override
	public JsonResultEntity updateNifiConstant(JSONObject object) {
		SysFlowNifiConstantEntity sysFlowNifiConstantEntity = null;
		try {
			sysFlowNifiConstantEntity = preCheck(object,"update");
		}catch(Exception e){
			return BaseResult.getFailureMessageEntity(e.getMessage());
		}
		sysFlowNifiConstantDao.update(sysFlowNifiConstantEntity);
		return BaseResult.getSuccessMessageEntity("更新成功");
	}

	/**
	 * 更新nifi常量
	 *
	 * @param object
	 * @return
	 */
	@Override
	public JsonResultEntity deleteNifiConstant(JSONObject object) {
		SysFlowNifiConstantEntity sysFlowNifiConstantEntity = null;
		try {
			sysFlowNifiConstantEntity = preCheck(object,"delete");
		}catch(Exception e){
			return BaseResult.getFailureMessageEntity(e.getMessage());
		}
		sysFlowNifiConstantDao.logicRemove(sysFlowNifiConstantEntity);
		return BaseResult.getSuccessMessageEntity("删除成功");
	}


	/**
	 * 参数校验
	 * @param entity
	 * @param type
	 */
	private void checkParams(SysFlowNifiConstantEntity entity,String type){
		Assert.notNull(entity,"请求参数不能为空");
		if ("save".equals(type)){
			Assert.notEmpty(entity.getNifiKey(),"nifiKey不能为空");
			Assert.notEmpty(entity.getNifiValue(),"nifiValue不能为空");
			//检查是否有重名的key
			SysFlowNifiConstantEntity nifi = new SysFlowNifiConstantEntity();
			nifi.setNifiKey(entity.getNifiKey());
			List<SysFlowNifiConstantEntity> query = sysFlowNifiConstantDao.query(nifi);
			if (CollectionUtils.isNotEmpty(query)){
				throw new BaseSystemException(nifi.getNifiKey()+"重复");
			}
		}else if ("update".equals(type)){
			Assert.notEmpty(entity.getId(),"id不能为空");
			Assert.notEmpty(entity.getNifiKey(),"key不能为空");
			Assert.notEmpty(entity.getNifiValue(),"value不能为空");
			SysFlowNifiConstantEntity nifi = new SysFlowNifiConstantEntity();
			nifi.setNifiKey(entity.getNifiKey());
			List<SysFlowNifiConstantEntity> query = sysFlowNifiConstantDao.query(nifi);
			if (CollectionUtils.isNotEmpty(query)){
				for (SysFlowNifiConstantEntity n : query) {
					if (!n.getId().equals(entity.getId())){
						throw new BaseSystemException(entity.getNifiKey()+"重复");
					}
				}
			}
		}else if ("delete".equals(type)){
			Assert.notEmpty(entity.getId(),"id不能为空");
		}else if ("get".equals(type)){
			Assert.notEmpty(entity.getId(),"id不能为空");
		}
	}

	//前置操作
	private SysFlowNifiConstantEntity preCheck(JSONObject object,String type){
		SysFlowNifiConstantEntity entity = getData("jsonStr", object,SysFlowNifiConstantEntity.class);
		checkParams(entity,type);
		return entity;
	}
}
