/*
 * 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.xrtygis.entity.TfDeviceArchiveManage;
import com.tofly.xrtygis.entity.TfIndexAllocation;
import com.tofly.xrtygis.entity.dto.TfDeviceArchiveImportDto;
import com.tofly.xrtygis.mapper.TfDeviceArchiveManageMapper;
import com.tofly.xrtygis.mapper.TfIndexAllocationMapper;
import com.tofly.xrtygis.service.TfDeviceArchiveManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 设备档案管理
 *
 * @author admin
 * @date Fri Mar 11 00:00:00 CST 2022
 */
@Service("tfDeviceArchivesManageService")
public class TfDeviceArchiveManageServiceImpl extends ServiceImpl<TfDeviceArchiveManageMapper, TfDeviceArchiveManage> implements TfDeviceArchiveManageService {


    @Autowired
    private TfIndexAllocationMapper tfIndexAllocationMapper;

    @Override
    public List<Long> getIdList(String deviceName, List<Long> stationIdList, String pipeStationType) {
        return this.baseMapper.getIdList(deviceName, stationIdList, pipeStationType);
    }

    @Override
    public  List<Map<String,String>>getDeviceTypeReport(){
         return this.baseMapper.getDeviceTypeReport();

    }

    @Override
    @Transactional
    public boolean ImportData(MultipartFile file) {
        if(file==null||file.isEmpty()){
            throw new RuntimeException("导入文件不能为空");
        }

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

        for (TfDeviceArchiveImportDto dto:dtos
             ) {
            String variableCode=dto.getComCode()+dto.getModelSpecification();
            String code=dto.getComCode()+dto.getFCode();//设备编码

           Long deviceId=this.baseMapper.selectList(
                    Wrappers.<TfDeviceArchiveManage>lambdaQuery().
                            eq(TfDeviceArchiveManage::getCode,code)
            ).stream().findFirst().orElse(new TfDeviceArchiveManage()).getId();

            TfDeviceArchiveManage tfDeviceArchiveManage=new TfDeviceArchiveManage();
            tfDeviceArchiveManage.setType("1");
            tfDeviceArchiveManage.setDeviceType(dto.getDeviceType());
            tfDeviceArchiveManage.setCode(code);
            //tfDeviceArchiveManage.setModelSpecification(dto.getModelSpecification());
            tfDeviceArchiveManage.setName(dto.getName());
           if(deviceId==null || deviceId==0){

               this.save(tfDeviceArchiveManage); //保存设备
               deviceId=tfDeviceArchiveManage.getId();
           }else{
               tfDeviceArchiveManage.setId(deviceId);
               this.updateById(tfDeviceArchiveManage);
           }
           //保存指标
            TfIndexAllocation tfIndexAllocation=new TfIndexAllocation();
            tfIndexAllocation.setDeviceId(deviceId);
            tfIndexAllocation.setVariableName(dto.getVariableName());
            tfIndexAllocation.setVariableCode(variableCode);
            tfIndexAllocation.setIsAlarm("0");
            tfIndexAllocation.setType(dto.getIndexType());
            tfIndexAllocation.setDisplayName(dto.getVariableName());
            tfIndexAllocation.setArchivesCode(code);
          Long index_id= tfIndexAllocationMapper.selectList(Wrappers.<TfIndexAllocation>lambdaQuery()
                    .eq(TfIndexAllocation::getVariableCode,variableCode)
                    .eq(TfIndexAllocation::getDeviceId,deviceId)).stream().findFirst().orElse(new TfIndexAllocation())
                  .getId();
            if(index_id==null || index_id==0){
                tfIndexAllocationMapper.insert(tfIndexAllocation);

            }else{
                tfIndexAllocation.setId(index_id);
                tfIndexAllocationMapper.updateById(tfIndexAllocation);
            }

        }

        return true;
    }


    /**
     * 文件转换为对象数据
     * @param fileList 上传的文件
     * @param sheetName sheet名称
     * @return List<DispatchInfoDto>
     * @throws Exception
     */
    private List<TfDeviceArchiveImportDto> 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(TfDeviceArchiveImportDto.class);
        List<TfDeviceArchiveImportDto> ips = opsExcel.importExcel(sheetName, inputStream);
        return ips;
    }
}
