package com.inspect.metadata.version.service.impl;

import com.inspect.metadata.eqpbook.domain.vo.PatrolTaskFtp;
import com.inspect.metadata.eqpbook.mapper.BasedataEqpBookMapper;
import com.inspect.metadata.station.domain.BasedataStation;
import com.inspect.metadata.station.mapper.BasedataStationMapper;
import com.inspect.metadata.version.domain.VersionManagement;
import com.inspect.metadata.version.mapper.VersionManagementMapper;
import com.inspect.metadata.version.service.IVersionManagementService;
import com.inspect.base.core.domain.DataMsg;
import com.inspect.base.core.utils.DateUtils;
import com.inspect.base.core.utils.SFTPUtil;

import java.io.InputStream;
import java.util.List;
import javax.annotation.Resource;

import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Service;

@Service
public class VersionManagementServiceImpl implements IVersionManagementService {
    @Resource
    private VersionManagementMapper versionManagementMapper;
    @Resource
    private BasedataStationMapper basedataStationMapper;
    @Resource
    private BasedataEqpBookMapper basedataEqpbookMapper;

    public VersionManagement selectVersionManagementByVersionId(Long versionId) {
        return this.versionManagementMapper.selectVersionManagementByVersionId(versionId);
    }

    public List<VersionManagement> selectVersionManagementList(VersionManagement versionManagement) {
        return this.versionManagementMapper.selectVersionManagementList(versionManagement);
    }

    public int insertVersionManagement(VersionManagement versionManagement) {
        versionManagement.setCreateTime(DateUtils.getNowDate());
        return this.versionManagementMapper.insertVersionManagement(versionManagement);
    }

    public int updateVersionManagement(VersionManagement versionManagement) {
        versionManagement.setUpdateTime(DateUtils.getNowDate());
        return this.versionManagementMapper.updateVersionManagement(versionManagement);
    }

    public int deleteVersionManagementByVersionIds(Long[] versionIds) {
        return this.versionManagementMapper.deleteVersionManagementByVersionIds(versionIds);
    }

    public int deleteVersionManagementByVersionId(Long versionId) {
        return this.versionManagementMapper.deleteVersionManagementByVersionId(versionId);
    }

    public List<VersionManagement> getNewVersionInfo() {
        return this.versionManagementMapper.getNewVersionInfo();
    }

    public String uploadPicMap(InputStream in, String filename) {
        String fileType = FilenameUtils.getExtension(filename).toLowerCase();
        filename = "EqpBookMap" + System.currentTimeMillis();
        PatrolTaskFtp patrolTaskFtp = this.basedataEqpbookMapper.selectPatrolTaskFtpByLineId(3L);
        List<BasedataStation> basedataStation = this.basedataStationMapper.selectBasedataStationList(new BasedataStation());
        String mapPath = basedataStation.get(0).getStationNo() + "/Map/";
        DataMsg DataMsg = new DataMsg();
        DataMsg.setIp(patrolTaskFtp.getFtpIp());
        DataMsg.setPort(patrolTaskFtp.getFtpPort());
        DataMsg.setUsername(patrolTaskFtp.getFtpUserName());
        DataMsg.setPassword(patrolTaskFtp.getFtpPwd());
        DataMsg.setBasePath(patrolTaskFtp.getFtpPath());
        DataMsg.setType("0");
        DataMsg.setFilePath(mapPath);
        DataMsg.setFileName(filename + "." + fileType);
        DataMsg.setInputStream(in);
        DataMsg.setIsImplicit("true");
        SFTPUtil.uploadFileForXml(DataMsg);
        return mapPath + filename + "." + fileType;
    }
}
