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


import com.brilliance.mda.runtime.mda.util.MdaUtils;
import com.brilliance.isc.bo.Pbl;
import com.brilliance.isc.common.bop.service.PblmodService;
import com.brilliance.isc.common.bop.service.Rmb2180Service;
import com.brilliance.isc.common.codetable.service.CodetableService;
import com.brilliance.isc.common.component.DyncDaoService;
import com.brilliance.isc.common.rmbbop.entity.*;
import com.brilliance.isc.common.rmbbop.model.Tgagrp;
import com.brilliance.isc.common.rmbbop.vo.Rmb2180;
import com.brilliance.isc.common.sysmod.SysmodService;
import com.brilliance.isc.mda.dao.PblMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service("tga")
public class Rmb2180ServiceImpl implements Rmb2180Service {
    @Autowired
    private DyncDaoService dyncDaoService;
    @Autowired
    private PblmodService pblmodService;
    @Autowired
    private SysmodService sysmodService;
    @Autowired
    private PblMapper pblMapper;

    @Override
    public void save(Rmb2180 rmb2180) {
        String objinr = rmb2180.getRecgrp().getAds().getInr();
        if (!MdaUtils.isEmpty(objinr)) {
            Pbl pbl = pblMapper.selectByObjtypAndObjinr("TGA", objinr);
            rmb2180.getPblmod().setWrkpbl(pbl);
        }
        pblmodService.storeAllinBusTx(rmb2180);
    }

    @Override
    public void updrmb(Rmb2180 rmb2180) {

    }

    @Override
    public void saveChildData(Rmb2180 rmb2180) {
        Tgagrp recgrp = rmb2180.getRecgrp();
        pblmodService.setListData(rmb2180.getPblmod(), recgrp.getTgb());
        recgrp.getAds().setTgblist(recgrp.getTgb().size());
        //存储Tgb数据
        for (Tgb tgb : recgrp.getTgb()) {
            tgb.setObjinr(recgrp.getAds().getInr());
            tgb.setObjtyp(rmb2180.getObjtyp());
            if (dyncDaoService.dyncReadNoDataByInr(tgb))
                sysmodService.sdbUpdateWithLog(tgb);
            else
                sysmodService.sdbInsertWithLog(tgb);
        }

        pblmodService.setListData(rmb2180.getPblmod(), recgrp.getTgc());
        recgrp.getAds().setTgclist(recgrp.getTgc().size());
        //存储TGC数据
        for (Tgc tgc : recgrp.getTgc()) {
            tgc.setObjinr(recgrp.getAds().getInr());
            tgc.setObjtyp(rmb2180.getObjtyp());
            if (dyncDaoService.dyncReadNoDataByInr(tgc))
                sysmodService.sdbUpdateWithLog(tgc);
            else
                sysmodService.sdbInsertWithLog(tgc);
        }

        pblmodService.setListData(rmb2180.getPblmod(), recgrp.getTgd());
        recgrp.getAds().setTgclist(recgrp.getTgd().size());
        //存储TGD数据
        for (Tgd tgd : recgrp.getTgd()) {
            tgd.setObjinr(recgrp.getAds().getInr());
            tgd.setObjtyp(rmb2180.getObjtyp());
            if (dyncDaoService.dyncReadNoDataByInr(tgd))
                sysmodService.sdbUpdateWithLog(tgd);
            else
                sysmodService.sdbInsertWithLog(tgd);
        }

        pblmodService.setListData(rmb2180.getPblmod(), recgrp.getTge());
        recgrp.getAds().setTgelist(recgrp.getTge().size());
        //存储TGE数据
        for (Tge tge : recgrp.getTge()) {
            tge.setObjinr(recgrp.getAds().getInr());
            tge.setObjtyp(rmb2180.getObjtyp());
            if (dyncDaoService.dyncReadNoDataByInr(tge))
                sysmodService.sdbUpdateWithLog(tge);
            else
                sysmodService.sdbInsertWithLog(tge);
        }

        pblmodService.setListData(rmb2180.getPblmod(), recgrp.getTgf());
        recgrp.getAds().setTgflist(recgrp.getTgf().size());
        //存储TGF数据
        for (Tgf tgf : recgrp.getTgf()) {
            tgf.setObjinr(recgrp.getAds().getInr());
            tgf.setObjtyp(rmb2180.getObjtyp());
            if (dyncDaoService.dyncReadNoDataByInr(tgf))
                sysmodService.sdbUpdateWithLog(tgf);
            else
                sysmodService.sdbInsertWithLog(tgf);
        }

    }

}
