package com.hzya.frame.sysnew.integratedForegroundTask.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzya.frame.base.PluginBaseEntity;
import com.hzya.frame.basedao.service.impl.BaseService;
import com.hzya.frame.sysnew.integratedForegroundTaskDetail.dao.ISysIntegratedForegroundTaskDetailDao;
import com.hzya.frame.sysnew.integratedForegroundTaskDetail.entity.SysIntegratedForegroundTaskDetailEntity;
import com.hzya.frame.sysnew.integratedForegroundTaskParameter.dao.ISysIntegratedForegroundTaskParameterDao;
import com.hzya.frame.sysnew.integratedForegroundTaskParameter.entity.SysIntegratedForegroundTaskParameterEntity;
import com.hzya.frame.sysnew.integratedForegroundTask.dao.ISysIntegratedForegroundTaskDao;
import com.hzya.frame.sysnew.integratedForegroundTask.entity.SysIntegratedForegroundTaskEntity;
import com.hzya.frame.sysnew.integratedForegroundTask.service.ISysIntegratedForegroundTaskService;
import com.hzya.frame.util.PluginUtils;
import com.hzya.frame.web.entity.BaseResult;
import com.hzya.frame.web.entity.JsonResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Service;

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

/**
 * 集成前台任务主表(SysIntegratedForegroundTask)表服务实现类
 *
 * @author makejava
 * @since 2024-03-18 14:35:04
 */
@Service(value = "sysIntegratedForegroundTaskService")
public class SysIntegratedForegroundTaskServiceImpl extends BaseService<SysIntegratedForegroundTaskEntity, String> implements ISysIntegratedForegroundTaskService {
     
    private ISysIntegratedForegroundTaskDao sysIntegratedForegroundTaskDao;
    @Autowired
    private ISysIntegratedForegroundTaskDetailDao sysIntegratedForegroundTaskDetailDao;
    @Autowired
    private ISysIntegratedForegroundTaskParameterDao sysIntegratedForegroundTaskParameterDao;
	@Resource
	private TaskExecutor taskExecutor;
    @Autowired
	public void setSysIntegratedForegroundTaskDao(ISysIntegratedForegroundTaskDao dao) {
		this.sysIntegratedForegroundTaskDao = dao;
		this.dao = dao;
	}

	/**
	 * @param object
	 * @return com.hzya.frame.web.entity.JsonResultEntity
	 * @Author lvleigang
	 * @Description 查询集成前台任务列表
	 * @Date 2:45 下午 2024/3/18
	 **/
	@Override
	public JsonResultEntity queryIntegratedForegroundTask(JSONObject object) {
		SysIntegratedForegroundTaskEntity entity = getData("jsonStr", object, SysIntegratedForegroundTaskEntity.class);
		//判断分页
		if (entity == null || entity.getPageNum() == null || entity.getPageSize() == null) {
			return BaseResult.getFailureMessageEntity("分页查询参数不存在");
		}
		PageHelper.startPage(entity.getPageNum(), entity.getPageSize());
		List<SysIntegratedForegroundTaskEntity> sysIntegratedForegroundTaskEntityList = sysIntegratedForegroundTaskDao.queryByLike(entity);
		PageInfo pageInfo = new PageInfo(sysIntegratedForegroundTaskEntityList);
		return BaseResult.getSuccessMessageEntity("查询数据成功", pageInfo);
	}
	/**
	 * @param object
	 * @return com.hzya.frame.web.entity.JsonResultEntity
	 * @Author lvleigang
	 * @Description 保存集成前台任务
	 * @Date 2:45 下午 2024/3/18
	 **/
	@Override
	public JsonResultEntity saveIntegratedForegroundTask(JSONObject object) {
		SysIntegratedForegroundTaskEntity entity = getData("jsonStr", object, SysIntegratedForegroundTaskEntity.class);
		if (entity == null) {
			return BaseResult.getFailureMessageEntity("参数不允许为空");
		}
		if (entity.getPlugId() == null || "".equals(entity.getPlugId())) {
			return BaseResult.getFailureMessageEntity("请先选择插件");
		}
		if (entity.getWorkshopName() == null || "".equals(entity.getWorkshopName())) {
			return BaseResult.getFailureMessageEntity("请先输入工坊名称");
		}
		if (entity.getSysIntegratedForegroundTaskDetailEntityList() == null || entity.getSysIntegratedForegroundTaskDetailEntityList().size() == 0 ) {
			return BaseResult.getFailureMessageEntity("请先输入插件对应的参数");
		}

		entity.setCreate();
		//保存主表
		sysIntegratedForegroundTaskDao.save(entity);
		//保存明细表
		JSONObject jsonObject = new JSONObject();
		for (int i = 0; i < entity.getSysIntegratedForegroundTaskDetailEntityList().size(); i++) {
			SysIntegratedForegroundTaskDetailEntity sysIntegratedForegroundTaskDetailEntity = entity.getSysIntegratedForegroundTaskDetailEntityList().get(i);
			sysIntegratedForegroundTaskDetailEntity.setFormmainId(entity.getId());
			sysIntegratedForegroundTaskDetailEntity.setCreate();
			if(sysIntegratedForegroundTaskDetailEntity.getArgValue() != null && !"".equals(sysIntegratedForegroundTaskDetailEntity.getArgValue())){
				jsonObject.put(sysIntegratedForegroundTaskDetailEntity.getArgCode(),sysIntegratedForegroundTaskDetailEntity.getArgValue());
			}
			sysIntegratedForegroundTaskDetailDao.save(sysIntegratedForegroundTaskDetailEntity);
		}
		SysIntegratedForegroundTaskParameterEntity sysIntegratedForegroundTaskParameterEntity = new SysIntegratedForegroundTaskParameterEntity();
		sysIntegratedForegroundTaskParameterEntity.setFormmainId(entity.getId());
		sysIntegratedForegroundTaskParameterEntity.setInParameter(jsonObject.toJSONString());
		sysIntegratedForegroundTaskParameterEntity.setCreate();
		sysIntegratedForegroundTaskParameterEntity.setStatus("1");
		sysIntegratedForegroundTaskParameterDao.save(sysIntegratedForegroundTaskParameterEntity);
		PluginBaseEntity pluginBaseEntity = PluginUtils.getPluginsById(entity.getPlugId());
		if(pluginBaseEntity != null ){
			taskExecutor.execute(() -> {
				try {
					pluginBaseEntity.executeBusiness(jsonObject);
					sysIntegratedForegroundTaskParameterEntity.setStatus("2");
					sysIntegratedForegroundTaskParameterEntity.setModify_time(new Date());
					sysIntegratedForegroundTaskParameterDao.update(sysIntegratedForegroundTaskParameterEntity);
				} catch (Exception e) {
					logger.error("插件："+entity.getPlugId()+"执行失败："+e.getMessage());
				}
			});
		}else {
			sysIntegratedForegroundTaskParameterEntity.setStatus("2");
			sysIntegratedForegroundTaskParameterEntity.setModify_time(new Date());
			sysIntegratedForegroundTaskParameterDao.update(sysIntegratedForegroundTaskParameterEntity);
			return BaseResult.getSuccessMessageEntity("提交成功，当前任务执行失败，请联系系统管理员");
		}
		return BaseResult.getSuccessMessageEntity("提交成功，执行结果请去查看任务日志");

	}
	/**
	 * @param object
	 * @return com.hzya.frame.web.entity.JsonResultEntity
	 * @Author lvleigang
	 * @Description 修改集成前台任务
	 * @Date 2:45 下午 2024/3/18
	 **/
	@Override
	public JsonResultEntity updateIntegratedForegroundTask(JSONObject object) {
		SysIntegratedForegroundTaskEntity entity = getData("jsonStr", object, SysIntegratedForegroundTaskEntity.class);
		if (entity == null) {
			return BaseResult.getFailureMessageEntity("参数不允许为空");
		}
		if (entity.getId() == null || "".equals(entity.getId())) {
			return BaseResult.getFailureMessageEntity("系统错误");
		}
		if (entity.getPlugId() == null || "".equals(entity.getPlugId())) {
			return BaseResult.getFailureMessageEntity("请先选择插件");
		}
		if (entity.getWorkshopName() == null || "".equals(entity.getWorkshopName())) {
			return BaseResult.getFailureMessageEntity("请先输入工坊名称");
		}
		if (entity.getSysIntegratedForegroundTaskDetailEntityList() == null || entity.getSysIntegratedForegroundTaskDetailEntityList().size() == 0 ) {
			return BaseResult.getFailureMessageEntity("请先输入插件对应的参数");
		}
		entity.setUpdate();
		//保存主表
		sysIntegratedForegroundTaskDao.update(entity);
		//保存明细表
		//先删除明细
		SysIntegratedForegroundTaskDetailEntity deleteEntity = new SysIntegratedForegroundTaskDetailEntity();
		deleteEntity.setFormmainId(entity.getId());
		deleteEntity.setModify_time(new Date());
		deleteEntity.setModify_user_id(StpUtil.getLoginIdAsString());
		JSONObject jsonObject = new JSONObject();
		sysIntegratedForegroundTaskDetailDao.logicRemoveMultiCondition(deleteEntity);
		for (int i = 0; i < entity.getSysIntegratedForegroundTaskDetailEntityList().size(); i++) {
			SysIntegratedForegroundTaskDetailEntity sysIntegratedForegroundTaskDetailEntity = entity.getSysIntegratedForegroundTaskDetailEntityList().get(i);
			sysIntegratedForegroundTaskDetailEntity.setFormmainId(entity.getId());
			sysIntegratedForegroundTaskDetailEntity.setCreate();
			if(sysIntegratedForegroundTaskDetailEntity.getArgValue() != null && !"".equals(sysIntegratedForegroundTaskDetailEntity.getArgValue())){
				jsonObject.put(sysIntegratedForegroundTaskDetailEntity.getArgCode(),sysIntegratedForegroundTaskDetailEntity.getArgValue());
			}
			sysIntegratedForegroundTaskDetailDao.save(sysIntegratedForegroundTaskDetailEntity);
		}
		SysIntegratedForegroundTaskParameterEntity sysIntegratedForegroundTaskParameterEntity = new SysIntegratedForegroundTaskParameterEntity();
		sysIntegratedForegroundTaskParameterEntity.setFormmainId(entity.getId());
		sysIntegratedForegroundTaskParameterEntity.setInParameter(jsonObject.toJSONString());
		sysIntegratedForegroundTaskParameterEntity.setCreate();
		sysIntegratedForegroundTaskParameterEntity.setStatus("1");

		sysIntegratedForegroundTaskParameterDao.save(sysIntegratedForegroundTaskParameterEntity);
		PluginBaseEntity pluginBaseEntity = PluginUtils.getPluginsById(entity.getPlugId());
		if(pluginBaseEntity != null ){
			taskExecutor.execute(() -> {
				try {
					pluginBaseEntity.executeBusiness(jsonObject);
					sysIntegratedForegroundTaskParameterEntity.setStatus("2");
					sysIntegratedForegroundTaskParameterEntity.setModify_time(new Date());
					sysIntegratedForegroundTaskParameterDao.update(sysIntegratedForegroundTaskParameterEntity);
				} catch (Exception e) {
					logger.error("插件："+entity.getPlugId()+"执行失败："+e.getMessage());
				}
			});
		}else {
			sysIntegratedForegroundTaskParameterEntity.setStatus("2");
			sysIntegratedForegroundTaskParameterEntity.setModify_time(new Date());
			sysIntegratedForegroundTaskParameterDao.update(sysIntegratedForegroundTaskParameterEntity);
			return BaseResult.getSuccessMessageEntity("提交成功，当前任务执行失败，请联系系统管理员");
		}
		return BaseResult.getSuccessMessageEntity("提交成功，执行结果请去查看任务日志");
	}
	/**
	 * @param object
	 * @return com.hzya.frame.web.entity.JsonResultEntity
	 * @Author lvleigang
	 * @Description 删除集成前台任务
	 * @Date 2:45 下午 2024/3/18
	 **/
	@Override
	public JsonResultEntity deleteIntegratedForegroundTask(JSONObject object) {
		SysIntegratedForegroundTaskEntity entity = getData("jsonStr", object, SysIntegratedForegroundTaskEntity.class);
		if (entity == null) {
			return BaseResult.getFailureMessageEntity("参数不允许为空");
		}
		if (entity.getId() == null || "".equals(entity.getId())) {
			return BaseResult.getFailureMessageEntity("系统错误");
		}
		entity.setUpdate();
		//保存主表
		sysIntegratedForegroundTaskDao.logicRemoveMultiCondition(entity);
		//先删除明细
		SysIntegratedForegroundTaskDetailEntity deleteEntity = new SysIntegratedForegroundTaskDetailEntity();
		deleteEntity.setFormmainId(entity.getId());
		deleteEntity.setModify_time(new Date());
		deleteEntity.setModify_user_id(StpUtil.getLoginIdAsString());
		sysIntegratedForegroundTaskDetailDao.logicRemoveMultiCondition(deleteEntity);
		SysIntegratedForegroundTaskParameterEntity sysIntegratedForegroundTaskParameterEntity = new SysIntegratedForegroundTaskParameterEntity();
		sysIntegratedForegroundTaskParameterEntity.setFormmainId(entity.getId());
		sysIntegratedForegroundTaskParameterEntity.setModify_time(new Date());
		sysIntegratedForegroundTaskParameterEntity.setModify_user_id(StpUtil.getLoginIdAsString());
		sysIntegratedForegroundTaskParameterDao.logicRemoveMultiCondition(sysIntegratedForegroundTaskParameterEntity);
		return BaseResult.getSuccessMessageEntity("删除集成前台任务成功", entity.getId());
	}


	/**
	 * @param object
	 * @return com.hzya.frame.web.entity.JsonResultEntity
	 * @Author lvleigang
	 * @Description 获取集成前台任务
	 * @Date 2:45 下午 2024/3/18
	 **/
	@Override
	public JsonResultEntity getIntegratedForegroundTask(JSONObject object) {
		SysIntegratedForegroundTaskEntity entity = getData("jsonStr", object, SysIntegratedForegroundTaskEntity.class);
		if (entity == null) {
			return BaseResult.getFailureMessageEntity("参数不允许为空");
		}
		if (entity.getId() == null || "".equals(entity.getId())) {
			return BaseResult.getFailureMessageEntity("系统错误");
		}
		//保存主表
		entity = sysIntegratedForegroundTaskDao.get(entity.getId());
		if(entity == null ){
			return BaseResult.getFailureMessageEntity("获取集成前台任务失败");
		}
		SysIntegratedForegroundTaskDetailEntity getEntity = new  SysIntegratedForegroundTaskDetailEntity();
		getEntity.setFormmainId(entity.getId());
		List<SysIntegratedForegroundTaskDetailEntity> list = sysIntegratedForegroundTaskDetailDao.queryBase(getEntity);
		entity.setSysIntegratedForegroundTaskDetailEntityList(list);
		SysIntegratedForegroundTaskParameterEntity sysIntegratedForegroundTaskParameterEntity = new SysIntegratedForegroundTaskParameterEntity();
		sysIntegratedForegroundTaskParameterEntity.setFormmainId(entity.getId());
		List<SysIntegratedForegroundTaskParameterEntity> sysIntegratedForegroundTaskParameterEntities = sysIntegratedForegroundTaskParameterDao.entity_list_base_by_create(sysIntegratedForegroundTaskParameterEntity);
		entity.setSysIntegratedForegroundTaskParameterEntities(sysIntegratedForegroundTaskParameterEntities);
		return BaseResult.getSuccessMessageEntity("获取集成前台任务成功", entity);
	}
}
