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

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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.DsyJob;
import org.springblade.etl.entity.DsyJobPermission;
import org.springblade.etl.mapper.DsyJobPermissionMapper;
import org.springblade.system.client.DataPermClient;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 *	数据同步任务权限组 服务处理
 *
 * @author aokunsang
 * @date   2023-09-21
 */
@Lazy
@Service
public class DsyJobPermissionService extends ServiceImpl<DsyJobPermissionMapper, DsyJobPermission>{

	@Resource
	private DataPermClient dataPermClient;

	@Resource
	private DsyJobService dsyJobService;
	
	/**
	 * 访问权限
	 * @param visualCreateUserId
	 * @param bladeUser
	 * @return
	 */
	public boolean checkJobDataForUser(Integer jobId, Long visualCreateUserId, BladeUser bladeUser) {
		//检查4个方面：
		//1.当前用户是否创建人
		//2.是否公开
		//3.是否超级权限组
		//4.当前用户是否拥有权限
		return  visualCreateUserId.longValue() == bladeUser.getUserId()
				|| dsyJobService.count(Wrappers.<DsyJob>lambdaQuery().eq(DsyJob::getId, jobId).eq(DsyJob::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[否则权限组不生效]
			dsyJobService.update(Wrappers.<DsyJob>lambdaUpdate().set(DsyJob::getPublicStatus, 0).in(DsyJob::getId, jobPermissionDto.getJobIdList()));
			List<DsyJobPermission> dataList = new ArrayList<>(jobPermissionDto.getJobIdList().size());
			jobPermissionDto.getJobIdList().forEach(t -> {
				DsyJobPermission entity = new DsyJobPermission();
				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.<DsyJobPermission>lambdaQuery().eq(DsyJobPermission::getJobId, jobPermDoo.getJobId()));
		if(jobPermDoo.getPublicStatus() != null) {
			dsyJobService.update(Wrappers.<DsyJob>lambdaUpdate().set(DsyJob::getPublicStatus, jobPermDoo.getPublicStatus()).eq(DsyJob::getId, jobPermDoo.getJobId()));
		}
		//2.批量添加权限组
		if(CollectionUtil.isNotEmpty(jobPermDoo.getDataPermIdList())) {
			List<DsyJobPermission> dataList = new ArrayList<>(jobPermDoo.getDataPermIdList().size());
			jobPermDoo.getDataPermIdList().forEach(dataPermId -> {
				DsyJobPermission entity = new DsyJobPermission();
				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);
	}
}