package com.njcn.product.advance.responsility.service.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.oss.utils.FileStorageUtil;
import com.njcn.product.advance.responsility.mapper.RespDataResultMapper;
import com.njcn.product.advance.responsility.pojo.constant.CalculationType;
import com.njcn.product.advance.responsility.pojo.dto.CustomerData;
import com.njcn.product.advance.responsility.pojo.dto.CustomerResponsibility;
import com.njcn.product.advance.responsility.pojo.dto.ResponsibilityResult;
import com.njcn.product.advance.responsility.pojo.po.RespData;
import com.njcn.product.advance.responsility.pojo.po.RespDataResult;
import com.njcn.product.advance.responsility.service.IRespDataResultService;
import com.njcn.product.advance.responsility.service.IRespDataService;
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<>();
        RespData respDataQuery = respDataService.getById(id);
        String[] split = respDataQuery.getDataTimes().split(StrPool.COMMA);
        if (Objects.isNull(time)) {
            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);
                //处理排名前10数据
                InputStream respStream = fileStorageUtil.getFileStream(respDataResult.getUserResponsibility());
                String respStr = IoUtil.readUtf8(respStream);
                List<CustomerResponsibility> respData = JSONArray.parseArray(respStr, CustomerResponsibility.class);
                if(Objects.nonNull(respDataQuery.getType()) && respDataQuery.getType().equals(CalculationType.SYSTEM_TYPE.getCode())){
                    customerData.forEach(item->{if(item.getCustomerName().contains("(")) {
                        item.setCustomerName(item.getCustomerName().substring(0,item.getCustomerName().indexOf("(")));
                    }
                    });
                    respData.forEach(item->{if(item.getCustomerName().contains("(")) {
                        item.setCustomerName(item.getCustomerName().substring(0,item.getCustomerName().indexOf("(")));
                    }
                    });
                }
                responsibilityResult.setResponsibilities(respData);
                responsibilityResult.setDatas(customerData);

                responsibilityResults.add(responsibilityResult);
            }
        }
        return responsibilityResults;
    }
}
