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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.xmgl.modules.basicdata.entity.SystemDictType;
import com.zhgd.xmgl.modules.basicdata.service.ISystemDictTypeService;
import com.zhgd.xmgl.modules.basicdata.vo.SystemDictVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
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.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


 /**
 * @Title: Controller
 * @Description: 字典类型
 * @author： pds
 * @date：   2023-02-15
 * @version： V1.0
 */
@RestController
@RequestMapping("/xmgl/systemDictType")
@Slf4j
@Api(tags = "字典类型信息")
public class SystemDictTypeController {
	@Autowired
	private ISystemDictTypeService systemDictTypeService;
	
	/**
	  * 分页列表查询
	 * @return
	 */
	@OperLog(operModul = "字典类型管理", operType = "分页查询", operDesc = "分页列表查询字典数据信息")
	@ApiOperation(value = " 分页列表查询字典类型信息", notes = "分页列表查询字典类型信息", httpMethod="POST")
	@PostMapping(value = "/page")
	public Result<IPage<SystemDictType>> queryPageList(@RequestBody SystemDictVo systemDictVo) {
		LambdaQueryWrapper<SystemDictType> wrapper = Wrappers.<SystemDictType>lambdaQuery();
		if (StringUtils.isNotBlank(systemDictVo.getDictName())) {
			wrapper.like(SystemDictType::getDictName, systemDictVo.getDictName());
		}
		if (StringUtils.isNotBlank(systemDictVo.getDictType())) {
			wrapper.eq(SystemDictType::getDictType, systemDictVo.getDictType());
		}
		if (null != systemDictVo.getStatus()) {
			wrapper.eq(SystemDictType::getStatus, systemDictVo.getStatus());
		}
		wrapper.orderByDesc(SystemDictType::getCreateTime);
		Page<SystemDictType> page = new Page<>(systemDictVo.getPageNo(), systemDictVo.getPageSize());
		return Result.success(systemDictTypeService.page(page, wrapper));
	}

	 /**
	  * 列表查询
	  * @param systemDictType
	  * @param req
	  * @return
	  */
	 @OperLog(operModul = "字典类型管理", operType = "列表查询", operDesc = "列表查询字典类型信息")
	 @ApiOperation(value = " 列表查询字典类型信息", notes = "列表查询字典类型信息", httpMethod="POST")
	 @PostMapping(value = "/list")
	 public Result<List<SystemDictType>> queryList(@RequestBody SystemDictType systemDictType, HttpServletRequest req) {
		 Result<List<SystemDictType>> result = new Result<List<SystemDictType>>();
		 QueryWrapper<SystemDictType> queryWrapper = QueryGenerator.initQueryWrapper(systemDictType, req.getParameterMap());
		 List<SystemDictType> list = systemDictTypeService.list(queryWrapper);
		 result.setSuccess(true);
		 result.setResult(list);
		 return result;
	 }
	
	/**
	  *   添加
	 * @param systemDictType
	 * @return
	 */
	@OperLog(operModul = "字典类型管理", operType = "新增", operDesc = "添加字典类型信息")
	@ApiOperation(value = " 添加字典类型信息", notes = "添加字典类型信息" , httpMethod="POST")
	@PostMapping(value = "/add")
	public Result<SystemDictType> add(@RequestBody SystemDictType systemDictType) {
		Result<SystemDictType> result = new Result<SystemDictType>();
		systemDictType.setCreateTime(new Date());
		systemDictTypeService.save(systemDictType);
		result.success("添加成功！");
		return result;
	}
	
	/**
	  *  编辑
	 * @param systemDictType
	 * @return
	 */
	@OperLog(operModul = "字典类型管理", operType = "修改", operDesc = "编辑字典类型信息")
	@ApiOperation(value = "编辑字典类型信息", notes = "编辑字典类型信息" , httpMethod="POST")
	@PostMapping(value = "/edit")
	public Result<SystemDictType> edit(@RequestBody SystemDictType systemDictType) {
		Result<SystemDictType> result = new Result<SystemDictType>();
		SystemDictType systemDictTypeEntity = systemDictTypeService.getById(systemDictType.getDictId());
		if(systemDictTypeEntity==null) {
			result.error500("未找到对应实体");
		}else {
			boolean ok = systemDictTypeService.updateById(systemDictType);
			if(ok) {
				result.success("修改成功!");
			}
		}
		
		return result;
	}
	
	/**
	  *   通过id删除
	 * @return
	 */
	@OperLog(operModul = "字典类型管理", operType = "删除", operDesc = "删除字典类型信息")
	@ApiOperation(value = "删除字典类型信息", notes = "删除字典类型信息" , httpMethod="POST")
	@ApiImplicitParam(name = "id", value = "字典类型ID", paramType = "query", required = true, dataType = "Integer")
	@PostMapping(value = "/delete")
	public Result<SystemDictType> delete(@RequestBody Map<String,Object> map) {
		Result<SystemDictType> result = new Result<SystemDictType>();
		SystemDictType systemDictType = systemDictTypeService.getById(MapUtils.getString(map,"id"));
		if(systemDictType==null) {
			result.error500("未找到对应实体");
		}else {
			boolean ok = systemDictTypeService.removeById(MapUtils.getString(map,"id"));
			if(ok) {
				result.success("删除成功!");
			}
		}
		
		return result;
	}
	
	/**
	  *  批量删除
	 * @return
	 */
	@OperLog(operModul = "字典类型管理", operType = "批量删除", operDesc = "批量删除字典类型信息")
	@ApiOperation(value = "批量删除字典类型信息", notes = "批量删除字典类型信息",  httpMethod="POST")
    @ApiImplicitParam(name = "ids", value = "字典类型ID字符串", paramType = "query", required = true, dataType = "String")
	@PostMapping(value = "/deleteBatch")
	public Result<SystemDictType> deleteBatch(@RequestBody Map<String,Object> map) {
		Result<SystemDictType> result = new Result<SystemDictType>();
		String ids = MapUtils.getString(map,"ids");
		if(ids==null || "".equals(ids.trim())) {
			result.error500("参数不识别！");
		}else {
			this.systemDictTypeService.removeByIds(Arrays.asList(ids.split(",")));
			result.success("删除成功!");
		}
		return result;
	}
	
	/**
	  * 通过id查询
	 * @return
	 */
	@OperLog(operModul = "字典类型管理", operType = "查询", operDesc = "通过id查询字典类型信息")
	@ApiOperation(value = "通过id查询字典类型信息", notes = "通过id查询字典类型信息" , httpMethod="POST")
    @ApiImplicitParam(name = "id", value = "字典类型ID", paramType = "query", required = true, dataType = "Integer")
	@PostMapping(value = "/queryById")
	public Result<SystemDictType> queryById(@RequestBody Map<String,Object> map) {
		Result<SystemDictType> result = new Result<SystemDictType>();
		SystemDictType systemDictType = systemDictTypeService.getById(MapUtils.getString(map,"id"));
		if(systemDictType==null) {
			result.error500("未找到对应实体");
		}else {
			result.setResult(systemDictType);
			result.setSuccess(true);
		}
		return result;
	}
}
