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

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.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.huatek.busi.common.SiteConstants.ArresterTreeEnum;
import com.huatek.busi.common.SiteConstants.ExternalTypeEnum;
import com.huatek.busi.common.SiteConstants.SiteTreeEnum;
import com.huatek.busi.dto.ArresterReadTableDto;
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.ArresterReadTable;
import com.huatek.busi.entity.BusiOnlineMonitorData;
import com.huatek.busi.entity.BusiSiteTree;
import com.huatek.busi.mapper.ArresterReadTableMapper;
import com.huatek.busi.service.BusiSiteTreeService;
import com.huatek.busi.service.BusiSubTreeService;
import com.huatek.busi.service.arrester.ArresterReadTableService;
import com.huatek.busi.service.impl.pointtable.AbstractPointTableEtlService;
import com.huatek.busi.vo.ArresterReadTableVo;
import com.huatek.busi.vo.ReadTableCountVo;
import com.huatek.busi.vo.curve.PointItemVo;
import com.huatek.torch.frame.tools.ConstantUtil;
import com.huatek.torch.frame.tools.ResultUtil;

/**
 * 避雷器读表、拍照数据 ServiceImpl
 *
 * @author anker_cheng
 * @date 2019-8-22 16:35:02
 */
@Component
@Service(interfaceClass = ArresterReadTableService.class)
@Transactional(rollbackFor = Exception.class)
public class ArresterReadTableServiceImpl extends AbstractPointTableEtlService<ArresterReadTable>
    implements ArresterReadTableService {

    @Autowired
    private ArresterReadTableMapper arresterReadTableMapper;

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

    @Override
    public PageInfo<ArresterReadTableVo> findArresterReadTablePage(ArresterReadTableDto arresterReadTableDto) {
        return PageHelper.startPage(arresterReadTableDto.getPage(), arresterReadTableDto.getLimit())
            .doSelectPageInfo(() -> arresterReadTableMapper.selectArresterReadTablePage(arresterReadTableDto));
    }

    /**
     * 数据输入-避雷器读表、拍照原始数据查询
     */
    @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 = arresterReadTableMapper.findArresterReadTablePageData(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<ArresterReadTableVo> arresterReadTableList = arresterReadTableMapper.exportPointTableData(mapParams);
            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 ArresterReadTableVo());
            }
            returnMap.put("data", arresterReadTableList);
        } else {
            throw new RuntimeException("主设备编码不正确");
        }
        return returnMap;
    }

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

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

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

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

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

    @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 arresterReadTableMapper.getListEquInfo(param);
    }

	@Override
	public List<Map<String, Object>> queryHistory(String sensorCode, LocalDateTime startTime, LocalDateTime endTime) {
		return arresterReadTableMapper.queryHistory(sensorCode,  startTime,  endTime);
	}
}