/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 运维系统
 * 创建日期:2023/9/13
 */
package org.springblade.modules.map.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.oss.model.BladeFile;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.jobtask.TaskManager;
import org.springblade.modules.etl.service.IEtlAdCdService;
import org.springblade.modules.map.dto.*;
import org.springblade.modules.map.entity.*;
import org.springblade.modules.map.service.*;
import org.springblade.modules.map.task.MapRegionImportTask;
import org.springblade.modules.map.vo.MapRegionImportRecordVO;
import org.springblade.modules.map.vo.MapRegionVO;
import org.springblade.modules.map.wrapper.MapRegionWrapper;
import org.springblade.modules.resource.builder.oss.OssBuilder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.concurrent.FutureTask;

/***
 * Date:2023/9/13
 * Title:行政区地图数据管理模块
 * Description：对本文件的详细描述，原则上不能少于30字
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@RestController
@RequestMapping("galaxy-business/map/region")
@AllArgsConstructor
@Api(value = "行政区地图数据管理模块", tags = "行政区")
public class MapRegionController extends BladeController {

	private final IMapRegionService mapRegionService;

	private final IMapRegionPointService mapRegionPointService;

	private final IMapRegionImportService mapRegionImportService;

	private final IEtlAdCdService etlAdCdService;

	@Resource
	private OssBuilder ossBuilder;

	@Resource
	private TaskManager taskManager;

	@GetMapping("/page")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页")
	public R<IPage<MapRegionVO>> getPage(@ApiIgnore MapRegionDTO dto, Query query) {
		IPage<MapRegionVO> pages = mapRegionService.selectPage(Condition.getPage(query), dto);
		List<MapRegionVO> voList = pages.getRecords();
		for (MapRegionVO vo : voList) {
			MapRegionWrapper.build().entityVO(vo);
		}
		return R.data(pages);
	}

	@GetMapping("/import/page")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页")
	public R<IPage<MapRegionImportRecordVO>> getImportPage(Query query) {
		return R.data(mapRegionImportService.selectPage(Condition.getPage(query)));
	}

	/**
	 * 查询行政区多边形地理信息
	 *
	 * @param entity
	 * @return
	 */
	@GetMapping("/polygon")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "列表")
	public R<List<MapRegionPointEntity>> getPolygon(MapRegionEntity entity) {
		LambdaQueryWrapper<MapRegionPointEntity> wrapper = Wrappers.<MapRegionPointEntity>query().lambda();
		wrapper.eq(MapRegionPointEntity::getRegionId, entity.getId());
		List<MapRegionPointEntity> points = mapRegionPointService.list(wrapper);
		return R.data(points);
	}


	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "MapRegionEntity")
	public R<MapRegionVO> detail(MapRegionEntity entity) {
		MapRegionEntity detail = mapRegionService.getOne(Condition.getQueryWrapper(entity));
		MapRegionVO vo = MapRegionWrapper.build().entityVO(detail);
		LambdaQueryWrapper<MapRegionPointEntity> wrapper = Wrappers.<MapRegionPointEntity>query().lambda();
		wrapper.eq(MapRegionPointEntity::getRegionId, detail.getId());
		List<MapRegionPointEntity> list =mapRegionPointService.list(wrapper);
		vo.setPoints(list);
		return R.data(vo);
	}

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ID数组")
	public R remove(@ApiParam(value = "主键集合") @RequestParam String ids) {
		boolean temp = mapRegionService.deleteLogic(Func.toLongList(ids));
		return R.status(temp);
	}


	@ApiLog("行政区信息-导入")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "remark", value = "导入说明", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "saveType", value = "操作类型", paramType = "query", dataType = "int"),
		@ApiImplicitParam(name = "adType", value = "行政区类型", paramType = "query", dataType = "int"),
	})
	@PostMapping(value = "/import/json")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "导入测站信息", notes = "")
	public R importAdTable(@ApiIgnore MapRegionImportRecordDTO dto, MultipartFile file) {
		try {
			BladeFile bladeFile = ossBuilder.template().putFile(file);
			if (null == bladeFile) {
				return R.fail("导入失败");
			}

			MapRegionImportRecordEntity recordEntity = new MapRegionImportRecordEntity();
			recordEntity.setImportStatus(0L);
			recordEntity.setRemark(dto.getRemark());
			recordEntity.setSaveType(dto.getSaveType());
			recordEntity.setImportTime(Func.formatDateTime(new Date()));
			recordEntity.setFileName(file.getOriginalFilename());
			mapRegionImportService.save(recordEntity);

			MapRegionImportTask task = new MapRegionImportTask(bladeFile,dto.getAdType(), recordEntity,etlAdCdService, mapRegionService, mapRegionPointService, mapRegionImportService);
			FutureTask<Integer> futureTask = new FutureTask<>(task);
			taskManager.submitTask(futureTask);

			return R.success("导入任务提交成功");
		} catch (Exception e) {
			e.printStackTrace();
			return R.fail("导入错误 " + e.getMessage());
		}
	}

}
