package com.iformall.controller.device;

import com.iformall.annotation.SystemControllerLog;

import com.iformall.common.ErrorCode;
import com.iformall.common.Result;
import com.iformall.common.ResultData;
import com.iformall.controller.base.BaseController;
import com.iformall.domain.dto.KwMeterDataDto;

import com.iformall.domain.po.KwMeterData;

import com.iformall.domain.vo.KwMeterDataVo;
import com.iformall.enums.EnumMeterDataReportType;
import com.iformall.service.kw.KwMeterDataService;
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.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

@RestController
@RequestMapping("kwMeterData")
@Api(description="电表数据")
public class KwMeterDataController extends BaseController {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
    private KwMeterDataService kwMeterDataService;

	@ApiOperation("数据列表")
	@GetMapping("list")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
			@ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true)})
    @SystemControllerLog(description = "电表设备-电表数据")
	public Result list(@ModelAttribute KwMeterDataDto kwMeterDataDto, Integer pageNum, Integer pageSize) {
		if (kwMeterDataDto == null) kwMeterDataDto = new KwMeterDataDto();
		kwMeterDataDto.setTenantId(getTenantId());
		return new ResultData(kwMeterDataService.listVoAsPage(kwMeterDataDto, pageNum, pageSize));
	}


	@ApiOperation("数据列表")
	@GetMapping("listPower")
	@SystemControllerLog(description = "电表设备-电表数据")
	public Result listPower(@ModelAttribute KwMeterDataDto kwMeterDataDto) {
		if (kwMeterDataDto == null) kwMeterDataDto = new KwMeterDataDto();
		kwMeterDataDto.setTenantId(getTenantId());

		List<KwMeterDataVo> kwMeterDataVos = kwMeterDataService.findVoList(kwMeterDataDto);

		List<KwMeterDataVo> monthDailyPower = new ArrayList<>();

		if (kwMeterDataDto.getReportTime() != null){

			if (kwMeterDataDto.getReportType().equals(EnumMeterDataReportType.DAY.getCode())) {
				Date date = null;
				try {
					date = new SimpleDateFormat("yyyy-MM").parse(kwMeterDataDto.getReportTime());
				} catch (Exception e) {
					return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
				}

				LocalDate localDate= LocalDateTime.ofInstant(date.toInstant(),ZoneId.systemDefault()).toLocalDate();
				int days = localDate.lengthOfMonth();
				LocalDate firstDate = localDate.with(TemporalAdjusters.firstDayOfMonth());
				for (int i = 0; i < days; i++) {
					String dateStr = DateTimeFormatter.ofPattern("yyyy-MM-dd").format(firstDate.plusDays(i));
					Optional<KwMeterDataVo> optionalKwMeterDataVo = kwMeterDataVos.stream().filter(md -> md.getReportTime().equals(dateStr)).findFirst();
					if (optionalKwMeterDataVo.isPresent())
						monthDailyPower.add(optionalKwMeterDataVo.get());
					else {
						KwMeterDataVo kwMeterDataVo = new KwMeterDataVo();
						kwMeterDataVo.setReportTime(dateStr);
						monthDailyPower.add(kwMeterDataVo);
					}
				}
				return new ResultData(monthDailyPower);
			}

			if (kwMeterDataDto.getReportType().equals(EnumMeterDataReportType.HOUR.getCode())){

			}
			if (kwMeterDataDto.getReportType().equals(EnumMeterDataReportType.MONTH.getCode())){

			}
			if (kwMeterDataDto.getReportType().equals(EnumMeterDataReportType.BY_HOUR.getCode())){

			}

		}
		return new ResultData(kwMeterDataVos);
	}

	@ApiOperation("获取电量")
	@GetMapping("getPower")
	public Result getMeterPower(@ModelAttribute KwMeterDataDto kwMeterDataDto) {

		if (kwMeterDataDto == null)
			return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);
		if (kwMeterDataDto.getMeterId() == null)
			return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);

		if (kwMeterDataDto.getStartTime() == null && kwMeterDataDto.getStartNumber() == null)
			return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);

		if (kwMeterDataDto.getEndTime() == null && kwMeterDataDto.getEndNumber() == null)
			return new ResultData(ErrorCode.SYS_PARAMETER_ERROR);

		return new ResultData(kwMeterDataService.getPower(kwMeterDataDto));
	}

}
