package com.ruoyi.adjust.service.impl;

import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.adjust.domain.AsynchExpLog;
import com.ruoyi.adjust.mapper.AsynchExpLogMapper;
import com.ruoyi.adjust.service.IAsynchExpLogService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 导步导出日志Service业务层处理
 * 
 * @author klbc
 * @date 2022-05-22
 */
@Service
public class AsynchExpLogServiceImpl implements IAsynchExpLogService 
{
    @Autowired
    private AsynchExpLogMapper asynchExpLogMapper;
    
    public static Map<String,ExcelUtil.ExpStatus> EXP_STATUS_MAP=new ConcurrentHashMap<String,ExcelUtil.ExpStatus>();

    /**
     * 查询导步导出日志
     * 
     * @param id 导步导出日志主键
     * @return 导步导出日志
     */
    @Override
    public AsynchExpLog selectAsynchExpLogById(Long id)
    {
        return asynchExpLogMapper.selectAsynchExpLogById(id);
    }

    /**
     * 查询导步导出日志列表
     * 
     * @param asynchExpLog 导步导出日志
     * @return 导步导出日志
     */
    @Override
    public List<AsynchExpLog> selectAsynchExpLogList(AsynchExpLog asynchExpLog)
    {
    	List<AsynchExpLog>list=asynchExpLogMapper.selectAsynchExpLogList(asynchExpLog);
    	for(AsynchExpLog log:list) {
    		Long usedTime=log.getExpUsedTime();
    		
//    		asynchExpLog.setExpStatus("1")
    		if("1".equals(log.getExpStatus())) {//导出中
    			String key = "DPatentDataCollationInfoExp-"+log.getId();
    			if(EXP_STATUS_MAP.containsKey(key)) {
    				ExcelUtil.ExpStatus status=EXP_STATUS_MAP.get(key);
    				log.setCurrCnt(status.currRow);
    				
    				if(log.getTotalCnt()!=null && log.getTotalCnt().longValue()>0) {
    					
    					long curr=status.currRow;
        				long total=log.getTotalCnt();
        				double rated=(curr*100+0.0)/total;
        				DecimalFormat df = new DecimalFormat();
        				df.applyPattern("#.#");
        				String rate=df.format(rated)+"%";
        				log.setCurrRate(rate);
    				}else {
    					log.setCurrRate("--");
    				}
    			}else {
    				log.setCurrCnt(0L);
        			log.setCurrRate("0%");
        			log.setExpStatus("4");//导出中止了
    			}
    			
    		}else if("2".equals(log.getExpStatus())) {
    			log.setCurrCnt(log.getTotalCnt());
    			log.setCurrRate("100%");
    		}else {
    			log.setCurrCnt(0L);
    			log.setCurrRate("0%");
    		}
    	}
    	
        return list;
    }

    /**
     * 新增导步导出日志
     * 
     * @param asynchExpLog 导步导出日志
     * @return 结果
     */
    @Override
    public int insertAsynchExpLog(AsynchExpLog asynchExpLog)
    {
        asynchExpLog.setCreateTime(DateUtils.getNowDate());
        return asynchExpLogMapper.insertAsynchExpLog(asynchExpLog);
    }

    /**
     * 修改导步导出日志
     * 
     * @param asynchExpLog 导步导出日志
     * @return 结果
     */
    @Override
    public int updateAsynchExpLog(AsynchExpLog asynchExpLog)
    {
        asynchExpLog.setUpdateTime(DateUtils.getNowDate());
        return asynchExpLogMapper.updateAsynchExpLog(asynchExpLog);
    }

    /**
     * 批量删除导步导出日志
     * 
     * @param ids 需要删除的导步导出日志主键
     * @return 结果
     */
    @Override
    public int deleteAsynchExpLogByIds(Long[] ids)
    {
        return asynchExpLogMapper.deleteAsynchExpLogByIds(ids);
    }

    /**
     * 删除导步导出日志信息
     * 
     * @param id 导步导出日志主键
     * @return 结果
     */
    @Override
    public int deleteAsynchExpLogById(Long id)
    {
        return asynchExpLogMapper.deleteAsynchExpLogById(id);
    }
}
