package com.ruoyi.dimp.controller;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.dimp.domain.DPopulationDataInfo;
import com.ruoyi.dimp.service.IDPopulationDataInfoService;
import com.ruoyi.system.domain.BladeRegion;
import com.ruoyi.system.service.IBladeRegionService;
import com.ruoyi.utils.BladeRegionUtils;

/**
 * 云南人口数据导入Controller
 *
 * @author ruoyi
 * @date 2023-03-19
 */
@RestController
@RequestMapping("/dimp/dPopulationDataInfo")
public class DPopulationDataInfoController extends BaseController {
	private final IDPopulationDataInfoService dPopulationDataInfoService;
    @Autowired
    private IBladeRegionService bladeRegionService;
	public DPopulationDataInfoController(IDPopulationDataInfoService dPopulationDataInfoService) {
		this.dPopulationDataInfoService = dPopulationDataInfoService;
	}

	/**
	 * 查询云南人口数据导入列表
	 *
	 * @param dPopulationDataInfo 云南人口数据导入
	 * @return 云南人口数据导入集合
	 * @date 2023-03-19
	 */
	@PreAuthorize("@ss.hasPermi('dimp:dPopulationDataInfo:list')")
	@GetMapping("/list")
	public TableDataInfo list(DPopulationDataInfo dPopulationDataInfo) {
		String cityCode=null;
		if(StringUtils.isNotBlank(dPopulationDataInfo.getCityCode()) && ! "53".equals(dPopulationDataInfo.getCityCode())) {
			cityCode=dPopulationDataInfo.getCityCode().trim();
		}
		dPopulationDataInfo.setCityCode(null);
		if(StringUtils.isNotBlank(cityCode)) {
			BladeRegion bladeRegion = BladeRegionUtils.selectByCode(cityCode);
			if(bladeRegion!=null) {
				dPopulationDataInfo.setCityName(bladeRegion.getCityName());
			}
		}
		startPage();
		List<DPopulationDataInfo> list = dPopulationDataInfoService.queryList(dPopulationDataInfo);
		return getDataTable(list);
	}

	/**
	 * 导出云南人口数据导入列表
	 *
	 * @param dPopulationDataInfo 云南人口数据导入
	 * @date 2023-03-19
	 */
	@PreAuthorize("@ss.hasPermi('dimp:dPopulationDataInfo:export')")
	@Log(title = "云南人口数据导出", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	public void export(HttpServletResponse response, DPopulationDataInfo dPopulationDataInfo) {
		List<DPopulationDataInfo> list = dPopulationDataInfoService.list(new QueryWrapper<>());
		ExcelUtil<DPopulationDataInfo> util = new ExcelUtil<>(DPopulationDataInfo.class);
		util.exportExcel(response, list, "云南人口数据导入数据");
	}

	/**
	 * 获取云南人口数据导入详细信息
	 *
	 * @param id 主键id
	 * @return 结果
	 * @date 2023-03-19
	 */
	@PreAuthorize("@ss.hasPermi('dimp:dPopulationDataInfo:query')")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") Long id) {
		return AjaxResult.success(dPopulationDataInfoService.getById(id));
	}

	/**
	 * 新增云南人口数据导入信息
	 *
	 * @param dPopulationDataInfo 云南人口数据导入
	 * @return 结果
	 * @date 2023-03-19
	 */
	@PreAuthorize("@ss.hasPermi('dimp:dPopulationDataInfo:add')")
	@Log(title = "云南人口数据导入", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody DPopulationDataInfo dPopulationDataInfo) {
		return toAjax(dPopulationDataInfoService.save(dPopulationDataInfo));
	}

	/**
	 * 修改云南人口数据导入信息
	 *
	 * @param dPopulationDataInfo 云南人口数据导入
	 * @return 结果
	 * @date 2023-03-19
	 */
	@PreAuthorize("@ss.hasPermi('dimp:dPopulationDataInfo:edit')")
	@Log(title = "云南人口数据导入", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody DPopulationDataInfo dPopulationDataInfo) {
		return toAjax(dPopulationDataInfoService.updateById(dPopulationDataInfo));
	}

	/**
	 * 删除云南人口数据导入信息
	 *
	 * @param ids 主键ids
	 * @return 结果
	 * @date 2023-03-19
	 */
	@PreAuthorize("@ss.hasPermi('dimp:dPopulationDataInfo:remove')")
	@Log(title = "云南人口数据导入", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult remove(@PathVariable Long[] ids) {
		return toAjax(dPopulationDataInfoService.removeByIds(Arrays.asList(ids)));
	}

}
