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.MesWorkOrderInfo;
import net.vtstar.zhongtong.avi.equipment.domain.PaintshopArea;
import net.vtstar.zhongtong.avi.equipment.domain.PaintshopStation;
import net.vtstar.zhongtong.avi.equipment.mapper.MesWorkOrderMapper;
import net.vtstar.zhongtong.avi.monitoring.domain.Bus;
import net.vtstar.zhongtong.avi.monitoring.domain.vo.CarStatisticsVO;
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 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 List<PaintshopArea> findBusLocation(String orderNo, String workOrderNo) {
        List<PaintshopArea> ar = new ArrayList<>();
        List<String> nos = new ArrayList<>();
        if (null != orderNo){
            List<MesWorkOrderInfo> info = workOrderMapper.findList(new WhereFilter() {{
                addFilter("sales_order_no", Operation.EQUAL, orderNo);
            }}, MesWorkOrderInfo.class);
            nos = info.stream().map(MesWorkOrderInfo::getWorkOrderNo).distinct().collect(Collectors.toList());
        }
        else {
            nos.add(workOrderNo);
        }
        List<PaintshopArea> areas = webStationCache.get("stationList", ArrayList.class);
        for (PaintshopArea area : areas) {
            PaintshopArea paintshopArea = area;
            Map<String, PaintshopStation> newMap = new HashMap<>();
            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 (value.getBusNo().equals(no)){
                        newMap.put(key,value);
                    }
                }
            }
            paintshopArea.setStationMap(newMap);
            ar.add(paintshopArea);
        }
        return ar;
    }

    public Bus getByBusNo(String busNo) {
        List<MesWorkOrderInfo> info = workOrderMapper.findList(new WhereFilter() {{
            addFilter("work_order_no", Operation.EQUAL, busNo);
        }}, 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() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy:MM:dd");
        Calendar instance = Calendar.getInstance();
        String format = dateFormat.format(instance.getTime());

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