package com.tykj.dev.device.user.cache;

import com.tykj.dev.device.user.subject.entity.Units;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Data
public class UnitsCache {

    private Map<Integer, Units> idMap;

    private Map<Integer,Units> areaIdMpa;

    public UnitsCache(List<Units> unitsList){
        this.idMap = unitsList.stream().collect(Collectors.toMap(Units::getUnitId, Function.identity()));
        this.areaIdMpa=unitsList.stream().collect(Collectors.toMap(Units::getAreaId,Function.identity()));
    }

    public Map<Integer, Units> getIdMap() {
        return idMap;
    }


    public Units findByAreaId(Integer areaId){
       return areaIdMpa.get(areaId);
    }

    public Units findById(Integer id) {
        return idMap.get(id);
    }

    public List<Units> findAll(){
        return new ArrayList<>(idMap.values());
    }

    public List<Units> findAllByAreaExhibitionId(Integer areaExhibitionId){
        return new ArrayList<>(idMap.values()).stream().filter(units -> units.getExhibitionId().equals(areaExhibitionId)).collect(Collectors.toList());
    }

    /**
     * 查询大于等新序号，小于原序号
     * @return
     */
    public List<Units> findListGeNewAndLtOriginal(Integer newOrder,Integer OriginalOrder){
       return new ArrayList<>(idMap.values()).stream().filter(units -> (units.getShowOrder()>=newOrder) &&(units.getShowOrder() < OriginalOrder)).collect(Collectors.toList());
    }
    /**
     * 查询大于原序号，小于等于新序号
     */
    public List<Units> findListLgOriginalAndLeNew(Integer newOrder,Integer OriginalOrder){
        return new ArrayList<>(idMap.values()).stream().filter(units -> (units.getShowOrder()<=newOrder) &&(units.getShowOrder() > OriginalOrder)).collect(Collectors.toList());
    }

    public UnitsCache refresh(List<Units> unitsList){
        this.idMap = unitsList.stream().collect(Collectors.toMap(Units::getUnitId, Function.identity()));
        this.areaIdMpa=unitsList.stream().collect(Collectors.toMap(Units::getAreaId,Function.identity()));
        return this;
    }
}
