package com.blueland.render.rest;


/**
 * @author 代码自动生成工具
 * @version 1.0
 * @since  
 */
 
import java.util.List;
import java.util.Map;

import com.blueland.core.util.UserUtils;
import com.blueland.model.TPfApplication;
import com.blueland.model.TPfDict;
import com.blueland.result.R;
import com.blueland.service.IApplicationService;
import com.blueland.service.IDictService;
import com.blueland.service.SysRedisService;
import io.swagger.annotations.Api;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.blueland.bo.DictEntity;


@Api(value = "dict")
@RestController
@RequestMapping(value = "/dict")
public class DictRest {
	@Autowired
	private IDictService iDictService;
	@Autowired
	private IApplicationService applicationService;

	@Autowired
	private SysRedisService sysRedisService;

	/**
	 * 根据应用ID查询字典数据
	 * @return
	 **/
	@ApiOperation(value = "根据appId查询字典数据列表", notes = "根根据appId查询字典数据列表")
	@ApiImplicitParams({ @ApiImplicitParam(name = "appId", value = "应用id", dataType = "string", paramType = "path", required = true)})
	@RequestMapping(value = "/findDictList",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	@ResponseBody
	public R<List<DictEntity>> findDictList() {
		String appId = UserUtils.getTenantId();
		//先查询是否有该应用
		TPfApplication applicationEntry = applicationService.getById(appId);
		//如果不存在直接返回 应用不存在
		if(applicationEntry==null){
			throw new RuntimeException("应用不存在!");
		}
		//根据应用ID查询字典数据
//		List<DictEntity> dictList = dictService.findDictList(params);
		// 20200428 modify TASK816 系统管理 字典优化
		List<DictEntity> dictList = iDictService.findDictListByCache(appId);
		return R.ok(dictList);
	}

	@ApiOperation(value = "根据appId,dictCode查询子字典数据", notes = "根据appId,dictCode查询子字典数据")
	@ApiImplicitParams({ @ApiImplicitParam(name = "appId", value = "应用id", dataType = "string", paramType = "path"),
	@ApiImplicitParam(name = "dictCode", value = "字典父编码", dataType = "string", paramType = "path") })
	@RequestMapping(value = "/{dictCode}",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	@ResponseBody
	public R<List<DictEntity>> findByParentId(@PathVariable String dictCode){
		String appId = UserUtils.getTenantId();
		// 20200428 modify TASK816 系统管理 字典优化
		List<DictEntity> list = iDictService.findchildListByCache(appId, dictCode);
		return R.ok(list);
	}


	/**
	 * 查找字典对象（支持多条件查询）
	 **/
	@ApiOperation(value="查找字典对象（支持多条件查询）", notes="查找字典对象（支持多条件查询）")
	@RequestMapping(value = "",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	@ResponseBody
	public R<DictEntity> get(DictEntity model) {
		// 20200428 modify TASK816 系统管理 字典优化
		DictEntity dictEntity = iDictService.findByCache(model);
		return R.ok(dictEntity);
	}
	
	/**
	 * 20200908 add 根据父编码集合获取字典集合
	 **/
	@ApiOperation(value="根据父编码集合获取字典集合", notes="根据父编码集合获取字典集合")
	@RequestMapping(value = "getByParentcodes",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	@ResponseBody
	public R<Map<String,List<DictEntity>>> getByPrentcodes(@RequestBody Map<String,Object> map) {
		List<String> parentcodes = (List<String>) map.get("parentcodes");
		String appId = UserUtils.getTenantId();
		Map<String,List<DictEntity>> dictMap = iDictService.findByParentcodesCache(appId, parentcodes);
		return R.ok(dictMap);
	}

	/**
	 * 根据所有字典数据
	 * @return
	 **/
	@ApiOperation(value = "根据所有字典数据", notes = "根据所有字典数据")
	@RequestMapping(value = "/list",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	@ResponseBody
	public R<List<DictEntity>> findAllDict() {
		List<DictEntity> dictList = iDictService.getDictList();
		return R.ok(dictList);
	}

	/**
	 * 重新加载字典数据
	 *
	 * @return
	 */
	@ApiOperation(value = "/initDict", httpMethod = "GET", notes = "重新加载字典数据")
	@GetMapping("/initDict")
	public R loadDict() {
		try {
			sysRedisService.initDict();
		} catch (Exception e) {
			return R.fail(e.getMessage());
		}
		return R.ok();
	}
	/**
	 * 对比修改缓存
	 */
	@ApiOperation(value = "/update", httpMethod = "GET", notes = "重新加载字典数据")
	@GetMapping("/update")
	public R updateCached() {
		sysRedisService.flushDict();
		return R.ok();
	}
}



