package com.tofly.xrtymis.custom.service.export.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tofly.api.CodeClient;
import com.tofly.xrtymis.custom.commons.Constate;
import com.tofly.xrtymis.custom.service.TfYwpnPipenodeService;
import com.tofly.xrtymis.custom.service.export.ExportNodeService;
import com.tofly.xrtymis.entity.TfYwpnPipenode;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;

@AllArgsConstructor
@Service("exportNodeService")
public class ExportNodeServiceImpl implements ExportNodeService {

    private final TfYwpnPipenodeService nodeService;
    private final CodeClient codeClient;

    @Override
    public List<Map<String, Object>> getExportData(String dto, String templateFlag) {
        List<String> list = Arrays.asList(dto.split(Constate.CSV));
        ArrayList<TfYwpnPipenode> result = new ArrayList<>();
        if (list.size() > 0 ) {
            for (String id : list) {
                TfYwpnPipenode pipenode = nodeService.getById(id);
                if (pipenode != null) {
                    TfYwpnPipenode ywpnPipenode = tfYwpnPipenodeCode2Name(pipenode);
                    result.add(ywpnPipenode);
                }
            }
        }
        List<Map<String, Object>> returnMap = new ArrayList<>();
        Map<String, Object> parentMap = new HashMap<>();
        String resultListToString = JSONArray.toJSONStringWithDateFormat(result,"yyyy-MM-dd");
        List<Map> result1 = JSONObject.parseArray(resultListToString , Map.class);
        parentMap.put("list", result1);
        returnMap.add(parentMap);
        return returnMap;
    }

    private TfYwpnPipenode tfYwpnPipenodeCode2Name(TfYwpnPipenode pipenode) {
        if(StringUtils.isNotEmpty(pipenode.getAspecttype())) {
            String aspecttype = codeClient.getname(Long.parseLong(pipenode.getAspecttype()));
            if (StringUtils.isNotBlank(aspecttype)) {
                pipenode.setAspecttype(aspecttype);
            }
        }
        if(StringUtils.isNotEmpty(pipenode.getFixturetype())) {
            String fixturetype = codeClient.getname(Long.parseLong(pipenode.getFixturetype()));
            if (StringUtils.isNotBlank(fixturetype)) {
                pipenode.setFixturetype(fixturetype);
            }
        }
        if(StringUtils.isNotEmpty(pipenode.getPrecisionclass())) {
            String precisionclass = codeClient.getname(Long.parseLong(pipenode.getPrecisionclass()));
            if (StringUtils.isNotBlank(precisionclass)) {
                pipenode.setPrecisionclass(precisionclass);
            }
        }
        if(StringUtils.isNotEmpty(pipenode.getManholetype())) {
            String manholetype = codeClient.getname(Long.parseLong(pipenode.getManholetype()));
            if (StringUtils.isNotBlank(manholetype)) {
                pipenode.setManholetype(manholetype);
            }
        }
        if(StringUtils.isNotEmpty(pipenode.getManholematerial())) {
            String manholematerial = codeClient.getname(Long.parseLong(pipenode.getManholematerial()));
            if (StringUtils.isNotBlank(manholematerial)) {
                pipenode.setManholematerial(manholematerial);
            }
        }
        if(StringUtils.isNotEmpty(pipenode.getWelltype())) {
            String welltype = codeClient.getname(Long.parseLong(pipenode.getWelltype()));
            if (StringUtils.isNotBlank(welltype)) {
                pipenode.setWelltype(welltype);
            }
        }
        if(StringUtils.isNotEmpty(pipenode.getWellmateial())) {
            String wellmateial = codeClient.getname(Long.parseLong(pipenode.getWellmateial()));
            if (StringUtils.isNotBlank(wellmateial)) {
                pipenode.setWellmateial(wellmateial);
            }
        }
        return pipenode;
    }

    @Override
    public Boolean excelDataStorage(List<Object> dataList) {
        return null;
    }
}
