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

import cn.hutool.core.lang.Assert;
import com.alibaba.fastjson.JSONObject;
import com.hzya.frame.sys.dictionaryshopNew.service.ISysDictionaryshopNewService;
import com.hzya.frame.sys.flow.dao.ISysFlowClassRuleDao;
import com.hzya.frame.sys.flow.dao.ISysFlowDao;
import com.hzya.frame.sys.flow.entity.SysFlowClassEntity;
import com.hzya.frame.sys.flow.dao.ISysFlowClassDao;
import com.hzya.frame.sys.flow.entity.SysFlowClassRuleEntity;
import com.hzya.frame.sys.flow.entity.SysFlowEntity;
import com.hzya.frame.sys.flow.service.ISysFlowClassService;
import com.hzya.frame.sysnew.user.dao.ISysUserDao;
import com.hzya.frame.sysnew.user.entity.SysUserEntity;
import com.hzya.frame.uuid.UUIDLong;
import com.hzya.frame.uuid.UUIDUtils;
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 com.hzya.frame.basedao.service.impl.BaseService;

import java.util.Date;
import java.util.List;

/**
 * 流程分类;对应数环通项目分类(SysFlowClass)表服务实现类
 *
 * @author xiang2lin
 * @since 2025-04-29 10:16:27
 */
@Service(value = "sysFlowClassService")
public class SysFlowClassServiceImpl extends BaseService<SysFlowClassEntity, String> implements ISysFlowClassService {
     
    private ISysFlowClassDao sysFlowClassDao;
	@Autowired
	private ISysFlowDao sysFlowDao;
	@Autowired
	private ISysFlowClassRuleDao flowClassRuleDao;
	@Autowired
	private ISysUserDao sysUserDao;
    @Autowired
	public void setSysFlowClassDao(ISysFlowClassDao dao) {
		this.sysFlowClassDao = dao;
		this.dao = dao;
	}

	/**
	 * 根据Id查询
	 *
	 * @param object
	 * @return
	 */
	@Override
	public JsonResultEntity getFlowClass(JSONObject object) {
		SysFlowClassEntity flowClass = getData("jsonStr",object,SysFlowClassEntity.class);
		try {
			this.checkParams(flowClass,"get");
		}catch(Exception e){
			return BaseResult.getFailureMessageEntity(e.getMessage());
		}
		SysFlowClassEntity sysFlowClassEntity = sysFlowClassDao.queryOne(flowClass);
		return BaseResult.getSuccessMessageEntity("查询详情成功",sysFlowClassEntity);
	}

	/**
	 * 新增流程分类
	 *
	 * @param object
	 * @return
	 */
	@Override
	public JsonResultEntity saveFlowClass(JSONObject object) {
		SysFlowClassEntity flowClass = getData("jsonStr",object,SysFlowClassEntity.class);
		try {
			this.checkParams(flowClass,"add");
		}catch(Exception e){
			return BaseResult.getFailureMessageEntity(e.getMessage());
		}
		flowClass.setId(UUIDUtils.getUUID());
		sysFlowClassDao.save(flowClass);
		//给创建分类的用户保存一下权限
		SysUserEntity sysUserEntity = sysUserDao.get(flowClass.getCreate_user_id());
		SysFlowClassRuleEntity ruleEntity = new SysFlowClassRuleEntity();
		ruleEntity.setFlowClassId(flowClass.getId());
		ruleEntity.setUserId(flowClass.getCreate_user_id());
		if (null != sysUserEntity){
			ruleEntity.setUserName(sysUserEntity.getPersonName());
			ruleEntity.setUserCode(sysUserEntity.getPersonCode());
		}
		ruleEntity.setCreate_time(new Date());
		ruleEntity.setModify_time(new Date());
		ruleEntity.setCreate_user_id(flowClass.getCreate_user_id());
		ruleEntity.setModify_user_id(flowClass.getModify_user_id());
		ruleEntity.setId(UUIDUtils.getUUID());
		flowClassRuleDao.save(ruleEntity);
		return BaseResult.getSuccessMessageEntity("新增成功");
	}

	/**
	 * 修改流程分类
	 *
	 * @param object
	 * @return
	 */
	@Override
	public JsonResultEntity updateFlowClass(JSONObject object) {
		SysFlowClassEntity flowClass = getData("jsonStr",object,SysFlowClassEntity.class);
		try {
			this.checkParams(flowClass,"update");
		}catch(Exception e){
			return BaseResult.getFailureMessageEntity(e.getMessage());
		}
		sysFlowClassDao.update(flowClass);
		return BaseResult.getSuccessMessageEntity("更新成功");
	}

	/**
	 * 删除流程分类
	 *
	 * @param object
	 * @return
	 */
	@Override
	public JsonResultEntity deleteFlowClass(JSONObject object) {
		SysFlowClassEntity flowClass = getData("jsonStr",object,SysFlowClassEntity.class);
		try {
			this.checkParams(flowClass,"delete");
		}catch(Exception e){
			return BaseResult.getFailureMessageEntity(e.getMessage());
		}
		sysFlowClassDao.logicRemove(flowClass);
		return BaseResult.getSuccessMessageEntity("删除成功");
	}
	/**
	 * 参数检查
	 * @param flowClass
	 * @param type
	 */
	private  void checkParams(SysFlowClassEntity flowClass,String type){
		Assert.notNull(flowClass,"请求参数不能为空");
		if ("add".equals(type)){//新增
			Assert.notEmpty(flowClass.getName(),"名称不能为空");
			//查询是否有同名的
			SysFlowClassEntity flowQuery = new SysFlowClassEntity();
			flowQuery.setName(flowClass.getName());
			List<SysFlowClassEntity> query = sysFlowClassDao.query(flowQuery);
			if (CollectionUtils.isNotEmpty(query)){
				throw new BaseSystemException(flowClass.getName()+"已存在");
			}
		}else if ("update".equals(type)){//更新
			Assert.notEmpty(flowClass.getId(),"id不能为空");
			//查一下有没有同名的
			SysFlowClassEntity flowQuery = new SysFlowClassEntity();
			flowQuery.setName(flowClass.getName());
			List<SysFlowClassEntity> query = sysFlowClassDao.query(flowQuery);
			if (CollectionUtils.isNotEmpty(query)){
				for (SysFlowClassEntity f : query) {
					if (!f.getId().equals(flowClass.getId())){
						throw new BaseSystemException(flowClass.getName()+"已存在");
					}
				}
			}
		}else if ("delete".equals(type)){//删除
			Assert.notEmpty(flowClass.getId(),"id不能为空");
			//查一下这个分类有没有被引用
			SysFlowEntity sysFlowEntity = new SysFlowEntity();
			sysFlowEntity.setClassId(flowClass.getId());
			List<SysFlowEntity> query = sysFlowDao.query(sysFlowEntity);
			if (CollectionUtils.isNotEmpty(query)){
				throw new BaseSystemException("该分类已被引用，删除失败");
			}
		}else if ("get".equals(type)){//查询详情
			Assert.notEmpty(flowClass.getId(),"id不能为空");
		}
	}
}
