package com.shkj.wms.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shkj.common.constant.RedisConstant;
import com.shkj.common.core.domain.Result;
import com.shkj.common.core.domain.entity.SysDictData;
import com.shkj.common.utils.MessageUtils;
import com.shkj.system.service.ISysDictDataService;
import com.shkj.wms.domain.BusinPlcStatus;
import com.shkj.wms.domain.BusinStockInv;
import com.shkj.wms.mapper.BusinStockInvMapper;
import com.shkj.wms.service.IBusinStockInvService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.shkj.wms.mapper.BusinPlcExceptionMapper;
import com.shkj.wms.domain.BusinPlcException;
import com.shkj.wms.service.IBusinPlcExceptionService;

import javax.annotation.Resource;

/**
 * PLC异常内容Service业务层处理
 *
 * @author zjx
 * @date 2023-09-27
 */
@Service
public class BusinPlcExceptionServiceImpl  extends ServiceImpl<BusinPlcExceptionMapper, BusinPlcException> implements IBusinPlcExceptionService
{
    @Resource
    private BusinPlcExceptionMapper businPlcExceptionMapper;

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private ISysDictDataService iSysDictDataService;

    /**
     * 查询PLC异常内容
     *
     * @param id PLC异常内容主键
     * @return PLC异常内容
     */
    @Override
    public Result<BusinPlcException> selectBusinPlcExceptionById(Long id)
    {
        return Result.ok().data(businPlcExceptionMapper.selectBusinPlcExceptionById(id));
    }

    /**
     * 查询PLC异常内容列表
     *
     * @param businPlcException PLC异常内容
     * @return PLC异常内容
     */
    @Override
    public List<BusinPlcException> selectBusinPlcExceptionList(BusinPlcException businPlcException)
    {
        return businPlcExceptionMapper.selectBusinPlcExceptionList(businPlcException);
    }

    /**
     * 新增PLC异常内容
     *
     * @param businPlcException PLC异常内容
     * @return 结果
     */
    @Override
    public Result<Boolean> insertBusinPlcException(BusinPlcException businPlcException)
    {
        return businPlcExceptionMapper.insertBusinPlcException(businPlcException)>0?Result.ok():Result.err();
    }

    /**
     * 修改PLC异常内容
     *
     * @param businPlcException PLC异常内容
     * @return 结果
     */
    @Override
    public Result<Boolean> updateBusinPlcException(BusinPlcException businPlcException)
    {
        return businPlcExceptionMapper.updateBusinPlcException(businPlcException)>0?Result.ok():Result.err();
    }

    /**
     * 批量删除PLC异常内容
     *
     * @param ids 需要删除的PLC异常内容主键
     * @return 结果
     */
    @Override
    public Result<Boolean> deleteBusinPlcExceptionByIds(Long[] ids)
    {
        return businPlcExceptionMapper.deleteBusinPlcExceptionByIds(ids)>0?Result.ok():Result.err();
    }

    /**
     * 删除PLC异常内容信息
     *
     * @param id PLC异常内容主键
     * @return 结果
     */
    @Override
    public int deleteBusinPlcExceptionById(Long id)
    {
        return businPlcExceptionMapper.deleteBusinPlcExceptionById(id);
    }

    @Override
    public List<BusinPlcException> getExecMsgFromRedis() {

        SysDictData sysDictData = new SysDictData();
        sysDictData.setDictType("plc_devtype");
        List<SysDictData> sysDictDataList = iSysDictDataService.selectDictDataList(sysDictData);
        List<BusinPlcException> allList = new ArrayList<>();
        for (SysDictData dictData : sysDictDataList) {
            List<BusinPlcException> businPlcExceptionList = (List<BusinPlcException>)redisTemplate.opsForValue().get(RedisConstant.redisPlcExecMsg + dictData.getDictValue());
            if(businPlcExceptionList != null){
                for (BusinPlcException businPlcException : businPlcExceptionList) {
                    businPlcException.setExecMsg(MessageUtils.message(businPlcException.getExecMsg()));
                }
                allList.addAll(businPlcExceptionList);
            }
        }
        return allList;
    }

    @Override
    public BusinPlcStatus getPlcStatusFromRedis() {
        //输送线状态
        BusinPlcStatus businPlcStatus =(BusinPlcStatus)redisTemplate.opsForValue().get(RedisConstant.redisPlcStatus);

        //堆垛机状态
        BusinPlcStatus businStkStatus =(BusinPlcStatus)redisTemplate.opsForValue().get(RedisConstant.redisStkStatus);
        if (businStkStatus != null){

            if (businStkStatus.getStkFault() !=null){
                businPlcStatus.setStkFault(businStkStatus.getStkFault());
            }

            if (businStkStatus.getStkFault() !=null){
                businPlcStatus.setStkPause(businStkStatus.getStkPause());
            }
            if (businStkStatus.getStkWait() !=null){
                businPlcStatus.setStkWait(businStkStatus.getStkWait());
            }

            if (businStkStatus.getStkRun() !=null){
                businPlcStatus.setStkRun(businStkStatus.getStkRun());
            }

            if (businStkStatus.getStkStop() !=null){
                businPlcStatus.setStkStop(businStkStatus.getStkStop());
            }
        }


        return businPlcStatus;
    }


}
