package com.brilliance.isc.doc.handle.impl.entmod;

import com.brilliance.mda.runtime.mda.CodetableItem;
import com.brilliance.isc.common.cbsmod.service.XrtmodService;
import com.brilliance.isc.common.codetable.service.CodetableService;
import com.brilliance.isc.common.entmod.vo.EntmodVo;
import com.brilliance.isc.common.util.Utils;
import com.brilliance.isc.vo.model.EntinfVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.*;

@Component
public class BuildEntmodLetter {

    @Autowired
    private CodetableService codetableService;

    @Autowired
    private XrtmodService xrtmodService;

    private static List<CodetableItem> taxcodList;

    private static List<CodetableItem> entrakList;

    @PostConstruct
    public void initFintypList() {
        taxcodList = new ArrayList<>();
        taxcodList.add(new CodetableItem("一般贸易区", "00"));
        taxcodList.add(new CodetableItem("保税区", "01"));
        taxcodList.add(new CodetableItem("出口加工区", "02"));
        taxcodList.add(new CodetableItem("保税物流中心B型", "03"));
        taxcodList.add(new CodetableItem("保税物流园区", "04"));
        taxcodList.add(new CodetableItem("钻石交易所", "05"));
        taxcodList.add(new CodetableItem("保税港区", "06"));
        taxcodList.add(new CodetableItem("综合保税区", "07"));
        taxcodList.add(new CodetableItem("跨境工业园区", "08"));
        taxcodList.add(new CodetableItem("保税物流中心A型", "09"));
        taxcodList.add(new CodetableItem("出口监管仓库", "10"));
        taxcodList.add(new CodetableItem("进口保税仓库", "11"));
        taxcodList.add(new CodetableItem("自由贸易试验区(特殊监管区)", "12"));
        taxcodList.add(new CodetableItem("自由贸易试验区(非特殊监管区)", "60"));
        taxcodList.add(new CodetableItem("其他", "99"));

        entrakList = new ArrayList<>();
        entrakList.add(new CodetableItem("A类企业", "A"));
        entrakList.add(new CodetableItem("B类企业", "B"));
        entrakList.add(new CodetableItem("C类企业", "C"));

    }

    public Map<String, Object> build(EntmodVo entmodVo) {
        Map<String, Object> map = new HashMap<>();
        EntinfVo entinfVo = entmodVo.getEntinf();
        map.put("corpid", entinfVo.getCorpid());
        map.put("corpname", entinfVo.getCorpname());
        map.put("safecode", entinfVo.getSafecode());
        map.put("safename", entinfVo.getSafename());
        String paylist = codetableService.getTblLabel(entinfVo.getPaylist(), "CGYN");
        map.put("paylist", paylist);
        String newcorp = codetableService.getTblLabel(entinfVo.getNewcorp(), "CGYN");
        map.put("newcorp", newcorp);
        String istaxfre = codetableService.getTblLabel(entinfVo.getIstaxfre(), "ISYN");
        map.put("istaxfre", istaxfre);
        Optional<CodetableItem> item = taxcodList.stream().filter(f -> entinfVo.getTaxfrecod().equals(f.getValue())).findFirst();
        String taxfrecod = "";
        if (item.isPresent()) {
            taxfrecod = item.get().getLabel();
        }
        map.put("taxfrecod", taxfrecod);
        map.put("rakbegdat", Utils.fmtChineseDate(entinfVo.getRakbegdat()));
        map.put("rakenddat", Utils.fmtChineseDate(entinfVo.getRakenddat()));
        String entranknam = "";
        item = entrakList.stream().filter(f -> entinfVo.getEntrank().equals(f.getValue())).findFirst();
        if (item.isPresent()) {
            entranknam = item.get().getLabel();
        }
        map.put("entranknam", entranknam);
        String ismoncorp = codetableService.getTblLabel(entinfVo.getIsmoncorp(), "CGYN");
        map.put("ismoncorp", ismoncorp);
        map.put("entrank", entinfVo.getEntrank());
        if(!Objects.isNull(entinfVo.getPaybal())) {
            map.put("paybal", xrtmodService.amtRnd(entinfVo.getPaybal(), "USD").toPlainString());
        }
        if(!Objects.isNull(entinfVo.getRcvbal())) {
            map.put("rcvbal", xrtmodService.amtRnd(entinfVo.getRcvbal(), "USD").toPlainString());
        }
        map.put("code", entinfVo.getCode());
        map.put("detail", entinfVo.getDetail());
        map.put("prtdat", Utils.fmtChineseDate(entinfVo.getPrtdat()) + " " + Utils.fmtDate(new Date(), "HH:mm:ss"));
        return map;
    }
}
