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

import com.brilliance.mda.runtime.mda.CodetableItem;
import com.brilliance.mda.runtime.mda.util.MdaUtils;
import com.brilliance.mda.runtime.mda.util.Strings;
import com.brilliance.isc.bo.Spt;
import com.brilliance.isc.common.codetable.service.CodetableService;
import com.brilliance.isc.common.trnmod.service.PendingCheckService;
import com.brilliance.isc.mda.dao.SptMapper;
import com.brilliance.isc.utils.StringUtils;
import com.brilliance.isc.vo.PendingCheckResult;
import com.brilliance.isc.vo.PendingCheckVo;
import com.brilliance.isc.vo.WarnVo;
import com.brilliance.isc.warn.WarnService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class PendingCheckServiceImpl implements PendingCheckService {

    @Resource
    private SptMapper sptMapper;

    @Resource
    private WarnService warnService;

    @Resource
    private CodetableService codetableService;

    @Override
    public PendingCheckResult checkPendingSPTs(PendingCheckVo pendingCheckVo) {
        PendingCheckResult pendingCheckResult = new PendingCheckResult();
        String dataLabel = pendingCheckVo.getObjtyp()+"#"+pendingCheckVo.getObjinr();
        if(MdaUtils.isEmpty(pendingCheckVo.getObjinr()) || MdaUtils.isEmpty(pendingCheckVo.getObjtyp())){
            return pendingCheckResult;
        }
        //设置Child 类型
        pendingCheckVo.setChildTypes(getChildType(pendingCheckVo));
        String transName = pendingCheckVo.getTransName();
        boolean ameCanFlag = false;
        if (StringUtils.isNotEmpty(transName)) {
            if (transName.indexOf("AME") > -1 || transName.indexOf("PAM") > -1 || transName.indexOf("CAN") > -1) {
                ameCanFlag = true;
            }
        }
        //非修改和注销交易会检查当前交易是否存在暂存数据
        if (!ameCanFlag) {
            List<Spt> penSptList = sptMapper.getPenSptList(pendingCheckVo);
            if(!MdaUtils.isEmpty(penSptList)){
                if(pendingCheckVo.isSetWarning()){
                    String sptText = internalProcessSPTSTM(penSptList);
                    sptText = String.format("当前业务存在以下暂存数据:<br/>%s ",sptText);
                    sptText = sptText.replaceAll(" ","&nbsp;");
                    warnService.generalWarn(dataLabel,"S", WarnVo.SOURCE_BACK,sptText);
                }
                pendingCheckResult.setPenSptList(penSptList);
            }
        }
        List<Spt> ameSptList = sptMapper.getAmeSptList(pendingCheckVo);
        if(!MdaUtils.isEmpty(ameSptList)){
            if(pendingCheckVo.isSetWarning()){
                String sptText = internalProcessSPTSTM(ameSptList);
                sptText = String.format("当前业务(%s)存在以下暂存的修改交易数据:<br/>%s，请检查金额、日期等信息！",ameSptList.get(0).getOwnref(),sptText);
                sptText = sptText.replaceAll(" ","&nbsp;");
                warnService.generalWarn(dataLabel,"R", WarnVo.SOURCE_BACK,sptText);
            }
            pendingCheckResult.setPenSptList(ameSptList);
        }
        List<Spt> canSptList = sptMapper.getCanSptList(pendingCheckVo);
        if(!MdaUtils.isEmpty(canSptList)){
            if(pendingCheckVo.isSetWarning()){
                String sptText = internalProcessSPTSTM(canSptList);
                sptText = String.format("当前业务(%s)存在以下暂存的注销交易数据:<br/>%s，请检查金额、日期等信息！",canSptList.get(0).getOwnref(),sptText);
                sptText = sptText.replaceAll(" ","&nbsp;");
                warnService.generalWarn(dataLabel,"R", WarnVo.SOURCE_BACK,sptText);
            }
            pendingCheckResult.setPenSptList(canSptList);
        }
        List<CodetableItem> codetableItems = codetableService.selectList("ATPTXT","CN");
        Map<String,String> mapStb = codetableItems.stream().collect(Collectors.toMap(CodetableItem::getValue,CodetableItem::getLabel));
        if(!MdaUtils.isEmpty(pendingCheckResult.getPenSptList())){
            pendingCheckResult.getPenSptList().stream().forEach(
                    item -> item.setTxt(mapStb.get(item.getFrm()))
            );
        }
        return pendingCheckResult;
    }

    public List<PendingCheckVo.ChildType> getChildType(PendingCheckVo pendingCheckVo){
        switch (pendingCheckVo.getObjtyp()){
            case "GID": {
                PendingCheckVo.ChildType childType = new PendingCheckVo.ChildType();
                childType.setChildtyp("pnttyp");
                childType.setChildinr("pntinr");
                childType.setChildtbl("GCD");
                return Arrays.asList(childType);
            }
            case "NID": {
                PendingCheckVo.ChildType childType = new PendingCheckVo.ChildType();
                childType.setChildtyp("pnttyp");
                childType.setChildinr("pntinr");
                childType.setChildtbl("NCD");
                return Arrays.asList(childType);
            }
        }
        return new ArrayList<>();
    }

    private String internalProcessSPTSTM(List<Spt> sptList){

        return sptList.stream().reduce(new StringBuilder(),(sb,spt)->{
            String dateStr = "";
            if(!MdaUtils.isEmpty(spt.getDattim())){
                dateStr = new SimpleDateFormat("yyyy.MM.dd").format(spt.getDattim());
            }
            String staStr = codetableService.getTblLabel(spt.getSta(),"SPTSTA");
            String line = staStr +
                    Strings.mid(" ",1,10)+
                    dateStr+
                    Strings.mid(" ",1,10)+
                    spt.getObjnam()+
                    Strings.mid(" ",1,10)+
                    spt.getUsr()
                    ;
            sb.append(line);
            sb.append("<br/>");
            return sb;
        },(a,b)->a.append(b)).toString();
    }
}
