package com.sqx.modules.helpCenter.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sqx.common.utils.DateUtils;
import com.sqx.common.utils.PageUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.helpCenter.entity.HelpClassify;
import com.sqx.modules.helpCenter.entity.HelpWord;
import com.sqx.modules.helpCenter.service.HelpClassifyService;
import com.sqx.modules.helpCenter.service.HelpWordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@Api(value = "帮助中心", tags = {"帮助中心"})
@RequestMapping(value = "/helpWord")
public class HelpWordController {

    @Autowired
    private HelpClassifyService helpClassifyService;
    @Autowired
    private HelpWordService helpWordService;


    @PostMapping("/insertHelpClassify")
    @ApiOperation("添加帮助分类")
    public Result insertHelpClassify(@RequestBody HelpClassify helpClassify) {
        helpClassify.setCreateTime(DateUtils.format(new Date()));
        helpClassifyService.save(helpClassify);
        return Result.success();
    }

    @PostMapping("/updateHelpClassify")
    @ApiOperation("修改帮助分类")
    public Result updateHelpClassify(@RequestBody HelpClassify helpClassify) {
        helpClassifyService.updateById(helpClassify);
        return Result.success();
    }

    @PostMapping("/deleteHelpClassify")
    @ApiOperation("删除帮助分类")
    public Result deleteHelpClassify(Long helpClassifyId) {
        helpClassifyService.removeById(helpClassifyId);
        return Result.success();
    }


    @GetMapping("/selectHelpClassifyList")
    @ApiOperation("查询帮助分类")
    public Result selectHelpClassifyList(Integer page, Integer limit, Long parentId, Integer types, String helpClassifyName) {
        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 10;
        }

        PageHelper.startPage(page, limit);
        List<HelpClassify> list = helpClassifyService.list(new QueryWrapper<HelpClassify>()
                .eq(types != null, "types", types)
                .eq(StringUtils.isNotBlank(helpClassifyName), "help_classify_name", helpClassifyName)
                .eq(parentId != null, "parent_id", parentId).orderByAsc("sort"));

        PageInfo<HelpClassify> pageInfo = new PageInfo<>(list);
        return Result.success().put("data", PageUtils.page(pageInfo));
    }


    @PostMapping("/insertHelpWord")
    @ApiOperation("添加帮助文档")
    public Result insertHelpWord(@RequestBody HelpWord helpWord) {
        helpWord.setCreateTime(DateUtils.format(new Date()));
        helpWordService.save(helpWord);
        return Result.success();
    }

    @PostMapping("/updateHelpWord")
    @ApiOperation("修改帮助文档")
    public Result updateHelpWord(@RequestBody HelpWord helpWord) {
        helpWordService.updateById(helpWord);
        return Result.success();
    }

    @PostMapping("/deleteHelpWord")
    @ApiOperation("删除帮助文档")
    public Result deleteHelpWord(Long helpWordId) {
        helpWordService.removeById(helpWordId);
        return Result.success();
    }


    @GetMapping("/selectHelpWordList")
    @ApiOperation("查询帮助文档")
    public Result selectHelpWordList(Integer page, Integer limit, Long helpClassifyId, String helpWordTitle) {
        PageHelper.startPage(page, limit);

        List<HelpWord> page1 = helpWordService.list(new QueryWrapper<HelpWord>()
                .eq(helpClassifyId != null, "help_classify_id", helpClassifyId)
                .eq(StringUtils.isNotBlank(helpWordTitle), "help_word_title", helpWordTitle).orderByAsc("sort"));

        PageInfo<HelpWord> pageInfo = new PageInfo<>(page1);
        return Result.success().put("data", PageUtils.page(pageInfo));
    }


}
