package com.pnkx.web.controller.blog.admin;

import com.pnkx.common.core.controller.BaseController;
import com.pnkx.common.core.domain.AjaxResult;
import com.pnkx.common.utils.SecurityUtils;
import com.pnkx.domain.po.PxBookkeepingRecord;
import com.pnkx.domain.po.PxDiary;
import com.pnkx.domain.po.PxNoteFolder;
import com.pnkx.domain.po.PxToDo;
import com.pnkx.domain.vo.PxArticleVo;
import com.pnkx.service.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 管理端controller
 *
 * @author phy
 * @date 2021-10-30
 */
@RestController
@RequestMapping("/admin")
public class PxAdminController extends BaseController {

    @Resource
    IPxArticleService pxArticleService;
    @Resource
    IPxToDoService pxToDoService;
    @Resource
    IPxBookkeepingRecordService pxBookkeepingRecordService;
    @Resource
    IPxDiaryService pxDiaryService;
    @Resource
    private IPxNoteFolderService pxNoteFolderService;
    /**
     * 全文检索
     */
    @GetMapping("/fullRetrieval")
    public AjaxResult retrieval(String searchCode) {
        logger.info("全文检索-检索条件为：" + searchCode);
        List<Map<String, Object>> result = new ArrayList<>();
        // 文章列表
        PxArticleVo pxArticle = new PxArticleVo();
        pxArticle.setSearchValue(searchCode);
        pxArticle.setCreateBy(SecurityUtils.getUserId().toString());
        List<PxArticleVo> pxArticles = pxArticleService.selectPxArticleNotContent(pxArticle);
        Map<String, Object> article = new HashMap<>();
        article.put("label", "博客文章");
        article.put("options", pxArticles);
        result.add(article);
        // 待办事项
        PxToDo pxToDo = new PxToDo();
        pxToDo.setSearchValue(searchCode);
        List<PxToDo> pxToDos = pxToDoService.selectPxToDoList(pxToDo);
        Map<String, Object> todo = new HashMap<>();
        todo.put("label", "待办事项");
        todo.put("options", pxToDos);
        result.add(todo);
        // 生活账本
        PxBookkeepingRecord pxBookkeepingRecord = new PxBookkeepingRecord();
        pxBookkeepingRecord.setSearchValue(searchCode);
        List<?> pxBookkeepingRecords = pxBookkeepingRecordService.selectPxBookkeepingRecordList(pxBookkeepingRecord).getRows();
        Map<String, Object> bookkeeping = new HashMap<>();
        bookkeeping.put("label", "生活账本");
        bookkeeping.put("options", pxBookkeepingRecords);
        result.add(bookkeeping);
        // 日记
        List<PxDiary> diaryList = pxDiaryService.retrieval(searchCode);
        Map<String, Object> diary = new HashMap<>();
        diary.put("label", "日记");
        diary.put("options", diaryList);
        result.add(diary);
        // 笔记
        PxNoteFolder pxNoteFolder = new PxNoteFolder();
        pxNoteFolder.setName(searchCode);
        List<PxNoteFolder> noteList = pxNoteFolderService.selectPxNoteFolderList(pxNoteFolder);
        Map<String, Object> note = new HashMap<>();
        note.put("label", "笔记");
        note.put("options", noteList);
        result.add(note);
        return AjaxResult.success(result);
    }
}
