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.Rmb2102Service;
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.Tfagrp;
import com.brilliance.isc.common.rmbbop.vo.Rmb2102;
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("tfa")
public class Rmb2102ServiceImpl implements Rmb2102Service {
    @Autowired
    private DyncDaoService dyncDaoService;
    @Autowired
    private CodetableService codetableService;
    @Autowired
    private PblmodService pblmodService;
    @Autowired
    private SysmodService sysmodService;

    @Autowired
    private PblMapper pblMapper;


    @Override
    public void save(Rmb2102 rmb2102) {

        String objinr = rmb2102.getRecgrp().getAds().getInr();
        if (!MdaUtils.isEmpty(objinr)) {
            Pbl pbl = pblMapper.selectByObjtypAndObjinr("TFA", objinr);
            rmb2102.getPblmod().setWrkpbl(pbl);
        }
        pblmodService.storeAllinBusTx(rmb2102);
    }

    @Override
    public void updrmb(Rmb2102 rmb2102) {

    }

    @Override
    public void saveChildData(Rmb2102 rmb2102) {
        Tfagrp recgrp = rmb2102.getRecgrp();
        //savdatafrompanel
        pblmodService.setListData(rmb2102.getPblmod(), recgrp.getTfc());
        pblmodService.setListData(rmb2102.getPblmod(), recgrp.getTfd());
        pblmodService.setListData(rmb2102.getPblmod(), recgrp.getTfe());
        pblmodService.setListData(rmb2102.getPblmod(), recgrp.getTff());
        pblmodService.setListData(rmb2102.getPblmod(), recgrp.getTfg());
        recgrp.getAds().setTfc(recgrp.getTfc().size());
        recgrp.getAds().setTfd(recgrp.getTfd().size());
        recgrp.getAds().setTfe(recgrp.getTfe().size());
        recgrp.getAds().setTff(recgrp.getTff().size());
        recgrp.getAds().setTfg(recgrp.getTfg().size());
        //存储数据
        for (Tfc tfc : recgrp.getTfc()) {
            tfc.setObjinr(recgrp.getAds().getInr());
            tfc.setObjtyp(rmb2102.getObjtyp());
            if (dyncDaoService.dyncReadNoDataByInr(tfc))
                sysmodService.sdbUpdateWithLog(tfc);
            else
                sysmodService.sdbInsertWithLog(tfc);
        }
        for (Tfd tfd : recgrp.getTfd()) {
            tfd.setObjinr(recgrp.getAds().getInr());
            tfd.setObjtyp(rmb2102.getObjtyp());
            if (dyncDaoService.dyncReadNoDataByInr(tfd))
                sysmodService.sdbUpdateWithLog(tfd);
            else
                sysmodService.sdbInsertWithLog(tfd);
        }
        for (Tfe tfe : recgrp.getTfe()) {
            tfe.setObjinr(recgrp.getAds().getInr());
            tfe.setObjtyp(rmb2102.getObjtyp());
            if (dyncDaoService.dyncReadNoDataByInr(tfe))
                sysmodService.sdbUpdateWithLog(tfe);
            else
                sysmodService.sdbInsertWithLog(tfe);
        }
        for (Tff tff : recgrp.getTff()) {
            tff.setObjinr(recgrp.getAds().getInr());
            tff.setObjtyp(rmb2102.getObjtyp());
            if (dyncDaoService.dyncReadNoDataByInr(tff))
                sysmodService.sdbUpdateWithLog(tff);
            else
                sysmodService.sdbInsertWithLog(tff);
        }
        for (Tfg tfg : recgrp.getTfg()) {
            tfg.setObjinr(recgrp.getAds().getInr());
            tfg.setObjtyp(rmb2102.getObjtyp());
            if (dyncDaoService.dyncReadNoDataByInr(tfg))
                sysmodService.sdbUpdateWithLog(tfg);
            else
                sysmodService.sdbInsertWithLog(tfg);
        }
    }











}
