package com.huatek.busi.controller;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.GetMapping;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.util.StringUtil;
import com.huatek.busi.service.BusiStabilityService;
import com.huatek.busi.vo.BusiStabilityVo;
import com.huatek.busi.vo.stability.BusiCapacitanceEquipmentImStabilityVo;
import com.huatek.busi.vo.stability.BusiLightningrodimStabilityVo;
import com.huatek.busi.vo.stability.BusiOilStabilityVo;
import com.huatek.busi.vo.stability.BusiSF6StabilityVo;
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;

/**
 * @ClassName: BusiStabilityIndexController
 * @FullClassPath: com.huatek.busi.controller.BusiStabilityIndexController
 * @Description: 稳定性指标控制器
 * @author: Arno
 * @date: Aug 13, 2019 10:44:01 AM
 * @version: 1.0
 */

@Api(tags = { "稳定性指标API接口" })
@RestController
@RequestMapping("/api/busiStabilityIndex")
public class BusiStabilityIndexController {

	
	@Reference(check = false)
	private BusiStabilityService busiStabilityService;
	
	
	@ApiOperation(value = "保存铁芯夹件稳定性指标")
	@PostMapping(value = "/save", produces = { "application/json;charset=utf-8" })
	public Map<String, Object> saveIegc(@RequestBody BusiStabilityVo vo) {
		Boolean flag = busiStabilityService.insertIegcBusiStability(vo);
		return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, flag ? "保存铁芯夹件稳定性指标成功":"保存铁芯夹件稳定性指标失败", "");
	}
	
	@ApiOperation(value = "保存避雷器稳定性指标")
	@PostMapping(value = "/savelightningrodIm", produces = { "application/json;charset=utf-8" })
	public Map<String, Object> savelightningrodIm(@RequestBody BusiLightningrodimStabilityVo vo) {
		Boolean flag = busiStabilityService.insertLightningrodImStability(vo);
		return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, flag ? "保存避雷器稳定性指标成功":"保存避雷器稳定性指标失败", "");
	}
	
	@ApiOperation(value = "保存容性稳定性指标")
	@PostMapping(value = "/saveCapacitanceIm", produces = { "application/json;charset=utf-8" })
	public Map<String, Object> saveCapacitanceIm(@RequestBody BusiCapacitanceEquipmentImStabilityVo vo) {
		Boolean flag = busiStabilityService.insertCapacitanceImStability(vo);
		return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, flag ? "保存容性稳定性指标成功":"保存容性稳定性指标失败", "");
	}
	
	@ApiOperation(value = "保存SF6稳定性指标")
	@PostMapping(value = "/saveSF6", produces = { "application/json;charset=utf-8" })
	public Map<String, Object> saveSF6(@RequestBody BusiSF6StabilityVo vo) {
		Boolean flag = busiStabilityService.insertSF6Stability(vo);
		return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, flag ? "保存容性稳定性指标成功":"保存容性稳定性指标失败", "");
	}
	
	
	@ApiOperation(value = "保存油色谱稳定性指标")
	@PostMapping(value = "/saveOil", produces = { "application/json;charset=utf-8" })
	public Map<String, Object> saveOil(@RequestBody BusiOilStabilityVo vo) {
		Boolean flag = busiStabilityService.insertOilStability(vo);
		return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, flag ? "保存油色谱稳定性指标成功":"保存油色谱稳定性指标失败", "");
	}
	
	
    @ApiOperation(value = "根据状态获取设备数据")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "sensorCode", value = "传感器编码"),
        @ApiImplicitParam(name = "status", value = "状态：0-无（SF6/油色譜 无发电抽水空闲状态） 1-发电  2-抽水  3-空闲  "),
        @ApiImplicitParam(name = "type", value = "类别: 1-铁芯  2-夹件  3-避雷器  4-容性 5-SF6 6-套管 7-油色譜"),
        @ApiImplicitParam(name = "beginTime", value = "开始时间"),
        @ApiImplicitParam(name = "endTime", value = "结束时间"),
    })
    @GetMapping(value = "/getEquipmentData")
    public Map<String, Object> getEquipmentData(@RequestParam("sensorCode") String sensorCode, @RequestParam("status") String status,
        @RequestParam("type") String type,  @RequestParam("beginTime") String beginTime , @RequestParam("endTime") String endTime) {
    	DateTimeFormatter dateTimeFormater = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    	LocalDateTime begin = null;
    	LocalDateTime end =  null;
    	if(beginTime!= null && StringUtil.isNotEmpty(beginTime)){
    		 begin = LocalDateTime.parse(beginTime.trim()+" 00:00:00" , dateTimeFormater);
    	}
    	if(endTime!= null && StringUtil.isNotEmpty(endTime)){
    		 end =  LocalDateTime.parse(endTime.trim()+" 23:59:59", dateTimeFormater);
    	}
    	List<?> list = busiStabilityService.getEquipmentData(sensorCode, status, type, begin, end);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "", list);
    }


}
