/**
 * 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.baseinfo.rtu.entity.RtuInfoEntity;
import org.springblade.modules.baseinfo.rtu.service.IRtuBaseInfoService;
import org.springblade.modules.map.dto.DangerAreaImportRecordDTO;
import org.springblade.modules.map.dto.DangerAreaDTO;
import org.springblade.modules.map.entity.DangerAreaImportRecordEntity;
import org.springblade.modules.map.entity.DangerAreaEntity;
import org.springblade.modules.map.entity.DangerAreaPointEntity;
import org.springblade.modules.map.service.*;
import org.springblade.modules.map.task.DangerAreaImportTask;
import org.springblade.modules.map.vo.DangerAreaImportRecordVO;
import org.springblade.modules.map.vo.DangerAreaVO;
import org.springblade.modules.map.wrapper.DangerAreaWrapper;
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.ArrayList;
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/dangerarea")
@AllArgsConstructor
@Api(value = "危险区地图数据管理模块", tags = "危险区")
public class MapDangerAreaController extends BladeController {

	private final IDangerAreaService dangerAreaService;

	private final IDangerAreaPointService pointService;

	private final IDangerAreaImportServcie danagerAreaImportServcie;

	private final IRtuBaseInfoService rtuBaseInfoService;

	@Resource
	private OssBuilder ossBuilder;

	@Resource
	private TaskManager taskManager;

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

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

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


	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入DangerAreaEntity")
	public R<DangerAreaVO> detail(DangerAreaEntity entity) {
		DangerAreaEntity detail = dangerAreaService.getOne(Condition.getQueryWrapper(entity));
		DangerAreaVO vo = DangerAreaWrapper.build().entityVO(detail);
		List<RtuInfoEntity> rtus = new ArrayList<>();
		String stCode = vo.getStCode();
		if (null != stCode) {
			String rtuName = "";
			String[] stCodeArr = stCode.split(",");
			for (String st : stCodeArr) {
				LambdaQueryWrapper<RtuInfoEntity> rtuwrapper = Wrappers.<RtuInfoEntity>query().lambda();
				rtuwrapper.eq(RtuInfoEntity::getRtuCode, st);
				RtuInfoEntity rtuInfoEntity = rtuBaseInfoService.getOne(rtuwrapper);
				rtus.add(rtuInfoEntity);
			}
			vo.setRtus(rtus);
		}
		LambdaQueryWrapper<DangerAreaPointEntity> wrapper = Wrappers.<DangerAreaPointEntity>query().lambda();
		wrapper.eq(DangerAreaPointEntity::getDangerAreaId, detail.getId());
		List<DangerAreaPointEntity> list =pointService.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 = dangerAreaService.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"),
	})
	@PostMapping(value = "/import/json")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "导入测站信息", notes = "")
	public R importDangerAreaTable(@ApiIgnore DangerAreaImportRecordDTO dto, MultipartFile file) {
		try {
			BladeFile bladeFile = ossBuilder.template().putFile(file);
			if (null == bladeFile) {
				return R.fail("导入失败");
			}

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

			DangerAreaImportTask task = new DangerAreaImportTask(bladeFile, recordEntity, dangerAreaService, pointService,danagerAreaImportServcie);
			FutureTask<Integer> futureTask = new FutureTask<>(task);
			taskManager.submitTask(futureTask);

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

}
