package com.ruoyi.web.controller.home;



import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.ruoyi.system.domain.KCategory;
import com.ruoyi.system.domain.KCategoryVO;
import com.ruoyi.system.domain.KContent;
import com.ruoyi.system.service.KCategoryService;
import com.ruoyi.system.service.KContentService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


/**
 * 知识库查询
 *  @author wangxy
 */
@Controller
public class HomeController {

	@Autowired
	private KContentService contentService;

	@Autowired
	private KCategoryService categoryService;


	public static final String PAGE_SIZE = "15";

	@ApiOperation("知识库查询")
	@GetMapping("/home")
	public String home(ModelMap mmap,
					   @RequestParam(required = false) Integer categoryid,
					   @RequestParam(required = false) String kname,
					   @RequestParam(required = false, defaultValue = "1", value = "p") Integer pageNum,
					   @RequestParam(required = false ,defaultValue = PAGE_SIZE, value = "ps") Integer pageSize) {
		//知识库查询
		PageMethod.startPage(pageNum,pageSize);
		List<KContent> list = contentService.lambdaQuery()
				.eq(Objects.nonNull(categoryid), KContent::getCategoryid, categoryid)
				.like(Objects.nonNull(kname), KContent::getKname, kname)
				.orderByDesc(KContent::getUpdatetime).list();
		list.forEach(kContent -> {
			KCategory  category = categoryService.lambdaQuery()
					.eq(KCategory::getId, kContent.getCategoryid())
					.one();
			kContent.setCategoryname( Objects.nonNull(category) ? category.getCategoryname() : "" );
		});
		PageInfo<KContent> page = new PageInfo<>(list, pageSize);
		mmap.put("page", page);
		mmap.put("pageUrl", "/home");
		mmap.put("categoryid", categoryid);
		mmap.put("kname", kname);
		return "home/search _list";
	}


	@ApiOperation("详情")
	@GetMapping("/public_view.html")
	public String content(ModelMap mmap, @RequestParam String id) {
		KContent content = contentService.getById(id);
		mmap.put("content", content);
		return "home/public_view";
	}


	@ApiOperation("类别树")
	@GetMapping("/search")
	@ResponseBody
	public List<KCategory> search(
			@RequestParam(required = false) Integer categoryid) {
		return categoryService.lambdaQuery()
				.eq(KCategory::getHigherupid, Objects.nonNull(categoryid) ? categoryid : 0).list();
	}

	@ApiOperation("类别树")
	@GetMapping("/getTree")
	@ResponseBody
	public List<KCategoryVO> getTree(@RequestParam(required = false,defaultValue = "0") Integer categoryid) {
        LambdaQueryWrapper<KCategory> tagWrapper = new LambdaQueryWrapper<>();
		tagWrapper.eq(KCategory::getHigherupid, categoryid);
		List<KCategory> datas = categoryService.list(tagWrapper);
        List<KCategoryVO> tagTreeVOList = new ArrayList<>(Convert.toList(KCategoryVO.class, datas));
		tagTreeVOList.forEach(this::buildChildren);
		return tagTreeVOList;
	}


	/**
	 * 构建子节点
	 *
	 * @param pNode 父级
	 */
	private void buildChildren(KCategoryVO pNode) {
		// 获取子节点
		LambdaQueryWrapper<KCategory> tagWrapper = new LambdaQueryWrapper<>();
		tagWrapper.eq(KCategory::getHigherupid, pNode.getId());
		List<KCategory> dataTags = categoryService.list(tagWrapper);
		List<KCategoryVO> children = Convert.toList(KCategoryVO.class,dataTags);
		// 递归获取子节点
		children.forEach(this::buildChildren);
		pNode.setChildren(children);
	}


}
