package com.huatek.busi.service.impl;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

import com.huatek.busi.entity.BusiCableCurrentResult;
import com.huatek.torch.sql.beans.WherePrams;
import com.huatek.torch.sql.dao.impl.BaseDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.huatek.busi.dto.curve.IecBydworkParamPointDto;
import com.huatek.busi.entity.BusiSiteTree;
import com.huatek.busi.entity.IecBydworkParam;
import com.huatek.busi.mapper.IecBydworkParamMapper;
import com.huatek.busi.service.BusiEquipmentRelationshipService;
import com.huatek.busi.service.BusiSiteTreeService;
import com.huatek.busi.service.IecBydworkParamService;
import com.huatek.busi.vo.TransformerTmpVo;
import com.huatek.busi.vo.curve.IecBydworkParamPointVo;
import com.huatek.torch.frame.exception.OwlException;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName: IecBydworkParamServiceImpl
 * @Description: TODO.
 * @author: arno
 * @Email : arno_fu@huatek.com
 * @date: 2018-09-18 15:50:29
 * @version: 1.0
 */
@Slf4j
@Component
@Service(interfaceClass = IecBydworkParamService.class)
@Transactional(rollbackFor = Exception.class)
public class IecBydworkParamServiceImpl extends BaseDao<IecBydworkParam, String> implements IecBydworkParamService {

    @Autowired
    private IecBydworkParamMapper iecBydworkParamMapper;

    @Reference(check = false)
    private BusiSiteTreeService busiSiteTreeService;
    @Reference(check = false)
    private BusiEquipmentRelationshipService busiEquipmentRelationshipService;

    @Override
    public List<IecBydworkParam> getOneData(String siteId, String eqmCode, Integer stype, Date start, Date end,
                                            String type) {
        IecBydworkParam iecBydworkParam = new IecBydworkParam();
        iecBydworkParam.setSiteId(siteId);
        iecBydworkParam.setEqmCode(eqmCode);
        iecBydworkParam.setStype(stype);
        iecBydworkParam.setStartTime(start);
        iecBydworkParam.setStopTime(end);
        return iecBydworkParamMapper.getOneData(iecBydworkParam);
    }

    @Override
    public String geCabletIecType(String subTreeId, LocalDateTime cacDate) {

        // String equipmentId = mainTreeId;
        Integer iecType = null;
        List<String> transformerIds = Lists.newArrayList();

        // 获取关联变压器
        List<TransformerTmpVo> transformerList = busiEquipmentRelationshipService.getCurrentTransformerInfo(subTreeId);
        if (CollectionUtils.isEmpty(transformerList)) {
            log.error("未获取到关联变压器无法获取变压器工作状态，mainTreeId: {}，mainEquipmentName: {}", subTreeId);
            return "";
        }
        for (TransformerTmpVo transformer : transformerList) {
            IecBydworkParam iecBydworkParam = iecBydworkParamMapper.getIecType2(transformer.getTransformerId());
            if (iecBydworkParam == null || iecBydworkParam.getStype() == null) {
                continue;
            }
            if (iecType == null) {
                iecType = iecBydworkParam.getStype();
            } else if (iecType > iecBydworkParam.getStype().intValue()) {
                iecType = iecBydworkParam.getStype();
            }
        }
        if (iecType == null) {
            log.warn("未取到变压器工作状态，mainTreeId: {}，cacDate： {}", subTreeId, cacDate);
            return "";
        }
        return String.valueOf(iecType);
    }

    @Override
    public String getIecType(String mainTreeId, LocalDateTime cacDate) {
        BusiSiteTree busiSiteTree = busiSiteTreeService.findByPK(mainTreeId);
        if (busiSiteTree == null) {
            throw new OwlException("站点设备树节点不存在：" + mainTreeId);
        }
        // String equipmentId = mainTreeId;
        Integer iecType = null;
        List<String> transformerIds = Lists.newArrayList();

        // 获取关联变压器
        List<TransformerTmpVo> transformerList = busiEquipmentRelationshipService.getCurrentTransformerInfo(mainTreeId);
        if (CollectionUtils.isEmpty(transformerList)) {
            log.error("未获取到关联变压器无法获取变压器工作状态，mainTreeId: {}，mainEquipmentName: {}", mainTreeId,
                    busiSiteTree.getName());
            transformerList = busiEquipmentRelationshipService.getCurrentTransformerInfo2(mainTreeId);
//                return "";
        }

        for (TransformerTmpVo transformer : transformerList) {
            String transformerId = transformer.getTransformerId();
            IecBydworkParam iecBydworkParam = iecBydworkParamMapper.getIecType(transformerId, cacDate);
            if (iecBydworkParam == null) {
                iecBydworkParam = iecBydworkParamMapper.getIecType2(transformerId);
            }
            if (iecBydworkParam == null || iecBydworkParam.getStype() == null) {
                continue;
            }
            if (iecType == null) {
                iecType = iecBydworkParam.getStype();
            } else if (iecType > iecBydworkParam.getStype()) {
                iecType = iecBydworkParam.getStype();
            }
        }
        if (iecType == null) {
            log.warn("未取到变压器工作状态，mainTreeId: {}，cacDate： {}", mainTreeId, cacDate);
            return "";
        }
        return String.valueOf(iecType);
    }


    @Override
    public List<IecBydworkParam> getWorks(String type, String siteId, String eqmCode, Date startTime, Date endTime) {
        if (type.equals("1")) {
            return iecBydworkParamMapper.getBeforToCurrenttWork(siteId, eqmCode, startTime, endTime);
        } else if (type.equals("2")) {
            return iecBydworkParamMapper.getCurrnetWork(siteId, eqmCode, startTime, endTime);
        } else if (type.equals("3")) {
            return iecBydworkParamMapper.getCurrentToAfterWork(siteId, eqmCode, startTime, endTime);
        }
        return null;
    }

    @Override
    public IecBydworkParam getIecBydworkParam(String equipmentId, LocalDateTime time) {
        return iecBydworkParamMapper.getIecBydworkParam(equipmentId, time);
    }

    @Override
    public PageInfo<IecBydworkParamPointVo> queryIecBydworkParamPointList(IecBydworkParamPointDto param) {
        return PageHelper.startPage(param.getPage(), param.getLimit()).doSelectPageInfo(() -> {
            iecBydworkParamMapper.queryIecBydworkParamPointList(param);
        });
    }

    @Override
    public List<IecBydworkParamPointVo> getdworkParamByEqmCode(String eqmCode) {
        return iecBydworkParamMapper.getdworkParamByEqmCode(eqmCode);
    }

    @Override
    public List<IecBydworkParam> getIecBydworkParamWorks(String siteId, String eqmCode, Date startTime, Date endTime) {
        return iecBydworkParamMapper.getIecBydworkParamWorks(siteId, eqmCode, startTime, endTime);
    }

}
