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.DMarketsubDataInfoPreImpMapper;
import com.ruoyi.dimp.domain.DHighValueInfoPreImp;
import com.ruoyi.dimp.domain.DMarketsubDataInfoPreImp;
import com.ruoyi.dimp.service.IDMarketsubDataInfoPreImpService;

/**
 * 市场主体数据导入记录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-03-19
 */
@Service
public class DMarketsubDataInfoPreImpServiceImpl extends ServiceImpl<DMarketsubDataInfoPreImpMapper, DMarketsubDataInfoPreImp> implements IDMarketsubDataInfoPreImpService {

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

	@Autowired
	private RuoYiConfig ruoYiConfig;
	@Autowired
	private DMarketsubDataInfoPreImpMapper dMarketsubDataInfoPreImpMapper;
	
	public String getImpPath() {
		return ruoYiConfig.getProfile() + "/DMarketsubDataInfoPreImp";
	}

	
	public DMarketsubDataInfoPreImp importData(MultipartFile file,String year) {
		DMarketsubDataInfoPreImp imp = new DMarketsubDataInfoPreImp();
		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.setYear(year);
			dMarketsubDataInfoPreImpMapper.insert(imp);

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

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