package org.springblade.modules.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.modules.system.entity.PlatomixDocument;
import org.springblade.modules.system.entity.PlatomixDocumentCate;
import org.springblade.modules.system.service.PlatomixDocumentCateService;
import org.springblade.modules.system.service.PlatomixDocumentService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *
 * 控制器
 *
 */
@RestController
@AllArgsConstructor
@RequestMapping("/system/document")
@Api(value = "ES文档管理", tags = "ES文档管理")
public class DocumentController extends BladeController {

	@Resource
	private PlatomixDocumentCateService documentCateService;

	@Resource
	private PlatomixDocumentService documentService;

	/**
	 * 获取ES文档分类列表
	 */
	@GetMapping("/cate/list")
	@ApiOperation(value = "获取ES文档分类列表", notes = "获取ES文档分类列表")
	public R<IPage<PlatomixDocumentCate>> cateList(PlatomixDocumentCate entity, Query query) {
		LambdaQueryWrapper queryWrapper =  new LambdaQueryWrapper<PlatomixDocumentCate>().orderByDesc(PlatomixDocumentCate::getId);
		IPage<PlatomixDocumentCate> page = Condition.getPage(query);
		IPage<PlatomixDocumentCate> pages = documentCateService.page(page, queryWrapper);
		return R.data(pages);
	}

	/**
	 * 添加编辑ES文档分类
	 */
	@PostMapping("/cate/save")
	@ApiOperation(value = "添加编辑ES文档分类", notes = "添加编辑ES文档分类")
	public R<Object> cateSave(@RequestBody PlatomixDocumentCate entity) {
		if(StringUtils.isBlank(entity.getName())){
			return R.fail("名称不能为空");
		}
		Boolean result = documentCateService.customSave(entity);
		if(result){
			return R.data("保存成功.");
		}else{
			return R.fail("保存失败.");
		}
	}


	/**
	 * 删除ES文档分类
	 */
	@PostMapping("/cate/remove")
	@ApiOperation(value = "删除ES文档分类", notes = "删除ES文档分类")
	public R<Object> cateRemove(@ApiParam(value = "主键集合", required = true) @RequestBody List<Long> idList) {
		if(CollectionUtils.isEmpty(idList)){
			return R.fail("ES文档分类ID值不能为空.");
		}
		Boolean result = documentCateService.customRemove(idList);
		if(result){
			return R.data("删除成功.");
		}else{
			return R.fail("删除失败.");
		}
	}

	/**
	 * 获取ES文档列表
	 */
	@GetMapping("/list")
	@ApiOperation(value = "获取ES文档列表", notes = "获取ES文档列表")
	public R<IPage<PlatomixDocument>> list(PlatomixDocument entity, Query query) {
		LambdaQueryWrapper queryWrapper =  new LambdaQueryWrapper<PlatomixDocument>()
				.eq(ObjectUtils.isNotEmpty(entity.getCateId()), PlatomixDocument::getCateId, entity.getCateId())
				.like(StringUtils.isNotBlank(entity.getName()), PlatomixDocument::getName, entity.getName())
				.orderByDesc(PlatomixDocument::getCreateTime);
		IPage<PlatomixDocument> page = Condition.getPage(query);
		IPage<PlatomixDocument> pages = documentService.page(page, queryWrapper);
		if(pages.getRecords()!=null){
			List<Long> cateIdList = pages.getRecords().stream().map(obj->obj.getCateId()).collect(Collectors.toList());
			if(CollectionUtils.isNotEmpty(cateIdList)){
				LambdaQueryWrapper queryW =  new LambdaQueryWrapper<PlatomixDocumentCate>().in(PlatomixDocumentCate::getId, cateIdList);
				List<PlatomixDocumentCate> cateList = documentCateService.list(queryW);
				if(CollectionUtils.isNotEmpty(cateIdList)){
					Map<Long, String> cateMap = cateList.stream().collect(Collectors.toMap(PlatomixDocumentCate::getId, PlatomixDocumentCate::getName, (key1, key2)->key1));
					for(PlatomixDocument templateEntity: pages.getRecords()){
						if(cateMap.containsKey(templateEntity.getCateId())){
							templateEntity.setCateName(cateMap.get(templateEntity.getCateId()));
						}
					}
				}
			}
		}
		return R.data(pages);
	}

	/**
	 * 添加编辑ES文档
	 */
	@PostMapping("/save")
	@ApiOperation(value = "添加编辑ES文档", notes = "添加编辑ES文档")
	public R<Object> save(@RequestBody PlatomixDocument entity) {
		if(StringUtils.isBlank(entity.getName())){
			return R.fail("名称不能为空.");
		}
		if(ObjectUtils.isEmpty(entity.getCateId())){
			return R.fail("ES文档分类ID不能为空.");
		}
		Boolean result = documentService.customSave(entity);
		if(result){
			return R.data("保存成功.");
		}else{
			return R.fail("保存失败.");
		}
	}


	/**
	 * 删除ES文档
	 */
	@PostMapping("/remove")
	@ApiOperation(value = "删除ES文档", notes = "删除ES文档")
	public R<Object> remove(@ApiParam(value = "主键集合", required = true) @RequestBody List<Long> idList) {
		if(CollectionUtils.isEmpty(idList)){
			return R.fail("ES文档ID值不能为空.");
		}
		Boolean result = documentService.customRemove(idList);
		if(result){
			return R.data("删除成功.");
		}else{
			return R.fail("删除失败.");
		}
	}
}
