package com.shkj.wms.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * 巷道任务工具
 *
 * @author voidlove
 * @date 2022-05-04
 */
public class TunnelTaskUtil {
    //构造函数私有化
    private TunnelTaskUtil() {
    }

    private static TunnelTaskUtil taskUtil = null;

    private  RedisTemplate redisTemplate;

    //静态实例化
    public static TunnelTaskUtil getInstance() {
        if (taskUtil == null) {
            taskUtil = new TunnelTaskUtil();
        }
        return taskUtil;
    }

    //入库任务信息
    private Map<String, Integer> inboundTaskMap = new HashMap<>();

    //获取巷道入库任务数
    public Integer getInboundTaskNum(String tunnel) {
        if (!this.inboundTaskMap.containsKey(tunnel)) {
            this.inboundTaskMap.put(tunnel, 0);
            //redisTemplate.opsForHash().put("inboundTaskMap",tunnel,inboundTaskMap.get(tunnel));
        }
        return this.inboundTaskMap.get(tunnel);
    }

    //增加巷道入库任务数
    public void addInboundTaskNum(String tunnel) {
        this.setInboundTaskNum(tunnel,1);
        this.setTotalTaskNum(tunnel,1);
    }

    //递减巷道入库任务数
    public void subInboundTaskNum(String tunnel) {
        this.setInboundTaskNum(tunnel,-1);
        this.setTotalTaskNum(tunnel,-1);
    }

    private void setInboundTaskNum(String tunnel, Integer num) {
        if (this.inboundTaskMap.containsKey(tunnel)) {
            Integer curTaskNum = this.inboundTaskMap.get(tunnel);
            Integer taskNum = curTaskNum + num;
            if (taskNum >= 0) {
                this.inboundTaskMap.put(tunnel, taskNum);
                redisTemplate.opsForHash().put("inboundTaskMap",tunnel,inboundTaskMap.get(tunnel));
            }
        } else {
            if (num >= 0) {
                this.inboundTaskMap.put(tunnel, num);
                redisTemplate.opsForHash().put("inboundTaskMap",tunnel,inboundTaskMap.get(tunnel));
            }
        }
    }

    //出库任务信息
    private Map<String, Integer> outboundTaskMap = new HashMap<>();

    //获取巷道出库任务数
    public Integer getOutboundTaskNum(String tunnel) {
        if (!this.outboundTaskMap.containsKey(tunnel)) {
            this.outboundTaskMap.put(tunnel, 0);
            redisTemplate.opsForHash().put("outboundTaskMap",tunnel,outboundTaskMap.get(tunnel));
        }
        return this.outboundTaskMap.get(tunnel);
    }

    //递增巷道出库任务数
    public void addOutboundTaskNum(String tunnel) {
        this.setOutboundTaskNum(tunnel,1);
        this.setTotalTaskNum(tunnel,1);
    }

    //递减巷道出库任务数
    public void subOutboundTaskNum(String tunnel) {
        this.setOutboundTaskNum(tunnel,-1);
        this.setTotalTaskNum(tunnel,-1);
    }

    private void setOutboundTaskNum(String tunnel, Integer num) {
        if (this.outboundTaskMap.containsKey(tunnel)) {
            Integer curTaskNum = this.outboundTaskMap.get(tunnel);
            Integer taskNum = curTaskNum + num;
            if (taskNum >= 0) {
                this.outboundTaskMap.put(tunnel, taskNum);
                redisTemplate.opsForHash().put("outboundTaskMap",tunnel,outboundTaskMap.get(tunnel));
            }
        } else {
            if (num >= 0) {
                this.outboundTaskMap.put(tunnel, num);
                redisTemplate.opsForHash().put("outboundTaskMap",tunnel,outboundTaskMap.get(tunnel));
            }
        }
    }

    //总任务信息
    private Map<String, Integer> totalTaskMap = new HashMap<>();

    //获取巷道总任务数
    public Integer getTotalTaskNum(String tunnel) {
        if (!this.totalTaskMap.containsKey(tunnel)) {
            this.totalTaskMap.put(tunnel, 0);
            //redisTemplate.opsForHash().put("totalTaskMap",tunnel,totalTaskMap);
        }
        return this.totalTaskMap.get(tunnel);
    }

    private void setTotalTaskNum(String tunnel, Integer num) {
        if (this.totalTaskMap.containsKey(tunnel)) {
            Integer curTaskNum = this.totalTaskMap.get(tunnel);
            Integer taskNum = curTaskNum + num;
            if (taskNum >= 0) {
                this.totalTaskMap.put(tunnel, taskNum);
              //  redisTemplate.opsForHash().put("totalTaskMap",tunnel,totalTaskMap);
            }
        } else {
            if (num >= 0) {
                this.totalTaskMap.put(tunnel, num);
               // redisTemplate.opsForHash().put("totalTaskMap",tunnel,totalTaskMap);
            }
        }
    }

    //最后任务类型
    private Map<String, String> lastTaskTypeMap = new HashMap<>();

    //获取巷道最近任务类型
    public String getLastTaskType(String tunnel) {
        if (!this.lastTaskTypeMap.containsKey(tunnel)) {
            this.lastTaskTypeMap.put(tunnel, "");
        }
        return this.lastTaskTypeMap.get(tunnel);
    }

    //设置巷道最近任务类型
    public void setLastTaskType(String tunnel, String taskType) {
        this.lastTaskTypeMap.put(tunnel, taskType);
    }

}
