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.Vehicle;
import net.vtstar.zhongtong.avi.sqlserver.domain.MesWorkOrderInfo;
import net.vtstar.zhongtong.avi.sqlserver.mapper.MesWorkOrderMapper;
import net.vtstar.zhongtong.avi.utils.EquipDataUtil;
import org.springframework.beans.factory.annotation.Autowired;
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.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class PDAService {

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

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

        // TODO: 2019-11-02 判断车身工单号是否在计划中
        List<MesWorkOrderInfo> list = mesWorkOrderMapper.findList(new WhereFilter() {{
            addFilter("work_order_no", Operation.EQUAL, workOrderNo);
            addFilter("status", Operation.EQUAL, 4);
        }}, MesWorkOrderInfo.class);
        if (CollecUtils.isEmpty(list)){
            return false;
        }
        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();
        Skid targetSkid = target.getSkid();
        Assert.notNull(sourceSkid, sourceStation + "：此工位没有滑撬！");
        Assert.notNull(targetSkid, targetStation + "：此工位没有滑撬！");

        source.setBusNo(null);
        source.setWorkOrderNo(null);
        sourceSkid.setBusNo(null);
        sourceSkid.setWorkOrderNo(null);
        source.setSkid(sourceSkid);

        target.setBusNo(sourceSkid.getBusNo());
        target.setWorkOrderNo(sourceSkid.getWorkOrderNo());
        targetSkid.setBusNo(sourceSkid.getBusNo());
        targetSkid.setWorkOrderNo(sourceSkid.getWorkOrderNo());
        target.setSkid(targetSkid);

        valueOperations.set(Constant.SKID_PREFIX + sourceSkid.getRfid(), source);
        valueOperations.set(Constant.STATION_PREFIX + sourceStation, source);
        valueOperations.set(Constant.STATION_PREFIX + targetStation, target);
    }

    public void repair(String vehicleNo, String busNo) {

        // TODO: 2019-11-02 平移车绑定车身号

    }
}
