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

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.common.core.util.StringUtil;
import com.tofly.xrtygis.entity.TfCameraManage;
import com.tofly.xrtygis.entity.TfDeviceArchiveManage;
import com.tofly.xrtygis.entity.TfIndexAllocation;
import com.tofly.xrtygis.entity.dto.TfCameraManageDto;
import com.tofly.xrtygis.entity.dto.TfDeviceArchiveImportDto;
import com.tofly.xrtygis.service.TfCameraManageService;
import org.springframework.stereotype.Service;
import com.tofly.xrtygis.mapper.TfCameraManageMapper;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

/**
 * 摄像头管理
 *
 * @author admin
 * @date Fri Mar 11 00:00:00 CST 2022
 */
@Service("tfCameraManageService")
public class TfCameraManageServiceImpl extends ServiceImpl<TfCameraManageMapper, TfCameraManage> implements TfCameraManageService {
    @Override
    public boolean ImportData(MultipartFile file) {
        if(file==null||file.isEmpty()){
            throw new RuntimeException("导入文件不能为空");
        }

        List<TfCameraManageDto> dtos = null;
        try {
            dtos=getExeData(file,"");
        }catch (Exception e){
            throw new RuntimeException("文件异常");
        }
        if(CollUtil.isEmpty(dtos)){
            throw new RuntimeException("数据不存在");
        }

        for (TfCameraManageDto dto:dtos
        ) {
            TfCameraManage tfCameraManage= this.baseMapper.selectList(Wrappers.<TfCameraManage>lambdaQuery()
                    .eq(TfCameraManage::getCode,dto.getCode().replace("\t",""))
                   .eq(TfCameraManage::getName,dto.getName().replace("\t",""))).stream().findFirst().orElse(new TfCameraManage());


            if(tfCameraManage!=null && tfCameraManage.getId()!=null && tfCameraManage.getId()>0){
                TfCameraManage model=new TfCameraManage();
                model.setCode(dto.getCode().replace("\t",""));
                model.setId(tfCameraManage.getId());
                model.setName(dto.getName().replace("\t",""));
                model.setDescribe(dto.getDescribe().replace("\t",""));
               model.setManufactor(dto.getManufactor().replace("\t",""));
                model.setModel(dto.getModel().replace("\t",""));
                model.setLongitude(dto.getLongitude());
                model.setLatitude(dto.getLatitude());
              this.updateById(model);

            }else{

                TfCameraManage model=new TfCameraManage();
                model.setCode(dto.getCode().replace("\t",""));
                model.setName(dto.getName().replace("\t",""));
                model.setDescribe(dto.getDescribe().replace("\t",""));
                model.setBelongStatus("0");
                if(StringUtil.isNotEmpty(dto.getWaterInfoId())){
                    model.setBelongStatus("1");
                    model.setWaterId(Long.parseLong(dto.getWaterInfoId()));
                }
                model.setManufactor(dto.getManufactor().replace("\t",""));
                model.setModel(dto.getModel().replace("\t",""));
                model.setLongitude(dto.getLongitude());
                model.setLatitude(dto.getLatitude());
               this.save(model);

            }


        }
        return true;
    }

    private List<TfCameraManageDto> getExeData(MultipartFile fileList, String sheetName) throws Exception {
        InputStream inputStream = fileList.getInputStream();
        com.tofly.common.core.util.io.ExcelUtil opsExcel = new com.tofly.common.core.util.io.ExcelUtil(TfCameraManageDto.class);
        List<TfCameraManageDto> ips = opsExcel.importExcel(sheetName, inputStream);
        return ips;
    }
}
