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

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzya.frame.sys.flow.entity.SysFlowClassRuleEntity;
import com.hzya.frame.sys.flow.dao.ISysFlowClassRuleDao;
import com.hzya.frame.sys.flow.service.ISysFlowClassRuleService;
import com.hzya.frame.sysnew.user.dao.ISysUserDao;
import com.hzya.frame.sysnew.user.entity.SysUserEntity;
import com.hzya.frame.uuid.UUIDUtils;
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 java.util.List;

/**
 * 流程分类权限表(SysFlowClassRule)表服务实现类
 *
 * @author xiang2lin
 * @since 2025-04-29 10:16:27
 */
@Service(value = "sysFlowClassRuleService")
public class SysFlowClassRuleServiceImpl extends BaseService<SysFlowClassRuleEntity, String> implements ISysFlowClassRuleService {
     
    private ISysFlowClassRuleDao sysFlowClassRuleDao;
	@Autowired
	private ISysUserDao sysUserDao;
    @Autowired
	public void setSysFlowClassRuleDao(ISysFlowClassRuleDao dao) {
		this.sysFlowClassRuleDao = dao;
		this.dao = dao;
	}


	/**
	 * 新增流程分类权限
	 *
	 * @param object
	 * @return
	 */
	@Override
	public JsonResultEntity saveFlowClassRule(JSONObject object) {
		SysFlowClassRuleEntity ruleEntity = getData("jsonStr",object,SysFlowClassRuleEntity.class);
		try {
			this.checkParams(ruleEntity,"save");
		}catch(Exception e){
			return BaseResult.getFailureMessageEntity(e.getMessage());
		}
		addRule(ruleEntity);
		return BaseResult.getSuccessMessageEntity("保存成功");
	}

	//保存
	private void addRule(SysFlowClassRuleEntity ruleEntity) {
		List<SysFlowClassRuleEntity> ruleList = ruleEntity.getRuleList();
		for (SysFlowClassRuleEntity r : ruleList) {
			r.setId(UUIDUtils.getUUID());
			r.setCreate_time(Convert.toDate(ruleEntity.getCreate_time(),ruleEntity.getModify_time()));
			r.setCreate_user_id(Convert.toStr(ruleEntity.getCreate_user_id(),ruleEntity.getModify_user_id()));
			r.setModify_time(r.getCreate_time());
			r.setModify_user_id(r.getCreate_user_id());
			r.setFlowClassId(ruleEntity.getFlowClassId());
			sysFlowClassRuleDao.save(r);
		}
	}

	/**
	 * 修改流程分类权限
	 *
	 * @param object
	 * @return
	 */
	@Override
	public JsonResultEntity updateFlowClassRule(JSONObject object) {
		SysFlowClassRuleEntity ruleEntity = getData("jsonStr",object,SysFlowClassRuleEntity.class);
		try {
			this.checkParams(ruleEntity,"update");
		}catch(Exception e){
			return BaseResult.getFailureMessageEntity(e.getMessage());
		}
		//先删除 再重新保存
		SysFlowClassRuleEntity rule = new SysFlowClassRuleEntity();
		rule.setFlowClassId(ruleEntity.getFlowClassId());
		sysFlowClassRuleDao.logicRemoveMultiCondition(rule);
		addRule(ruleEntity);
		return BaseResult.getSuccessMessageEntity("更新成功");
	}

	/**
	 * 删除流程分类权限
	 *
	 * @param object
	 * @return
	 */
	@Override
	public JsonResultEntity deleteFlowClassRule(JSONObject object) {
		SysFlowClassRuleEntity ruleEntity = getData("jsonStr",object,SysFlowClassRuleEntity.class);
		try {
			this.checkParams(ruleEntity,"delete");
		}catch (Exception e){
			return BaseResult.getFailureMessageEntity(e.getMessage());
		}
		SysFlowClassRuleEntity deleteRuleEntity = new SysFlowClassRuleEntity();
		deleteRuleEntity.setFlowClassId(ruleEntity.getFlowClassId());
		deleteRuleEntity.setUserId(ruleEntity.getUserId());
		sysFlowClassRuleDao.logicRemoveMultiCondition(deleteRuleEntity);
		return BaseResult.getSuccessMessageEntity("删除成功");
	}

	/**
	 * 列表查询
	 *
	 * @param object
	 * @return
	 */
	@Override
	public JsonResultEntity queryRuleList(JSONObject object) {
		SysFlowClassRuleEntity ruleEntity = getData("jsonStr",object,SysFlowClassRuleEntity.class);
		try {
			checkParams(ruleEntity,"queryList");
		}catch(Exception e){
			return BaseResult.getFailureMessageEntity(e.getMessage());
		}
		List<SysFlowClassRuleEntity> sysFlowClassRuleEntities = sysFlowClassRuleDao.queryByLike(ruleEntity);
		SysFlowClassRuleEntity reuslt = new SysFlowClassRuleEntity();
		reuslt.setFlowClassId(ruleEntity.getFlowClassId());
		reuslt.setRuleList(sysFlowClassRuleEntities);
		return BaseResult.getSuccessMessageEntity("查询数据成功",reuslt);
	}

	/**
	 * 分页查询
	 *
	 * @param object
	 * @return
	 */
	@Override
	public JsonResultEntity queryRulePagedInfo(JSONObject object) {
		SysFlowClassRuleEntity ruleEntity = getData("jsonStr",object,SysFlowClassRuleEntity.class);
		try {
			checkParams(ruleEntity,"queryPaged");
		}catch(Exception e){
			return BaseResult.getFailureMessageEntity(e.getMessage());
		}
		PageHelper.startPage(ruleEntity.getPageNum(), ruleEntity.getPageSize());
		List<SysFlowClassRuleEntity> queryByLike = this.dao.queryByLike(ruleEntity);
		PageInfo pageInfo = new PageInfo(queryByLike);
		return BaseResult.getSuccessMessageEntity("查询数据成功",pageInfo);
	}
	/**
	 * 查询待分配权限的用户列表
	 *
	 * @param object
	 * @return
	 */
	@Override
	public JsonResultEntity queryUserList(JSONObject object) {
		SysUserEntity userEntity = getData("jsonStr",object,SysUserEntity.class);
		if (StrUtil.isEmpty(userEntity.getFlowClassId())){
			return BaseResult.getFailureMessageEntity("flowClassId不能为空");
		}
		List<SysUserEntity> sysUserEntities = sysUserDao.queryList(userEntity, "com.hzya.frame.sysnew.user.dao.impl.SysUserDaoImpl.entity_list_notin_sys_flowClass");
		return BaseResult.getSuccessMessageEntity("查询成功",sysUserEntities);
	}
	/**
	 * 检查参数
	 * @param entity 参数对象
	 * @param type 操作；类型
	 */
	private void checkParams(SysFlowClassRuleEntity entity,String type){
		Assert.notNull(entity,"请求参数不能为空");
		Assert.notEmpty(entity.getFlowClassId(),"flowClassId不能为空");
		if ("save".equals(type)){
			Assert.notEmpty(entity.getRuleList(),"ruleList不能为空");
		}else if ("update".equals(type)){
			Assert.notEmpty(entity.getRuleList(),"ruleList不能为空");
		}else if ("delete".equals(type)){

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

	}
}
