package com.zhgd.xmgl.modules.basicdata.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhgd.annotation.OperLog;
import com.zhgd.jeecg.common.api.vo.Result;
import com.zhgd.jeecg.common.system.query.QueryGenerator;
import com.zhgd.jeecg.common.util.PageUtil;
import com.zhgd.xmgl.modules.basicdata.entity.SystemConfig;
import com.zhgd.xmgl.modules.basicdata.service.ISystemConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


 /**
 * @Title: Controller
 * @Description: 参数配置信息
 * @author： pds
 * @date：   2023-02-15
 * @version： V1.0
 */
@RestController
@RequestMapping("/xmgl/systemConfig")
@Slf4j
@Api(tags = "系统参数配置信息")
public class SystemConfigController {
	@Autowired
	private ISystemConfigService systemConfigService;
	
	/**
	  * 分页列表查询
	 * @return
	 */
	@OperLog(operModul = "系统配置管理", operType = "分页查询", operDesc = "分页列表查询参数配置信息信息")
	@ApiOperation(value = " 分页列表查询参数配置信息信息", notes = "分页列表查询参数配置信息信息", httpMethod="POST")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "configName", value = "系统配置名称", paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "pageNo", value = "页码", paramType = "query", required = true , defaultValue = "1", dataType = "Integer"),
			@ApiImplicitParam(name = "pageSize", value = "每页条数", paramType = "query", required = true , defaultValue = "10", dataType = "Integer")
	})
	@PostMapping(value = "/page")
	public Result<IPage<SystemConfig>> queryPageList(@RequestBody Map<String, Object> map) {
		Result<IPage<SystemConfig>> result = new Result<IPage<SystemConfig>>();
		QueryWrapper<SystemConfig> queryWrapper = QueryGenerator.initPageQueryWrapper(SystemConfig.class, map);
		Page<SystemConfig> page = PageUtil.getPage(map);
		IPage<SystemConfig> pageList = systemConfigService.page(page, queryWrapper);
		result.setSuccess(true);
		result.setResult(pageList);
		return result;
	}

	 /**
	  * 列表查询
	  * @param systemConfig
	  * @param req
	  * @return
	  */
	 @OperLog(operModul = "系统配置管理", operType = "列表查询", operDesc = "列表查询参数配置信息信息")
	 @ApiOperation(value = " 列表查询参数配置信息信息", notes = "列表查询参数配置信息信息", httpMethod="POST")
	 @PostMapping(value = "/list")
	 public Result<List<SystemConfig>> queryList(@RequestBody SystemConfig systemConfig, HttpServletRequest req) {
		 Result<List<SystemConfig>> result = new Result<List<SystemConfig>>();
		 QueryWrapper<SystemConfig> queryWrapper = QueryGenerator.initQueryWrapper(systemConfig, req.getParameterMap());
		 List<SystemConfig> list = systemConfigService.list(queryWrapper);
		 result.setSuccess(true);
		 result.setResult(list);
		 return result;
	 }
	
	/**
	  *   添加
	 * @param systemConfig
	 * @return
	 */
	@OperLog(operModul = "系统配置管理", operType = "新增", operDesc = "添加参数配置信息信息")
	@ApiOperation(value = " 添加参数配置信息信息", notes = "添加参数配置信息信息" , httpMethod="POST")
	@PostMapping(value = "/add")
	public Result<SystemConfig> add(@RequestBody SystemConfig systemConfig) {
		Result<SystemConfig> result = new Result<SystemConfig>();
		systemConfigService.save(systemConfig);
		result.success("添加成功！");
		return result;
	}
	
	/**
	  *  编辑
	 * @param systemConfig
	 * @return
	 */
	@OperLog(operModul = "系统配置管理", operType = "修改", operDesc = "编辑参数配置信息信息")
	@ApiOperation(value = "编辑参数配置信息信息", notes = "编辑参数配置信息信息" , httpMethod="POST")
	@PostMapping(value = "/edit")
	public Result<SystemConfig> edit(@RequestBody SystemConfig systemConfig) {
		Result<SystemConfig> result = new Result<SystemConfig>();
		SystemConfig systemConfigEntity = systemConfigService.getById(systemConfig.getConfigId());
		if(systemConfigEntity==null) {
			result.error500("未找到对应实体");
		}else {
			boolean ok = systemConfigService.updateById(systemConfig);
			if(ok) {
				result.success("修改成功!");
			}
		}
		
		return result;
	}
	
	/**
	  *   通过id删除
	 * @return
	 */
	@OperLog(operModul = "系统配置管理", operType = "删除", operDesc = "删除参数配置信息信息")
	@ApiOperation(value = "删除参数配置信息信息", notes = "删除参数配置信息信息" , httpMethod="POST")
	@ApiImplicitParam(name = "configId", value = "参数配置信息ID", paramType = "query", required = true, dataType = "Integer")
	@PostMapping(value = "/delete")
	public Result<SystemConfig> delete(@RequestBody SystemConfig systemConfig) {
		Result<SystemConfig> result = new Result<SystemConfig>();
		SystemConfig systemConfigEntity = systemConfigService.getById(systemConfig.getConfigId());
		if(systemConfigEntity==null) {
			result.error500("未找到对应实体");
		}else {
			boolean ok = systemConfigService.removeById(systemConfig.getConfigId());
			if(ok) {
				result.success("删除成功!");
			}
		}
		
		return result;
	}
	
	/**
	  * 通过id查询
	 * @return
	 */
	@OperLog(operModul = "系统配置管理", operType = "查询", operDesc = "通过id查询参数配置信息信息")
	@ApiOperation(value = "通过id查询参数配置信息信息", notes = "通过id查询参数配置信息信息" , httpMethod="POST")
    @ApiImplicitParam(name = "configId", value = "参数配置信息ID", paramType = "query", required = true, dataType = "Integer")
	@PostMapping(value = "/queryById")
	public Result<SystemConfig> queryById(@RequestBody SystemConfig systemConfigEntity) {
		Result<SystemConfig> result = new Result<SystemConfig>();
		SystemConfig systemConfig = systemConfigService.getById(systemConfigEntity.getConfigId());
		if(systemConfig==null) {
			result.error500("未找到对应实体");
		}else {
			result.setResult(systemConfig);
			result.setSuccess(true);
		}
		return result;
	}
}
