package com.brilliance.isc.common.api.bopcod.impl;

import com.brilliance.isc.bo.Bopcod;
import com.brilliance.isc.common.api.bopcod.BopcodService;
import com.brilliance.isc.common.util.StringUtils;
import com.brilliance.isc.mda.dao.BopcodMapper;
import com.brilliance.mda.runtime.mda.config.IniConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class BopcodServiceImpl implements BopcodService {
    private IniConfig config;
    @Autowired
    private BopcodMapper bopcodMapper;

    @PostConstruct
    public void loadConfig() {
        try {
            org.springframework.core.io.Resource resource = new ClassPathResource("cfg/bopcod.ini");
            config = IniConfig.loadIniFile("bopcod.ini", resource.getInputStream(), "utf-8");
        } catch (Exception e) {
            log.error("加载来报通用静态配置[bopcod.ini]异常", e);
            //弄个空的配置，避免影响其他程序
            config = new IniConfig();
        }
    }

    @Override
    public List<Bopcod> getBopcodList(String iotyp) {
        List<Bopcod> bopcodList;
        Bopcod bopcod = new Bopcod();
        bopcod.setSta("A");
        bopcod.setDir(iotyp);
        bopcodList = bopcodMapper.queryBopcodList(bopcod);
        String flev = iotyp + "ghutyp";
        String slev = iotyp + "waitjm";
        Map<String, String> ghutypCodeTable = config.getBlock(flev.toUpperCase());
        Map<String, String> waitjmCodeTable = config.getBlock(slev.toUpperCase());
        List<Bopcod> deleteBopcodlst = new ArrayList<>();
        List<Bopcod> addBopcodlst = new ArrayList<>();
        bopcodList.forEach(item -> {
            if (StringUtils.isNotEmpty(item.getGhutyp())) {
                String ghutypl = ghutypCodeTable.get(item.getGhutyp());
                if (!org.apache.commons.lang3.StringUtils.isBlank(ghutypl)) {
                    item.setGhutypl(ghutypl);
                } else {
                    item.setGhutypl(item.getGhutyp());
                }
            }
            if (StringUtils.isNotEmpty(item.getWaitjm())) {
                String[] waitjms = item.getWaitjm().split(",");
                if (waitjms.length > 1) {
                    deleteBopcodlst.add(item);
                    for (String waitjm : waitjms) {
                        Bopcod bopcodtmp = cloneBopcod(item,waitjm);
                        String waitjml = waitjmCodeTable.get(waitjm);
                        if (StringUtils.isNotEmpty(waitjml)) {
                            bopcodtmp.setWaitjml(waitjml);
                        } else {
                            bopcodtmp.setWaitjml(waitjm);
                        }
                        addBopcodlst.add(bopcodtmp);
                    }
                }else{
                    String waitjml = waitjmCodeTable.get(item.getWaitjm());
                    if (StringUtils.isNotEmpty(waitjml)) {
                        item.setWaitjml(waitjml);
                    } else {
                        item.setWaitjml(item.getWaitjm());
                    }
                }
            }
        });
        bopcodList.removeAll(deleteBopcodlst);
        bopcodList.addAll(addBopcodlst);
        return bopcodList;
    }

    private Bopcod cloneBopcod(Bopcod item,String waitjm) {
        Bopcod bopcod = new Bopcod();
        bopcod.setInr(item.getInr());
        bopcod.setVer(item.getVer());
        bopcod.setCod(item.getCod());
        bopcod.setDir(item.getDir());
        bopcod.setTyp(item.getTyp());
        bopcod.setTxt(item.getTxt());
        bopcod.setSta(item.getSta());
        bopcod.setGhutyp(item.getGhutyp());
        bopcod.setGhutypl(item.getGhutypl());
        bopcod.setWaitjm(waitjm);
        return bopcod;
    }
}
