package com.fzm.mall.server.admin.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fzm.mall.server.admin.annotation.AdminLog;
import com.fzm.mall.server.admin.enums.log.OperateModuleEnum;
import com.fzm.mall.server.admin.enums.log.OperateTypeEnum;
import com.fzm.mall.server.admin.repo.ResponseFactory;
import com.fzm.mall.server.admin.repo.ResponseVO;
import com.fzm.mall.server.admin.system.entity.dto.SysQuestionDTO;
import com.fzm.mall.server.admin.system.entity.dto.SysQuestionSaveDTO;
import com.fzm.mall.server.admin.system.entity.dto.SysQuestionUpdateDTO;
import com.fzm.mall.server.admin.system.service.ISysQuestionService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 系统中心-常见问题 前端控制器
 * </p>
 *
 * @author fzm
 * @since 2021-03-29
 */
@Api(tags = "系统中心")
@RestController
@RequestMapping("/sys-question")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysQuestionController {

    private final ISysQuestionService questionService;
    private final ResponseFactory resFac;

    @ApiOperation(value = "系统中心-常见问题-新增")
    @ApiOperationSupport(order = 1)
    @AdminLog(description = "系统中心-常见问题-新增", module = OperateModuleEnum.SYSTEM_CENTER, opType = OperateTypeEnum.ADD)
    @PostMapping("/save")
    public ResponseVO save(@RequestBody @Valid SysQuestionSaveDTO questionSaveDTO) {

        questionService.save(questionSaveDTO);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "系统中心-常见问题-编辑")
    @ApiOperationSupport(order = 2)
    @AdminLog(description = "系统中心-常见问题-编辑", module = OperateModuleEnum.SYSTEM_CENTER, opType = OperateTypeEnum.UPDATE)
    @PutMapping("/update")
    public ResponseVO update(@RequestBody @Valid SysQuestionUpdateDTO questionUpdateDTO) {

        questionService.update(questionUpdateDTO);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "系统中心-常见问题-删除")
    @ApiOperationSupport(order = 3)
    @AdminLog(description = "系统中心-常见问题-删除", module = OperateModuleEnum.SYSTEM_CENTER, opType = OperateTypeEnum.DELETE)
    @DeleteMapping("/delete/{questionId}")
    public ResponseVO delete(
            @ApiParam(value = "questionId", required = true) @PathVariable(value = "questionId") String questionId) {
        questionService.delete(questionId);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "系统中心-常见问题-主键查询")
    @ApiOperationSupport(order = 4)
    @GetMapping("/{questionId}")
    public ResponseVO<SysQuestionDTO> read(
            @ApiParam(value = "questionId", required = true) @PathVariable(value = "questionId") String questionId) {
        SysQuestionDTO result = questionService.read(questionId);
        return resFac.getSuccessResponseWithData(result);
    }

    @ApiOperation(value = "系统中心-常见问题-列表查询")
    @ApiOperationSupport(order = 5)
    @GetMapping("/list")
    public ResponseVO list(@ApiParam(value = "问题关键字") @RequestParam(required = false) String keyword,
                           @ApiParam(value = "导航编号") @RequestParam(required = false) String navigationId,
                           @ApiParam(value = "每页条数") @RequestParam(required = false) Long pageSize,
                           @ApiParam(value = "页码") @RequestParam(required = false) Long pageNum) {
        Page<SysQuestionDTO> page = new Page<>();
        if (ObjectUtils.isNotEmpty(pageSize) && ObjectUtils.isNotEmpty(pageNum)) {
            page.setSize(pageSize);
            page.setCurrent(pageNum);
        }

        IPage<SysQuestionDTO> result = questionService.page(page, keyword, navigationId);
        return resFac.getSuccessResponseWithData(result);
    }
}
