package com.brilliance.isc.common.register.cci.impl;


import com.brilliance.isc.bo.Cci;
import com.brilliance.isc.bo.Pts;
import com.brilliance.isc.common.register.cci.CliBanInfo;
import com.brilliance.isc.common.vo.PtsptaVo;
import com.brilliance.isc.mda.dao.PtsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Component("TRD.cciRegister")
public class TrdCliBanInfo implements CliBanInfo {

    @Autowired
    private PtsMapper ptsMapper;

    @Override
    public void buildValue(String objinr, String objtyp, List<PtsptaVo> ptsptaVos, Cci cci) {
        //! Defines role of the bank and the customer of the passed contract and PTAINR of the role as well
        //! The passed contract has to be loaded completely
        // role can be set dynamically dependent on the contract context
        //getTrdCliBanInfo
        List<Pts> ptsList = ptsMapper.getPtsByObjInrTyp(objinr, objtyp);
        if (ptsList == null || ptsList.size() == 0) {
            ptsList = new ArrayList<>();
            for (PtsptaVo ptsptaVo : ptsptaVos) {
                ptsList.add(ptsptaVo.getPts());
            }
        }
        if (ptsList == null || ptsList.size() == 0) {
            return;
        }
        cci.setCcicusrol("FIP");
        Optional<Pts> optionalPts = ptsList.stream().filter(pts -> cci.getCcicusrol().equalsIgnoreCase(pts.getRol())).findFirst();
        if (optionalPts.isPresent()) {
            cci.setCcicusptainr(optionalPts.get().getPtainr());
        }
        cci.setCcibanrol("");
        optionalPts = ptsList.stream().filter(pts -> cci.getCcibanrol().equalsIgnoreCase(pts.getRol())).findFirst();
        if (optionalPts.isPresent()) {
            cci.setCcibanptainr(optionalPts.get().getPtainr());
        }
        /*Trd trd = trdMapper.selectByInr(objinr);
        if (trd == null) {
            throw new CommonServiceException("无法找到业务主表数据");
        }
        String hndtyp = trd.getHndtyp();
        if ("OT".equalsIgnoreCase(hndtyp) || hndtyp.indexOf("F") > -1) {
            //转开或者出口
            cci.setCcicusrol("BEN");
            Optional<Pts> optionalPts = ptsList.stream().filter(pts -> cci.getCcicusrol().equalsIgnoreCase(pts.getRol())).findFirst();
            if (optionalPts.isPresent()) {
                cci.setCcicusptainr(optionalPts.get().getPtainr());
            }
            cci.setCcibanrol("APL");
            optionalPts = ptsList.stream().filter(pts -> cci.getCcibanrol().equalsIgnoreCase(pts.getRol())).findFirst();
            if (optionalPts.isPresent()) {
                cci.setCcibanptainr(optionalPts.get().getPtainr());
            }
        } else {
            Optional<Pts> optionalPts = ptsList.stream().filter(pts -> "CTR".equalsIgnoreCase(pts.getRol())).findFirst();
            if (optionalPts.isPresent()) {
                cci.setCcicusrol("CTR");
                cci.setCcicusptainr(optionalPts.get().getPtainr());
            } else {
                optionalPts = ptsList.stream().filter(pts -> "APL".equalsIgnoreCase(pts.getRol())).findFirst();
                if (optionalPts.isPresent()) {
                    cci.setCcicusrol("APL");
                    cci.setCcicusptainr(optionalPts.get().getPtainr());
                }
            }

            cci.setCcibanrol("BEN");
            optionalPts = ptsList.stream().filter(pts -> cci.getCcibanrol().equalsIgnoreCase(pts.getRol())).findFirst();
            if (optionalPts.isPresent()) {
                cci.setCcibanptainr(optionalPts.get().getPtainr());
            }
        }*/
    }
}
