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.TfYwpnPipeService;
import com.tofly.xrtymis.custom.service.export.ExportPipeService;
import com.tofly.xrtymis.entity.TfYwpnHydrant;
import com.tofly.xrtymis.entity.TfYwpnPipe;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;

@AllArgsConstructor
@Service("exportPipeService")
public class ExportPipeServiceImpl implements ExportPipeService {

    private final TfYwpnPipeService pipeService;
    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<TfYwpnPipe> result = new ArrayList<>();
        if (list.size() > 0 ) {
            for (String id : list) {
                TfYwpnPipe pipe = pipeService.getById(id);
                if (pipe != null) {
                    TfYwpnPipe ywpnPipe = tfYwpnPipeCode2Name(pipe);
                    result.add(ywpnPipe);
                }
            }
        }
        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 TfYwpnPipe tfYwpnPipeCode2Name(TfYwpnPipe pipe) {
        if(StringUtils.isNotEmpty(pipe.getStartctype())) {
            String startctype = codeClient.getname(Long.parseLong(pipe.getStartctype()));
            if (StringUtils.isNotBlank(startctype)) {
                pipe.setStartctype(startctype);
            }
        }
        if(StringUtils.isNotEmpty(pipe.getEndctype())) {
            String endctype = codeClient.getname(Long.parseLong(pipe.getEndctype()));
            if (StringUtils.isNotBlank(endctype)) {
                pipe.setEndctype(endctype);
            }
        }
        if(StringUtils.isNotEmpty(pipe.getMaterial())) {
            String material = codeClient.getname(Long.parseLong(pipe.getMaterial()));
            if (StringUtils.isNotBlank(material)) {
                pipe.setMaterial(material);
            }
        }
        if(StringUtils.isNotEmpty(pipe.getBurytype())) {
            String burytype = codeClient.getname(Long.parseLong(pipe.getBurytype()));
            if (StringUtils.isNotBlank(burytype)) {
                pipe.setBurytype(burytype);
            }
        }
        if(StringUtils.isNotEmpty(pipe.getUsestatus())) {
            String usestatus = codeClient.getname(Long.parseLong(pipe.getUsestatus()));
            if (StringUtils.isNotBlank(usestatus)) {
                pipe.setUsestatus(usestatus);
            }
        }
        if(StringUtils.isNotEmpty(pipe.getLinecode())) {
            String linecode = codeClient.getname(Long.parseLong(pipe.getLinecode()));
            if (StringUtils.isNotBlank(linecode)) {
                pipe.setLinecode(linecode);
            }
        }
        if(StringUtils.isNotEmpty(pipe.getDirection())) {
            String direction = codeClient.getname(Long.parseLong(pipe.getDirection()));
            if (StringUtils.isNotBlank(direction)) {
                pipe.setDirection(direction);
            }
        }
        if(StringUtils.isNotEmpty(pipe.getIsantirot())) {
            String isantirot = codeClient.getname(Long.parseLong(pipe.getIsantirot()));
            if (StringUtils.isNotBlank(isantirot)) {
                pipe.setIsantirot(isantirot);
            }
        }
        if(StringUtils.isNotEmpty(pipe.getDrivematerial())) {
            String drivematerial = codeClient.getname(Long.parseLong(pipe.getDrivematerial()));
            if (StringUtils.isNotBlank(drivematerial)) {
                pipe.setDrivematerial(drivematerial);
            }
        }
        if(StringUtils.isNotEmpty(pipe.getDepthlocation())) {
            String depthlocation = codeClient.getname(Long.parseLong(pipe.getDepthlocation()));
            if (StringUtils.isNotBlank(depthlocation)) {
                pipe.setDepthlocation(depthlocation);
            }
        }
        return pipe;
    }

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