/**
 * 
 */
package org.springblade.etl.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.etl.dto.JobPermissionDoo;
import org.springblade.etl.dto.JobPermissionDto;
import org.springblade.etl.entity.Job;
import org.springblade.etl.entity.JobPermission;
import org.springblade.etl.mapper.JobPermissionMapper;
import org.springblade.system.client.DataPermClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * @author wookvn
 *
 */
@Service
public class JobPermissionService extends ServiceImpl<JobPermissionMapper, JobPermission> {

	@Autowired
	private DataPermClient dataPermClient;
	
	@Autowired
	private JobService jobService;
	
	/**
	 * 访问权限
	 * @param visualCreateUserId
	 * @param bladeUser
	 * @return
	 */
	public boolean checkJobDataForUser(Integer jobId, Long visualCreateUserId, BladeUser bladeUser) {
		//检查4个方面：
		//1.当前用户是否创建人
		//2.是否公开
		//3.是否超级权限组
		//4.当前用户是否拥有权限
		return  visualCreateUserId.longValue() == bladeUser.getUserId()
				|| jobService.count(Wrappers.<Job>lambdaQuery().eq(Job::getId, jobId).eq(Job::getPublicStatus, 1)) > 0
				|| Optional.<Boolean>ofNullable(dataPermClient.checkSuperPermission(bladeUser.getUserId())).orElse(false)
				|| baseMapper.checkJobForUser(jobId, bladeUser.getUserId()) > 0;
	}
	
	/**
	 * 权限组管理入口 
	 * @param visualDataPermDto
	 * @return
	 */
	@Transactional
	public boolean doSaveOrUpdate(JobPermissionDto jobPermissionDto, BladeUser bladeUser) {
		//通过关联删除，删除分类ID的数据
		baseMapper.deleteByCategoryIdAndPermId(jobPermissionDto.getJobCategoryId(), jobPermissionDto.getDataPermId());
		//2.再添加
		if(CollectionUtil.isNotEmpty(jobPermissionDto.getJobIdList())) {
			//更新公开状态为0[否则权限组不生效]
			jobService.update(Wrappers.<Job>lambdaUpdate().set(Job::getPublicStatus, 0).in(Job::getId, jobPermissionDto.getJobIdList()));
			List<JobPermission> dataList = new ArrayList<>(jobPermissionDto.getJobIdList().size());
			jobPermissionDto.getJobIdList().forEach(t -> {
				JobPermission entity = new JobPermission();
				entity.setCreateBy(bladeUser.getNickName());
				entity.setCreateTime(new Date());
				entity.setCreateUserId(bladeUser.getUserId());
				entity.setDataPermId(jobPermissionDto.getDataPermId());
				entity.setJobId(t);
				dataList.add(entity);
			});
			saveBatch(dataList);
		}
		return true;
	}
	
	/**
	 * 任务流程列表的权限组管理入口
	 * @param visualDataPermDoo
	 * @param bladeUser
	 * @return
	 */
	@Transactional
	public boolean doPerform(JobPermissionDoo jobPermDoo, BladeUser bladeUser) {
		//1.根据任务ID删除所属权限组
		baseMapper.delete(Wrappers.<JobPermission>lambdaQuery().eq(JobPermission::getJobId, jobPermDoo.getJobId()));
		if(jobPermDoo.getPublicStatus() != null) {
			jobService.update(Wrappers.<Job>lambdaUpdate().set(Job::getPublicStatus, jobPermDoo.getPublicStatus()).eq(Job::getId, jobPermDoo.getJobId()));
		}
		//2.批量添加权限组
		if(CollectionUtil.isNotEmpty(jobPermDoo.getDataPermIdList())) {
			List<JobPermission> dataList = new ArrayList<>(jobPermDoo.getDataPermIdList().size());
			jobPermDoo.getDataPermIdList().forEach(dataPermId -> {
				JobPermission entity = new JobPermission();
				entity.setCreateBy(bladeUser.getNickName());
				entity.setCreateTime(new Date());
				entity.setCreateUserId(bladeUser.getUserId());
				entity.setDataPermId(dataPermId);
				entity.setJobId(jobPermDoo.getJobId());
				dataList.add(entity);
			});
			saveBatch(dataList);
		}
		return true;
	}
	
	/**
	 * 
	 * @param dataPermId
	 * @param classifyId
	 * @return
	 */
	public List<Integer> listJobId(Integer dataPermId, Integer categoryId){
		return baseMapper.selectJobIdList(categoryId, dataPermId);
	}
	
	/**
	 * 
	 * @param visualId
	 * @return
	 */
	public List<JSONObject> listDataPerm(Integer visualId){
		return baseMapper.selectDataPermList(visualId);
	}
}
