package com.yida.data.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yida.data.common.core.annotation.ControllerLog;
import com.yida.data.common.core.common.ModuleName;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.common.ResultMsgType;
import com.yida.data.common.core.entity.instructions.EduInstructionsContent;
import com.yida.data.common.core.entity.system.enums.OperationLogTypeEnum;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.log.annotation.OperationLog;
import com.yida.data.system.service.EduInstructionsContentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * Controller
 *
 * @author ccl
 * @date 2021-08-30 15:54:11
 */
@Api(tags = "系统指南")
@Slf4j
@Validated
@RestController
@RequestMapping("instructionsContent")
@RequiredArgsConstructor
public class EduInstructionsContentController {

    private final EduInstructionsContentService eduInstructionsContentService;

    @ApiOperation("分页获取系统指南内容LIST")
    @GetMapping("/findPageList")
    public ResultBean<IPage<EduInstructionsContent>> findPageList(
        @ApiParam("指南类型") @RequestParam(required = false) String typeCode,
        @ApiParam("指南标题") @RequestParam(required = false) String title,
        @ApiParam("当前页码,默认1") @RequestParam(defaultValue = "1") Integer pageNum,
        @ApiParam("分页大小，默认10") @RequestParam(defaultValue = "10") Integer pageSize
    ) {
        try {
            return ResultBean.buildSuccess(
                eduInstructionsContentService.listPageEduInstructionsContent(new Page(pageNum, pageSize), typeCode, title));
        } catch (Exception e) {
            log.error("分页获取系统指南内容LIST失败: {}", e.getMessage(), e);
            return ResultBean.buildError(ResultMsgType.QUERY_FAIL.getValue());
        }
    }

    @ApiOperation("根据typeCode或者Id获取单条指南内容")
    @PostMapping("/getOneEduInstructionsContent")
    public ResultBean<EduInstructionsContent> getOneEduInstructionsContent(@RequestParam(required = false) String typeCode,
        @RequestParam(required = false) Integer id
    ) {
        try {
            return ResultBean.buildSuccess(eduInstructionsContentService.getOneEduInstructionsContent(typeCode, id));
        } catch (Exception e) {
            log.error("根据typeCode或者Id获取单条指南内容失败: {}", e.getMessage(), e);
            return ResultBean.buildError(ResultMsgType.QUERY_FAIL.getValue());
        }
    }


    @ApiOperation("新增或修改系统指南内容")
    @PostMapping("/saveOrUpdateInstructions")
    @OperationLog(module = ModuleName.INSTRUCTION, methods = "新增或修改系统指南内容", type = OperationLogTypeEnum.SAVE)
    public ResultBean saveOrUpdateInstructions(@RequestBody EduInstructionsContent eduInstructionsContent) {
        try {
            boolean b = eduInstructionsContentService.saveInstructionsContent(eduInstructionsContent, FebsUtil.getCurrentUser());

            if (!b) {
                return ResultBean.buildError("该类型已存在操作指南");
            }
        } catch (Exception e) {
            log.error("新增或修改系统指南内容失败: {}", e.getMessage(), e);
            return ResultBean.buildError(ResultMsgType.SAVE_FAIL.getValue());
        }
        return ResultBean.buildSuccess();
    }


    @ApiOperation("删除系统指南内容")
    @PostMapping("/removeBatch")
    @OperationLog(module = ModuleName.INSTRUCTION, methods = "删除系统指南内容", type = OperationLogTypeEnum.DELETE)
    public ResultBean selectProduct(@ApiParam("系统指南内容id") @RequestBody List<Integer> ids) {
        try {
            eduInstructionsContentService.deleteByIds(ids);
            return ResultBean.buildSuccess();
        } catch (Exception e) {
            log.error("删除系统指南内容失败: {}", e.getMessage(), e);
            return ResultBean.buildError(ResultMsgType.DELETE_FAIL.getValue());
        }
    }

}
