/**
 * 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.map.dto.DangerAreaLeaveLineDTO;
import org.springblade.modules.map.dto.DangerAreaLeaveLineImportRecordDTO;
import org.springblade.modules.map.entity.*;
import org.springblade.modules.map.service.*;
import org.springblade.modules.map.task.DangerAreaLeaveLineImportTask;
import org.springblade.modules.map.vo.DangerAreaLeaveLineImportRecordVO;
import org.springblade.modules.map.vo.DangerAreaLeaveLineVO;
import org.springblade.modules.map.wrapper.DangerAreaLeaveLineWrapper;
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：
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@RestController
@RequestMapping("galaxy-business/map/leaveline")
@AllArgsConstructor
@Api(value = "转移路线地图数据管理模块", tags = "转移路线")
public class MapDangerAreaLeaveLineController extends BladeController {

	private final IDangerAreaLeaveLineService leaveLineService;

	private final IDangerAreaLeaveLinePointService leaveLinePointService;

	private final IDangerAreaLeaveLineImportServcie leaveLineImportServcie;

	@Resource
	private OssBuilder ossBuilder;

	@Resource
	private TaskManager taskManager;

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

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

	@GetMapping("/polyline")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "列表")
	public R<List<DangerAreaLeaveLinePointEntity>> getPolyline(DangerAreaLeaveLineEntity entity) {
		LambdaQueryWrapper<DangerAreaLeaveLinePointEntity> wrapper = Wrappers.<DangerAreaLeaveLinePointEntity>query().lambda();
		wrapper.eq(DangerAreaLeaveLinePointEntity::getLeaveLineId, entity.getId());
		List<DangerAreaLeaveLinePointEntity> points = leaveLinePointService.list(wrapper);
		return R.data(points);
	}


	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入DangerAreaEntity")
	public R<DangerAreaLeaveLineVO> detail(DangerAreaLeaveLineEntity entity) {
		DangerAreaLeaveLineEntity detail = leaveLineService.getOne(Condition.getQueryWrapper(entity));
		DangerAreaLeaveLineVO vo = DangerAreaLeaveLineWrapper.build().entityVO(detail);
		LambdaQueryWrapper<DangerAreaLeaveLinePointEntity> wrapper = Wrappers.<DangerAreaLeaveLinePointEntity>query().lambda();
		wrapper.eq(DangerAreaLeaveLinePointEntity::getLeaveLineId, entity.getId());
		List<DangerAreaLeaveLinePointEntity> points = leaveLinePointService.list(wrapper);
		vo.setPoints(points);
		return R.data(vo);
	}

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ID数组")
	public R remove(@ApiParam(value = "主键集合") @RequestParam String ids) {
		boolean temp = leaveLineService.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 importLeaveLineTable(@ApiIgnore DangerAreaLeaveLineImportRecordDTO dto, MultipartFile file) {
		try {
			BladeFile bladeFile = ossBuilder.template().putFile(file);
			if (null == bladeFile) {
				return R.fail("导入失败");
			}

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

			DangerAreaLeaveLineImportTask task = new DangerAreaLeaveLineImportTask(bladeFile, recordEntity, leaveLineService, leaveLinePointService,leaveLineImportServcie);
			FutureTask<Integer> futureTask = new FutureTask<>(task);
			taskManager.submitTask(futureTask);

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