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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tofly.xrtymis.custom.commons.Constate;
import com.tofly.xrtymis.custom.service.TfYwpnWatermeterService;
import com.tofly.xrtymis.custom.service.export.ExportMeterService;
import com.tofly.xrtymis.custom.vo.TfYwpnWatermeterVO;
import com.tofly.xrtymis.entity.TfYwpnWatermeter;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;

@AllArgsConstructor
@Service("exportMeterService")
public class ExportMeterServiceImpl implements ExportMeterService {

    private final TfYwpnWatermeterService watermeterService;

    @Override
    public List<Map<String, Object>> getExportData(String dto, String templateFlag) {
        List<String> list = Arrays.asList(dto.split(Constate.CSV));
        ArrayList<TfYwpnWatermeterVO> result = new ArrayList<>();
        if (list.size() > 0 ) {
            for (String id : list) {
                List<TfYwpnWatermeter> meters = watermeterService.getBaseMapper().selectList(Wrappers.<TfYwpnWatermeter>lambdaQuery()
                        .eq(TfYwpnWatermeter::getRepId, id).eq(TfYwpnWatermeter::getStatus, "1"));
                if (meters != null && meters.size() > 0) {
                    for (TfYwpnWatermeter meter : meters) {
                        TfYwpnWatermeterVO vo = watermeterService.watermMter2VO(meter);
                        result.add(vo);
                    }
                }
            }
        }
        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;
    }

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