package com.shkj.wms.utils;

import com.shkj.wms.bo.TunnelTask;
import com.shkj.wms.mapper.BusinDataTaskMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 巷道任务工具
 *
 * @author voidlove
 * @date 2022-05-04
 */
@Component
@Slf4j
public class TunnelTaskUtil {
    @Resource
    private RedisTemplate redisTemplate;

    @Autowired
    private BusinDataTaskMapper businDataTaskMapper;


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

    //获取巷道入库任务数
    public Integer getInboundTaskNum(String tunnel) {
        if (!this.inboundTaskMap.containsKey(tunnel)) {
            this.inboundTaskMap.put(tunnel, 0);
            redisTemplate.opsForValue().set("inboundTaskMap",inboundTaskMap);
        }
        Map redistaskmap=new HashMap();
        redistaskmap= (Map) redisTemplate.opsForValue().get("inboundTaskMap");
        if (redisTemplate!=null){
        return (Integer) redistaskmap.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);
    }

    public 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.opsForValue().set("inboundTaskMap",this.inboundTaskMap);
            }
        } else {
            if (num >= 0) {
                this.inboundTaskMap.put(tunnel, num);
                redisTemplate.opsForValue().set("inboundTaskMap",this.inboundTaskMap);
            }
        }
    }

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

    //获取巷道出库任务数
    public Integer getOutboundTaskNum(String tunnel) {
        if (!this.outboundTaskMap.containsKey(tunnel)) {
            this.outboundTaskMap.put(tunnel, 0);
            redisTemplate.opsForValue().set("outboundTaskMap",outboundTaskMap);
        }
        Map redistaskmap=new HashMap();
        redistaskmap= (Map) redisTemplate.opsForValue().get("outboundTaskMap");
        if (redisTemplate!=null){
            return (Integer) redistaskmap.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);
    }

    public 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.opsForValue().set("outboundTaskMap",outboundTaskMap);
            }
        } else {
            if (num >= 0) {
                this.outboundTaskMap.put(tunnel, num);
                redisTemplate.opsForValue().set("outboundTaskMap",this.outboundTaskMap);
            }
        }
    }

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

    //获取当前巷道产品数量
    public Integer getGoodLocationNum(String goodNo, String tunnel){
        List<TunnelTask> taskCountByTunnel = businDataTaskMapper.getGoodLocationNum(goodNo);
        if (ObjectUtils.isNotEmpty(taskCountByTunnel)){
            for (TunnelTask tunnelTask : taskCountByTunnel){
                if (tunnel.equals(tunnelTask.getTunnel())){
                    return tunnelTask.getCount();
                }
            }
        }
        return 0;
    }

    //获取巷道总任务数
    public Integer getTotalTaskNum(String tunnel,int i) {
        List<TunnelTask> taskCountByTunnel = new ArrayList<>();
        if (i==0){
            taskCountByTunnel = businDataTaskMapper.getTaskCountByTunnel();
        }else {
            taskCountByTunnel = businDataTaskMapper.getOutTaskCountByTunnel();
        }

        if (ObjectUtils.isNotEmpty(taskCountByTunnel)){
            for (TunnelTask tunnelTask : taskCountByTunnel){
                if (tunnel.equals(tunnelTask.getTunnel())){
                    return tunnelTask.getCount();
                }
            }
        }
        return 0;
//        if (!this.totalTaskMap.containsKey(tunnel)) {
//            this.totalTaskMap.put(tunnel, 0);
//            redisTemplate.opsForValue().set("totalTaskMap",totalTaskMap);
//        }
//        Map redistaskmap=new HashMap();
//        redistaskmap= (Map) redisTemplate.opsForValue().get("totalTaskMap");
//        if (redistaskmap!=null){
//            return (Integer) redistaskmap.get(tunnel);
//        }
//        return this.totalTaskMap.get(tunnel);
    }

    public 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.opsForValue().set("totalTaskMap",totalTaskMap);
            }
        } else {
            if (num >= 0) {
                this.totalTaskMap.put(tunnel, num);
                redisTemplate.opsForValue().set("totalTaskMap",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);
    }



}
