package com.huatek.frame.base.model.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.huatek.frame.base.dto.ModelComponentDataDTO;
import com.huatek.frame.base.dto.ModelDTO;
import com.huatek.frame.base.service.ModelService;
import com.huatek.torch.frame.tools.ConstantUtil;
import com.huatek.torch.frame.tools.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author key 模型
 */
@Api(tags={"模型配置接口"})
@RestController
@RequestMapping("/api/model")
public class ModelController {

	@SuppressWarnings("unused")
	private static final Logger logger = LoggerFactory.getLogger(ModelController.class);

	@Reference(check = false)
	private ModelService modelService;

	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~模型管理~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
	/**
	 * 查询主设备列表
	 *
	 * @param
	 * @return
	 */
	@ApiOperation(value = "基础数据-查询主设备类型列表")
	@GetMapping(value = "/modelType/list")
	public  Map<String, Object> modelTypeList() {
		Map<String, Object> map = modelService.selectModelTypeList();
		return map;
	}

	/**
	 * 查询站点信息
	 *
	 * @param
	 * @return
	 */
	@ApiOperation(value = "基础数据-查询可用站点信息")
	@PostMapping(value="/station/list")
	public  Map<String, Object> selectStationList() {
		Map<String, Object> map = modelService.selectStationList();
		return map;
	}

	/**
	 * 模型列表同步
	 */
	@ApiOperation(value = "模型管理-模型同步")
	@PostMapping(value="/updateModels")
	public  Map<String, Object> updateModels(@RequestBody List<String> originalNameList) {
		logger.info("ModelController.updateModels.param:{}", JSON.toJSONString(originalNameList));
		Map<String, Object> map = modelService.updateModelList(originalNameList);
		return map;
	}

	/**
	 * 查询模型列表
	 */
	@ApiOperation(value = "模型管理-模型列表")
	@PostMapping(value="/selectModels")
	public  Map<String, Object> selectModelList(@RequestBody ModelDTO dto) {
		Map<String, Object> map = modelService.selectModelList(dto);
		return map;
	}


	/**
	 * 模型编辑
	 */
	@ApiOperation(value = "模型管理-模型编辑")
	@PostMapping(value="/updateModel")
	public  Map<String, Object> updateModel(@RequestBody ModelDTO dto) {
		Map<String, Object> map = modelService.updateModel(dto);
		return map;
	}
	/**
	 * 模型编辑
	 */
	@ApiOperation(value = "模型管理-模型编辑")
	@PostMapping(value="/updateModelParams")
	public  Map<String, Object> updateModelParams(@RequestBody ModelDTO dto) {
		Map<String, Object> map = modelService.updateModelParams(dto);
		return map;
	}

	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~部件管理~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//

	/**
	 * 查询模型类型对应装置类型列表
	 *         BYQ("111", "变压器"),
	 *         GIS("211", "GIS设备"),
	 *         CW("311", "开关柜"),
	 *         BLQ("411", "避雷器"),
	 *         GEN("611", "发电机"),
	 *         DL("711", "电缆");
	 * @param
	 * @return
	 */
	@ApiOperation(value = "部件管理-查询主设备列表")
	@GetMapping(value="/selectMainEquipmentListByModelType")
	public  Map<String, Object> selectMainEquipmentListByModelType(@RequestParam(value = "siteId") String siteId, @RequestParam(value = "modelType")  String modelType) {
		Map<String, Object> map = modelService.selectMainEquipmentListByModelType(siteId, modelType);
		return map;
	}

	/**
	 * 查询模型类型对应装置类型列表
	 *
	 * @param
	 * @return
	 */
	@ApiOperation(value = "部件管理-查询模型类型对应装置类型列表")
	@GetMapping(value="/selectDeviceListByModelType")
	public  Map<String, Object> selectDeviceListByModelType(@RequestParam("modelType")  String modelType) {
		Map<String, Object> map = modelService.selectDeviceListList(modelType);
		return map;
	}

	/**
	 * 查询主站模型类型对应装置类型列表
	 *
	 * @param
	 * @return
	 */
	@ApiOperation(value = "部件管理-查询主站模型类型对应装置类型列表")
	@GetMapping(value="/selectMasterStationDeviceListByModelType")
	public  Map<String, Object> selectMasterStationDeviceListByModelType(@RequestParam("equipmentType")  String equipmentType,@RequestParam("id")  String id) {
		Map<String, Object> map = modelService.selectMasterStationDeviceListByModelType(equipmentType,id);
		return map;
	}

	@ApiOperation(value = "部件管理-查询某一设备下某个装置类型下所有的传感器装置")
	@GetMapping(value = "/selectSensors")
	public Map<String, Object> selectSensors(@RequestParam String modelType, @RequestParam String mainTreeId, @RequestParam String deviceCode ) {
		Map<String, Object> map = new HashMap<>();
		map = modelService.selectSensors(modelType, mainTreeId, deviceCode);
		return map;
	}

	/**
	 * 查询某一装置类型对应的数据项
	 *
	 * @param
	 * @return
	 */
	@ApiOperation(value = "部件管理-查询某一装置类型对应的所有数据项")
	@GetMapping(value="/selectDataItemsByDeviceCode")
	public  Map<String, Object> selectDataItemsByDeviceCode(@RequestParam("deviceCode")  String deviceCode, @RequestParam(value = "amplType", required = false) String amplType) {
		Map<String, Object> map = modelService.selectDataItemsByDeviceCode(deviceCode, amplType);
		return map;
	}

	/**
	 *  进数据配置页面传modelId
	 *  列表查询字段：mainTreeId（selectMainEquipmentListByModelType接口返回的id）、deviceCode、status
	 */
	@ApiOperation(value = "部件管理-部件数据项列表查询")
	@PostMapping(value = "/selectComponentDataList")
	public Map<String, Object> selectComponentDataList(@RequestBody ModelComponentDataDTO dto) {
		Map<String, Object> map = modelService.selectComponentDataList(dto);
		return map;
	}

	@ApiOperation(value = "部件管理-部件数据项新增/编辑")
	@PostMapping(value = "/settingModelComponentData")
	public Map<String, Object> settingModelComponentData(@RequestBody ModelComponentDataDTO dto) {
		Map<String, Object> map = modelService.settingModelComponentData(dto);
		return map;
	}

	@ApiOperation(value = "部件管理-部件数据项删除")
	@PostMapping(value = "/deleteModelComponentData/{ids}")
	public Map<String, Object> deleteModelComponentData(@PathVariable("ids") String[] ids) {
		Map<String, Object> map = modelService.deleteModelComponentData(ids);
		return map;
	}

	@ApiOperation(value = "部件管理-部件数据项启用/禁用(0:禁用，1:启用)")
	@PostMapping(value = "/enableModelComponentData")
	public Map<String, Object> deleteModelComponentData(String enableType, String[] ids) {
		Map<String, Object> map = modelService.enableModelComponentData(enableType, ids);
		return map;
	}

	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~首页实时数据~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
	/**
	 * @param mainEquipmentType BYQ("111", "变压器"),GIS("211", "GIS设备"),CW("311", "开关柜"),BLQ("411", "避雷器"),GEN("611", "发电机"),DL("711", "电缆");
	 *
	 * 拿出主设备下所有的传感器的最近一条数据，是一个list； 模型配置那里也是获取的一个list，然后前台去与配置数据列表进行匹配
	 *
	 *  /selectComponentDataList接口传 page=1，limit=9999，mainTreeId=#{} 查询该设备下的所有数据配置
	 */
	@ApiOperation(value = "首页数据-实时数据查询")
	@GetMapping(value = "/home/selectByqRecentlyData")
	@ApiImplicitParams({@ApiImplicitParam(name = "mainEquipmentType", value = "变压器:111,GIS设备:211,开关柜:311,避雷器:411,发电机:611,电缆:711"), @ApiImplicitParam(name = "mainTreeId", value = "主设备树id")})
	public Map<String, Object> selectRecentlyData(@RequestParam("mainEquipmentType") String mainEquipmentType, @RequestParam("mainTreeId") String mainTreeId) {
		Map<String, Object> map = modelService.selectRecentlyData(mainEquipmentType, mainTreeId);
		return map;
	}

	@ApiOperation(value = "部件管理-主站模型部件查询")
	@GetMapping(value = "/selectMasterStation")
	public Map<String, Object> selectMasterStation(@RequestParam("modelId") String modelId) {
		Map<String, Object> map = modelService.selectMasterStation(modelId);
		return map;
	}
}
