package com.unis.crk.basic.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.unis.crk.basic.entity.SysParams;
import com.unis.crk.basic.entity.vo.SysParamsVo;
import com.unis.crk.basic.service.ISysParamsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.unis.crk.utils.ResultUtil;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;


/**
 * <p>
 * 系统参数表 前端控制器
 * </p>
 *
 * @author ghh
 * @since 2021-10-12
 */
@Api(value = "系统参数表",tags = {"系统参数表"})
@RestController
@RequestMapping("sysParams")
@CrossOrigin
public class SysParamsController {

    private final static Logger LOGGER = LoggerFactory.getLogger(SysParamsController.class);


    @Autowired
    private ISysParamsService sysParamsService;


    /**
     * 根据ID查询数据
     * @param id
     * @return
     */
    @ApiOperation(value="查询", notes="根据ID查询数据")
    @GetMapping("getOne/{id}")
    public ResultUtil getOne(@PathVariable("id") String id){
        LOGGER.info("入参为:{}",id);
        ResultUtil<SysParams> data = new ResultUtil<>();
        if (StringUtils.isEmpty(id)){
            data.failed("传入的主键为空");
            return data;
        }
        data.success(this.sysParamsService.getById(id));
        return data;
    }

    /**
         * 分页高级查询
         * @param sysParamsVo
         */
    @ApiOperation(value="分页高级查询(可根据参数名称获取)", notes="分页高级查询(可根据参数名称获取)")
    @GetMapping(value="/findPage")
    public ResultUtil<IPage<SysParams>> findPage(@RequestBody SysParamsVo sysParamsVo) {
        ResultUtil<IPage<SysParams>> data = new ResultUtil<>();
        QueryWrapper<SysParams> queryWrapper = new QueryWrapper<>();
        Page<SysParams> p =new Page<>(sysParamsVo.getPageNum(),sysParamsVo.getPageSize());
        if(StrUtil.isNotBlank(sysParamsVo.getParamName())){
            queryWrapper.like("PARAM_NAME",ResultUtil.getQueryWrapperformat(sysParamsVo.getParamName()));
        }
        IPage<SysParams> page = sysParamsService.page(p, queryWrapper);
        data.success(page);
        return data;
    }

    /**
     * 新增一条数据
     * @param sysParams
     * @return
     */
    @ApiOperation(value="新增", notes="新增一条数据")
    @PostMapping("add")
    public ResultUtil add(@RequestBody SysParams sysParams){
        LOGGER.info("入参为:{}", sysParams);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (ObjectUtils.isEmpty(sysParams)){
            data.failed("所接收对象为null");
            return data;
        }
        boolean save = this.sysParamsService.save(sysParams);
        data.success(save);
        return data;
    }


    /**
     * 修改一条数据
     * @param sysParams
     * @return
     */
    @ApiOperation(value="修改", notes="修改一条数据")
    @PatchMapping("edit")
    public ResultUtil edit(@RequestBody  SysParams sysParams){
        LOGGER.info("入参为:{}", sysParams);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (ObjectUtils.isEmpty(sysParams)){
        data.failed("所接收对象为null");
        return data;
        }
        boolean b = sysParamsService.updateById(sysParams);
        data.success(b);
        return data;
    }


    /**
     * 根据ID删除数据
     * @param id
     * @return
     */
    @ApiOperation(value="删除", notes="根据ID删除数据")
    @DeleteMapping("delete/{id}")
    public ResultUtil delete(@PathVariable("id") String id){
        LOGGER.info("入参为:{}", id);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (StringUtils.isEmpty(id)){
            data.failed("所接收id为null");
            return data;
        }
        boolean b = sysParamsService.removeById(id);
        data.success(b);

        return data;
    }


}

