package com.bweb.controller;

import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.model.ResponseData;
import com.hfkj.service.CommonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping(value="/common")
@Api(value="共用接口")
public class CommonController {

	Logger log = LoggerFactory.getLogger(CommonController.class);
	
	@Resource
	private CommonService commonService;


	@RequestMapping(value="/getRegionsByParentId",method= RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "分级查询区域信息")
	public ResponseData getRegionsByParentId(@RequestParam(name = "regionId", required = false) Long regionId){
		try {
			if (regionId == null){
				return ResponseMsgUtil.success(commonService.getCities());
			}
			return ResponseMsgUtil.success(commonService.getRegionsByParentId(regionId));
		} catch (Exception e) {
			return ResponseMsgUtil.exception(e);
		}
	}

	@RequestMapping(value="/getParentInfosByRegionId",method= RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "查询父级区域信息")
	public ResponseData getParentInfosByRegionId(@RequestParam(name = "regionId", required = true) Long regionId) {
		try {
			return ResponseMsgUtil.success(commonService.getParentInfoByRegionId(regionId));
		} catch (Exception e) {
			return ResponseMsgUtil.exception(e);
		}
	}

	@RequestMapping(value="/getRegionsById",method= RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "查询区域详细信息")
	public ResponseData getRegionsById(@RequestParam(name = "regionId", required = true) Long regionId){
		try {
			return ResponseMsgUtil.success(commonService.getRegionsById(regionId));
		} catch (Exception e) {
			return ResponseMsgUtil.exception(e);
		}
	}

	@RequestMapping(value="/getProvinceList",method= RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "查询省地区数据")
	public ResponseData getProvinceList(){
		try {

			Map<String, Object> param = new HashMap<>();
			return ResponseMsgUtil.success(commonService.getProvinceList(param));

		} catch (Exception e) {
			return ResponseMsgUtil.exception(e);
		}
	}

/*
	@RequestMapping(value="/getDictionaries",method= RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "查询数据字典")
	public ResponseData getDictionaries(){
		try {
			return ResponseMsgUtil.success(commonService.getDictionaries());
		} catch (Exception e) {
			return ResponseMsgUtil.exception(e);
		}
	}*/



	@RequestMapping(value="/getDictionaryByCodeType",method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "根据 码值类型 查询数据字典")
	public ResponseData getDictionaryByCodeType(@RequestParam(name = "codeType", required = true) String codeType) {
		try {

			return ResponseMsgUtil.success(commonService.getDictionarys(codeType));

		} catch (Exception e) {
			log.error("CommonController --> getDictionaryByCodeType() error!", e);
			return ResponseMsgUtil.exception(e);
		}
	}

	@RequestMapping(value="/mappingSysNameOl",method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "根据 码值类型 查询数据字典")
	public ResponseData mappingSysNameOl(@RequestParam(name = "codeType", required = true) String codeType) {
		try {

			return ResponseMsgUtil.success(commonService.mappingSysNameOl(codeType));

		} catch (Exception e) {
			log.error("CommonController --> getDictionaryByCodeType() error!", e);
			return ResponseMsgUtil.exception(e);
		}
	}

	@RequestMapping(value="/getDictionaryByCodeTypeAndExt",method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "根据 码值类型 查询数据字典")
	public ResponseData getDictionaryByCodeTypeAndExt(@RequestParam(name = "codeType", required = true) String codeType, String ext1) {
		try {

			return ResponseMsgUtil.success(commonService.getDictionarysAndExt(codeType,ext1));

		} catch (Exception e) {
			log.error("CommonController --> getDictionaryByCodeType() error!", e);
			return ResponseMsgUtil.exception(e);
		}
	}

	@RequestMapping(value="/getIdAndNameByCodeType",method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "根据 码值类型 查询数据字典的id和name")
	public ResponseData getIdAndNameByCodeType(@RequestParam(name = "codeType", required = true) String codeType) {
		try {

			return ResponseMsgUtil.success(commonService.getIdAndName(codeType));

		} catch (Exception e) {
			log.error("CommonController --> getIdAndNameByCodeType() error!", e);
			return ResponseMsgUtil.exception(e);
		}
	}

	@RequestMapping(value="/getDictionaryByCodeTypeAndValue",method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "根据码值类型及value查询数据字典")
	public ResponseData getDictionaryByCodeType(@RequestParam(name = "codeType", required = true) String codeType,
												@RequestParam(name = "codeValue", required = true) String codeValue) {
		try {

			return ResponseMsgUtil.success(commonService.mappingSysCode(codeType,codeValue));

		} catch (Exception e) {
			log.error("CommonController --> getDictionaryByCodeTypeAndValue() error!", e);
			return ResponseMsgUtil.exception(e);
		}
	}

}
