package org.springblade.community.controller.statistic.controller;

import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;

import org.springblade.community.entity.Agency;
import org.springblade.community.service.IAgencyService;
import org.springblade.core.tool.api.R;


import org.springblade.device.vo.ElectricMeterStaticVO;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * Created By lidexi in 2021/1/16
 **/
@RestController
@AllArgsConstructor
@RequestMapping("/statistics")
@Api(value = "电表统计", tags = "电表统计")
public class ElectricMeterStatic1Controller {

	private RedisTemplate<String,String> redisTemplate;
//	private IElectricMeterStaticService electricMeterStaticService;
//	private IElectricMeterRecordService electricMeterRecordService;
	private IAgencyService agencyService;

	@ApiOperation(value = "电表大屏统计", notes = "传入agencyId(园区id)")
	@GetMapping("/electricMeterStatic")
	public R<ElectricMeterStaticVO> getStaticOf30Day(Long agencyId){
		Agency agency = agencyService.getById(agencyId);
		if(null == agency){
			return R.fail("agencyId对应的实体不存在");
		}
		String data = redisTemplate.opsForValue().get("ycwg:electricstatic:"+agencyId);
		ElectricMeterStaticVO electricMeterStaticVO;
		if(data != null){
			electricMeterStaticVO = JSON.parseObject(data, ElectricMeterStaticVO.class);
		}else {

			electricMeterStaticVO = new ElectricMeterStaticVO();
			List<String> dates = this.getDates();
			List<String> dayValues = new ArrayList<>();
			for(int i=0;i<dates.size();i++){
//				ElectricMeterStatic electricMeterStatic = electricMeterStaticService.getOne(new QueryWrapper<>(new ElectricMeterStatic()).lambda().eq(ElectricMeterStatic::getAgencyId, agencyId).eq(ElectricMeterStatic::getDate, dates.get(i)));
//				if(null != electricMeterStatic){
//					dayValues.add(electricMeterStatic.getDayValue());
//				}else {
//					ElectricMeterStatic newObj = new ElectricMeterStatic();
//					newObj.setAgencyId(agencyId);
//					newObj.setDate(dates.get(i));
//
//					//查询当前查询日期该园区用电总量
//					List<ElectricMeterRecord> electricMeterRecords = electricMeterRecordService.getBaseMapper().selectList(new QueryWrapper<>(new ElectricMeterRecord()).lambda().eq(ElectricMeterRecord::getReadTime, dates.get(i)).like(ElectricMeterRecord::getOrgPosition, agency.getOrgPosition()));
//					double daySum = electricMeterRecords.stream().mapToDouble(e -> Double.parseDouble(e.getDayValue())).sum();
//					double monthSum = electricMeterRecords.stream().mapToDouble(e -> Double.parseDouble(e.getDayValue())).sum();
//					double yearSum = electricMeterRecords.stream().mapToDouble(e -> Double.parseDouble(e.getDayValue())).sum();
//					dayValues.add(String.valueOf(daySum));
//
//					newObj.setDayValue(String.valueOf(daySum));
//					newObj.setMonthValue(String.valueOf(monthSum));
//					newObj.setYearValue(String.valueOf(yearSum));
//					this.electricMeterStaticService.save(newObj);
//				}
			}


			electricMeterStaticVO.setDates(dates);
			electricMeterStaticVO.setValues(dayValues);

			redisTemplate.opsForValue().set("ycwg:electricstatic:"+agencyId, JSON.toJSONString(electricMeterStaticVO));
		}
		return  R.data(electricMeterStaticVO);
	}

	private List<String> getDates() {
		List<String> dateList = new ArrayList<>();
		String dates = redisTemplate.opsForValue().get("ycwg:electricstatic:dates");
		if (dates == null) {
			dateList.add(LocalDate.now().plusDays(-30).toString());
			dateList.add(LocalDate.now().plusDays(-29).toString());
			dateList.add(LocalDate.now().plusDays(-28).toString());
			dateList.add(LocalDate.now().plusDays(-27).toString());
			dateList.add(LocalDate.now().plusDays(-26).toString());
			dateList.add(LocalDate.now().plusDays(-25).toString());
			dateList.add(LocalDate.now().plusDays(-24).toString());
			dateList.add(LocalDate.now().plusDays(-23).toString());
			dateList.add(LocalDate.now().plusDays(-22).toString());
			dateList.add(LocalDate.now().plusDays(-21).toString());
			dateList.add(LocalDate.now().plusDays(-20).toString());
			dateList.add(LocalDate.now().plusDays(-19).toString());
			dateList.add(LocalDate.now().plusDays(-18).toString());
			dateList.add(LocalDate.now().plusDays(-17).toString());
			dateList.add(LocalDate.now().plusDays(-16).toString());
			dateList.add(LocalDate.now().plusDays(-15).toString());
			dateList.add(LocalDate.now().plusDays(-14).toString());
			dateList.add(LocalDate.now().plusDays(-13).toString());
			dateList.add(LocalDate.now().plusDays(-12).toString());
			dateList.add(LocalDate.now().plusDays(-11).toString());
			dateList.add(LocalDate.now().plusDays(-10).toString());
			dateList.add(LocalDate.now().plusDays(-9).toString());
			dateList.add(LocalDate.now().plusDays(-8).toString());
			dateList.add(LocalDate.now().plusDays(-7).toString());
			dateList.add(LocalDate.now().plusDays(-6).toString());
			dateList.add(LocalDate.now().plusDays(-5).toString());
			dateList.add(LocalDate.now().plusDays(-4).toString());
			dateList.add(LocalDate.now().plusDays(-3).toString());
			dateList.add(LocalDate.now().plusDays(-2).toString());
			dateList.add(LocalDate.now().plusDays(-1).toString());
			redisTemplate.opsForValue().set("ycwg:electricstatic:dates", JSON.toJSONString(dateList));
		} else {
			dateList = JSON.parseObject(dates, ArrayList.class);
		}
		return dateList;
	}
}
