package com.jsowell.web.controller.pile;

import com.google.common.collect.ImmutableMap;
import com.jsowell.common.constant.Constants;
import com.jsowell.common.core.domain.AjaxResult;
import com.jsowell.pile.dto.GenerateOrderDTO;
import com.jsowell.pile.dto.QueryPileDTO;
import com.jsowell.service.OrderService;
import com.jsowell.service.PileRemoteService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
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;

/**
 * 远程控制controller
 */
@RestController
@RequestMapping("/pile/remote")
public class PileRemoteController {

	@Autowired
	private PileRemoteService pileRemoteService;

	@Autowired
	private OrderService orderService;

	/**
	 * 获取实时上传数据
	 * http://localhost:8080/pile/remote/getRealTimeMonitorData
	 * @return
	 */
	@PostMapping("/getRealTimeMonitorData")
	public AjaxResult getRealTimeMonitorData(@RequestBody QueryPileDTO queryPileDTO) {
		pileRemoteService.getRealTimeMonitorData(queryPileDTO.getPileSn(), queryPileDTO.getConnectorCode());
		return AjaxResult.success();
	}

	/**
	 * 远程重启
	 * http://localhost:8080/pile/remote/reboot
	 * @return
	 */
	@PostMapping("/reboot")
	public AjaxResult reboot(@RequestBody QueryPileDTO queryPileDTO) {
		pileRemoteService.reboot(queryPileDTO.getPileSn());
		return AjaxResult.success();
	}

	/**
	 * 后管-远程启动充电
	 * http://localhost:8080/pile/remote/remoteStartChargingForWeb
	 */
	@PostMapping("/remoteStartChargingForWeb")
	public AjaxResult remoteStartCharging(@RequestBody GenerateOrderDTO dto) {
		// pileRemoteService.remoteStartCharging(queryPileDTO.getPileSn(), queryPileDTO.getConnectorCode());
		// 生成订单并远程启动充电
		dto.setStartMode(Constants.ZERO);
		String orderCode = null;
		try {
			orderCode = orderService.generateOrder(dto);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return AjaxResult.success(ImmutableMap.of("orderCode", orderCode));
	}

	/**
	 * 远程停止充电
	 * http://localhost:8080/pile/remote/remoteStopCharging
	 * @param dto
	 * @return
	 */
	@PostMapping("/remoteStopCharging")
	public AjaxResult remoteStopCharging(@RequestBody QueryPileDTO dto) {
		pileRemoteService.remoteStopCharging(dto.getPileSn(), dto.getConnectorCode());
		return AjaxResult.success();
	}

	/**
	 * 远程下发二维码
	 * http://localhost:8080/pile/remote/issueQRCode
	 */
	@PostMapping("/issueQRCode")
	public AjaxResult issueQRCode(@RequestBody QueryPileDTO queryPileDTO) {
		pileRemoteService.issueQRCode(queryPileDTO.getPileSn());
		return AjaxResult.success();
	}

	/**
	 * 对时
	 * http://localhost:8080/pile/remote/proofreadTime
	 */
	@PostMapping("/proofreadTime")
	public AjaxResult proofreadTime(@RequestBody QueryPileDTO queryPileDTO) {
		pileRemoteService.proofreadTime(queryPileDTO.getPileSn());
		return AjaxResult.success();
	}

	/**
	 * 远程升级
	 * http://localhost:8080/pile/remote/updateFirmware
	 * @return
	 */
	@PostMapping("/updateFirmware")
	public AjaxResult updateFirmware(@RequestBody QueryPileDTO queryPileDTO) {
		if (CollectionUtils.isEmpty(queryPileDTO.getPileSns())) {
			return AjaxResult.error("参数不能为空");
		}
		pileRemoteService.updateFirmware(queryPileDTO.getPileSns());
		return AjaxResult.success();
	}

}
