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

import com.brilliance.isc.bo.Cci;
import com.brilliance.isc.common.ccimod.service.CcimodService;
import com.brilliance.isc.common.register.cci.CliBanInfoFactory;
import com.brilliance.isc.common.util.Utils;
import com.brilliance.isc.common.vo.PtsptaVo;
import com.brilliance.isc.mda.dao.CciMapper;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CcimodServiceImpl implements CcimodService {

    @Autowired
    private CciMapper cciMapper;

    @Autowired
    private CliBanInfoFactory cliBanInfoFactory;

    /**
     *   source:ccimod.0000.script
     */
    public void storeCCIForContract(List<PtsptaVo> ptsptaVos, Object argRec) {
        //! insert or update CCI-record associated to passed ArgMod
        //! ArgMod holds the group of contract
        //! set common fields from  parent contract but first read parent contract if not yet loaded
        //! after this set common fields from passed contract and other fields
        String objTyp = Utils.recGetObj(argRec);
        String objInr = Utils.recGetInr(argRec);
        Cci cci = cciMapper.getCciByObjinrAndObjTyp(objInr, objTyp);
        if (cci == null) {
            cci = new Cci();
        }
        //父类值copy给cci 此次不需要，后面还是用子类值copy给cci
        BeanUtils.copyProperties(argRec, cci);
        // set / update customer specific fields 空方法 不实现
        //setCCIFieldsCus( this.getCci(), argGrp );
        cliBanInfoFactory.getCliBanInfo(objTyp).buildValue(objInr, objTyp, ptsptaVos, cci);
        if( MdaUtils.isEmpty(cci.getObjtyp()) ) {
            cci.setObjtyp( objTyp);
            cci.setObjinr( objInr);
            cciMapper.insert(cci);
        } else {
            cciMapper.updateByPrimaryKeySelective(cci);
        }
    }
}
