package com.gitee.sop.website.controller.website;

import com.gitee.sop.website.common.resp.Result;
import com.gitee.sop.website.common.util.CopyUtil;
import com.gitee.sop.website.controller.website.vo.HelpDocVO;
import com.gitee.sop.website.dao.entity.HelpDoc;
import com.gitee.sop.website.service.common.StatusEnum;
import com.gitee.sop.website.service.help.HelpDocService;
import com.gitee.sop.website.service.help.dto.HelpDocDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * @author 六如
 */
@RestController
@RequestMapping("website/help/doc")
public class WebsiteHelpController {

    @Autowired
    private HelpDocService helpDocService;

    /**
     * 查询帮助文档树状结构
     *
     * @return
     */
    @GetMapping("tree")
    public Result<List<HelpDocVO>> listTree() {
        List<HelpDocDTO> list = helpDocService.listTree();
        List<HelpDocVO> retList = CopyUtil.copyList(list, HelpDocVO::new);
        return Result.ok(retList);
    }

    /**
     * 查询帮助文档
     *
     * @return
     */
    @GetMapping("detail")
    public Result<HelpDocVO> doc(Long id) {
        HelpDoc helpDoc = helpDocService.getById(id);
        if (Objects.equals(helpDoc.getStatus(), StatusEnum.DISABLE.getValue())) {
            throw new RuntimeException("文档不存在");
        }
        HelpDocVO helpDocVO = CopyUtil.copyBean(helpDoc, HelpDocVO::new);
        return Result.ok(helpDocVO);
    }

}
