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

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

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.Service;
import com.huatek.busi.dao.CapacitanceEquipmentSummaryDao;
import com.huatek.busi.entity.CapacitanceEquipmentSummary;
import com.huatek.busi.mapper.CapacitanceEquipmentSummaryMapper;
import com.huatek.busi.service.capacitance.CapacitanceEquipmentSummaryService;

/**
 * 避雷器-绝缘介质日统计表
 * 
 * @author buck_guo
 * @date 2019/07/16
 */
@Component
@Service(interfaceClass = CapacitanceEquipmentSummaryService.class)
@Transactional(rollbackFor = Exception.class)
public class CapacitanceEquipmentSummaryServiceImpl implements CapacitanceEquipmentSummaryService {

    @Autowired
    private CapacitanceEquipmentSummaryMapper capacitanceEquipmentSummaryMapper;
    @Autowired
    private CapacitanceEquipmentSummaryDao capacitanceEquipmentSummaryDao;

    @Override
    public Integer capacitranceSummary(LocalDate day) {
        // 删除数据
        capacitanceEquipmentSummaryMapper.deleteOneDayData(day);
        // 重新统计
        LocalDateTime startTime = day.atStartOfDay();
        LocalDateTime endTime = day.plusDays(1).atStartOfDay();
        List<Map<String, Object>> list = capacitanceEquipmentSummaryMapper.getOneDayData(startTime, endTime);
        if (CollectionUtils.isEmpty(list)) {
            return 0;
        }
        for (Map<String, Object> map : list) {
            // 统计维度
            String siteId = (String)map.get("siteId");
            String mainTreeId = (String)map.get("mainTreeId");
            String subTreeId = (String)map.get("subTreeId");
            String mainEquipmentId = (String)map.get("mainEquipmentId");
            String subEquipmentId = (String)map.get("subEquipmentId");
            // 保存记录
            CapacitanceEquipmentSummary entity = new CapacitanceEquipmentSummary();
            entity.setSiteId(siteId);
            entity.setMainTreeId(mainTreeId);
            entity.setSubTreeId(subTreeId);
            entity.setMainEquipmentId(mainEquipmentId);
            entity.setSubEquipmentId(subEquipmentId);
            entity.setDTime(day);
            entity.setCreateTime(LocalDateTime.now());

            // 获取统计值,分别统计各项
            map.put("phase", "A");
            Map<String, Object> data = capacitanceEquipmentSummaryMapper.getAvgData(map);
            entity.setAFullCurrent(getVal(data, "fullCurrent").toString());
            entity.setACapacitance(getVal(data, "capacitance").toString());
            entity.setALossFactor(getVal(data, "lossFactor").toString());

            map.put("phase", "B");
            data = capacitanceEquipmentSummaryMapper.getAvgData(map);
            entity.setBFullCurrent(getVal(data, "fullCurrent").toString());
            entity.setBCapacitance(getVal(data, "capacitance").toString());
            entity.setBLossFactor(getVal(data, "lossFactor").toString());

            map.put("phase", "C");
            data = capacitanceEquipmentSummaryMapper.getAvgData(map);
            entity.setCFullCurrent(getVal(data, "fullCurrent").toString());
            entity.setCCapacitance(getVal(data, "capacitance").toString());
            entity.setCLossFactor(getVal(data, "lossFactor").toString());
            capacitanceEquipmentSummaryDao.saveOrUpdate(entity);
        }
        return list.size();
    }

    private static BigDecimal getVal(Map<String, Object> data, String key) {
        if(data == null) {
            return BigDecimal.ZERO;
        }
        BigDecimal val = (BigDecimal)data.get(key);
        return val == null ? BigDecimal.ZERO : val;
    }
}
