package com.fkzy.warn.controller;

import com.fkzy.warn.common.constants.ResponseMsgConstants;
import com.fkzy.warn.common.request.RequestParameter;
import com.fkzy.warn.common.response.ApiResponse;
import com.fkzy.warn.model.Test;
import com.fkzy.warn.service.TestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @author yuyantian
 * @date 2023/04/26
 */
@Api(tags = "Test Api")
@RestController
@RequestMapping("test/")
@Slf4j
public class TestController {

    /**
     * TestService服务
     */
    @Autowired
    private TestService testService;

    @RequestMapping(value = "queryPage", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "分页查询xxx内容")
    public ApiResponse queryPage(@ApiParam() @RequestBody RequestParameter parameter) {
        log.info("分页查询 ==== 参数{" + parameter.toString() + "}");
        ApiResponse apiResponse = new ApiResponse();
        if (!Objects.isNull(parameter) && !Objects.isNull(parameter.getParameter())) {
            try {
               // Page<Test> result = testService.queryPage(parameter);
                apiResponse.setData(testService.list());
                apiResponse.setMessage(ResponseMsgConstants.OPERATE_SUCCESS);
            } catch (Exception e) {
                log.error("查询错误,errMsg==={}", e.getMessage());
                e.printStackTrace();
                apiResponse.recordError(ResponseMsgConstants.OPERATE_FAIL);
            }
        } else {
            apiResponse.recordError(ResponseMsgConstants.OPERATE_FAIL);
        }
        return apiResponse;
    }

    /**
     * 插入Test属性不为空的数据方法
     *
     * @param
     * @return
     */
    @ResponseBody
    @ApiOperation(value = "新增")
    @RequestMapping(value = "add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse add(@ApiParam() @RequestBody Test entity) {
        log.info("新增==== 参数{" + entity != null ? entity.toString() : "null" + "}");
        ApiResponse apiResponse = new ApiResponse();
        if (!Objects.isNull(entity)) {
            try {
                boolean result = testService.saveModel(entity);
                if (result) {
                    apiResponse.setMessage(ResponseMsgConstants.OPERATE_SUCCESS);
                } else {
                    apiResponse.recordError(ResponseMsgConstants.OPERATE_FAIL);
                }
            } catch (Exception e) {
                log.error("新增错误,errMsg==={}", e.getMessage());
                e.printStackTrace();
                apiResponse.recordError(ResponseMsgConstants.OPERATE_FAIL);
            }
        } else {
            apiResponse.recordError(ResponseMsgConstants.OPERATE_FAIL);
        }
        return apiResponse;
    }

    /**
     * 修改
     *
     * @param
     * @return
     */
    @ResponseBody
    @ApiOperation(value = "修改")
    @RequestMapping(value = "edit", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse edit(@ApiParam() @RequestBody Test entity) {
        log.info("修改==== 参数{" + entity != null ? entity.toString() : "null" + "}");
        ApiResponse apiResponse = new ApiResponse();
        if (!Objects.isNull(entity)) {
            try {
                boolean result = testService.updateModel(entity);
                if (result) {
                    apiResponse.setMessage(ResponseMsgConstants.OPERATE_SUCCESS);
                } else {
                    apiResponse.recordError(ResponseMsgConstants.OPERATE_FAIL);
                }
            } catch (Exception e) {
                log.error("修改错误,errMsg==={}", e.getMessage());
                e.printStackTrace();
                apiResponse.recordError(ResponseMsgConstants.OPERATE_FAIL);
            }
        } else {
            apiResponse.recordError(ResponseMsgConstants.OPERATE_FAIL);
        }
        return apiResponse;
    }

    /**
     * 通过id删除Test数据方法
     *
     * @param
     * @return
     */
    @ResponseBody
    @ApiOperation(value = "删除")
    @RequestMapping(value = "remove", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse remove(@ApiParam() @RequestBody Test entity) {
        log.info("删除 ==== 参数{" + entity != null ? entity.toString() : "null" + "}");
        ApiResponse apiResponse = new ApiResponse();
        if (!Objects.isNull(entity)) {
            try {
                boolean result = testService.deleteModel(entity);
                if (result) {
                    apiResponse.setMessage(ResponseMsgConstants.OPERATE_SUCCESS);
                } else {
                    apiResponse.recordError(ResponseMsgConstants.OPERATE_FAIL);
                }
            } catch (Exception e) {
                log.error("删除错误,errMsg==={}", e.getMessage());
                e.printStackTrace();
                apiResponse.recordError(ResponseMsgConstants.OPERATE_FAIL);
            }
        } else {
            apiResponse.recordError(ResponseMsgConstants.OPERATE_FAIL);
        }
        return apiResponse;
    }

}
