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.Operation;
import net.vtstar.user.mybatis.provider.OrderBy;
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 net.vtstar.zhongtong.avi.sqlserver.domain.MesWorkOrderInfo;
import net.vtstar.zhongtong.avi.sqlserver.mapper.MesWorkOrderMapper;
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.RedisTemplate;
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.Set;
import java.util.stream.Collectors;

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

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

    @Autowired
    private MesWorkOrderMapper mesWorkOrderMapper;

    @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);
    }

    @Test
    public void skidTest() {
        List<MesWorkOrderInfo> mesWorkOrderInfos = mesWorkOrderMapper.findList(new WhereFilter() {{
            addFilter("status", Operation.EQUAL, "4");
            addOrderBy("crt_date", OrderBy.DESC);
        }}, MesWorkOrderInfo.class);
        Set<String> veKeys = redisTemplate.keys(Constant.SKID_PREFIX + "*");
        int num = 0;
        for (String veKey : veKeys) {
            Skid skid = (Skid)valueOperations.get(veKey);
            MesWorkOrderInfo mesWorkOrderInfo = mesWorkOrderInfos.get(num);
            num ++;
            skid.setWorkOrderNo(mesWorkOrderInfo.getWorkOrderNo());
            skid.setBusNo(mesWorkOrderInfo.getVehicleModel());
            valueOperations.set(veKey, skid);
        }
        Set<String> veKeyStation = redisTemplate.keys(Constant.STATION_PREFIX + "*");
        for (String key : veKeyStation){
            PaintshopStation station = (PaintshopStation) valueOperations.get(key);
            if (null != station.getSkid()){
                Skid skid = (Skid)valueOperations.get(Constant.SKID_PREFIX + station.getSkid().getRfid());
                station.setSkid(skid);
                station.setWorkOrderNo(skid.getWorkOrderNo());
                station.setBusNo(skid.getBusNo());
                valueOperations.set(key, station);
            }
        }
    }
}
