package com.ruoyi.web.controller.system;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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 com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.ResponseData;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.BladeRegion;
import com.ruoyi.system.service.IBladeRegionService;
import com.ruoyi.utils.BladeRegionUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 行政区划Controller
 * 
 * @author dm
 * @date 2023-02-13
 */
@Api(value = "行政区划", tags = "行政区划")
@ApiSupport(author = "dm", order = 10)
@RestController
@RequestMapping("/system/bladeRegion")
public class BladeRegionController extends BaseController {
	@Autowired
	private IBladeRegionService bladeRegionService;
	@Autowired
	private RedisCache redisCache;

	/**
	 * 查询行政区划列表
	 */
//	@ApiOperation(value = "查询行政区划列表", notes = "查询行政区划列表")
//	@ApiOperationSupport(order = 10)
//	@PreAuthorize("@ss.hasPermi('system:bladeRegion:list')")
	@GetMapping("/list")
	public TableDataInfo<BladeRegion> list(BladeRegion bladeRegion) {
		startPage();
		List<BladeRegion> list = bladeRegionService.selectBladeRegionList(bladeRegion);
		return getDataTable(list);
	}

	/**
	 * 导出行政区划列表
	 */
	@ApiOperation(value = "导出行政区划列表", notes = "导出行政区划列表")
	@ApiOperationSupport(order = 20)
	@PreAuthorize("@ss.hasPermi('system:bladeRegion:export')")
	@Log(title = "行政区划", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	public void export(HttpServletResponse response, BladeRegion bladeRegion) {
		List<BladeRegion> list = bladeRegionService.selectBladeRegionList(bladeRegion);
		ExcelUtil<BladeRegion> util = new ExcelUtil<BladeRegion>(BladeRegion.class);
		util.exportExcel(response, list, "行政区划数据");
	}

	/**
	 * 获取行政区划详细信息
	 */
//	@ApiOperation(value = "获取行政区划详细信息", notes = "获取行政区划详细信息")
//	@ApiOperationSupport(order = 30)
//	@PreAuthorize("@ss.hasPermi('system:bladeRegion:query')")
	@GetMapping(value = "/{code}")
	public ResponseData<BladeRegion> getInfo(@PathVariable("code") String code) {
		return ResponseData.success(BladeRegionUtils.selectByCode(code));
	}

	/**
	 * 查询行政区划列表---不进行分页
	 */
//	@ApiOperation(value = "查询行政区划列表---不进行分页", notes = "查询行政区划列表---不进行分页")
//	@ApiOperationSupport(order = 70)
//	@PreAuthorize("@ss.hasPermi('system:bladeRegion:list')")
	@PostMapping("/getDataList")
	public ResponseData<List<BladeRegion>> getDataList(@RequestBody BladeRegion bladeRegion) {
		String key="ALLResponseData";
		
		ResponseData data= redisCache.getCacheObject(key);
		if(data!=null) {
			return data;
		}
		
		List<BladeRegion> list = bladeRegionService.selectBladeRegionList(new BladeRegion());
		for(BladeRegion br:list) {
			BladeRegion qo=new BladeRegion();
			qo.setParentCode(br.getCode());
			br.setAreaList(bladeRegionService.selectBladeRegionList(qo));
		}
		
		data=ResponseData.success(list);
		redisCache.setCacheObject(key, data);
		return data;
	}
	
	
}
