package com.njcn.advance.service.responsibility.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.text.StrPool;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.advance.mapper.responsibility.RespDataResultMapper;
import com.njcn.advance.pojo.dto.responsibility.CustomerData;
import com.njcn.advance.pojo.dto.responsibility.CustomerResponsibility;
import com.njcn.advance.pojo.dto.responsibility.ResponsibilityResult;
import com.njcn.advance.pojo.po.responsibility.RespData;
import com.njcn.advance.pojo.po.responsibility.RespDataResult;
import com.njcn.advance.service.responsibility.IRespDataResultService;
import com.njcn.advance.service.responsibility.IRespDataService;
import com.njcn.oss.utils.FileStorageUtil;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hongawen
 * @since 2023-07-24
 */
@Service
public class RespDataResultServiceImpl extends ServiceImpl<RespDataResultMapper, RespDataResult> implements IRespDataResultService {

    @Resource
    private FileStorageUtil fileStorageUtil;

    @Lazy
    @Resource
    private IRespDataService respDataService;

    @Override
    public List<ResponsibilityResult> displayHistoryData(String id, Integer time) {
        List<ResponsibilityResult> responsibilityResults = new ArrayList<>();
        if (Objects.isNull(time)) {
            RespData respData = respDataService.getById(id);
            String[] split = respData.getDataTimes().split(StrPool.COMMA);
            time = Integer.parseInt(split[0]);
        }
        LambdaQueryWrapper<RespDataResult> respDataResultLambdaQueryWrapper = new LambdaQueryWrapper<>();
        respDataResultLambdaQueryWrapper.eq(RespDataResult::getResDataId, id)
                .eq(RespDataResult::getTime, time);
        List<RespDataResult> respDataResults = this.baseMapper.selectList(respDataResultLambdaQueryWrapper);
        if (CollectionUtil.isNotEmpty(respDataResults)) {
            ResponsibilityResult responsibilityResult;
            for (RespDataResult respDataResult : respDataResults) {
                responsibilityResult = new ResponsibilityResult();
                responsibilityResult.setLimitValue(String.valueOf(respDataResult.getLimitValue()));
                responsibilityResult.setLimitSTime(DateUtil.format(respDataResult.getStartTime(), DatePattern.NORM_DATETIME_PATTERN));
                responsibilityResult.setLimitETime(DateUtil.format(respDataResult.getEndTime(), DatePattern.NORM_DATETIME_PATTERN));
                responsibilityResult.setResponsibilityDataIndex(respDataResult.getResDataId());
                //处理时间轴数据
                InputStream timeDataStream = fileStorageUtil.getFileStream(respDataResult.getTimeData());
                String timeDataStr = IoUtil.readUtf8(timeDataStream);
                List<Long> timeData = JSONArray.parseArray(timeDataStr, Long.class);
                responsibilityResult.setTimeDatas(timeData);
                //处理用户详细数据
                InputStream userDetailStream = fileStorageUtil.getFileStream(respDataResult.getUserDetailData());
                String userDetailStr = IoUtil.readUtf8(userDetailStream);
                List<CustomerData> customerData = JSONArray.parseArray(userDetailStr, CustomerData.class);
                responsibilityResult.setDatas(customerData);
                //处理排名前10数据
                InputStream respStream = fileStorageUtil.getFileStream(respDataResult.getUserResponsibility());
                String respStr = IoUtil.readUtf8(respStream);
                List<CustomerResponsibility> respData = JSONArray.parseArray(respStr, CustomerResponsibility.class);
                responsibilityResult.setResponsibilities(respData);
                responsibilityResults.add(responsibilityResult);
            }
        }
        return responsibilityResults;
    }
}
