/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.watermeter.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.watermeter.custom.util.DateTimeUtil;
import com.tofly.watermeter.em.Constant;
import com.tofly.watermeter.entity.File;
import com.tofly.watermeter.entity.Readmeterhis;
import com.tofly.watermeter.mapper.FileMapper;
import com.tofly.watermeter.service.FileService;
import com.tofly.watermeter.service.ReadmeterhisService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 公共文件表： TF_YWPN_FILE_W
 *
 * @author admin
 * @date Wed Mar 10 00:00:00 CST 2021
 */
@Service("fileService")
@Slf4j
public class FileServiceImpl extends ServiceImpl<FileMapper, File> implements FileService {

    @Autowired
    private ReadmeterhisService readmeterhisService;

    @Override
    public Integer getMaxId() {
        return baseMapper.getMaxId();
    }

    @Override
    public void saveManul(File localFile) {
        baseMapper.saveManul(localFile);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void s(String strTime,String eTime){
        long l2 = System.currentTimeMillis();


        Date startTime = DateTimeUtil.convertLDTToDate(DateTimeUtil.fromString2LocalDateTime(strTime, DateTimeUtil.FULL_FORMAT));
        Date endTime = DateTimeUtil.convertLDTToDate(DateTimeUtil.fromString2LocalDateTime(eTime, DateTimeUtil.FULL_FORMAT));

        List<File> fileList = this.list(new QueryWrapper<File>().lambda()
                .eq(File::getTableName, "TF_YWPN_READMETERHIS_W")
                .between(File::getCreateTime, startTime, endTime)
                .isNotNull(File::getId)
                .isNotNull(File::getFileName)
                .select(File::getId, File::getFileName));
        Map<String, Long> fileMap = fileList.stream().collect(Collectors.toMap(File::getFileName, File::getId, (key1, key2) -> key2));
        List<Readmeterhis> readmeterhisList = readmeterhisService.list(new QueryWrapper<Readmeterhis>().lambda()
                .isNotNull(Readmeterhis::getImgUrl)
                .isNotNull(Readmeterhis::getSoleSign)
                .between(Readmeterhis::getUpdateDate, startTime, endTime)
                .select(Readmeterhis::getSoleSign, Readmeterhis::getImgUrl,Readmeterhis::getFileid));

        Map<String,String> result = new HashMap<>(12);
        // 抄表图片路径处理
        readmeterhisList.forEach(info->{
            String soleSign = info.getSoleSign();
            String imgUrl = info.getImgUrl();
            if(null!=imgUrl){
                // 先以,拆分
                List<String> collect = Arrays.stream(imgUrl.split(Constant.CSV))
                        .collect(Collectors.toList());
                // 再以最后/拆分
                collect.forEach(l->{
                    String substring = l.substring(l.lastIndexOf(Constant.SYSTEM_SEPARATOR)+1);
                    Long aLong = fileMap.get(substring);
                    if(null!=aLong){
                        String out = result.get(soleSign);
                        if(StringUtils.isNotEmpty(out)){
                            out = out+Constant.CSV+aLong;
                            result.put(soleSign,out);
                        }else {
                            result.put(soleSign,aLong+"");
                        }
                    }

                });

            }
        });
        log.info("成功替换文件列表: {}",JSON.toJSONString(result));
        log.info("成功替换文件大小: {}",result.size());
        readmeterhisList.forEach(readmeterhis -> {
            String soleSign = readmeterhis.getSoleSign();
            String fileId = result.get(soleSign);
            if(StringUtils.isNotEmpty(fileId)){
                readmeterhis.setFileid(fileId);
            }
        });
//        readmeterhisList.forEach(his->{
//            readmeterhisService.update(new UpdateWrapper<Readmeterhis>().lambda()
//                    .eq(Readmeterhis::getSoleSign,his.getSoleSign()).set(Readmeterhis::getFileid,his.getFileid()));
//        });
//        baseMapper.updateCbFileId(readmeterhisList);
        log.info("成功替换文件");
        long l1 = System.currentTimeMillis();
        log.info("耗时 :"+(l1-l2)+" ms");
    }
}
