package cn.com.energy.controller;

import cn.com.energy.model.DvMachinery;
import cn.com.energy.model.DvMachineryData;
import cn.com.energy.service.IDvMachineryDataService;
import cn.com.energy.service.IDvMachineryService;
import cn.com.v2.common.base.BaseController;
import cn.com.v2.common.domain.AjaxResult;
import cn.hutool.core.date.DateUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

@RestController
@RequestMapping("/api/energy")
public class EnergyApiController extends BaseController {
	@Autowired
	private IDvMachineryDataService iDvMachineryDataService;
	@Autowired
	private IDvMachineryService iDvMachineryService;

	@ApiOperation(value = "获取数据", notes = "获取数据")
	@GetMapping("/getEnergyData")
	@ResponseBody
	public AjaxResult getEnergyData() {
		DvMachineryData dvMachineryData = new DvMachineryData();
		List<Map<String, Object>> dvMachineryDataList = iDvMachineryDataService.selectElectric(dvMachineryData);
		String val = "0";
		if(dvMachineryDataList.size() > 0 && dvMachineryDataList.get(0) != null){
			val = dvMachineryDataList.get(0).get("parser_data").toString();
			BigDecimal valBig = new BigDecimal(val).setScale(2, RoundingMode.UP);
			val = valBig.toString();
		}
		return success().put("data", val);
	}

	@ApiOperation(value = "获取数据", notes = "获取数据")
	@GetMapping("/getEnergyDayData")
	@ResponseBody
	public AjaxResult getEnergyDayData() {
		String now = DateUtil.format(new Date(), "yyyy-MM-dd");
		DvMachineryData dvMachineryData = new DvMachineryData();
		dvMachineryData.setCreateTimeStr(now);
		List<Map<String, Object>> dvMachineryDataList = iDvMachineryDataService.selectElectric(dvMachineryData);
		String val = "0";
		if(dvMachineryDataList.size() > 0 && dvMachineryDataList.get(0) != null){
			val = dvMachineryDataList.get(0).get("parser_data").toString();
			BigDecimal valBig = new BigDecimal(val).setScale(2, RoundingMode.UP);
			val = valBig.toString();
		}
		return success().put("data", val);
	}

	@ApiOperation(value = "获取设备数据", notes = "获取设备数据")
	@GetMapping("/getMachinery")
	@ResponseBody
	public AjaxResult getMachinery() {
		DvMachinery dvMachinery = new DvMachinery();
		List<DvMachinery> dvMachineryDataList = iDvMachineryService.selectDvMachineryList(dvMachinery);
		String val = dvMachineryDataList.size() + "";
		return success().put("data", val);
	}

	@ApiOperation(value = "获取周能耗数据", notes = "获取数据")
	@GetMapping("/getEnergyWeekData")
	@ResponseBody
	public AjaxResult getEnergyWeekData() {
		List<Map<String,Object>> dvMachineryWeekDataList=iDvMachineryDataService.selectElectricByWeek();
		return success().put("data", dvMachineryWeekDataList);
	}
}
