package com.emergency.avenue.admin.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.emergency.avenue.admin.api.dto.SysTreeDictDTO;
import com.emergency.avenue.admin.api.entity.SysTreeDict;
import com.emergency.avenue.admin.api.vo.TreeNode;
import com.emergency.avenue.admin.service.SysTreeDictService;
import com.emergency.avenue.common.core.constant.CacheConstants;
import com.emergency.avenue.common.core.util.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 工单字典表
 * </P>
 *
 * @Author: wenpeng_qing
 * @Date: 2019-05-27 03:59
 * @Version: 1.0.0
 */
@RestController
@AllArgsConstructor
@RequestMapping("/sys/dict")
@Tag(description = "tree-dict", name = "树级字典")
public class SysTreeDictController {

	private final SysTreeDictService sysTreeDictService;

	/**
	 * 简单分页查询
	 *
	 * @param page        分页对象
	 * @param sysTreeDict 工单字典表
	 */
	@GetMapping("/page")
	@Operation(description = "分页查询", summary = "分页查询")
	public R<IPage<SysTreeDict>> getOrderDictPage(Page<SysTreeDict> page, SysTreeDict sysTreeDict) {
		return R.ok(sysTreeDictService.getOrderDictPage(page, sysTreeDict));
	}

	@GetMapping("/list/{type}")
	@Operation(description = "根据type类型查询", summary = "根据type类型查询")
	public R<List<SysTreeDict>> getList(@PathVariable("type") Integer type) {
		return R.ok(sysTreeDictService.getList(type));
	}

	/**
	 * 通过id查询单条记录
	 *
	 * @return R
	 */
	@GetMapping("/{treeDictId}")
	@Operation(description = "通过id查询单条记录", summary = "通过id查询单条记录")
	public R<SysTreeDict> getById(@PathVariable("treeDictId") String treeDictId) {
		return R.ok(sysTreeDictService.getById(treeDictId));
	}

	/**
	 * 通过条件查询记录
	 *
	 * @return R
	 */
	@Operation(description = "通过条件查询记录", summary = "通过条件查询记录")
	@PostMapping("/selectSysTreeDict")
	public R selectSysTreeDict(@RequestBody SysTreeDictDTO sysTreeDictDTO) {
		LambdaQueryWrapper<SysTreeDict> queryWrapper = Wrappers.<SysTreeDict>lambdaQuery().eq(SysTreeDict::getType, sysTreeDictDTO.getType());
		if (CharSequenceUtil.isNotBlank(sysTreeDictDTO.getDifference())) {
			queryWrapper.eq(SysTreeDict::getDifference, sysTreeDictDTO.getDifference());
		}
		if (CharSequenceUtil.isNotBlank(sysTreeDictDTO.getId())) {
			queryWrapper.eq(SysTreeDict::getParentId, sysTreeDictDTO.getId());
		}

		if (CollUtil.isNotEmpty(sysTreeDictDTO.getIdList())) {
			queryWrapper.in(SysTreeDict::getParentId, sysTreeDictDTO.getIdList());
		}
		if (CollUtil.isNotEmpty(sysTreeDictDTO.getParentCodeList())) {
			queryWrapper.in(SysTreeDict::getParentCode, sysTreeDictDTO.getParentCodeList());
		}
		List<SysTreeDict> sysTreeDicts = sysTreeDictService.list(queryWrapper);
		return R.ok(sysTreeDicts);
	}

	/**
	 * 通过type查询字典树
	 *
	 * @return R
	 */
	@Operation(description = "通过type查询字典树", summary = "通过type查询字典树")
	@GetMapping("/tree/{type}")
	public R<List<TreeNode>> getDictTree(@PathVariable("type") Integer type) {
		return R.ok(sysTreeDictService.getDictTree(type));
	}

	/**
	 * 新增记录
	 *
	 * @return R
	 */
	@PostMapping
	@CacheEvict(value = CacheConstants.SYS_TREE_DICTTREE_DETAILS, allEntries = true)
	@Operation(description = "新增记录", summary = "新增记录")
	public R save(@RequestBody SysTreeDict sysTreeDict) {
		return R.ok(sysTreeDictService.insert(sysTreeDict));
	}

	/**
	 * 修改记录
	 *
	 * @return R
	 */
	@PutMapping
	@CacheEvict(value = CacheConstants.SYS_TREE_DICTTREE_DETAILS, allEntries = true)
	@Operation(description = "修改记录", summary = "修改记录")
	public R update(@RequestBody SysTreeDict sysTreeDict) {
		return R.ok(sysTreeDictService.updateById(sysTreeDict));
	}

	/**
	 * 通过id删除一条记录
	 *
	 * @return R
	 */
	@DeleteMapping("/{id}")
	@CacheEvict(value = CacheConstants.SYS_TREE_DICTTREE_DETAILS, allEntries = true)
	@Operation(description = "通过id删除一条记录", summary = "通过id删除一条记录")
	public R removeById(@PathVariable String id) {
		return R.ok(sysTreeDictService.removeById(id));
	}

	/**
	 * 根据ID查询数据
	 *
	 * @param dicId
	 * @return
	 */
	@GetMapping("/getById/{dicId}")
	@Operation(description = "根据ID查询数据", summary = "根据ID查询数据")
	public SysTreeDict getTreeDictById(@PathVariable("dicId") String dicId) {
		return sysTreeDictService.getById(dicId);
	}


}
