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.SysBusinessParameters;
import com.ssm.manage.query.BusinessParaRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmSysBusinessParaService;
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/businessPara")
@Slf4j
public class SsmSysBusinessParaController {
    @Autowired
    private SsmSysBusinessParaService ssmSysBusinessParaService;


    @ApiOperation(value = "分页查询业务参数   平台端端-业务参数-分页查询业务参数",notes = "",httpMethod = "POST",response = PageResponse.class)
    @RequestMapping(value = "/page")
    public PageResponse<SysBusinessParameters> selectPage(@RequestBody BusinessParaRequest businessParaRequest){
        PageResponse<SysBusinessParameters> pageResponse = new PageResponse<>();
        PageInfo<SysBusinessParameters> pageInfo = new PageInfo<>();
        try {
            pageInfo = ssmSysBusinessParaService.selectPage(businessParaRequest);
        }catch (Exception e){
            log.error("SsmSysBusinessParaController.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<SysBusinessParameters> add(@RequestBody BusinessParaRequest businessParaRequest){
        GenericResponse<SysBusinessParameters> genericResponse = new GenericResponse<>();
        if (StringUtils.isBlank(businessParaRequest.getParametersName())){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"参数名称");
            return genericResponse;
        }
        if (StringUtils.isBlank(businessParaRequest.getParametersValues())){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"参数值");
            return genericResponse;
        }
        int i = 0;
        try {
            i = ssmSysBusinessParaService.addGlobalPara(businessParaRequest);
        }catch (Exception e){
            log.error("SsmSysBusinessParaController.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<SysBusinessParameters> delete(@RequestBody BusinessParaRequest businessParaRequest){
        GenericResponse<SysBusinessParameters> genericResponse = new GenericResponse<>();
        if (businessParaRequest.getId() == null){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"id");
            return genericResponse;
        }
        int i = 0;
        try {
            i = ssmSysBusinessParaService.deleteGlobalPara(businessParaRequest);
        }catch (Exception e){
            log.error("SsmSysBusinessParaController.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<SysBusinessParameters> update(@RequestBody BusinessParaRequest businessParaRequest){
        GenericResponse<SysBusinessParameters> genericResponse = new GenericResponse<>();
        if (StringUtils.isBlank(businessParaRequest.getParametersName())){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"参数名称");
            return genericResponse;
        }
        if (StringUtils.isBlank(businessParaRequest.getParametersValues())){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"参数值");
            return genericResponse;
        }
        if (businessParaRequest.getId() == null){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"id");
            return genericResponse;
        }
        int i = 0;
        try {
            i = ssmSysBusinessParaService.updateGlobalPara(businessParaRequest);
        }catch (Exception e){
            log.error("SsmSysBusinessParaController.update",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0){
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }

}
