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

import com.brilliance.mda.runtime.mda.util.Dates;
import com.brilliance.isc.bo.*;
import com.brilliance.isc.bo.model.*;
import com.brilliance.isc.common.cbsmod.Cbsget;
import com.brilliance.isc.common.cbsmod.service.CbsmodService;
import com.brilliance.isc.common.component.PtsptaToolComponent;
import com.brilliance.isc.common.transaction.help.TransactionServiceSet;
import com.brilliance.isc.common.util.StringUtils;
import com.brilliance.isc.common.util.Utils;
import com.brilliance.isc.common.vo.FremsgVo;
import com.brilliance.isc.common.vo.PtsptaVo;
import com.brilliance.isc.mda.dao.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component(value = "lgToolComponentCommon")
public class LgToolComponent {
    private static final String GID_DATA_TYPE = "GID";

    private static final String GCD_DATA_TYPE = "GCD";

    private static final String NID_DATA_TYPE = "NID";

    private static final String NCD_DATA_TYPE = "NCD";


    @Autowired
    private GidMapper gidMapper;

    @Autowired
    private NidMapper nidMapper;

    @Autowired
    private NitMapper nitMapper;

    @Autowired
    private NcdMapper ncdMapper;

    @Autowired
    private NctMapper nctMapper;

    @Autowired
    private GitMapper gitMapper;

    @Autowired
    private GcdMapper gcdMapper;

    @Autowired
    private GctMapper gctMapper;

    @Autowired
    private CbsmodService cbsmodService;

    @Autowired
    private Cbsget cbsget;

    @Autowired
    private PtsptaToolComponent ptsptaToolComponent;

    public void collectCbsMap(TransactionServiceSet set, String prefix, Giacbs cbs, String... cbsNames) {
        Map<String, Cbb> cbsMap = set.getCbsMap();
        if (cbsMap == null) {
            cbsMap = new HashMap<>();
        }
        for (String name : cbsNames) {
            Cbb cbb = Utils.getFieldValue(cbs, name);
            if (cbb != null) {
                cbsMap.put(prefix.toUpperCase() + name.toUpperCase(), cbb);
            }
        }
        set.setCbsMap(cbsMap);
    }

    public void collectCbsMap(TransactionServiceSet set, String prefix, Tracbs cbs, String... cbsNames) {
        Map<String, Cbb> cbsMap = set.getCbsMap();
        if (cbsMap == null) {
            cbsMap = new HashMap<>();
        }
        for (String name : cbsNames) {
            Cbb cbb = Utils.getFieldValue(cbs, name);
            if (cbb != null) {
                cbsMap.put(prefix.toUpperCase() + name.toUpperCase(), cbb);
            }
        }
        set.setCbsMap(cbsMap);
    }

    public void collectCbsMap(TransactionServiceSet set, String prefix, Gcacbs cbs, String... cbsNames) {
        Map<String, Cbb> cbsMap = set.getCbsMap();
        if (cbsMap == null) {
            cbsMap = new HashMap<>();
        }
        for (String name : cbsNames) {
            Cbb cbb = Utils.getFieldValue(cbs, name);
            if (cbb != null) {
                cbsMap.put(prefix.toUpperCase() + name.toUpperCase(), cbb);
            }
        }
        set.setCbsMap(cbsMap);
    }

    public void collectPtsList(TransactionServiceSet set, Gidgrp gidgrp, String... ptsNames) {
        for (String name : ptsNames) {
            PtsptaVo ptspta = Utils.getFieldValue(gidgrp, name);
            if (ptspta != null && ptspta.getPts() != null && StringUtils.isNotEmpty(ptspta.getPts().getRol())) {
                set.addPtspta(transferFrom(ptspta));
            }
        }
    }

    public void collectPtsList(TransactionServiceSet set, Gcdgrp gcdgrp, String... ptsNames) {
        for (String name : ptsNames) {
            PtsptaVo ptspta = Utils.getFieldValue(gcdgrp, name);
            if (ptspta != null) {
                set.addPtspta(transferFrom(ptspta));
            }
        }
    }

    public void collectPtsList(TransactionServiceSet set, Nidgrp nidgrp, String... ptsNames) {
        for (String name : ptsNames) {
            PtsptaVo ptspta = Utils.getFieldValue(nidgrp, name);
            if (ptspta != null) {
                set.addPtspta(transferFrom(ptspta));
            }
        }
    }

    public void collectPtsList(TransactionServiceSet set, Trdgrp trdgrp, String... ptsNames) {
        if (ptsNames == null) {
            //找所有的ptspta对象
            Field[] fields = trdgrp.getClass().getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                if (PtsptaVo.class.equals(field.getType())) {
                    PtsptaVo ptspta = Utils.getFieldValue(trdgrp, field.getName());
                    if (ptspta != null) {
                        set.addPtspta(transferFrom(ptspta));
                    }
                }
            }
        } else {
            for (String name : ptsNames) {
                PtsptaVo ptspta = Utils.getFieldValue(trdgrp, name);
                if (ptspta != null && ptspta.getPts() != null && StringUtils.isNotEmpty(ptspta.getPts().getRol())) {
                    set.addPtspta(transferFrom(ptspta));
                }
            }
        }
    }

    public void collectPtsList(TransactionServiceSet set, FremsgVo fremsg, String... ptsNames) {
        for (String name : ptsNames) {
            PtsptaVo ptspta = Utils.getFieldValue(fremsg, name);
            if (ptspta != null) {
                set.addPtspta(transferFrom(ptspta));
            }
        }
    }

    public void collectPtsList(TransactionServiceSet set, FremsgVo fremsg, String[] fremsgPtsNames, Gidgrp gidgrp, String[] gidgrpPtsNames) {
        for (String name : fremsgPtsNames) {
            PtsptaVo ptspta = Utils.getFieldValue(fremsg, name);
            if (ptspta != null) {
                set.addPtspta(transferFrom(ptspta));
            }
        }
        for (String name : gidgrpPtsNames) {
            PtsptaVo ptspta = Utils.getFieldValue(gidgrp, name);
            if (ptspta != null) {
                set.addPtspta(transferFrom(ptspta));
            }
        }
    }

    public void collectPtsList(TransactionServiceSet set, FremsgVo fremsg, String[] fremsgPtsNames, Nidgrp nidgrp, String[] nidgrpPtsNames) {
        for (String name : fremsgPtsNames) {
            PtsptaVo ptspta = Utils.getFieldValue(fremsg, name);
            if (ptspta != null) {
                set.addPtspta(transferFrom(ptspta));
            }
        }
        for (String name : nidgrpPtsNames) {
            PtsptaVo ptspta = Utils.getFieldValue(nidgrp, name);
            if (ptspta != null) {
                set.addPtspta(transferFrom(ptspta));
            }
        }
    }

    public void collectPtsList(TransactionServiceSet set, Ncdgrp ncdgrp, String... ptsNames) {
        for (String name : ptsNames) {
            PtsptaVo ptspta = Utils.getFieldValue(ncdgrp, name);
            if (ptspta != null) {
                set.addPtspta(transferFrom(ptspta));
            }
        }
    }

    public void collectPtsList(TransactionServiceSet set, Fcdgrp fcdgrp, String... ptsNames) {
        for (String name : ptsNames) {
            PtsptaVo ptspta = Utils.getFieldValue(fcdgrp, name);
            if (ptspta != null) {
                set.addPtspta(transferFrom(ptspta));
            }
        }
    }
    public void collectPtsaddg(TransactionServiceSet set, List<Ptsaddg> ptsaddg) {
            set.setPtsaddg(ptsaddg);
    }
    /**
     * 根据STL结构的ptspta转化成抽象组件的ptspta，结构不一样
     *
     * @param ptsptas
     * @return List<com.brilliance.common.vo.PtsptaVo>
     */
    protected PtsptaVo transferFrom(PtsptaVo ptsptas) {
        PtsptaVo ptspta = ptsptas;
        ptspta.setRol(ptsptas.getPts().getRol());
        ptspta.setPtsinr(ptsptas.getPts().getInr());
        ptspta.setNam(ptsptas.getPts().getNam());
        ptspta.setPtyinr(ptsptas.getPts().getPtyinr());
        ptspta.setPtainr(ptsptas.getPts().getPtainr());
        ptspta.setExtkey(ptsptas.getPts().getExtkey());
        ptspta.setDftdsp(ptsptas.getPts().getDftdsp());
        ptspta.setDftcur(ptsptas.getPts().getDftcur());
        ptspta.setDftact(ptsptas.getPts().getDftact());
        ptspta.setDftfeecur(ptsptas.getPts().getDftfeecur());
        ptspta.setDftactptainr(ptsptas.getPts().getDftactptainr());
        ptspta.setDftselrol(ptsptas.getPts().getDftsetrol());
        ptspta.setGlggrpflg(ptsptas.getPts().getGlggrpflg());
        ptspta.setRef(ptsptas.getPts().getRef());
        ptspta.setPtytyp(ptsptas.getPtytyp());
        ptspta.setPtyextkey(ptsptas.getPtyextkey());
        ptspta.setAdrblk(ptsptas.getPts().getAdrblk());
//        ptspta.setPts(new Pts());
//        BeanUtils.copyProperties(ptsptas.getPts(), ptspta.getPts());
        //ptspta.setRolsetflg(ptsptas.getRolsetflg());
        return ptspta;
    }

    public Gidgrp getGidgrpByInr(String inr) {
        Gidgrp gidgrp = new Gidgrp();
        gidgrp.reset();
        //找GID的数据
        Gid gid = gidgrp.getRec();
        gid.setInr(inr);
        gid = gidMapper.selectByPrimaryKey(gid);
        gidgrp.setRec(gid);
        //找GIT的数据
        Git git = gidgrp.getBlk();
        git.setInr(inr);
        git = gitMapper.selectByPrimaryKey(git);
        gidgrp.setBlk(git);
        //找giacbs数据
        Date date = Dates.add(Utils.getMaxDate(), -1);


        cbsmodService.cbsGetBalance(gidgrp.getCbs().getMax(), GID_DATA_TYPE, inr, "MAXSUM", "AMT1", date);
        cbsmodService.cbsGetBalance(gidgrp.getCbs().getOpn1(), GID_DATA_TYPE, inr, "OPN", "AMT1", date);
        cbsmodService.cbsGetBalance(gidgrp.getCbs().getCnf(), GID_DATA_TYPE, inr, "CNFSUM", "AMT1", date);
        Map<String, Cbb> cbbMap = new HashMap<>();
        gidgrp.getCbs().fillToMap(cbbMap);
        cbsget.getAamCbsPerDate(cbbMap, GID_DATA_TYPE, inr, "GIDGRP\\REC", date);
        // DO5DOS.011084
        //DNGDEV.000389
        cbsmodService.cbsGetBalance(gidgrp.getCbs().getMaxint(), GID_DATA_TYPE, inr, "MAXSUM", "AMTINT", date);
        cbsmodService.cbsGetBalance(gidgrp.getCbs().getOpnint(), GID_DATA_TYPE, inr, "OPN", "AMTINT", date);
        // DNGDEV.001449
        cbsmodService.cbsGetBalance(gidgrp.getCbs().getMac(), GID_DATA_TYPE, inr, "MACSUM", "AMT1", date);
        cbsmodService.cbsGetBalance(gidgrp.getCbs().getOpc1(), GID_DATA_TYPE, inr, "OPC", "AMT1", date);
        //找ptspta数据
//        Map<String, PtsptaVo> maps = ptsptaToolComponent.getPtsptaList(inr, GID_DATA_TYPE);
//        if (maps.size() > 0) {
//            maps.keySet().forEach(key -> {
//                Utils.setFieldValue(gidgrp, key.toLowerCase(), maps.get(key));
//            });
//        }
        storePtsToGrp(gidgrp,inr,GID_DATA_TYPE);

        return gidgrp;
    }

    public Nidgrp getNidgrpByInr(String inr) {
        Nidgrp nidgrp = new Nidgrp();
        nidgrp.reset();
        //找GID的数据
        Nid nid = nidgrp.getRec();
        nid.setInr(inr);
        nid = nidMapper.selectByPrimaryKey(nid);
        nidgrp.setRec(nid);
        //找GIT的数据
        Nit nit = nidgrp.getBlk();
        nit.setInr(inr);
        nit = nitMapper.selectByPrimaryKey(nit);
        nidgrp.setBlk(nit);
        //找giacbs数据
        Date date = Dates.add(Utils.getMaxDate(), -1);


        cbsmodService.cbsGetBalance(nidgrp.getCbs().getMax(), NID_DATA_TYPE, inr, "MAXSUM", "AMT1", date);
        cbsmodService.cbsGetBalance(nidgrp.getCbs().getOpn1(), NID_DATA_TYPE, inr, "OPN", "AMT1", date);
        cbsmodService.cbsGetBalance(nidgrp.getCbs().getCnf(), NID_DATA_TYPE, inr, "CNFSUM", "AMT1", date);
        Map<String, Cbb> cbbMap = new HashMap<>();
        nidgrp.getCbs().fillToMap(cbbMap);
        cbsget.getAamCbsPerDate(cbbMap, NID_DATA_TYPE, inr, "NIDGRP\\REC", date);
        // DO5DOS.011084
        //DNGDEV.000389
        cbsmodService.cbsGetBalance(nidgrp.getCbs().getMaxint(), NID_DATA_TYPE, inr, "MAXSUM", "AMTINT", date);
        cbsmodService.cbsGetBalance(nidgrp.getCbs().getOpnint(), NID_DATA_TYPE, inr, "OPN", "AMTINT", date);
        // DNGDEV.001449
        cbsmodService.cbsGetBalance(nidgrp.getCbs().getMac(), NID_DATA_TYPE, inr, "MACSUM", "AMT1", date);
        cbsmodService.cbsGetBalance(nidgrp.getCbs().getOpc1(), NID_DATA_TYPE, inr, "OPC", "AMT1", date);
        //找ptspta数据
//        Map<String, PtsptaVo> maps = ptsptaToolComponent.getPtsptaList(inr, NID_DATA_TYPE);
//        if (maps.size() > 0) {
//            maps.keySet().forEach(key -> {
//                Utils.setFieldValue(nidgrp, key.toLowerCase(), maps.get(key));
//            });
//        }
        storePtsToGrp(nidgrp,inr,NID_DATA_TYPE);

        return nidgrp;
    }


    public Gcdgrp getGcdgrpByInr(String inr) {
        Gcdgrp gcdgrpVo = new Gcdgrp();
        gcdgrpVo.reset();
        //找Gcd的数据
        Gcd gcd = gcdgrpVo.getRec();
        gcd.setInr(inr);
        gcd = gcdMapper.selectByPrimaryKey(gcd);
        gcdgrpVo.setRec(gcd);
        //找GIT的数据
        Gct gct = gcdgrpVo.getBlk();
        gct.setInr(inr);
        gct = gctMapper.selectByPrimaryKey(gct);
        gcdgrpVo.setBlk(gct);
        //找giacbs数据
        Date date = Dates.add(Utils.getMaxDate(), -1);

        cbsmodService.cbsGetBalance(gcdgrpVo.getCbs().getMax(), GCD_DATA_TYPE, inr, "MAXSUM", "AMT1", date);
        cbsmodService.cbsGetBalance(gcdgrpVo.getCbs().getOpn1(), GCD_DATA_TYPE, inr, "OPN", "AMT1", date);
        Map<String, Cbb> cbbMap = new HashMap<>();
        gcdgrpVo.getCbs().fillToMap(cbbMap);
        cbsget.getAamCbsPerDate(cbbMap, GCD_DATA_TYPE, inr, "GCDGRP\\REC", date);

        //找ptspta数据
//        Map<String, PtsptaVo> maps = ptsptaToolComponent.getPtsptaList(inr, GCD_DATA_TYPE);
//        if (maps.size() > 0) {
//            maps.keySet().forEach(key -> {
//                Utils.setFieldValue(gcdgrpVo, key.toLowerCase(), maps.get(key));
//            });
//        }
        storePtsToGrp(gcdgrpVo,inr,GCD_DATA_TYPE);
        return gcdgrpVo;
    }

    public Ncdgrp getNcdgrpByInr(String inr) {
        Ncdgrp ncdgrpVo = new Ncdgrp();
        ncdgrpVo.reset();
        //找Ncd的数据
        Ncd ncd = ncdgrpVo.getRec();
        ncd.setInr(inr);
        ncd = ncdMapper.selectByPrimaryKey(ncd);
        ncdgrpVo.setRec(ncd);
        //找GIT的数据
        Nct nct = ncdgrpVo.getBlk();
        nct.setInr(inr);
        nct = nctMapper.selectByPrimaryKey(nct);
        ncdgrpVo.setBlk(nct);
        //找giacbs数据
        Date date = Dates.add(Utils.getMaxDate(), -1);

        cbsmodService.cbsGetBalance(ncdgrpVo.getCbs().getMax(), NCD_DATA_TYPE, inr, "MAXSUM", "AMT1", date);
        cbsmodService.cbsGetBalance(ncdgrpVo.getCbs().getOpn1(), NCD_DATA_TYPE, inr, "OPN", "AMT1", date);
        Map<String, Cbb> cbbMap = new HashMap<>();
        ncdgrpVo.getCbs().fillToMap(cbbMap);
        cbsget.getAamCbsPerDate(cbbMap, NCD_DATA_TYPE, inr, "NCDGRP\\REC", date);

        //找ptspta数据
//        Map<String, PtsptaVo> maps = ptsptaToolComponent.getPtsptaList(inr, NCD_DATA_TYPE);
//        if (maps.size() > 0) {
//            maps.keySet().forEach(key -> {
//                Utils.setFieldValue(ncdgrpVo, key.toLowerCase(), maps.get(key));
//            });
//        }
        storePtsToGrp(ncdgrpVo,inr,NCD_DATA_TYPE);
        return ncdgrpVo;
    }

    public PtsptaVo getOldGidRolByInr(String gidinr, String rol) {
        Map<String, PtsptaVo> ptsptaVoMap = ptsptaToolComponent.getPtsptaList(gidinr, GID_DATA_TYPE);
        return ptsptaVoMap.get(rol);
    }

    public PtsptaVo getOldNidRolByInr(String nidinr, String rol) {
        Map<String, PtsptaVo> ptsptaVoMap = ptsptaToolComponent.getPtsptaList(nidinr, NID_DATA_TYPE);
        return ptsptaVoMap.get(rol);
    }

    public Gid getOldRecByInr(String gidinr) {
        Gid gid = new Gid();
        gid.setInr(gidinr);
        gid = gidMapper.selectByPrimaryKey(gid);
        return gid;
    }

    public Nid getNidByInr(String nidinr) {
        Nid nid = new Nid();
        nid.setInr(nidinr);
        nid = nidMapper.selectByPrimaryKey(nid);
        return nid;
    }

    public Giacbs getCbsByInr(String inr) {
        Giacbs giacbs = new Giacbs();
        giacbs.reset();
        Date date = Dates.add(Utils.getMaxDate(), -1);
        cbsmodService.cbsGetBalance(giacbs.getMax(), GID_DATA_TYPE, inr, "MAXSUM", "AMT1", date);
        cbsmodService.cbsGetBalance(giacbs.getOpn1(), GID_DATA_TYPE, inr, "OPN", "AMT1", date);
        cbsmodService.cbsGetBalance(giacbs.getCnf(), GID_DATA_TYPE, inr, "CNFSUM", "AMT1", date);
        Map<String, Cbb> cbbMap = new HashMap<>();
        giacbs.fillToMap(cbbMap);
        cbsget.getAamCbsPerDate(cbbMap, GID_DATA_TYPE, inr, "GIDGRP\\REC", date);
        // DO5DOS.011084
        //DNGDEV.000389
        cbsmodService.cbsGetBalance(giacbs.getMaxint(), GID_DATA_TYPE, inr, "MAXSUM", "AMTINT", date);
        cbsmodService.cbsGetBalance(giacbs.getOpnint(), GID_DATA_TYPE, inr, "OPN", "AMTINT", date);
        // DNGDEV.001449
        cbsmodService.cbsGetBalance(giacbs.getMac(), GID_DATA_TYPE, inr, "MACSUM", "AMT1", date);
        cbsmodService.cbsGetBalance(giacbs.getOpc1(), GID_DATA_TYPE, inr, "OPC", "AMT1", date);
        return giacbs;
    }

    public void storePtsToGrp(Object grpObject,String objInr,String objType){
        //找ptspta数据
        Map<String, PtsptaVo> maps = ptsptaToolComponent.getPtsptaList(objInr, objType);
        if (maps.size() > 0) {
            maps.keySet().forEach(key -> {
                if(key.equalsIgnoreCase("INT")){
                    key = "tmpint";
                }
                Utils.setFieldValue(grpObject, key.toLowerCase(), maps.get(key));
            });
        }
    }

}
