package com.tofly.xrtygis.custom.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tofly.common.core.util.DateUtil;
import com.tofly.common.oauth.exception.ToflyDeniedException;
import com.tofly.xrtygis.custom.entity.vo.WaterTrendVo;
import com.tofly.xrtygis.custom.mapper.ExportAboutMapper;
import com.tofly.xrtygis.custom.service.WaterTrendService;
import com.tofly.xrtygis.entity.Scadastaitem;
import com.tofly.xrtygis.entity.Scadastation;
import com.tofly.xrtygis.service.ScadastaitemService;
import com.tofly.xrtygis.service.ScadastationService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 陈浩
 */
@AllArgsConstructor
@Service("waterTrendService")
public class WaterTrendServiceImpl implements WaterTrendService {

    private final ScadastaitemService scadastaitemService;

    private final ExportAboutMapper exportAboutMapper;

    private final ScadastationService scadastationService;

    /**
     * excel获取导出数据
     *
     * @param dto          筛选条件对象
     * @param templateFlag 0为正式导出/1为演示模板导出
     * @return 封装好的导出对象
     */
    @Override
    public List<Map<String, Object>> getExportData(String dto, String templateFlag) {
        List<Map<String, Object>> returnMap = new ArrayList<>();
        Map<String, Object> parentMap = new HashMap<>(12);
        WaterTrendVo waterTrendVo = JSONObject.parseObject(dto, WaterTrendVo.class);

        Scadastation station = scadastationService.getById(waterTrendVo.getStid());

        String[] itnms = waterTrendVo.getItnm().split(",");

        //进水量指标
        Scadastaitem waterIngress = scadastaitemService.getOne(Wrappers.<Scadastaitem>lambdaQuery()
                .select(Scadastaitem::getItcd)
                .eq(Scadastaitem::getItnm, itnms[0])
                .eq(Scadastaitem::getStationId, waterTrendVo.getStid()));
        if (waterIngress == null) {
            throw new ToflyDeniedException("测站<" + station.getStnm() + ">未绑定指标项：" + itnms[0]);
        }

        String wiItcd = waterIngress.getItcd();

        //供水量指标
        Scadastaitem waterSupply = scadastaitemService.getOne(Wrappers.<Scadastaitem>lambdaQuery()
                .select(Scadastaitem::getItcd)
                .eq(Scadastaitem::getItnm, itnms[1])
                .eq(Scadastaitem::getStationId, waterTrendVo.getStid()));

        if (waterSupply == null) {
            throw new ToflyDeniedException("测站<" + station.getStnm() + ">未绑定指标项：" + itnms[1]);
        }

        String wsItcd = waterSupply.getItcd();

        List<Map> waterInfo;

        switch (waterTrendVo.getCode()) {
            case "0":
                waterInfo = exportAboutMapper.viewWaterInfoByDay(wiItcd, wsItcd, waterTrendVo.getStart(), waterTrendVo.getEnd());
                break;
            case "1":
                waterInfo = exportAboutMapper.viewWaterInfoByMonth(wiItcd, wsItcd, waterTrendVo.getStart(), waterTrendVo.getEnd());
                break;
            case "2":

                int month = DateUtil.getMonth(DateUtil.formatDate(waterTrendVo.getStart(), "yyyy-MM")) + 1;

                String mon = "";
                if (month < 10) {
                    mon = "%0" + month;
                } else {
                    mon = "%" + month;
                }
                waterInfo = exportAboutMapper.viewWaterContrast(wiItcd, wsItcd, waterTrendVo.getStart(), waterTrendVo.getEnd(), mon);
                break;
            default:
                throw new ToflyDeniedException("code error");
        }

        parentMap.put("list", waterInfo);
        returnMap.add(parentMap);

        return returnMap;
    }

    /**
     * excel导入的数据落库
     *
     * @param dataList 导入的数据
     * @return 是否存储成功
     */
    @Override
    public Boolean excelDataStorage(List<Object> dataList) {
        return null;
    }
}
