package com.jeeplus.test.help.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.aop.logging.annotation.ApiLog;
import com.jeeplus.sys.constant.enums.LogTypeEnum;
import com.jeeplus.test.help.domain.Help;
import com.jeeplus.test.help.service.HelpService;
import com.jeeplus.test.help.service.dto.HelpDTO;
import io.swagger.annotations.Api;
import org.apache.ibatis.annotations.Param;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@Api("帮助管理")
@RestController
@RequestMapping(value = "/help")
public class HelpController {

    @Resource
    private HelpService helpService;

    /**
     * 查询帮助管理列表
     * @param helpDTO
     * @param page
     * @return
     * @throws Exception
     */
    @ApiLog("查询帮助管理列表")
    @PreAuthorize("hasAuthority('help:list')")
    @GetMapping("list")
    public ResponseEntity<IPage<HelpDTO>> data(HelpDTO helpDTO, Page<HelpDTO> page) throws Exception {
        IPage<HelpDTO> result = new Page<HelpDTO>();
        result = helpService.findList (page,helpDTO);
        return ResponseEntity.ok (result);
    }

    /**
     * 查询帮助管理数据
     * @param id
     * @return
     */
    @ApiLog("查询帮助管理数据")
    @PreAuthorize("hasAnyAuthority('help:view','help:add','help:edit')")
    @GetMapping("queryById")
    public ResponseEntity queryById(@RequestParam("id") String id) {
        HelpDTO helpDTO = helpService.queryById ( id );
        return ResponseEntity.ok (helpDTO);
    }

    /**
     * 保存帮助
     * @param helpDTO
     * @return
     */
    @ApiLog(value = "修改/新增帮助", type = LogTypeEnum.SAVE)
    @PreAuthorize("hasAnyAuthority('help:add','help:edit')")
    @PostMapping("save")
    public ResponseEntity save(@Valid @RequestBody HelpDTO helpDTO) throws Exception {
        helpService.saveHelp(helpDTO);
        return ResponseEntity.ok ("操作成功");
    }

    /**
     * 删除帮助
     * @param ids
     * @return
     */
    @ApiLog(value = "删除帮助", type = LogTypeEnum.SAVE)
    @PreAuthorize ("hasAuthority('help:del')")
    @DeleteMapping("delete")
    public ResponseEntity delete(String ids) {
        return helpService.deleteByIds(ids);
    }

    /**
     * 帮助管理状态 启用/停用
     * @return
     */
    @ApiLog(value = "帮助管理状态启用/停用", type = LogTypeEnum.SAVE)
    @PreAuthorize("hasAnyAuthority('help:add','help:edit')")
    @PostMapping("updateStatus")
    public ResponseEntity updateStatus(@RequestBody HelpDTO helpDTO) {
        return helpService.updateStatus(helpDTO);
    }
}
