package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.busi.jx;

import cn.estsh.i3plus.ext.mes.pcn.api.busi.jx.IJxPlcExtService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.mes.pcn.serviceimpl.busi.dispatch.cachedispatch.WorkCellDataCacheDispatch;
import cn.estsh.i3plus.platform.plugin.opc.iservice.IOpcUAService;
import cn.estsh.i3plus.platform.plugin.opc.service.OpcUAService;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.MesPcnEnumUtil;
import cn.estsh.i3plus.pojo.hardswitch.bean.OpcUAParam;
import cn.estsh.i3plus.pojo.mes.bean.MesPlc;
import cn.estsh.i3plus.pojo.mes.repository.MesPlcRepository;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.MessageFormat;

/**
 * @Author: wangjie
 * @CreateDate: 2021/01/18 11:41 上午
 * @Description:
 **/
@Slf4j
@Service
public class JxPlcExtService implements IJxPlcExtService {

    @Autowired
    private MesPlcRepository plcRepository;

    @Override
    public MesPlc getPlcDb(String organizeCode, String plcCode) {
        if (StringUtils.isEmpty(organizeCode) || StringUtils.isEmpty(plcCode)) return null;
        return plcRepository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.PLC_CODE},
                new Object[]{organizeCode, CommonEnumUtil.IS_VAILD.VAILD.getValue(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), plcCode});
    }

    @Override
    public Object doReadOpcParamValue(MesPlc plc) {
        IOpcUAService opcService = new OpcUAService();
        String tagAddress = MessageFormat.format("{0}.{1}.{2}", plc.getChannel(), plc.getDevice(), plc.getTagAddress());
        OpcUAParam opcParam = new OpcUAParam();
        opcParam.setServerUrl(plc.getOpcUrl());
        opcParam.setTagAddress(tagAddress);
        opcParam.setNamespaceIndex(plc.getNameSpaceIndex());
        opcParam.setTagValueType(plc.getDataType());
        Object result = opcService.getOpcParamValue(opcParam);
        return result;
    }

    @Override
    public Boolean doWriteOpcParamValue(MesPlc plc, String value) {
        IOpcUAService opcService = new OpcUAService();
        String tagAddress = MessageFormat.format("{0}.{1}.{2}", plc.getChannel(), plc.getDevice(), plc.getTagAddress());
        OpcUAParam opcParam = new OpcUAParam();
        opcParam.setServerUrl(plc.getOpcUrl());
        opcParam.setTagAddress(tagAddress);
        opcParam.setNamespaceIndex(plc.getNameSpaceIndex());
        opcParam.setTagValueType(plc.getDataType());
        opcParam.setTagValue(value);
        Boolean result = opcService.editOpcParamValue(opcParam);
        return result;
    }

    @Override
    public MesPlc getPlcData(String organizeCode, String plcCode, String key) {
        WorkCellDataCacheDispatch dispatch = new WorkCellDataCacheDispatch.builder().key(key).item(plcCode).build().get();
        if (!StringUtils.isEmpty(dispatch.getValue())) return JSONObject.parseObject(dispatch.getValue(), MesPlc.class);
        MesPlc plc = getPlcDb(organizeCode, plcCode);
        if (null == plc) plc = new MesPlc();
        new WorkCellDataCacheDispatch.builder().key(key).item(plcCode).value(JSONObject.toJSONString(plc)).timeSecond(MesPcnEnumUtil.EXPIRE_TIME.NEVER.getValue()).org(organizeCode).build().save();
        return plc;
    }
}