package com.brilliance.isc.common.bop.service.impl;

import com.brilliance.mda.runtime.mda.util.MdaUtils;
import com.brilliance.mda.runtime.mda.util.Strings;
import com.brilliance.mda.runtime.mda.util.Systems;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import com.brilliance.isc.common.bop.service.JshmapService;
import com.brilliance.isc.common.bopset.service.JshmodService;
import com.brilliance.isc.common.util.JshmodUtils;
import com.brilliance.isc.common.jshmod.common.JshObjType;
import com.brilliance.isc.common.jshmod.vo.Jshmod;
import com.brilliance.isc.common.util.SettleExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class JshmapServiceImpl implements JshmapService {

    private static Logger logger = LoggerFactory.getLogger(JshmapServiceImpl.class);

    @Autowired
    private JshmodService jshmodService;

    /**
     * //!保存结售汇申报数据前，重新赋值。
     * //!当重新赋值前后的数据保持一致时，继续保存交易；
     * //!当重新赋值前后的数据不一致时，保存中断，并在Save按钮处弹出提示信息。
     * //!===================================================================
     * //!返回值：无
     * //!变量：无
     * //!===================================================================
     *
     * @param jshmod
     */
    public void savIfNotChanged(Jshmod jshmod) {
        //!保存结售汇申报数据前，重新赋值。
        //!当重新赋值前后的数据保持一致时，继续保存交易；
        //!当重新赋值前后的数据不一致时，保存中断，并在Save按钮处弹出提示信息。
        //!===================================================================
        //!返回值：无
        //!变量：无
        //!===================================================================
        JshObjType jshObjType = jshmod.getCtlmod().getJshObjectMap().get(JshmodUtils.getJshSetTyp(jshmod));
        String bastyp = jshObjType.getBastyp();
        String dcltyp = jshObjType.getDcltyp();
        String vrftyp = jshObjType.getVrftyp();
        String subtyp = jshObjType.getSubtyp();

        if (MdaUtils.compareTo(Strings.toUpper(bastyp + dcltyp + vrftyp), "NNN") != 0) {
            Object recgrp = jshmod.getDbjgrp();
            if ("DBW".equals(subtyp)) {
                recgrp = jshmod.getDbwgrp();
            }
            String mag1 = Systems.macGen(MdaUtils.toJson(recgrp), 0);
            processMappDataMethod2(jshmod);
            String mag2 = Systems.macGen(MdaUtils.toJson(recgrp), 0);
            if (MdaUtils.compareTo(mag1, mag2) == 0) {
                logger.info("Continue saving transaction as JSHData has not changed");
            } else {
                //改为抛运行时异常，因为抛异常才能回滚数据库事务
                SettleExceptionUtils.sysDump("结售汇申报数据已发生变化，请核查");
            }
        }

    }

    public void processMappDataMethod2(Jshmod jshmod) {
        //TODO 按照TD的意思是升序执行多个函数，目前没发现，先待定执行一个函数
        jshmodService.mappingJSHDataAAAAAA(jshmod);
    }
}
