package cn.api.atv;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import cn.component.influxdb.InfluxDbUtil;
import cn.orm.sqltoy.plus.query.LambdaQueryWrapper;
import cn.pojo.atv.*;
import cn.util.core.LogKit;
import cn.util.string.JsonKit;
import cn.util.uuid.UUIDKit;
import com.alibaba.fastjson2.JSONArray;
import org.influxdb.dto.QueryResult;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson2.JSONObject;
import cn.facade.abs.IService;
import cn.common.annotation.Log;
import cn.common.enums.BizOpType;
import cn.common.enums.ResultCode;
import cn.common.exception.BusinessException;
import cn.common.page.response.Result;
import cn.orm.sqltoy.model.Page;
import cn.orm.sqltoy.plus.Wrappers;
import cn.orm.sqltoy.plus.query.QueryWrapper;
import cn.pojo.atv.vo.AtvBoardCardFaultPage;
import cn.pojo.atv.vo.AtvInputVo;
import cn.pojo.atv.vo.AtvKnowledgePage;
import cn.service.atv.IAtvBoardCardFaultService;
import cn.service.atv.IAtvKnowledgeService;
import cn.service.atv.IAtvOutputAndInputStockService;
import cn.service.atv.IAtvService;
import cn.util.core.IsKit;
import cn.util.core.TypeKit;
import cn.pojo.atv.vo.AtvDispatchPage;
import cn.pojo.atv.vo.AtvEquipmentOnlineStatus;
import org.springframework.beans.BeanUtils;
import javax.servlet.http.HttpServletRequest;

/**地铁广播系统专用请求*/
@RestController
@RequestMapping("/api")
public class AtvCtrl extends IService
{
	@Resource
	private IAtvService atvService;
	//app上传文件保存目录
	private final String appDirName = "app";

	//从设备下载日志gainLog
	@GetMapping(value = "/atvEquipment/downloadLog")
	@Log(title = "设备维护-获取日志/音频-downloadLog", businessType = BizOpType.OTHER)
	public Result<?> downloadLog(@RequestParam Map<String,Object> whereMap)
	{
		Boolean result = atvService.downloadLog(whereMap);
		if (result) {
			return Result.okMsg("获取成功，请等待上传!");
		}
		return Result.warnMsg("获取失败，车辆离线!");
	}


	public AtvCtrl(IAtvService atvService, IAtvOutputAndInputStockService atvOutputAndInputStockService, IAtvBoardCardFaultService atvBoardCardFaultService, IAtvKnowledgeService atvKnowledgeService) {
		this.atvService = atvService;
		this.atvOutputAndInputStockService = atvOutputAndInputStockService;
		this.atvBoardCardFaultService = atvBoardCardFaultService;
		this.atvKnowledgeService = atvKnowledgeService;
	}
	/************设备控制******/
	@GetMapping("/atvEquipment/rebootAll")
	@Log(title = "设备维护-单个设备类型重启-rebootAll", businessType = BizOpType.OTHER)
	public Result<?> rebootAll(@RequestParam Map<String,Object> whereMap) 
	{
		Boolean result = atvService.rebootAll(whereMap);
		if (result) {
			return Result.okMsg("重启成功!");
		}
		return Result.warnMsg("重启失败，车辆离线!");
	}

	@GetMapping("/atvEquipment/rebootOne")
	@Log(title = "设备维护-单个设备重启-rebootOne", businessType = BizOpType.OTHER)
	public Result<?> rebootOne(@RequestParam Map<String,Object> whereMap) 
	{
		Boolean result = atvService.rebootOne(whereMap);
		if (result) {
			return Result.okMsg("重启成功!");
		}
		return Result.warnMsg("重启失败，车辆离线!");
	}
	/**页面约定：升级只能上传一个文件*/
	@GetMapping("/atvEquipment/upgradeOne")
	@Log(title = "设备维护-单个设备升级-upgradeOne", businessType = BizOpType.OTHER)
	public Result<?> upgradeOne(@RequestParam Map<String,Object> whereMap) 
	{
		Boolean result = atvService.upgradeOne(whereMap);
		if (result) {
			return Result.okMsg("升级成功!");
		}
		return Result.warnMsg("升级失败，车辆离线!");
	}

	@GetMapping(value = "/atvEquipment/upgradeAll")
	@Log(title = "设备维护-单个设备类型升级-upgradeAll", businessType = BizOpType.OTHER)
	public Result<?> upgradeAll(@RequestParam Map<String,Object> pageMap) 
	{
		return atvService.upgradeAll(pageMap);
	}


	/************入库操作接口******/
	private final IAtvOutputAndInputStockService atvOutputAndInputStockService;

	@PostMapping(value = "/atvOutputAndInputStock/inputAdd")
	@Log(title = "出入库操作-入库操作添加-inputAdd", businessType = BizOpType.INSERT)
	public Result<?> stockInputAdd(@RequestBody AtvInputVo atvInputVo) 
	{
		//未做非空验证，必填项前端要做验证
		AtvOutputAndInputStock atvOutputAndInputStock = new AtvOutputAndInputStock();
		BeanUtils.copyProperties(atvInputVo, atvOutputAndInputStock);
		Boolean result = atvOutputAndInputStockService.stockInput(atvOutputAndInputStock, atvInputVo.getAtvOutputAndInputStockItems());
		return Result.ok(result);
	}
	/** 出库操作接口*/
	@PostMapping(value = "/atvOutputAndInputStock/outputAdd")
	@Log(title = "出入库操作-出库操作-outputAdd", businessType = BizOpType.SELECT)
	public Result<?> stockOutputAdd(@RequestBody AtvInputVo atvInputVo) {
		//未做非空验证，必填项前端要做验证
		return atvOutputAndInputStockService.stockOutput(atvInputVo, atvInputVo.getAtvOutputAndInputStockItems());
	}

	/************板卡操作******/
	@Resource
	private IAtvBoardCardFaultService atvBoardCardFaultService;

	@PostMapping(value = "/atvBoardCardFault/delete")
	@Log(title = "板卡操作-删除板卡故障及文件-deleteBoardCardFault", businessType = BizOpType.DELETE)
	public Result<?> boardCardFaultDelete(@RequestBody Map<String,Object> whereMap)
	{
		int delNum = whereMap.size();
		Long count = atvBoardCardFaultService.deleteFaultAndFile(whereMap);
		if (count == 0) {
			return Result.warnMsg("批量删除失败!");
		}
		if (count < delNum) {
			return Result.warnMsg("部分删除成功");
		}
		return Result.ok("批量删除成功!");
	}

	@GetMapping(value = "/atvBoardCardFault/lineWorkers")
	@Log(title = "板卡操作-查询线路工人信息-lineWorkers", businessType = BizOpType.SELECT)
	public Result<?> boardCardFaultLineWorkers(@RequestParam(name = "lineId") String lineId)
	{
		return Result.ok(atvBoardCardFaultService.queryLineWorkers(lineId));
	}
	/**手动派单*/
	@PostMapping(value = "/atvBoardCardFault/dispatch")
	@Log(title = "板卡操作-手动派单-dispatch", businessType = BizOpType.SELECT)
	public Result<?> boardCardFaultDispatch(@RequestBody AtvDispatchPage atvDispatchPage) {
		return atvBoardCardFaultService.dispatch(atvDispatchPage);
	}

	/**分页列表查询*/
	@GetMapping(value = "/atvBoardCardFaultFile/list")
	@Log(title = "板卡操作-板卡故障文件列表查询-faultFileQueryPageList", businessType = BizOpType.SELECT)
	public Result<?> boardCardFaultFileListByPage(@RequestParam Map<String,Object> paramMap)
	{
		System.out.println("paramMap:"+paramMap);
		Page<?> pageResult;
		try {
			pageResult = daoService.getPage("atv_board_card_fault_file",paramMap);
		} catch (BusinessException e) {
			return Result.warnMsg(ResultCode.SQL_RUN.getMsg());
		}
		if(IsKit.isNullOrEmpty(pageResult)) {
			return Result.warnMsg(ResultCode.DATA_NOEXIST.getMsg());
		}
		List<Map<String, Object>> resultList = TypeKit.cast(pageResult.getRows());
		return Result.ok_ePlus(pageResult.getRecordCount(),resultList);
	}

	/**新增故障.页面数据含有[,]格式数据*/
	@PostMapping(value = "/atvBoardCardFault/add")
	@Log(title = "板卡操作-新增故障-addBoardCardFault", businessType = BizOpType.INSERT)
	public Result<?> boardCardFaultInsert(@RequestBody JSONObject entity) 
	{
		AtvBoardCardFaultPage atvBoardCardFaultPage = JSONObject.parseObject(entity.toString(),AtvBoardCardFaultPage.class);
		AtvBoardCardFault atvBoardCardFault = new AtvBoardCardFault();
		BeanUtils.copyProperties(atvBoardCardFaultPage, atvBoardCardFault);
		atvBoardCardFault.setFaultSource(1);

		atvBoardCardFaultService.saveMain(atvBoardCardFault, atvBoardCardFaultPage.getAtvBoardCardFaultFileList());
		return Result.ok("添加成功！");
	}

	/** 是否需要？？？：编辑故障.页面数据含有[,]格式数据*/
	@PostMapping(value = "/atvBoardCardFault/update")
	@Log(title = "板卡操作-编辑故障-updateBoardCardFault", businessType = BizOpType.UPDATE)
	public Result<?> boardCardFaultUpdate(@RequestBody JSONObject entity) 
	{
		AtvBoardCardFaultPage atvBoardCardFaultPage = JSONObject.parseObject(entity.toString(),AtvBoardCardFaultPage.class);
		AtvBoardCardFault atvBoardCardFault = new AtvBoardCardFault();
		BeanUtils.copyProperties(atvBoardCardFaultPage, atvBoardCardFault);
		atvBoardCardFaultService.updateMain(atvBoardCardFault, atvBoardCardFaultPage.getAtvBoardCardFaultFileList());
		return Result.ok("更新成功！");
	}

	/************知识库操作******/
	private final IAtvKnowledgeService atvKnowledgeService;

	@PostMapping(value = "/atvKnowledge/add")
	@Log(title = "知识库-添加知识-knowledgeAdd", businessType = BizOpType.INSERT)
	public Result<?> knowledgeAdd(@RequestBody AtvKnowledgePage atvKnowledgePage) {
		AtvKnowledge atvKnowledge = new AtvKnowledge();
		BeanUtils.copyProperties(atvKnowledgePage, atvKnowledge);
		atvKnowledgeService.saveMain(atvKnowledge, atvKnowledgePage.getAtvKnowledgeFileList());
		return Result.ok("添加成功！");
	}

	@GetMapping(value = "/atvKnowledge/isRelease")
	@Log(title = "知识库-是否发布-atvKnowledgeIsRelease", businessType = BizOpType.UPDATE)
	public Result<?> knowledgeIsRelease(@RequestParam Map<String,Object> pageParam) 
	{
		return Result.ok(atvKnowledgeService.updateIsRelease(pageParam));
	}

	/**批量删除 Arrays.asList(ids.split(","))*/
	@DeleteMapping(value = "/atvKnowledge/delBatch")
	@Log(title = "知识库-批量删除-delBatch", businessType = BizOpType.DELETE)
	public Result<?> knowledgeIsDelBatch(@RequestBody List<String> idList) 
	{
		return Result.ok(atvKnowledgeService.delBatch(idList));
	}

	@PutMapping(value = "/atvKnowledge/update")
	@Log(title = "知识库-编辑-knowledgeUpdate", businessType = BizOpType.UPDATE)
	public Result<?> knowledgeUpdate(@RequestBody AtvKnowledgePage atvKnowledgePage) 
	{
		AtvKnowledge atvKnowledge = new AtvKnowledge();
		BeanUtils.copyProperties(atvKnowledgePage, atvKnowledge);
		/*不可能出现
        AtvKnowledge atvDemoEntity = atvKnowledgeService.getById(atvKnowledge.getId());
        if (atvDemoEntity == null) {
            return Result.warnMsg("未找到对应数据");
        }
		 */
		atvKnowledgeService.updateMain(atvKnowledge, atvKnowledgePage.getAtvKnowledgeFileList());
		return Result.ok("编辑成功!");
	}
	/**分页列表查询*/
	@GetMapping(value = "/atvKnowledgeFile/list")
	@Log(title = "知识库附件表-分页列表查询-atvKnowledgeFileList", businessType = BizOpType.SELECT)
	public Result<?> knowledgeFileList(@RequestParam Map<String,Object> paramMap)
	{
		Page<?> pageResult;
		try {
			pageResult = daoService.getPage("atv_knowledge_file",paramMap);
			List<Map<String, Object>> resultList = TypeKit.cast(pageResult.getRows());
			return Result.ok_ePlus(pageResult.getRecordCount(),resultList);
		} catch (Exception e) {
			return Result.warnMsg(ResultCode.SQL_RUN.getMsg());
		}
	}
	/**
	 * 通过id查询@RequestParam Map<String,Object> paramMap
	 */
	@GetMapping(value = "/atvKnowledge/getKnowledgeDetailById")
	@Log(title = "知识库表-通过id查询-getKnowledgeDetailById", businessType = BizOpType.SELECT)
	public Result<?> knowledgeDetailById(@RequestParam(name = "id") String id)
	{
		AtvKnowledge atvKnowledge = atvKnowledgeService.getKnowledgeDetailById(id);
		if (IsKit.isNull(atvKnowledge)) {
			return Result.warnMsg("未找到对应数据");
		}
		return Result.ok(atvKnowledge);
	}

	/************车辆监控******/

	@GetMapping(value = "/trainMonitor/getLogCount")
	@Log(title = "实时监控-根据时间查询日志数量-trainMonitorLogCount", businessType = BizOpType.SELECT)
	public Result<?> trainMonitorLogCount(@RequestParam(name = "trainId") String trainId,
			@RequestParam(name = "startTime") String startTime,
			@RequestParam(name = "endTime") String endTime) 
	{
		Map<String, Object> data = new HashMap<>();
		Long quantity = atvService.logCount(trainId, startTime, endTime);
		List<AtvEquipmentOnlineStatus> atvEquipmentOnlineStatuses = atvService.findOnlineStatus(trainId, startTime);
		data.put("quantity", quantity);
		data.put("atvEquipmentOnlineStatuses", atvEquipmentOnlineStatuses);
		return Result.ok(data);
	}

	@GetMapping(value = "/trainMonitor/logPlayback")
	@Log(title = "实时监控-日志回放-trainMonitorLogPlayback", businessType = BizOpType.SELECT)
	public Result<?> trainMonitorLogPlayback(@RequestParam(name = "trainId") String trainId,
			@RequestParam(name = "startTime") String startTime,
			@RequestParam(name = "endTime") String endTime,
			@RequestParam(name = "number") Integer number,
			@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) 
	{
		Page<?> pageResult;
		try {
			pageResult = atvService.logPlayback(trainId, startTime, endTime, number, pageSize);
			List<Map<String, Object>> resultList = TypeKit.cast(pageResult.getRows());
			return Result.ok_ePlus(pageResult.getRecordCount(),resultList);
		} catch (Exception e) {
			return Result.warnMsg(ResultCode.SQL_RUN.getMsg());
		}
	}
	@GetMapping(value = "/trainMonitor/queryById")
	@Log(title = "列车工作日志详情-queryById", businessType = BizOpType.SELECT)
	public Result<?> trainMonitorLQueryById(@RequestParam(name = "id") String id)
	{
		QueryWrapper<AtvTrainWorkingLog> queryWrapper = Wrappers.wrapper(AtvTrainWorkingLog.class)
				.eq("id", id);
		AtvTrainWorkingLog atvTrainWorkingLog = daoService.findOne(queryWrapper);
		if (IsKit.isNull(atvTrainWorkingLog)) {
			return Result.warnMsg("未找到对应数据");
		}
		return Result.ok(atvTrainWorkingLog);
	}

	/**
	 * 获取阈值信息，先从redis中获取，如果没有再从数据库中获取
	 * @param boardCardType 板卡类型
	 * @return 该板卡对应的阈值信息
	 */
	@GetMapping("/getThreshold")
	@Log(title = "单表操作-获取阈值信息-getThreshold", businessType=BizOpType.SELECT)
	public Result<?> getThreshold(@NotNull @RequestParam String boardCardType)
	{
		// 从redis中获取阈值信息
		Object redisAtvThreshold = redisCache.hget("threshold", boardCardType);
		if (IsKit.isNotNull(redisAtvThreshold)) {
			return Result.ok(redisAtvThreshold);
		}
		/*小周：从数据库中获取阈值信息
		Map<String, Object> paramMap = new HashMap<>();
		paramMap.put("boardCardType", boardCardType);
		Map<String, Object> sqlAtvThreshold = daoService.getMap("atv_threshold",  paramMap);
		 */
		QueryWrapper<AtvThreshold> queryWrapper = Wrappers.wrapper(AtvThreshold.class)
				.eq("boardCardType", boardCardType);
		AtvThreshold sqlAtvThreshold = daoService.findOne(queryWrapper);
		if (IsKit.isNotNull(sqlAtvThreshold)) {
			// 将阈值信息存入redis
			redisCache.hset("threshold", boardCardType, sqlAtvThreshold);
			return Result.ok(sqlAtvThreshold);
		}
		return Result.warnMsg(ResultCode.SQL_RUN.getMsg());
	}


	/**
	 * 按orders字段排序获取车厢信息
	 * @param trainId 车辆id
	 * @return 车辆中车厢信息
	 */
	@GetMapping("/getCarriageListByOrder")
	@Log(title = "库表操作-按orders字段排序获取全部记录-getCarriageListByOrder", businessType = BizOpType.SELECT)
	public Result<?> getListByOrder(@NotNull @RequestParam String trainId)
	{
		QueryWrapper<AtvCarriage> queryWrapper = Wrappers.wrapper(AtvCarriage.class)
				.eq("trainId", trainId)
				.orderByAsc("orders");
		List<AtvCarriage> list = daoService.findList(queryWrapper);
		if (IsKit.isEmpty(list)) {
			return Result.warnMsg("未找到对应数据");
		}
		return Result.ok(list);
	}
	
	@PostMapping("/copyTrain")
	@Log(title = "多表操作-拷贝车辆信息-copyTrain", businessType=BizOpType.INSERT)
	public Result<?> copyTrain(@RequestBody AtvTrain entity)
	{
		QueryWrapper<AtvCarriage> queryWrapper = Wrappers.wrapper(AtvCarriage.class)
				.eq("trainId", entity.getId());
		List<AtvCarriage>  listEntity = daoService.findList(queryWrapper);
        //保存车辆信息
		String id = UUIDKit.encodingUUID();
		entity.setId(id);
		daoService.save(entity);
		//写车厢信息
		listEntity.stream().filter(bean -> {
			bean.setId(UUIDKit.encodingUUID());
			bean.setTrainId(id);
			return true;
		}).collect(Collectors.toList());
		return Result.ok(daoService.saveAll(listEntity));
	}
	
	@PostMapping("/updateSaveTrain")
	@Log(title = "库表操作-存在更新先不存在插入-updateSaveTrain", businessType = BizOpType.DELETEINSERT)
	public Result<?> updateSaveTrain( @RequestBody Map<String, Object> jsonArray)
	{
		String jsonListStr = TypeKit.objToStr(jsonArray.get("data"));
		List<Map<String,Object>> paramList = JsonKit.json2List_gson(jsonListStr);
		//页面传来的atv_carriage数据
		List<AtvCarriage> pageEtity = new JsonKit().jsonToList_jackson(jsonListStr, AtvCarriage.class);
         //页面传来的查询条件
		Map<String,Object> whereMap = JsonKit.json2Map_jackson(TypeKit.objToStr(jsonArray.get("where")));
        //获取表中已经存在的记录
		QueryWrapper<AtvCarriage> wrapper = Wrappers.wrapper(AtvCarriage.class)
				.select("id")
		        .eq("trainId", whereMap.get("trainId"));
		List<AtvCarriage> idList = daoService.findList(wrapper);
		List<String> idStrList = idList.stream().map(AtvCarriage::getId).collect(Collectors.toList());

		//获取需要更新和出入的记录
		List<AtvCarriage> updateList = new ArrayList<>();
		List<AtvCarriage> insertList = new ArrayList<>();
		for(AtvCarriage entity: pageEtity){
			String id = entity.getId();
			//新数据
			if(IsKit.isNullOrEmpty(id)){
				insertList.add(entity);
			}
			else{
				//已存在数据
				if(idStrList.contains(id)){
					updateList.add(entity);
					idStrList.remove(id);
				}
			}
		}
		daoService.updateAll(updateList);
		daoService.saveAll(insertList);

        //删除表中无用的记录
		LambdaQueryWrapper<AtvCarriage> delWrapper = Wrappers.lambdaWrapper(AtvCarriage.class)
				.in(AtvCarriage::getId, idStrList);
		daoService.delete(delWrapper);
		return Result.ok();
	}


	/************计算阈值*****/
	public static String[] threshold_Attributes =
			new String[] {
					"current5","current3v3","current12",
					"voltage12","voltage3v3","voltage5",
					"temperature","audiochannel1","audiochannel2",
					"audiochannel3","audiochannel4","audiochannel5",
					"audiochannel6","audiochannel7","audiochannel8"};

	@GetMapping("/calculateThreshold/{attributes}")
	@Log(title = "设备维护-单个设备类型重启-calculateThreshold", businessType = BizOpType.OTHER)
	public Result<?> calculateThreshold(@PathVariable String attributes)
	{
		LogKit.info("阈值计算开始");
		String[] new_Attributes = attributes.split(",");
		JSONArray jsonArray;
		List<AtvThreshold> atvThresholdList = new ArrayList<>();
		for (String attribute:new_Attributes) {
			int cardtype = Integer.parseInt(attribute);
			String command = String.format(
					"SELECT MEAN(%s)-3*STDDEV(%s),MEAN(%s)+3*STDDEV(%s)," +
							"MEAN(%s)-3*STDDEV(%s),MEAN(%s)+3*STDDEV(%s)," +
							"MEAN(%s)-3*STDDEV(%s),MEAN(%s)+3*STDDEV(%s)," +
							"MEAN(%s)-3*STDDEV(%s),MEAN(%s)+3*STDDEV(%s)," +
							"MEAN(%s)-3*STDDEV(%s),MEAN(%s)+3*STDDEV(%s)," +
							"MEAN(%s)-3*STDDEV(%s),MEAN(%s)+3*STDDEV(%s)," +
							"MAX(%s)+1,MAX(%s),MAX(%s),"+
							"MAX(%s),MAX(%s),MAX(%s),"+
							"MAX(%s),MAX(%s),MAX(%s) FROM \"boardCards\" WHERE cardtype=\'%d\'",
					threshold_Attributes[0],threshold_Attributes[0],threshold_Attributes[0],threshold_Attributes[0],
					threshold_Attributes[1],threshold_Attributes[1],threshold_Attributes[1],threshold_Attributes[1],
					threshold_Attributes[2],threshold_Attributes[2],threshold_Attributes[2],threshold_Attributes[2],
					threshold_Attributes[3],threshold_Attributes[3],threshold_Attributes[3],threshold_Attributes[3],
					threshold_Attributes[4],threshold_Attributes[4],threshold_Attributes[4],threshold_Attributes[4],
					threshold_Attributes[5],threshold_Attributes[5],threshold_Attributes[5],threshold_Attributes[5],

					threshold_Attributes[6], threshold_Attributes[7],threshold_Attributes[8],threshold_Attributes[9],
					threshold_Attributes[10],threshold_Attributes[11],threshold_Attributes[12],
					threshold_Attributes[13], threshold_Attributes[14],cardtype);

			QueryResult result = InfluxDbUtil.query(command);
			System.out.println(result);
			List<QueryResult.Result> results = result.getResults();
			List< QueryResult.Series> series = results.get(0).getSeries();
			List<String> key = series.get(0).getColumns();
			List<List<Object>> value = series.get(0).getValues();


			AtvThreshold atvThreshold = new AtvThreshold();

			atvThreshold.setId(attribute);
			atvThreshold.setBoardCardType(cardtype);
			//atvThreshold.setId(UUIDKit.fastUUID_S());
			for(List<Object> v:value){

				atvThreshold.setCurrentMax5(get(v, 2));
				atvThreshold.setCurrentMin5(get(v,1));

				atvThreshold.setCurrentMax3v3(get(v,4));
				atvThreshold.setCurrentMin3v3(get(v,3));

				atvThreshold.setCurrentMax12(get(v,6));
				atvThreshold.setCurrentMin12(get(v,5));

				atvThreshold.setVoltageMax12(get(v,8));
				atvThreshold.setVoltageMin12(get(v,7));


				atvThreshold.setVoltageMax3v3(get(v,10));
				atvThreshold.setVoltageMin3v3(get(v,9));

				atvThreshold.setVoltageMax5(get(v,12));
				atvThreshold.setVoltageMin5(get(v,11));

				atvThreshold.setTemperatureMax(get(v,13));


				List<Object> list = v.subList(v.size()-8, v.size());

				Double maxDouble = get(list, 0);

				for (int i = 1; i < list.size(); i++) {
					Double c =get(list, i);
					if(c>maxDouble){
						maxDouble=c;
					}
				}
				if(maxDouble!=0.0){
					atvThreshold.setVolumeMax(maxDouble+5);
				}
				else {
					atvThreshold.setVolumeMax(maxDouble);
				}
				atvThresholdList.add(atvThreshold);
			}

		}
		daoService.saveOrUpdateAll(atvThresholdList);
		System.out.println("boardCardType:" +attributes);
		return Result.ok();
	}

	public Double get(List<Object> list, int index){
		if(list.get(index) == null){
			return 0.0;
		}
		return Double.parseDouble(list.get(index).toString());
	}
}