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

import cn.hutool.core.lang.Assert;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzya.frame.sys.dictionaryshopNew.entity.SysDictionaryshopNew;
import com.hzya.frame.sys.dictionaryshopNew.service.ISysDictionaryshopNewService;
import com.hzya.frame.sys.flow.entity.SysFlowEntity;
import com.hzya.frame.sys.flow.dao.ISysFlowDao;
import com.hzya.frame.sys.flow.service.ISysFlowService;
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;

/**
 * 流程主表;流程就是数环通的Linkup(SysFlow)表服务实现类
 *
 * @author xiang2lin
 * @since 2025-04-29 10:16:26
 */
@Service(value = "sysFlowService")
public class SysFlowServiceImpl extends BaseService<SysFlowEntity, String> implements ISysFlowService {
     
    private ISysFlowDao sysFlowDao;
	@Autowired
	private ISysDictionaryshopNewService sysDictionaryshopNewService;
    @Autowired
	public void setSysFlowDao(ISysFlowDao dao) {
		this.sysFlowDao = dao;
		this.dao = dao;
	}

	/**
	 * 保存流程主表
	 *
	 * @param object
	 * @return
	 */
	@Override
	public JsonResultEntity saveFlow(JSONObject object) {
		SysFlowEntity flowEntity = getData("jsonStr",object,SysFlowEntity.class);
		try {
			checkParams(flowEntity,"save");
		} catch (Exception e) {
			return BaseResult.getFailureMessageEntity(e.getMessage());
		}
		sysFlowDao.save(flowEntity);
		return BaseResult.getSuccessMessageEntity("保存成功",flowEntity);
	}

	/**
	 * 更新流程主表
	 *
	 * @param object
	 * @return
	 */
	@Override
	public JsonResultEntity updateFlow(JSONObject object) {
		SysFlowEntity flowEntity = getData("jsonStr",object,SysFlowEntity.class);
		try {
			checkParams(flowEntity,"update");
		} catch (Exception e) {
			return BaseResult.getFailureMessageEntity(e.getMessage());
		}
		sysFlowDao.update(flowEntity);
		return BaseResult.getSuccessMessageEntity("更新成功");
	}

	/**
	 * 删除流程主表
	 *
	 * @param object
	 * @return
	 */
	@Override
	public JsonResultEntity deleteFlow(JSONObject object) {
		SysFlowEntity flowEntity = getData("jsonStr",object,SysFlowEntity.class);
		try {
			checkParams(flowEntity,"delete");
		} catch (Exception e) {
			return BaseResult.getFailureMessageEntity(e.getMessage());
		}
		//删除主表
		sysFlowDao.logicRemove(flowEntity);
		//删除子表
		return BaseResult.getSuccessMessageEntity("删除成功");
	}

	/**
	 * 列表查询
	 *
	 * @param object
	 * @return
	 */
	@Override
	public JsonResultEntity queryFlowList(JSONObject object) {
		SysFlowEntity flowEntity = getData("jsonStr",object,SysFlowEntity.class);
		try {
			checkParams(flowEntity,"queryList");
		} catch (Exception e) {
			return BaseResult.getFailureMessageEntity(e.getMessage());
		}
		List<SysFlowEntity> list = sysFlowDao.query(flowEntity);
		if (CollectionUtils.isNotEmpty(list)){
			for (SysFlowEntity sysFlowEntity : list) {
				transferDictionary(sysFlowEntity);
			}
		}
		return BaseResult.getSuccessMessageEntity("查询数据成功",list);
	}

	/**
	 * 分页查询
	 *
	 * @param object
	 * @return
	 */
	@Override
	public JsonResultEntity queryFlowPagedInfo(JSONObject object) {
		SysFlowEntity flowEntity = getData("jsonStr",object,SysFlowEntity.class);
		try {
			checkParams(flowEntity,"queryPaged");
		} catch (Exception e) {
			return BaseResult.getFailureMessageEntity(e.getMessage());
		}
		PageHelper.startPage(flowEntity.getPageNum(),flowEntity.getPageSize());
		List<SysFlowEntity> queryByLike = sysFlowDao.queryByLike(flowEntity);
		if (CollectionUtils.isNotEmpty(queryByLike)){
			for (SysFlowEntity sysFlowEntity : queryByLike) {
				transferDictionary(sysFlowEntity);
			}
		}
		PageInfo pageInfo = new PageInfo(queryByLike);
		return BaseResult.getSuccessMessageEntity("pageInfo",pageInfo);
	}

	/**
	 * 参数检查
	 * @param entity
	 * @param type
	 */
	private void checkParams(SysFlowEntity entity,String type){
		Assert.notNull(entity,"请求参数不能为空");
		if ("save".equals(type)){
			Assert.notEmpty(entity.getClassId(),"classId不能为空");
			Assert.notEmpty(entity.getName(),"name不能为空");
			SysFlowEntity flow = new SysFlowEntity();
			flow.setName(entity.getName());
			List<SysFlowEntity> flowList = sysFlowDao.query(flow);
			if (CollectionUtils.isNotEmpty(flowList)){
				throw new BaseSystemException(entity.getName()+"重复");
			}
		}else if("update".equals(type)){
			Assert.notEmpty(entity.getId(),"Id不能为空");
			SysFlowEntity flow = new SysFlowEntity();
			flow.setName(entity.getName());
			List<SysFlowEntity> flowList = sysFlowDao.query(flow);
			if (CollectionUtils.isNotEmpty(flowList)){
				for (SysFlowEntity sysFlowEntity : flowList) {
					if (!sysFlowEntity.getId().equals(entity.getId())){
						throw new BaseSystemException(entity.getName()+"重复");
					}
				}
			}
		}else if ("delete".equals(type)){
			Assert.notEmpty(entity.getId(),"Id不能为空");

		}else if ("queryPaged".equals(type)){
			Assert.notNull(entity.getPageNum(),"pageNum不能为空");
			Assert.notNull(entity.getPageSize(),"pageSize不能为空");
		}
	}

	private void transferDictionary(SysFlowEntity sysFlowEntity){
		if (null != sysFlowEntity){
			SysDictionaryshopNew dictionaryshopByValue = sysDictionaryshopNewService.getDictionaryshopByValue("sys_flow", "trigger_mode_id", sysFlowEntity.getTriggerModeId());
			if (null != dictionaryshopByValue){
				sysFlowEntity.setTriggerModeName(dictionaryshopByValue.getColumnContent());
			}
		}
	}
}
