/**
 * BladeX Commercial License Agreement
 * Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
 * <p>
 * Use of this software is governed by the Commercial License Agreement
 * obtained after purchasing a license from BladeX.
 * <p>
 * 1. This software is for development use only under a valid license
 * from BladeX.
 * <p>
 * 2. Redistribution of this software's source code to any third party
 * without a commercial license is strictly prohibited.
 * <p>
 * 3. Licensees may copyright their own code but cannot use segments
 * from this software for such purposes. Copyright of this software
 * remains with BladeX.
 * <p>
 * Using this software signifies agreement to this License, and the software
 * must not be used for illegal purposes.
 * <p>
 * THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
 * not liable for any claims arising from secondary or illegal development.
 * <p>
 * Author: Chill Zhuang (bladejava@qq.com)
 */
package org.springblade.modules.nh.api;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.modules.nh.pojo.dto.DeviceDTO;
import org.springblade.modules.nh.pojo.dto.PowerStationDTO;
import org.springblade.modules.nh.pojo.dto.StationDTO;
import org.springblade.modules.nh.pojo.dto.open.YXInverterDataDTO;
import org.springblade.modules.nh.pojo.dto.open.YXPowerStationDTO;
import org.springblade.modules.nh.pojo.entity.PowerStationEntity;
import org.springblade.modules.nh.pojo.vo.PowerStationVO;
import org.springblade.modules.nh.service.IDeviceService;
import org.springblade.modules.nh.service.IInverterDataService;
import org.springblade.modules.nh.service.IPowerStationService;
import org.springblade.modules.nh.wrapper.PowerStationWrapper;
import org.springblade.modules.order.pojo.vo.OrderVo;
import org.springblade.modules.order.service.impl.OperationOrderServiceImpl;

import org.springframework.web.bind.annotation.*;
import scala.collection.mutable.StringBuilder;

import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * 设备管理 控制器
 *
 * @author AlexWang
 * @since 2024-05-31
 */
@RestController
@AllArgsConstructor
@RequestMapping("/open/station")
@Tag(name = "设备管理", description = "设备管理接口")
@Slf4j
public class OpenPowerStationController extends BladeController {

	private final IPowerStationService stationService;

	private final IInverterDataService inverterDataService;

	private final OperationOrderServiceImpl orderService;


	@PostMapping("/queryList")
	@ApiOperationSupport(order = 2)
	@Operation(summary = "越秀查询电站列表", description  = "传入powerStation")
	public R<IPage<YXPowerStationDTO>> queryList(StationDTO powerStation, HttpServletRequest request, Query query) {
		log.info(getCurrentDomain(request));
		if(checkName(powerStation.getUserName(), powerStation.getPassword())){

			IPage<YXPowerStationDTO> pages = stationService.queryListForYX(Condition.getPage(query),powerStation);
			return R.data(pages);
		}
		return R.fail(201,"账号认证失败");

	}


	public String getCurrentDomain(HttpServletRequest request){
		StringBuffer buffer = request.getRequestURL();
		return buffer.toString();
	}

	@PostMapping("/queryInverterData")
	@ApiOperationSupport(order = 2)
	@Operation(summary = "越秀查询逆变器数据表", description  = "传入powerStation")
	public R<IPage<YXInverterDataDTO>> queryInverterData(String startTime, String endTime, String userName,String password, Query query) {
		if(checkName(userName, password)){
			if(StringUtil.isBlank(startTime)){
				Calendar calendar = Calendar.getInstance(); // 获取当前时间
				calendar.add(Calendar.HOUR_OF_DAY, -1); // 减去一小时
				startTime = DateUtil.formatDateTime(calendar.getTime());
			}
			IPage<YXInverterDataDTO> list = inverterDataService.queryYXList(Condition.getPage(query),startTime,endTime);
			return R.data(list);
		}
		return R.fail(201,"账号认证失败");
	}

	@PostMapping("/initLocation")
	@ApiOperationSupport(order = 2)
	@Operation(summary = "越秀查询工单数据表", description  = "传入powerStation")
	public R<IPage<OrderVo>> initLocation(String userName,String password, Query query) {
		if(checkName(userName, password)){
			IPage<OrderVo> list = orderService.findOrderList(Condition.getPage(query));
			return R.data(list);
		}
		return R.fail(201,"账号认证失败");
	}

	/**
	 * 判断账号认证
	*/
	public boolean checkName(String name,String pass){
		if("nhkj001".equals(name.trim()) && "Password@123".equals(pass.trim())){
			return true;
		}
		return false;
	}


}
