package com.shkj.wms.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shkj.common.constant.RedisConstant;
import com.shkj.common.core.domain.Result;
import com.shkj.plc.sdk.device.PLCReadAndWrite;
import com.shkj.plc.sdk.device.PlcOperate;
import com.shkj.wms.domain.BasePlcDps;
import com.shkj.wms.mapper.BasePlcDpsMapper;
import com.shkj.wms.plc.PlcInit;
import com.shkj.wms.service.IBasePlcDpsService;
import com.shkj.wms.vo.BasePlcDpsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 输送线空闲点位Service业务层处理
 *
 * @author zjx
 * @date 2024-05-06
 */
@Service
@Slf4j
public class BasePlcDpsServiceImpl extends ServiceImpl<BasePlcDpsMapper, BasePlcDps> implements IBasePlcDpsService {
    @Resource
    private BasePlcDpsMapper basePlcDpsMapper;


    @Autowired
    private PlcInit plcInit;

    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 查询输送线空闲点位
     *
     * @param id 输送线空闲点位主键
     * @return 输送线空闲点位
     */
    @Override
    public BasePlcDps selectBasePlcDpsById(Long id) {
        return basePlcDpsMapper.selectBasePlcDpsById(id);
    }

    /**
     * 查询输送线空闲点位列表
     *
     * @param basePlcDps 输送线空闲点位
     * @return 输送线空闲点位
     */
    @Override
    public List<BasePlcDpsVo> selectBasePlcDpsList(BasePlcDps basePlcDps) {

        PLCReadAndWrite plcReadAndWrite = new PLCReadAndWrite();
        List<BasePlcDpsVo> basePlcDpsVos = basePlcDpsMapper.selectBasePlcDpsList(basePlcDps);

        for (BasePlcDpsVo basePlcDpsVo : basePlcDpsVos) {
              PlcOperate plcOperate = plcInit.getPlcOperateByIp(basePlcDpsVo.getPlcIp());

            String plcPoint = basePlcDpsVo.getPlcPoint();

           Map<String, String> readMap = new HashMap(1);

            readMap.put(plcPoint, "Boolean");
            String readPoint = JSON.toJSONString(readMap);
            Map<String, Object> pointMap = plcReadAndWrite.readPlcData(plcOperate, readPoint);
            if (pointMap !=null&& pointMap.size()>0){
                if (pointMap.get(plcPoint).toString().equals("true")){
                    basePlcDpsVo.setPlcPointV("是");
                }else {
                    basePlcDpsVo.setPlcPointV("否");
                }
            }

            String freePoint = "stackerLock:";
            String stkState = String.valueOf(redisTemplate.opsForValue().get(freePoint + basePlcDpsVo.getTunnel()));
            if (stkState.equals("true")) {
                basePlcDpsVo.setWcsPlcDpsVo("是");
            } else {
                basePlcDpsVo.setWcsPlcDpsVo("否");
            }
        }
        return basePlcDpsVos;
    }

    /**
     * 新增输送线空闲点位
     *
     * @param basePlcDps 输送线空闲点位
     * @return 结果
     */
    @Override
    public Result<Boolean> insertBasePlcDps(BasePlcDps basePlcDps) {
        return basePlcDpsMapper.insertBasePlcDps(basePlcDps) > 0 ? Result.ok() : Result.err();
    }

    /**
     * 修改输送线空闲点位
     *
     * @param basePlcDps 输送线空闲点位
     * @return 结果
     */
    @Override
    public Boolean updateBasePlcDps(BasePlcDps basePlcDps) {
        String freePoint = "stackerLock:";
        redisTemplate.opsForValue().set(freePoint + basePlcDps.getPlcPoint(), "false");
        return true;
    }

    /**
     * 批量删除输送线空闲点位
     *
     * @param ids 需要删除的输送线空闲点位主键
     * @return 结果
     */
    @Override
    public int deleteBasePlcDpsByIds(Long[] ids) {
        return basePlcDpsMapper.deleteBasePlcDpsByIds(ids);
    }

    /**
     * 删除输送线空闲点位信息
     *
     * @param id 输送线空闲点位主键
     * @return 结果
     */
    @Override
    public int deleteBasePlcDpsById(Long id) {
        return basePlcDpsMapper.deleteBasePlcDpsById(id);
    }
}
