package net.vtstar.zhongtong.avi.monitoring.service;

import net.vtstar.user.mybatis.provider.Operation;
import net.vtstar.user.mybatis.provider.WhereFilter;
import net.vtstar.utils.CollecUtils;
import net.vtstar.zhongtong.avi.equipment.domain.PaintshopStation;
import net.vtstar.zhongtong.avi.global.constant.Constant;
import net.vtstar.zhongtong.avi.monitoring.domain.Skid;
import net.vtstar.zhongtong.avi.monitoring.domain.enums.StationState;
import net.vtstar.zhongtong.avi.sqlserver.domain.MesHZWorkOrderInfo;
import net.vtstar.zhongtong.avi.sqlserver.mapper.MesHZWorkOrderMapper;
import net.vtstar.zhongtong.avi.utils.EquipDataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

@Service
public class PDAService {

    @Resource(name = "redisTemplate")
    protected HashOperations hashOperations;
    @Resource(name = "redisTemplate")
    protected ValueOperations valueOperations;
    @Autowired
    private MesHZWorkOrderMapper mesWorkOrderMapper;
    @Resource(name = "redisTemplate")
    protected ValueOperations testValueOperations;
    @Resource(name = "laneStationCache")
    private Cache laneStationCache;

    //钣金上车绑定
    public boolean bound(String vehicleNo, String busNo, String workOrderNo) {

        // TODO: 2019-11-02 判断车身工单号是否在计划中
        List<MesHZWorkOrderInfo> list = mesWorkOrderMapper.findList(new WhereFilter() {{
            addFilter("WONo", Operation.EQUAL, workOrderNo);
        }}, MesHZWorkOrderInfo.class);
        if (CollecUtils.isEmpty(list)){
            return false;
        }
        if (null == busNo){
            busNo = "";
        }
        workOrderNo.replace("HZ", "TZ");
        String keyW = EquipDataUtil.getTestValueKey("ROTARY_ROLLER_BED_W");
        testValueOperations.set(keyW, null == testValueOperations.get(keyW) ? 0 : ((int)testValueOperations.get(keyW)) + 1);
        if (vehicleNo.equals("HZC1")) {
            valueOperations.set(Constant.HANZHUANG_VEHICLE_BUSNO_KEY + "rfid68", busNo);
            valueOperations.set(Constant.HANZHUANG_VEHICLE_WORKNO_KEY + "rfid68", workOrderNo);
        }
        if (vehicleNo.equals("HZC2")) {
            valueOperations.set(Constant.HANZHUANG_VEHICLE_BUSNO_KEY + "rfid69", busNo);
            valueOperations.set(Constant.HANZHUANG_VEHICLE_WORKNO_KEY + "rfid69", workOrderNo);
        }
        return true;
    }


    public void changeSkid(String sourceStation, String targetStation) {
        PaintshopStation source = (PaintshopStation) valueOperations.get(Constant.STATION_PREFIX + sourceStation);
        PaintshopStation target = (PaintshopStation) valueOperations.get(Constant.STATION_PREFIX + targetStation);

        Skid sourceSkid = source.getSkid();
        Assert.notNull(sourceSkid, sourceStation + "：此工位没有滑撬！");

        List<PaintshopStation> lands = new ArrayList<>();
        List<PaintshopStation> temStation = laneStationCache.get(target.getLaneId(), ArrayList.class);
        for (PaintshopStation land : temStation) {
            PaintshopStation s = (PaintshopStation) valueOperations.get(Constant.STATION_PREFIX + land.getCode());
            lands.add(s);
        }
        lands.sort(Comparator.comparing(v -> v.getSequence()));
        PaintshopStation end = lands.get(lands.size() - 1);
        for (int j = 0; j < lands.size() - 1; j++) {
            PaintshopStation tem = lands.get(j);
            PaintshopStation tem2 = lands.get(j + 1);
            tem.setState(tem2.getState());
            tem.setSkid(tem2.getSkid());
            tem.setWorkOrderNo(tem2.getWorkOrderNo());
            tem.setBusType(tem2.getBusType());
            tem.setBusNo(tem2.getBusNo());
        }
        end.setInTime(new Date());
        end.setBusNo(sourceSkid.getBusNo());
        end.setWorkOrderNo(sourceSkid.getWorkOrderNo());
        end.setState(StationState.USE);
        end.setBusType(sourceSkid.getBusType());
        end.setSkid(null);
        for (PaintshopStation station : lands){
            valueOperations.set(Constant.STATION_PREFIX + station.getCode(), station);
        }


        source.setBusNo(null);
        source.setWorkOrderNo(null);
        sourceSkid.setBusNo(null);
        sourceSkid.setWorkOrderNo(null);
        source.setSkid(null);
        source.setState(StationState.FREE);
        valueOperations.set(Constant.SKID_PREFIX + sourceSkid.getRfid(), sourceSkid);
        valueOperations.set(Constant.STATION_PREFIX + sourceStation, source);
    }

    public void repair(String vehicleNo, String busNo) {

        // TODO: 2019-11-02 平移车绑定车身号
        valueOperations.set(Constant.VEHICLE_PREFIX_REWORK, vehicleNo);
        valueOperations.set(Constant.VEHICLE_PREFIX_REWORK_BUSNO, busNo);
    }
}
