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

import java.time.LocalDateTime;
import java.util.List;

import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.etl.entity.DsyJob;
import org.springblade.etl.entity.DsyJobCategory;
import org.springblade.etl.service.DsyJobCategoryService;
import org.springblade.etl.service.DsyJobService;
import org.springblade.system.client.DataPermClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 *	数据同步任务分类 操作处理
 *
 * @author aokunsang
 * @date   2023-09-21
 */
@RestController
@RequestMapping("/dsy-job-category")
@Api(value = "数据同步任务分类相关接口", tags = "数据同步任务分类接口")
public class DsyJobCategoryController extends BladeController {

	@Autowired
	private DsyJobCategoryService dsyJobCategoryService;
	
	@Autowired
	private DsyJobService dsyJobService;
	
	@Autowired
	private DataPermClient dataPermClient;
	
	/**
	 * 	列表数据
	 * @param dsyJobCategory
	 * @return
	 */
	@GetMapping("/list")
	@ApiOperation(value = "查询数据同步任务分类列表", httpMethod = "GET")
	public R<List<DsyJobCategory>> list(DsyJobCategory dsyJobCategory){
		QueryWrapper<DsyJobCategory> queryWrapper = Condition.getQueryWrapper(dsyJobCategory);
		long currUserId = getUser().getUserId();
		List<DsyJobCategory> dataList = dsyJobCategoryService.list(queryWrapper.lambda().orderByDesc(DsyJobCategory::getSortNum).orderByDesc(DsyJobCategory::getId));
		Boolean checkSuperPermRet = dataPermClient.checkSuperPermission(currUserId);
		if(checkSuperPermRet != null && checkSuperPermRet) { //是否有超级权限
			return R.data(dataList);
		}else {
			//查询所有任务有权限的分类ID集合
			List<Integer> categoryIdList = dsyJobService.categoryIdListOfPerm(currUserId);
			//1.获取这些分类的父级ID[包含自己创建的分类父级别ID]
			dataList.stream().filter(f -> (categoryIdList.contains(f.getId()) || f.getCreateUserId().longValue() == currUserId) && f.getParentId() != 0).map(DsyJobCategory::getParentId).forEach(categoryIdList::add);
			//2.删除不在权限内的分类[且createUserId不是本人的]
			dataList.removeIf(y -> !categoryIdList.contains(y.getId()) && y.getCreateUserId().longValue() != currUserId);
			return R.data(dataList);
		}
	}
	
	/**
	 *	修改数据接口
	 * @param dsyJobCategory
	 * @return
	 */
	@ApiLog("数据同步任务分类")
	@PostMapping("/submit")
	@ApiOperation(value = "增加或修改数据同步任务分类", httpMethod = "POST")
	public R<String> doUpdate(@Validated @RequestBody DsyJobCategory dsyJobCategory) {
		//判重
		List<DsyJobCategory> list = dsyJobCategoryService.lambdaQuery()
				.eq(DsyJobCategory::getCateName, dsyJobCategory.getCateName())
				.ne(dsyJobCategory.getId() != null, DsyJobCategory::getId, dsyJobCategory.getId()).list();
		if (CollectionUtil.isNotEmpty(list)) return R.fail("名字重复");
		if(dsyJobCategory.getId() == null) {
			dsyJobCategory.setCreateTime(LocalDateTime.now());
			dsyJobCategory.setCreateUserId(AuthUtil.getUserId());
			dsyJobCategory.setCreateBy(AuthUtil.getNickName());
		}
		return R.status(dsyJobCategoryService.saveOrUpdate(dsyJobCategory));
	}
	
	/**
	 * 	删除数据接口
	 * @param id
	 * @return
	 */
	@ApiLog("数据同步任务分类")
	@PostMapping("/delete")
	@ApiOperation(value = "根据ID删除数据同步任务分类", httpMethod = "POST")
	public R<String> doDelete(@RequestParam Integer id) {
		long cnt = dsyJobService.count(Wrappers.<DsyJob>lambdaQuery().eq(DsyJob::getCategoryId, id));
		if(cnt > 0) {
			return R.fail("该分类下有任务，不能删除。");
		}
		long count = dsyJobCategoryService.count(Wrappers.<DsyJobCategory>lambdaQuery().eq(DsyJobCategory::getParentId, id));
		if(count > 0) {
			return R.fail("该分类下有子分类，不能删除。");
		}
		return R.status(dsyJobCategoryService.removeById(id));
	}
}