package com.foreverwin.mesnac.integration.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.foreverwin.mesnac.common.erp.ZprodordconfStruIn;
import com.foreverwin.mesnac.common.erp.ZprodordconfStruOut;
import com.foreverwin.mesnac.common.erp.holders.TableOfZprodordconfStruInHolder;
import com.foreverwin.mesnac.common.erp.holders.TableOfZprodordconfStruOutHolder;
import com.foreverwin.mesnac.common.util.ERPAPI;
import com.foreverwin.mesnac.integration.mapper.IntegrationLogMapper;
import com.foreverwin.mesnac.integration.model.IntegrationLog;
import com.foreverwin.mesnac.integration.service.IntegrationLogService;
import com.foreverwin.modular.core.exception.BaseException;
import com.foreverwin.modular.core.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.rmi.RemoteException;
import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Leon.L
 * @since 2021-06-16
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class IntegrationLogServiceImpl extends ServiceImpl<IntegrationLogMapper, IntegrationLog> implements IntegrationLogService {


    @Autowired
    private IntegrationLogMapper integrationLogMapper;

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void saveIntegrationLog(IntegrationLog integrationLog) {
        integrationLogMapper.insert(integrationLog);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void updateIntegrationLog(IntegrationLog integrationLog) {
        integrationLogMapper.updateById(integrationLog);
    }

    @Override
    public String redoSend(String handle) {
        IntegrationLog byId = getById(handle);
        if (byId==null||!byId.getStatus().equals("R")){
            throw new BaseException("状态已变更，请刷新");
        }
        String param = byId.getParam();
        JSONObject jsonObject = JSONObject.parseObject(param);
        ZprodordconfStruIn struIn = JSON.toJavaObject(jsonObject, ZprodordconfStruIn.class);
        ZprodordconfStruIn[] ins = new ZprodordconfStruIn[1];
        ins[0] = struIn;
        TableOfZprodordconfStruInHolder inHolder = new TableOfZprodordconfStruInHolder(ins);
        ZprodordconfStruOut[] outs = new ZprodordconfStruOut[1];
        ZprodordconfStruOut struOut = new ZprodordconfStruOut();
        struOut.setAufnr(ins[0].getAufnr());
        struOut.setVornr(ins[0].getVornr());
        struOut.setAueru("1");
        struOut.setLmnga(ins[0].getLmnga());
        struOut.setRet("");
        struOut.setMsg("");
        outs[0] = struOut;
        TableOfZprodordconfStruOutHolder outHolder = new TableOfZprodordconfStruOutHolder(outs);
        String message = "";
        String status = "";
        try {
            ERPAPI.erpWebService().zmesProdordconf(inHolder, outHolder);
            status = outHolder.value[1].getRet();
            message = outHolder.value[1].getMsg();
            if (!status.equals("S")) {
                byId.setResultMessage("ERP接口返回错误消息:" + message);
                updateById(byId);
                return "ERP接口返回错误消息:" + message;
            }
            byId.setStatus(status);
            byId.setResultMessage(message);
            updateById(byId);
        } catch (RemoteException e) {
            byId.setRequestDateTime(LocalDateTime.now());
            byId.setResultMessage("ERP接口错误：" + e.getMessage());
            updateById(byId);
            return e.getMessage();
        }
        return null;

    }


}