package com.ruoyi.dimp.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.dimp.domain.DHighValueInfoPreImp;
import com.ruoyi.dimp.mapper.DHighValueInfoPreImpMapper;
import com.ruoyi.dimp.service.IDHighValueInfoPreImpService;

/**
 * 高价值数据导入记录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-03-19
 */
@Service
public class DHighValueInfoPreImpServiceImpl extends ServiceImpl<DHighValueInfoPreImpMapper, DHighValueInfoPreImp>
		implements IDHighValueInfoPreImpService {

	private static final Logger logger = LoggerFactory.getLogger(DHighValueInfoPreImpServiceImpl.class);

	@Autowired
	private RuoYiConfig ruoYiConfig;
	@Autowired
	private DHighValueInfoPreImpMapper dHighValueInfoPreImpMapper;

	public DHighValueInfoPreImp importData(MultipartFile file,String impBatch) {
		DHighValueInfoPreImp imp = new DHighValueInfoPreImp();
		try {
			String impres = FileUploadUtils.upload(getImpPath(), file);

			impres = impres.substring(Constants.RESOURCE_PREFIX.length());
			impres = RuoYiConfig.getProfile() + impres;

			imp.setFilePath(impres);
			
			String filePath = impres;
			File path = new File(filePath).getParentFile();
			
			String fileName = new File(impres).getName();
			
			String resFileName = fileName.substring(0, fileName.lastIndexOf(".")) + ""
					+ fileName.substring(fileName.lastIndexOf("."), fileName.length());
			
			int dirLastIndex = RuoYiConfig.getProfile().length() + 1;
			String currentDir = StringUtils.substring(path.getAbsolutePath(), dirLastIndex);
			String res = Constants.RESOURCE_PREFIX + "/" + currentDir + "/" + resFileName;

			imp.setFileUrl(ruoYiConfig.getFilePath() + res);
			
			imp.setPreImpTime(new Date());
			imp.setFileName(fileName);
			imp.setImpState("0");//
			imp.setDataSrc("Web导入");
			imp.setInsertNum(0L);
			imp.setSkipNum(0L);
			imp.setUpdateNum(0L);
			imp.setErrorNum(0L);
			imp.setErrReason("系统已接收");
			imp.setImpBatch(impBatch);
			dHighValueInfoPreImpMapper.insert(imp);

		} catch (IOException e) {
			logger.error("高价值数据导入失败," + e.getMessage(), e);
			String res = "导入失败，原因:" + e.getMessage();
			imp.setErrReason(res);
		}

		return imp;
	}

	public String getImpPath() {
		return ruoYiConfig.getProfile() + "/DHighValueInfoPreImp";
	}

	/**
	 * 查询高价值数据导入记录列表
	 *
	 * @param dHighValueInfoPreImp 高价值数据导入记录
	 * @return 高价值数据导入记录集合
	 * @date 2023-03-19
	 */
	@Override
	public List<DHighValueInfoPreImp> queryList(DHighValueInfoPreImp dHighValueInfoPreImp) {
		QueryWrapper<DHighValueInfoPreImp> queryWrapper = Wrappers.query();
		if (StringUtils.isNotNull(dHighValueInfoPreImp.getPreImpTime())) {
			queryWrapper.eq("pre_imp_time", dHighValueInfoPreImp.getPreImpTime());
		}
		if (StringUtils.isNotEmpty(dHighValueInfoPreImp.getFileName())) {
			queryWrapper.like("file_name", dHighValueInfoPreImp.getFileName());
		}
		if (StringUtils.isNotEmpty(dHighValueInfoPreImp.getFileUrl())) {
			queryWrapper.eq("file_url", dHighValueInfoPreImp.getFileUrl());
		}
		if (StringUtils.isNotEmpty(dHighValueInfoPreImp.getImpState())) {
			queryWrapper.eq("imp_state", dHighValueInfoPreImp.getImpState());
		}
		if (StringUtils.isNotEmpty(dHighValueInfoPreImp.getTenantNo())) {
			queryWrapper.eq("tenant_no", dHighValueInfoPreImp.getTenantNo());
		}
		if (StringUtils.isNotNull(dHighValueInfoPreImp.getInsertNum())) {
			queryWrapper.eq("insert_num", dHighValueInfoPreImp.getInsertNum());
		}
		if (StringUtils.isNotNull(dHighValueInfoPreImp.getUpdateNum())) {
			queryWrapper.eq("update_num", dHighValueInfoPreImp.getUpdateNum());
		}
		if (StringUtils.isNotNull(dHighValueInfoPreImp.getSkipNum())) {
			queryWrapper.eq("skip_num", dHighValueInfoPreImp.getSkipNum());
		}
		if (StringUtils.isNotNull(dHighValueInfoPreImp.getErrorNum())) {
			queryWrapper.eq("error_num", dHighValueInfoPreImp.getErrorNum());
		}
		if (StringUtils.isNotEmpty(dHighValueInfoPreImp.getDataSrc())) {
			queryWrapper.eq("data_src", dHighValueInfoPreImp.getDataSrc());
		}
		if (StringUtils.isNotEmpty(dHighValueInfoPreImp.getErrReason())) {
			queryWrapper.eq("err_reason", dHighValueInfoPreImp.getErrReason());
		}
		if (StringUtils.isNotEmpty(dHighValueInfoPreImp.getResFileName())) {
			queryWrapper.like("res_file_name", dHighValueInfoPreImp.getResFileName());
		}
		if (StringUtils.isNotEmpty(dHighValueInfoPreImp.getResFileUrl())) {
			queryWrapper.eq("res_file_url", dHighValueInfoPreImp.getResFileUrl());
		}
		queryWrapper.orderByDesc("id");
		return this.list(queryWrapper);
	}
}
