package com.mx.cneeds.server.logupload.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mx.cneeds.common.pager.Constant;
import com.mx.cneeds.server.dao.DeviceLogmediaDao;
import com.mx.cneeds.server.entity.DeviceLogEntity;
import com.mx.cneeds.server.entity.DeviceLogmediaEntity;
import com.mx.cneeds.server.logupload.service.DeviceLogMediaService;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName DeviceLogMediaServiceImpl
 * @Author zzrdark
 * @Date 2020-04-23 17:19
 * @Description TODO
 **/
@Service
public class DeviceLogMediaServiceImpl extends ServiceImpl<DeviceLogmediaDao, DeviceLogmediaEntity> implements DeviceLogMediaService  {

    @Override
    public void savewechatMedias(Long logId, List<Map<String,String>> fileMaps){
        List<DeviceLogmediaEntity> deviceLogmediaEntities = new LinkedList<DeviceLogmediaEntity>();
        for (Map<String,String> fileMap:fileMaps){
            DeviceLogmediaEntity deviceLogmediaEntity = new DeviceLogmediaEntity();
            deviceLogmediaEntity.setLocationUrl(fileMap.get(Constant.WechatFileDtoProp.Url.getKey()));
            deviceLogmediaEntity.setLogId(logId);
            deviceLogmediaEntity.setType(fileMap.get(Constant.WechatFileDtoProp.Url.getKey()).substring(fileMap.get(Constant.WechatFileDtoProp.Url.getKey()).indexOf(".")+1));
            deviceLogmediaEntity.setFileSize(Long.valueOf(fileMap.get(Constant.WechatFileDtoProp.Size.getKey())));
            deviceLogmediaEntities.add(deviceLogmediaEntity);
        }
        saveBatch(deviceLogmediaEntities);
    }

    @Override
    public void savewechatMedia(Long logId, Long fileSize, String locationUrl){
        DeviceLogmediaEntity deviceLogmediaEntity = new DeviceLogmediaEntity();
        deviceLogmediaEntity.setLocationUrl(locationUrl);
        deviceLogmediaEntity.setLogId(logId);
        deviceLogmediaEntity.setType(locationUrl.substring(locationUrl.lastIndexOf(".")+1));
        deviceLogmediaEntity.setFileSize(fileSize);
        save(deviceLogmediaEntity);
    }

}
