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.constant.CacheConstants;
import com.zhgd.xmgl.modules.basicdata.entity.SystemDictData;
import com.zhgd.xmgl.modules.basicdata.service.ISystemDictDataService;
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.cache.annotation.CacheEvict;
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/systemDictData")
@Slf4j
@Api(tags = "字典数据信息")
public class SystemDictDataController {
	@Autowired
	private ISystemDictDataService systemDictDataService;
	
	/**
	  * 分页列表查询
	 * @return
	 */
	@OperLog(operModul = "字典数据管理", operType = "分页查询", operDesc = "分页列表查询字典数据信息")
	@ApiOperation(value = " 分页列表查询字典数据信息", notes = "分页列表查询字典数据信息", httpMethod="GET")
	@PostMapping(value = "/page")
	public Result<IPage<SystemDictData>> queryPageList(@RequestBody SystemDictVo systemDictVo) {
		LambdaQueryWrapper<SystemDictData> wrapper = Wrappers.<SystemDictData>lambdaQuery();
		if (StringUtils.isNotBlank(systemDictVo.getDictName())) {
			wrapper.like(SystemDictData::getDictValue, systemDictVo.getDictName());
		}
		if (StringUtils.isNotBlank(systemDictVo.getDictType())) {
			wrapper.eq(SystemDictData::getDictType, systemDictVo.getDictType());
		}
		if (null != systemDictVo.getStatus()) {
			wrapper.eq(SystemDictData::getStatus, systemDictVo.getStatus());
		}
		wrapper.orderByDesc(SystemDictData::getDictSort);
		Page<SystemDictData> page = new Page<>(systemDictVo.getPageNo(), systemDictVo.getPageSize());
		return Result.success(systemDictDataService.page(page, wrapper));
	}

	 /**
	  * 列表查询
	  * @param systemDictData
	  * @param req
	  * @return
	  */
	 @OperLog(operModul = "字典数据管理", operType = "列表查询", operDesc = "列表查询字典数据信息")
	 @ApiOperation(value = " 列表查询字典数据信息", notes = "列表查询字典数据信息", httpMethod="POST")
	 @PostMapping(value = "/list")
	 public Result<List<SystemDictData>> queryList(@RequestBody SystemDictData systemDictData, HttpServletRequest req) {
		 Result<List<SystemDictData>> result = new Result<List<SystemDictData>>();
		 QueryWrapper<SystemDictData> queryWrapper = QueryGenerator.initQueryWrapper(systemDictData, req.getParameterMap());
		 List<SystemDictData> list = systemDictDataService.list(queryWrapper);
		 result.setSuccess(true);
		 result.setResult(list);
		 return result;
	 }
	
	/**
	  *   添加
	 * @param systemDictData
	 * @return
	 */
	@OperLog(operModul = "字典数据管理", operType = "新增", operDesc = "添加字典数据信息")
	@ApiOperation(value = " 添加字典数据信息", notes = "添加字典数据信息" , httpMethod="POST")
	@PostMapping(value = "/add")
	@CacheEvict(value = CacheConstants.DICT_DETAILS, key = "#systemDictData.dictType")
	public Result<SystemDictData> add(@RequestBody SystemDictData systemDictData) {
		Result<SystemDictData> result = new Result<SystemDictData>();
		systemDictData.setCreateTime(new Date());
		systemDictDataService.save(systemDictData);
		result.success("添加成功！");
		return result;
	}
	
	/**
	  *  编辑
	 * @param systemDictData
	 * @return
	 */
	@OperLog(operModul = "字典数据管理", operType = "修改", operDesc = "编辑字典数据信息")
	@ApiOperation(value = "编辑字典数据信息", notes = "编辑字典数据信息" , httpMethod="POST")
	@PostMapping(value = "/edit")
	@CacheEvict(value = CacheConstants.DICT_DETAILS, key = "#systemDictData.dictType")
	public Result<SystemDictData> edit(@RequestBody SystemDictData systemDictData) {
		Result<SystemDictData> result = new Result<SystemDictData>();
		SystemDictData systemDictDataEntity = systemDictDataService.getById(systemDictData.getDictCode());
		if(systemDictDataEntity==null) {
			result.error500("未找到对应实体");
		}else {
			boolean ok = systemDictDataService.updateById(systemDictData);
			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<SystemDictData> delete(@RequestBody Map<String,Object> map) {
		Result<SystemDictData> result = new Result<SystemDictData>();
		SystemDictData systemDictData = systemDictDataService.getById(MapUtils.getString(map,"id"));
		if(systemDictData==null) {
			result.error500("未找到对应实体");
		}else {
			boolean ok = systemDictDataService.delInfo(systemDictData);
			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<SystemDictData> deleteBatch(@RequestBody Map<String,Object> map) {
		Result<SystemDictData> result = new Result<SystemDictData>();
		String ids = MapUtils.getString(map,"ids");
		if(ids==null || "".equals(ids.trim())) {
			result.error500("参数不识别！");
		}else {
			this.systemDictDataService.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<SystemDictData> queryById(@RequestBody Map<String,Object> map) {
		Result<SystemDictData> result = new Result<SystemDictData>();
		SystemDictData systemDictData = systemDictDataService.getById(MapUtils.getString(map,"id"));
		if(systemDictData==null) {
			result.error500("未找到对应实体");
		}else {
			result.setResult(systemDictData);
			result.setSuccess(true);
		}
		return result;
	}
}
