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

import com.brilliance.mda.runtime.mda.util.MdaUtils;
import com.brilliance.isc.bo.*;
import com.brilliance.isc.common.smh.service.SmhCommonService;
import com.brilliance.isc.common.smh.vo.SmhQueryVo;
import com.brilliance.isc.common.util.StringUtils;
import com.brilliance.isc.mda.dao.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class SmhCommonServiceImpl implements SmhCommonService {
    final String SND_DIR = ">";
    final String RCV_DIR = "<";

    @Resource
    private GcdMapper gcdMapper;

    @Resource
    private NcdMapper ncdMapper;

    @Autowired
    private BrdMapper brdMapper;

    @Autowired
    private BedMapper bedMapper;

    @Autowired
    private BtdMapper btdMapper;

    @Autowired
    private BddMapper bddMapper;

    @Autowired
    private BfdMapper bfdMapper;

    @Resource
    private SmhMapper smhMapper;

    @Override
    public List<Smh> queryRcvSmhByObjTypAndInr(SmhQueryVo smhQueryVo) {
        List<Smh> list = new ArrayList<>();
        //查看当前来报
        String smhinr = smhQueryVo.getSmhinr();
        Smh smh = smhMapper.getSmhInfoBySptSmhinr(smhinr);
        if (smh != null) {
            list.add(smh);
        }
        //查看历史业务来报
        if(!MdaUtils.isEmpty(smhQueryVo.getObjtyp()) && !MdaUtils.isEmpty(smhQueryVo.getObjinr())) {
            List<Smh> smhList = smhMapper.selectByObjtypAndObjinrAndDir(smhQueryVo.getObjtyp(), smhQueryVo.getObjinr(), RCV_DIR);
            if (smhList != null && smhList.size() > 0) {
                if(smh != null){
                    for (int i = 0; i < smhList.size(); i++) {
                        if(!smhList.get(i).getInr().equals(smh.getInr())){
                            list.add(smhList.get(i));
                        }
                    }
                }else {
                    list.addAll(smhList);
                }

            }
            //查看历史父业务来报
            List<Smh> parentSmhList = this.queryParentSmh(smhQueryVo, RCV_DIR);
            if (parentSmhList != null && parentSmhList.size() > 0) {
                list.addAll(parentSmhList);
            }
        }
        return list;
    }

    @Override
    public List<Smh> querySndSmhByObjTypAndInr(SmhQueryVo smhQueryVo) {
        List<Smh> list = new ArrayList<>();
        //查看历史业务来报
        if(!MdaUtils.isEmpty(smhQueryVo.getObjtyp()) && !MdaUtils.isEmpty(smhQueryVo.getObjinr())) {
            List<Smh> smhList = smhMapper.selectByObjtypAndObjinrAndDir(smhQueryVo.getObjtyp(), smhQueryVo.getObjinr(), SND_DIR);
            if (smhList != null && smhList.size() > 0) {
                list.addAll(smhList);
            }
            //查看历史父业务来报
            List<Smh> parentSmhList = this.queryParentSmh(smhQueryVo, SND_DIR);
            if (parentSmhList != null && parentSmhList.size() > 0) {
                list.addAll(parentSmhList);
            }
        }
        return list;
    }

    private List<Smh> queryParentSmh(SmhQueryVo smhQueryVo, String dir) {
        String objtyp = smhQueryVo.getObjtyp();
        String objinr = smhQueryVo.getObjinr();
        if (StringUtils.isEmpty(objtyp) || StringUtils.isEmpty(objinr)) {
            return null;
        }
        List<Smh> list = new ArrayList<>();
        switch (objtyp.toUpperCase()){
            case "GCD":
                //国际保函索赔
                Gcd gcd = new Gcd();
                gcd.setInr(objinr);
                gcd = gcdMapper.selectByPrimaryKey(gcd);
                if (gcd != null) {
                    list = smhMapper.selectByObjtypAndObjinrAndDir("GID", gcd.getPntinr(), dir);
                }
                break;
            case "NCD":
                //国内保函索赔
                Ncd ncd = new Ncd();
                ncd.setInr(objinr);
                ncd = ncdMapper.selectByPrimaryKey(ncd);
                if (ncd != null) {
                    list = smhMapper.selectByObjtypAndObjinrAndDir("NID", ncd.getPntinr(), dir);
                }
                break;
            case "BRD":
                //进口信用证单据
                Brd brd = brdMapper.queryByInr(objinr);
                if (brd != null) {
                    list = smhMapper.selectByObjtypAndObjinrAndDir(brd.getPnttyp(), brd.getPntinr(), dir);
                }
                break;
            case "BED":
                //出口信用证单据
                Bed bed = new Bed();
                bed.setInr(objinr);
                bed = bedMapper.selectByPrimaryKey(bed);
                if (bed != null) {
                    list = smhMapper.selectByObjtypAndObjinrAndDir(bed.getPnttyp(), bed.getPntinr(), dir);
                }
                break;
            case "BTD":
                //转让信用证单据
                String ltdinr = btdMapper.selectLtdInrByBtdInr(objinr);
                list = smhMapper.selectByObjtypAndObjinrAndDir("LTD", ltdinr, dir);
                break;
            case "BDD":
                //买方信用证单据
                Bdd bdd = new Bdd();
                bdd.setInr(objinr);
                bdd = bddMapper.selectByPrimaryKey(bdd);
                if (bdd != null) {
                    list = smhMapper.selectByObjtypAndObjinrAndDir(bdd.getPnttyp(), bdd.getPntinr(), dir);
                }
                break;
            case "BFD":
                //卖方信用证单据
                Bfd bfd = new Bfd();
                bfd.setInr(objinr);
                bfd = bfdMapper.selectByPrimaryKey(bfd);
                if (bfd != null) {
                    list = smhMapper.selectByObjtypAndObjinrAndDir(bfd.getPnttyp(), bfd.getPntinr(), dir);
                }
                break;
            default:
                break;
        }
        return list;
    }

}
