package net.vtstar.zhongtong.avi;

import lombok.extern.slf4j.Slf4j;
import net.vtstar.scada.base.equipmgt.domain.EquipDataTemplateItem;
import net.vtstar.scada.base.equipmgt.mapper.EquipDataTemplateItemMapper;
import net.vtstar.user.mybatis.provider.WhereFilter;
import net.vtstar.zhongtong.avi.equipment.domain.PaintshopStation;
import net.vtstar.zhongtong.avi.equipment.mapper.PaintshopStationMapper;
import net.vtstar.zhongtong.avi.global.constant.Constant;
import net.vtstar.zhongtong.avi.monitoring.domain.Skid;
import net.vtstar.zhongtong.avi.monitoring.domain.enums.ProductionType;
import net.vtstar.zhongtong.avi.monitoring.domain.enums.StationState;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@ActiveProfiles("dev-yxh")
public class StationTest {

    @Autowired
    private PaintshopStationMapper stationMapper;
    @Autowired
    private EquipDataTemplateItemMapper itemMapper;
    @Resource(name = "redisTemplate")
    private ValueOperations valueOperations;

    @Test
    public void test() {
        List<EquipDataTemplateItem> tem = itemMapper.findByTemplateId(2);
        for (EquipDataTemplateItem item : tem) {
            item.setTemplateId(1);
            item.setFieldDesc("正转信号");
            item.setFunctionCode(1);
            item.setAddress(null);
            item.setId(null);
            itemMapper.insert(item);
            item.setFieldDesc("反转信号");
            itemMapper.insert(item);
        }
    }

    @Test
    public void stationTest() {

        List<EquipDataTemplateItem> list = itemMapper.findByTemplateId(2);

        for (EquipDataTemplateItem station : list) {
            String newCdoe;
            if (station.getId() <= 145) {
                continue;
            }
            String code = station.getFieldName();
            String[] split = code.split(":");
            String stationCode = split[2];
            String s = split[1];
            if (s.endsWith("F")){
                newCdoe = "station:turnF:" + stationCode;
            }else {
                newCdoe = "station:turnR:" + stationCode;
            }
            station.setFieldName(newCdoe);
            itemMapper.update(station);
        }
    }

    @Test
    public void createStation() {
        PaintshopStation station = new PaintshopStation();
        station.setWorkshopCode("1");
        station.setKeyStation(true);
        station.setStatus("1");
        for (int i = 1; i < 34; i++) {
            station.setCode("HDG5-" + i);
            stationMapper.insert(station);
        }
    }

    @Test
    public void tt(){

        PaintshopStation paintshopStation = (PaintshopStation) valueOperations.get(Constant.STATION_PREFIX + "HDG1-13");
        Skid skid = new Skid();
        skid.setBusNo("eeee");
        skid.setBusType(ProductionType.OWN);
        paintshopStation.setState(StationState.USE);
        paintshopStation.setSkid(skid);
        valueOperations.set("station:HDG1-13",paintshopStation);
    }
}
