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

import com.brilliance.isc.bo.*;
import com.brilliance.isc.common.doctre.service.DoctreService;
import com.brilliance.isc.common.doctre.vo.DoctreRequestVo;
import com.brilliance.isc.common.doctre.vo.DoctreResponseVo;
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 DoctreServiceImpl implements DoctreService {
    final String SND_DIR = ">";
    final String RCV_DIR = "<";

    @Autowired
    private SmhMapper smhMapper;

    @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;

    @Override
    public DoctreResponseVo assertDftcre(DoctreRequestVo request) {
        DoctreResponseVo doctreResponseVo = new DoctreResponseVo();
        List<Smh> shwinc = new ArrayList<>();
        //查看当前来报
        String smhinr = request.getSmhinr();
        Smh smh = smhMapper.getSmhInfoBySptSmhinr(smhinr);
        if (smh != null) {
            shwinc.add(smh);
        }
        //查看历史业务来报
        List<Smh> smhList = smhMapper.selectByObjtypAndObjinrAndDir(request.getObjtyp(), request.getObjinr(), RCV_DIR);
        if (smhList != null && smhList.size() > 0) {
            shwinc.addAll(smhList);
        }
        //查看历史父业务来报
        List<Smh> parentSmhList = this.queryParentSmh(request, RCV_DIR);
        if (parentSmhList != null && parentSmhList.size() > 0) {
            shwinc.addAll(parentSmhList);
        }
        doctreResponseVo.setShwinc(shwinc);

        List<Smh> shwout = new ArrayList<>();
        //查看历史业务来报
        List<Smh> smhList1 = smhMapper.selectByObjtypAndObjinrAndDir(request.getObjtyp(), request.getObjinr(), SND_DIR);
        if (smhList1 != null && smhList1.size() > 0) {
            shwout.addAll(smhList1);
        }
        //查看历史父业务来报
        List<Smh> parentSmhList1 = this.queryParentSmh(request, SND_DIR);
        if (parentSmhList1 != null && parentSmhList1.size() > 0) {
            shwout.addAll(parentSmhList1);
        }
        doctreResponseVo.setShwout(shwout);
        return doctreResponseVo;
    }

    private List<Smh> queryParentSmh(DoctreRequestVo request, String dir) {
        String objtyp = request.getObjtyp();
        String objinr = request.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;
    }

}
