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

import net.vtstar.user.mybatis.provider.Operation;
import net.vtstar.user.mybatis.provider.WhereFilter;
import net.vtstar.zhongtong.avi.equipment.domain.PaintshopArea;
import net.vtstar.zhongtong.avi.equipment.domain.PaintshopStation;
import net.vtstar.zhongtong.avi.monitoring.domain.Bus;
import net.vtstar.zhongtong.avi.monitoring.domain.vo.CarStatisticsVO;
import net.vtstar.zhongtong.avi.sqlserver.domain.MesWorkOrderInfo;
import net.vtstar.zhongtong.avi.sqlserver.mapper.MesWorkOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class AreaService {

    @Resource(name = "redisTemplate")
    protected ValueOperations valueOperations;
    @Resource(name = "redisTemplate")
    private RedisTemplate redisTemplate;
    @Resource(name = "webStationCache")
    private Cache webStationCache;

    @Autowired
    private MesWorkOrderMapper workOrderMapper;

    public List<PaintshopArea> getInfoByAreaNo(String areaNo) {
        List<PaintshopArea> areas = webStationCache.get("stationList", ArrayList.class);
        if (null != areaNo) {
            for (PaintshopArea area : areas) {
                if (areaNo.equals(area.getCode())) {
                    ArrayList<PaintshopArea> paintshopAreas = new ArrayList<>();
                    paintshopAreas.add(area);
                    return paintshopAreas;
                }
            }
        } else {
            return areas;
        }
        return areas;
    }

    public Map<String, PaintshopStation> findBusLocation(String orderNo, String workOrderNo) {
        if (StringUtils.isEmpty(orderNo) && StringUtils.isEmpty(workOrderNo)) {
            return new HashMap<>();
        }

        List<PaintshopArea> ar = new ArrayList<>();
        List<String> nos = new ArrayList<>();
        if (!StringUtils.isEmpty(orderNo)) {
            List<MesWorkOrderInfo> info = workOrderMapper.findList(new WhereFilter() {{
                addFilter("sales_order_no", Operation.EQUAL, orderNo);
                addFilter("status", Operation.EQUAL, "4");
            }}, MesWorkOrderInfo.class);
            nos = info.stream().map(MesWorkOrderInfo::getWorkOrderNo).distinct().collect(Collectors.toList());
        } else {
            nos.add(workOrderNo);
        }
        List<PaintshopArea> areas = webStationCache.get("stationList", ArrayList.class);
        Map<String, PaintshopStation> newMap = new HashMap<>();
        for (PaintshopArea area : areas) {
            Map<String, PaintshopStation> map = area.getStationMap();
            for (Map.Entry<String, PaintshopStation> entry : map.entrySet()) {
                PaintshopStation value = entry.getValue();
                String key = entry.getKey();
                for (String no : nos) {
                    if (null == value.getBusNo()) {
                        continue;
                    }
                    if (value.getBusNo().equals(no)) {
                        newMap.put(key, value);
                    }
                }
            }
        }
        return newMap;
    }

    public Bus getByBusNo(String busNo) {
        List<MesWorkOrderInfo> info = workOrderMapper.findList(new WhereFilter() {{
            addFilter("work_order_no", Operation.EQUAL, busNo);
            addFilter("status", Operation.EQUAL, "4");
        }}, MesWorkOrderInfo.class);
        if (CollectionUtils.isEmpty(info)) {
            return null;
        } else {
            MesWorkOrderInfo orderInfo = info.get(0);
            Bus bus = new Bus();
            bus.setBusType(orderInfo.getVehicleModel());
            bus.setLine(orderInfo.getProductionLine());
            bus.setOrderSize(new BigDecimal(orderInfo.getOrderQuantity()));
            bus.setUnit(orderInfo.getUnit());
            bus.setShop(orderInfo.getWorkshop());
            bus.setPlanOrderNo(orderInfo.getMasterWorkOrderNo());
            bus.setSaleOrderNo(orderInfo.getSalesOrderNo());
            bus.setCode(orderInfo.getWorkOrderNo());
            return bus;
        }
    }

    public List<CarStatisticsVO> getBusSta() {

        List<CarStatisticsVO> sta = webStationCache.get("busSta", ArrayList.class);
        return sta;
    }
}
