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

import java.time.LocalDate;
import java.time.LocalDateTime;
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.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Maps;
import com.huatek.busi.common.SiteConstants.ExternalTypeEnum;
import com.huatek.busi.common.SiteConstants.SiteTreeEnum;
import com.huatek.busi.common.SiteConstants.TransfromerTreeEnum;
import com.huatek.busi.dto.EquipmentInfoDto;
import com.huatek.busi.dto.curve.BaseCurveQueryDto;
import com.huatek.busi.dto.curve.PointItemDto;
import com.huatek.busi.entity.BusiOnlineMonitorData;
import com.huatek.busi.entity.BusiSiteTree;
import com.huatek.busi.entity.TransformerReadTable;
import com.huatek.busi.mapper.TransformerReadTableMapper;
import com.huatek.busi.service.BusiSiteTreeService;
import com.huatek.busi.service.BusiSubTreeService;
import com.huatek.busi.service.impl.pointtable.AbstractPointTableEtlService;
import com.huatek.busi.service.transformer.TransformerReadTableService;
import com.huatek.busi.vo.ReadTableCountVo;
import com.huatek.busi.vo.TransformerReadTableVo;
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 = TransformerReadTableService.class)
@Transactional(rollbackFor = Exception.class)
public class TransformerReadTableServiceImpl extends AbstractPointTableEtlService<TransformerReadTable>
    implements TransformerReadTableService {

    @Autowired
    private TransformerReadTableMapper transformerReadTableMapper;

    @Autowired
    private BusiSubTreeService busiSubTreeService;
    
    @Autowired
    private BusiSiteTreeService busiSiteTreeService;

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

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

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

    @Override
    protected List<Map<String, Object>> queryMonitorDataList(LocalDateTime startTime, LocalDateTime endTime) {
        return transformerReadTableMapper.queryMonitorDataList(startTime, endTime);
    }

    @Override
    protected void saveBusiOnlineMonitorData(BusiOnlineMonitorData busiOnlineMonitorData, Map<String, Object> item) {
        busiOnlineMonitorData.setSubEquipmentType(TransfromerTreeEnum.TP_EQM.getValue());
        busiOnlineMonitorData.setExtType(ExternalTypeEnum.BYQ.getValue());
        busiOnlineMonitorData.setEquipmentType(SiteTreeEnum.BYQ_EQM.getValue());
        // 传感器信息
        EquipmentInfoDto equipmentInfo =
            busiSubTreeService.findEquipmentInfo(busiOnlineMonitorData.getSensorCode(), SiteTreeEnum.BYQ.getValue());
        if (equipmentInfo != null) {
            busiOnlineMonitorData.setSubTreeId(equipmentInfo.getSubTreeId());
            busiOnlineMonitorData.setMainEquipmentId(equipmentInfo.getMainEquipmentId());
            busiOnlineMonitorData.setSubEquipmentId(equipmentInfo.getSubEquipmentId());
        }
        super.saveBusiOnlineMonitorData(busiOnlineMonitorData, item);
    }

    @Override
    public Map<String, Object> findPointTablePageData(Map<String, Object> params) {
        if (params.get("page") != null && params.get("limit") != null) {
            PageHelper.startPage(page(params), limit(params));
        }
        Page<Map<String, Object>> page = transformerReadTableMapper.queryTransformerReadTablePage(params);
        return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, page.getTotal(), "", page.getResult());
    }

    @Override
    public Map<String, Object> exportPointTableData(Map<String, Object> mapParams) {
        Map<String, Object> returnMap = new HashMap<>();
        if (mapParams.containsKey("mainTreeId") && StringUtils.isNotEmpty(mapParams.get("mainTreeId") + "")) {
            BusiSiteTree busiSiteTree = busiSiteTreeService.findByPK(mapParams.get("mainTreeId") + "");
            List<TransformerReadTableVo> transformerReadTableList = transformerReadTableMapper.exportPointTableData(mapParams);
            returnMap.put("fileName", String.format("%s%s", busiSiteTree.getName(), "图片监测数据"));
            returnMap.put("title", String.format("%s%s", busiSiteTree.getName(), "图片监测数据"));
            if (transformerReadTableList.isEmpty()) {
            	transformerReadTableList = new ArrayList<>();
            	transformerReadTableList.add(new TransformerReadTableVo());
            }
            returnMap.put("data", transformerReadTableList);
        } else {
            throw new RuntimeException("主设备编码不正确");
        }
        return returnMap;
    }

    @Override
    public ReadTableCountVo getListEquInfo(String siteId, String sensorCode, LocalDateTime startTime, LocalDateTime endTime) {
        Map<String, Object> param = Maps.newLinkedHashMapWithExpectedSize(4);
        param.put("siteId", siteId);
        param.put("sensorCode", sensorCode);
        param.put("startTime", startTime);
        param.put("endTime", endTime);
        return transformerReadTableMapper.getListEquInfo(param);
    }

	@Override
	public List<Map<String, Object>> queryHistory(String sensorCode, LocalDateTime startTime, LocalDateTime endTime) {
		// TODO Auto-generated method stub
		return transformerReadTableMapper.queryHistory(sensorCode, startTime, endTime);
	}
}
