package com.huatek.busi.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.huatek.busi.entity.EquipmentEvaluateStatus;
import com.huatek.busi.entity.TransformerEvaluateStatus;
import com.huatek.busi.mapper.TransformerEvaluateMapper;
import com.huatek.busi.service.TransformerEvaluateService;
import lombok.extern.slf4j.Slf4j;
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 java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Slf4j
@Component
@Service(interfaceClass = TransformerEvaluateService.class)
@Transactional(rollbackFor = Exception.class)
public class TransformerEvaluateServiceImpl implements TransformerEvaluateService {
    @Autowired
    TransformerEvaluateMapper transformerEvaluateMapper;
    @Override
    public List<TransformerEvaluateStatus> findEvaluateStatus() {
        List<TransformerEvaluateStatus> findList = transformerEvaluateMapper.findEvaluateStatus();
       //使用迭代器移除
        Iterator<TransformerEvaluateStatus> iterator = findList.iterator();
        while (iterator.hasNext()){
            TransformerEvaluateStatus x = iterator.next();
                if (StringUtils.isEmpty(x.getHealthIndex())||
                    StringUtils.isEmpty(x.getScore())){
                    iterator.remove();
            }
        }
        //使用foreach遍历移除
//        List<TransformerEvaluateStatus> findList_remove = new ArrayList<>();
//        for (TransformerEvaluateStatus transformerEvaluateStatus: findList) {
//            if (StringUtils.isEmpty(transformerEvaluateStatus.getHealthIndex()) ||
//                    StringUtils.isEmpty(transformerEvaluateStatus.getScore())){
//                   //将要移除的对象添加到新的数组里,最后统一移除
//                    findList_remove.add(transformerEvaluateStatus);
//            }
//        }
//        //统一移除
//        findList.removeAll(findList_remove);
        return findList;
    }

    @Override
    public List<EquipmentEvaluateStatus> findEquipmentEvaluateStatus() {
        List<EquipmentEvaluateStatus> findList = transformerEvaluateMapper.findEquipmentEvaluateStatus();
        List<EquipmentEvaluateStatus> findList_remove = new ArrayList<>();
        int count1 = 0;
        int count2 = 0;
        int count3 = 0;
        int count4 = 0;
        int count5 = 0;
        for (EquipmentEvaluateStatus equipmentEvaluateStatus: findList) {
            if (StringUtils.isEmpty(equipmentEvaluateStatus.getHealthIndex()) ||
                    StringUtils.isEmpty(equipmentEvaluateStatus.getScore())){
                   //将要移除的对象添加到新的数组里,最后统一移除
                    findList_remove.add(equipmentEvaluateStatus);
            }
            String evaluate = equipmentEvaluateStatus.getHealthIndex();
            if (evaluate!=null){
                if (evaluate.equals("优秀")){
                    count1 += 1;
                    equipmentEvaluateStatus.setNumber(count1);
                }
                if (evaluate.equals("良好")){
                    count2 += 1;
                    equipmentEvaluateStatus.setNumber(count2);
                }
                if (evaluate.equals("一般")){
                    count3 += 1;
                    equipmentEvaluateStatus.setNumber(count3);
                }
                if (evaluate.equals("故障")){
                    count4 += 1;
                    equipmentEvaluateStatus.setNumber(count4);
                }
                if (evaluate.equals("严重故障")){
                    count5 += 1;
                    equipmentEvaluateStatus.setNumber(count5);
                }

            }
        }
        //统一移除
        findList.removeAll(findList_remove);

        return findList;
    }
}
