 package com.huatek.busi.service.impl.transformer;

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

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huatek.busi.dto.TransformerRunStatusDto;
import com.huatek.busi.dto.curve.BaseCurveQueryDto;
import com.huatek.busi.dto.curve.PointItemDto;
import com.huatek.busi.entity.BusiSiteTree;
import com.huatek.busi.entity.TransformerRunStatus;
import com.huatek.busi.mapper.TransformerRunStatusMapper;
import com.huatek.busi.service.BusiSiteTreeService;
import com.huatek.busi.service.PageUtility;
import com.huatek.busi.service.transformer.TransformerRunStatusService;
import com.huatek.busi.vo.TransformerRunStatusCountVo;
import com.huatek.busi.vo.TransformerRunStatusVo;
import com.huatek.busi.vo.curve.PointItemVo;
import com.huatek.torch.frame.tools.ConstantUtil;
import com.huatek.torch.frame.tools.ResultUtil;

/**
 * 变压器运行工况历史数据查询
 * @author buck_guo
 * @date 2019/08/22
 */
@Component
@Service(interfaceClass = TransformerRunStatusService.class)
@Transactional(rollbackFor = Exception.class)
public class TransformerRunStatusServiceImpl implements TransformerRunStatusService, PageUtility {
    @Autowired
    private TransformerRunStatusMapper transformerRunStatusMapper;

    @Reference(check = false)
    private BusiSiteTreeService busiSiteTreeService;

    @Override
    public Class<?> persistentClass() {
        return TransformerRunStatus.class;
    }

    @Override
    public Map<String, Object> findTransformerRunStatusPage(Map<String, Object> params){
        if (params.get("page") != null && params.get("limit") != null) {
            PageHelper.startPage(page(params), limit(params));
        }
        Page<Map<String, Object>> pageInfo = transformerRunStatusMapper.findTransformerRunStatusPage(params);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, pageInfo.getTotal(), "", pageInfo.getResult());
    }
	@Override
	public Map<String, Object> exportPointTableData(String mainTreeId,LocalDate timeStamp,LocalDate timeStampEnd) {
		Map<String, Object> returnMap = new HashMap<>();
		if (StringUtils.isNotEmpty(mainTreeId)) {
			BusiSiteTree busiSiteTree = busiSiteTreeService.findByPK(mainTreeId);
			List<TransformerRunStatusVo> arresterReadTableList = transformerRunStatusMapper.exportPointTableData(mainTreeId,timeStamp,timeStampEnd);
			returnMap.put("fileName", String.format("%s%s", busiSiteTree.getName(),"运行工况监测数据"));
			returnMap.put("title", String.format("%s%s", busiSiteTree.getName(),"运行工况监测数据"));
			if (arresterReadTableList.isEmpty()) {
				arresterReadTableList = new ArrayList<>();
				arresterReadTableList.add(new TransformerRunStatusVo());
			}
			returnMap.put("data", arresterReadTableList);
		} else {
			throw new RuntimeException("主设备编码不正确");
		}
		return returnMap;
	}

    @Override
    public List<BaseCurveQueryDto> findSensorCodeList(LocalDate day) {
        return transformerRunStatusMapper.findSensorCodeList(day.atStartOfDay(), day.plusDays(1).atStartOfDay());
    }

    @Override
    public List<PointItemVo> findDaylyPointList(BaseCurveQueryDto sensorCode, LocalDate day, String subType) {
        return transformerRunStatusMapper.findDaylyPointList(sensorCode, subType, day.atStartOfDay(),
            day.plusDays(1).atStartOfDay());
    }

    @Override
    public PointItemDto getDaylyPointStatistics(BaseCurveQueryDto param) {
        return transformerRunStatusMapper.getDaylyPointStatistics(param);
    }

    @Override
    public PageInfo<TransformerRunStatusVo> transformerRunStatusCurrent(TransformerRunStatusDto params) {
        return PageHelper.startPage(params.getPage(), params.getLimit())
            .doSelectPageInfo(() -> transformerRunStatusMapper.transformerRunStatusCurrent(params));
    }

    @Override
    public List<TransformerRunStatusCountVo> getListEquInfo(LocalDate day) {
        return transformerRunStatusMapper.getListEquInfo(day.atStartOfDay(), day.plusDays(1).atStartOfDay());
    }


}
