package com.bweb.controller;

import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.model.ResponseData;
import com.hfkj.service.CommonService;
import com.hfkj.service.sec.SecDictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping(value="/common")
@Api(value="共用接口")
public class CommonController {
	Logger log = LoggerFactory.getLogger(CommonController.class);
	@Resource
	private SecDictionaryService secDictionaryService;
	@Resource
	private CommonService commonService;

	@RequestMapping(value="/getRegion",method= RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "分级查询区域信息")
	public ResponseData getRegion(@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="/queryDictionary",method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "查询数据字典")
	public ResponseData queryDictionary(@RequestParam(value = "codeType" , required = false) String codeType,
									    @RequestParam(value = "codeValue" , required = false) String codeValue) {
		try {

			if (StringUtils.isBlank(codeType) && StringUtils.isBlank(codeValue)) {
				return ResponseMsgUtil.success(secDictionaryService.getDictionary());

			} else if (StringUtils.isNotBlank(codeType) && StringUtils.isNotBlank(codeValue)) {
				return ResponseMsgUtil.success(secDictionaryService.getDictionary(codeType, codeValue));

			} else if (StringUtils.isNotBlank(codeType)) {
				return ResponseMsgUtil.success(secDictionaryService.getDictionary(codeType));
			}

			return ResponseMsgUtil.success(null);

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

}
