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.DPopulationDataInfoPreImpMapper;
import com.ruoyi.dimp.domain.DHighValueInfoPreImp;
import com.ruoyi.dimp.domain.DPopulationDataInfoPreImp;
import com.ruoyi.dimp.service.IDPopulationDataInfoPreImpService;

/**
 * 云南人口数据导入记录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-03-19
 */
@Service
public class DPopulationDataInfoPreImpServiceImpl extends ServiceImpl<DPopulationDataInfoPreImpMapper, DPopulationDataInfoPreImp> implements IDPopulationDataInfoPreImpService {

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

	@Autowired
	private RuoYiConfig ruoYiConfig;
	@Autowired
	private DPopulationDataInfoPreImpMapper dPopulationDataInfoPreImpMapper;
	
	public String getImpPath() {
		return ruoYiConfig.getProfile() + "/DPopulationDataInfoPreImp";
	}

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

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

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