package com.gitee.sop.admin.controller.help;

import com.gitee.fastmybatis.core.PageInfo;
import com.gitee.fastmybatis.core.query.LambdaQuery;
import com.gitee.sop.admin.common.req.IdParam;
import com.gitee.sop.admin.common.resp.Result;
import com.gitee.sop.admin.common.util.CopyUtil;
import com.gitee.sop.admin.controller.doc.param.HelpDocAddParam;
import com.gitee.sop.admin.controller.doc.param.HelpDocSearchParam;
import com.gitee.sop.admin.controller.doc.param.HelpDocUpdateParam;
import com.gitee.sop.admin.controller.help.vo.HelpDocVO;
import com.gitee.sop.admin.dao.entity.HelpDoc;
import com.gitee.sop.admin.service.help.HelpDocService;
import com.gitee.sop.admin.service.help.dto.HelpDocDTO;
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.RestController;

import java.util.List;


/**
 * HelpDoc
 *
 * @author thc
 */
@RestController
@RequestMapping("help/doc")
public class HelpDocController {

    @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);
        HelpDocVO helpDocVO = CopyUtil.copyBean(helpDoc, HelpDocVO::new);
        return Result.ok(helpDocVO);
    }

    /**
     * 分页查询
     *
     * @param param 查询参数
     * @return 返回分页结果
     */
    @GetMapping("/page")
    public Result<PageInfo<HelpDocVO>> page(HelpDocSearchParam param) {
        LambdaQuery<HelpDoc> query = param.toLambdaQuery(HelpDoc.class);
        PageInfo<HelpDocVO> pageInfo = helpDocService.doPage(query)
                .convert(data -> CopyUtil.copyBean(data, HelpDocVO::new));
        return Result.ok(pageInfo);
    }

    /**
     * 新增记录
     *
     * @param param 表单参数
     * @return 返回添加后的主键值
     */
    @PostMapping("/add")
    public Result<Long> add(@Validated @RequestBody HelpDocAddParam param) {
        HelpDoc helpDoc = CopyUtil.copyBean(param, HelpDoc::new);
        helpDocService.save(helpDoc);
        // 返回添加后的主键值
        return Result.ok(helpDoc.getId());
    }

    /**
     * 修改记录
     *
     * @param param 表单数据
     * @return 返回影响行数
     */
    @PostMapping("/update")
    public Result<Integer> update(@Validated @RequestBody HelpDocUpdateParam param) {
        HelpDoc helpDoc = CopyUtil.copyBean(param, HelpDoc::new);
        return Result.ok(helpDocService.update(helpDoc));
    }


    /**
     * 删除记录
     *
     * @param param 参数
     * @return 返回影响行数
     */
    @PostMapping("/delete")
    public Result<Integer> delete(@Validated @RequestBody IdParam param) {
        return Result.ok(helpDocService.deleteById(param.getId()));
    }

}
