package com.hzya.frame.sysnew.application.pluginType.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.hzya.frame.sysnew.application.entity.SysApplicationEntity;
import com.hzya.frame.sysnew.application.plugin.dao.ISysApplicationPluginDao;
import com.hzya.frame.sysnew.application.plugin.entity.SysApplicationPluginEntity;
import com.hzya.frame.sysnew.application.pluginType.entity.SysApplicationPluginTypeEntity;
import com.hzya.frame.sysnew.application.pluginType.dao.ISysApplicationPluginTypeDao;
import com.hzya.frame.sysnew.application.pluginType.service.ISysApplicationPluginTypeService;
import com.hzya.frame.web.entity.BaseResult;
import com.hzya.frame.web.entity.JsonResultEntity;
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 org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 插件类型表(SysApplicationPluginType)表服务实现类
 *
 * @author makejava
 * @since 2024-09-19 09:56:24
 */
@Service(value = "sysApplicationPluginTypeService")
public class SysApplicationPluginTypeServiceImpl extends BaseService<SysApplicationPluginTypeEntity, String> implements ISysApplicationPluginTypeService {
     
    private ISysApplicationPluginTypeDao sysApplicationPluginTypeDao;

	@Resource
	private ISysApplicationPluginDao sysApplicationPluginDao;
    
    @Autowired
	public void setSysApplicationPluginTypeDao(ISysApplicationPluginTypeDao dao) {
		this.sysApplicationPluginTypeDao = dao;
		this.dao = dao;
	}

	@Override
	public JsonResultEntity queryPluginType(JSONObject jsonObject){
		SysApplicationPluginTypeEntity entity = getData("jsonStr", jsonObject, SysApplicationPluginTypeEntity.class);
		if(entity == null){
			entity = new SysApplicationPluginTypeEntity();
		}
		List<SysApplicationPluginTypeEntity> list = sysApplicationPluginTypeDao.queryByLike(entity);
		return BaseResult.getSuccessMessageEntity("查询数据成功",list);
	}

	@Override
	public JsonResultEntity savePluginType(JSONObject jsonObject) {
		SysApplicationPluginTypeEntity entity = getData("jsonStr", jsonObject, SysApplicationPluginTypeEntity.class);
		if(entity == null){
			return BaseResult.getFailureMessageEntity("参数错误");
		}
		if(entity.getName() == null || "".equals(entity.getName())){
			return BaseResult.getFailureMessageEntity("插件类型名称不能为空");
		}
		entity.setCreate();
		sysApplicationPluginTypeDao.save(entity);
		return BaseResult.getSuccessMessageEntity("保存数据成功",entity);
	}

	@Override
	public JsonResultEntity updatePluginType(JSONObject jsonObject) {
		SysApplicationPluginTypeEntity entity = getData("jsonStr", jsonObject, SysApplicationPluginTypeEntity.class);
		if(entity == null){
			return BaseResult.getFailureMessageEntity("参数错误");
		}
		if(entity.getId() == null || "".equals(entity.getId())){
			return BaseResult.getFailureMessageEntity("ID不能为空");
		}
		if(entity.getName() == null || "".equals(entity.getName())){
			return BaseResult.getFailureMessageEntity("插件类型名称不能为空");
		}
		entity.setUpdate();
		sysApplicationPluginTypeDao.update(entity);
		return BaseResult.getSuccessMessageEntity("更新数据成功",entity);
	}

	@Transactional
	@Override
	public JsonResultEntity deletePluginType(JSONObject jsonObject) {
		SysApplicationPluginTypeEntity entity = getData("jsonStr", jsonObject, SysApplicationPluginTypeEntity.class);
		if(entity == null){
			return BaseResult.getFailureMessageEntity("参数错误");
		}
		if(entity.getId() == null || "".equals(entity.getId())){
			return BaseResult.getFailureMessageEntity("ID不能为空");
		}
		// 同步删除匹配插件中的插件类型，将其赋值为null
		SysApplicationPluginEntity pluginEntity = new SysApplicationPluginEntity();
		pluginEntity.setTypeId(entity.getId());
		List<SysApplicationPluginEntity> pluginList = sysApplicationPluginDao.queryBase(pluginEntity);
		for(SysApplicationPluginEntity plugin : pluginList){
			plugin.setTypeId("无");
			plugin.setUpdate();
			sysApplicationPluginDao.update(plugin);
		}
		entity.setUpdate();
		sysApplicationPluginTypeDao.logicRemove(entity);
		return BaseResult.getSuccessMessageEntity("删除数据成功");
	}


}
