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 com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.DateUtils;

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.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.dimp.mapper.DHighValueInfoPreImpMapper;
import com.ruoyi.dimp.mapper.DPatentEffectiveInfoPreImpMapper;
import com.ruoyi.dimp.domain.DHighValueInfoPreImp;
import com.ruoyi.dimp.domain.DPatentEffectiveInfoPreImp;
import com.ruoyi.dimp.service.IDPatentEffectiveInfoPreImpService;

/**
 * 专利有效导入记录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-03-19
 */
@Service
public class DPatentEffectiveInfoPreImpServiceImpl extends ServiceImpl<DPatentEffectiveInfoPreImpMapper, DPatentEffectiveInfoPreImp> implements IDPatentEffectiveInfoPreImpService {

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

	@Autowired
	private RuoYiConfig ruoYiConfig;
	@Autowired
	private DPatentEffectiveInfoPreImpMapper dPatentEffectiveInfoPreImpMapper;
	
	public String getImpPath() {
		return ruoYiConfig.getProfile() + "/DPatentEffectiveInfoPreImp";
	}

	
	public DPatentEffectiveInfoPreImp importData(MultipartFile file,String impBatch) {
		DPatentEffectiveInfoPreImp imp = new DPatentEffectiveInfoPreImp();
		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);
			
			dPatentEffectiveInfoPreImpMapper.insert(imp);

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

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