package com.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.ssm.manage.response.GenericResponse;
import com.ssm.manage.response.PageResponse;
import com.ssm.manage.entity.SysGlobalParameters;
import com.ssm.manage.query.GlobalParaRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmSysGlobalParaService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/demay/ssm/globalPara")
@Slf4j
public class SsmSysGlobalParaController {
    @Autowired
    private SsmSysGlobalParaService ssmSysGlobalParaService;


    @ApiOperation(value = "分页查询全局参数  平台端端-全局参数-分页查询全局参数",notes = "",httpMethod = "POST",response = PageResponse.class)
    @RequestMapping(value = "/page")
    public PageResponse<SysGlobalParameters> selectPage(@RequestBody GlobalParaRequest globalParaRequest){
        PageResponse<SysGlobalParameters> pageResponse = new PageResponse<>();
        PageInfo<SysGlobalParameters> pageInfo = new PageInfo<>();
        try {
            pageInfo = ssmSysGlobalParaService.selectPage(globalParaRequest);
        }catch (Exception e){
            log.error("SsmSysGlobalParaController.selectPage",e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setPageInfo(pageInfo);
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        return pageResponse;
    }

    @ApiOperation(value = "添加全局参数  平台端端-全局参数-添加全局参数",notes = "",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping(value = "/add")
    public GenericResponse<SysGlobalParameters> add(@RequestBody GlobalParaRequest globalParaRequest){
        GenericResponse<SysGlobalParameters> genericResponse = new GenericResponse<>();
        if (StringUtils.isBlank(globalParaRequest.getParametersName())){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"参数名称");
            return genericResponse;
        }
        if (StringUtils.isBlank(globalParaRequest.getParametersValues())){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"参数值");
            return genericResponse;
        }
        int i = 0;
        try {
            i = ssmSysGlobalParaService.addGlobalPara(globalParaRequest);
        }catch (Exception e){
            log.error("SsmSysGlobalParaController.add",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0){
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }

    @ApiOperation(value = "删除全局参数  平台端端-全局参数-删除全局参数",notes = "",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping(value = "/del")
    public GenericResponse<SysGlobalParameters> delete(@RequestBody GlobalParaRequest globalParaRequest){
        GenericResponse<SysGlobalParameters> genericResponse = new GenericResponse<>();
        if (globalParaRequest.getId() == null){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"id");
            return genericResponse;
        }
        int i = 0;
        try {
            i = ssmSysGlobalParaService.deleteGlobalPara(globalParaRequest);
        }catch (Exception e){
            log.error("SsmSysGlobalParaController.delete",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0){
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }

    @ApiOperation(value = "修改全局参数   平台端端-全局参数-修改全局参数",notes = "",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping(value = "/update")
    public GenericResponse<SysGlobalParameters> update(@RequestBody GlobalParaRequest globalParaRequest){
        GenericResponse<SysGlobalParameters> genericResponse = new GenericResponse<>();
        if (StringUtils.isBlank(globalParaRequest.getParametersName())){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"参数名称");
            return genericResponse;
        }
        if (StringUtils.isBlank(globalParaRequest.getParametersValues())){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"参数值");
            return genericResponse;
        }
        if (globalParaRequest.getId() == null){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"id");
            return genericResponse;
        }
        int i = 0;
        try {
            i = ssmSysGlobalParaService.updateGlobalPara(globalParaRequest);
        }catch (Exception e){
            log.error("SsmSysGlobalParaController.update",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0){
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }

}
