package com.fjhx.service.excel.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.base.BaseEntity;
import com.fjhx.base.Condition;
import com.fjhx.constants.VRedisKeyConstant;
import com.fjhx.entity.FileInfo;
import com.fjhx.entity.excel.ExcelImportLog;
import com.fjhx.feign.IFileClient;
import com.fjhx.mapper.excel.ExcelImportLogMapper;
import com.fjhx.params.excel.ExcelProcessingProgress;
import com.fjhx.service.excel.ExcelImportLogService;
import com.fjhx.utils.Assert;
import com.fjhx.utils.wrapperUtil.IWrapper;
import org.springblade.core.excel.support.ExcelException;
import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-12-07
 */
@Service
public class ExcelImportLogServiceImpl extends ServiceImpl<ExcelImportLogMapper, ExcelImportLog> implements ExcelImportLogService {

    @Resource
    private BladeRedis bladeRedis;

    @Autowired
    private IFileClient fileClient;

    @Override
    public void saveLog(Long flag, Integer businessType, MultipartFile file) {

        R<FileInfo> fileInfoR = fileClient.uploadFile(file);
        FileInfo result = Assert.result(fileInfoR);

        // 上传记录记录数据库
        ExcelImportLog excelImportLog = new ExcelImportLog();
        excelImportLog.setId(flag);
        excelImportLog.setBusinessType(businessType);
        excelImportLog.setStatus(1);
        excelImportLog.setFileId(result.getId());
        excelImportLog.setFileName(result.getFileName());
        excelImportLog.setFilePath(result.getFilePath());
        excelImportLog.setCreateTime(new Date());
        excelImportLog.setCreateUser(AuthUtil.getUserId());
        save(excelImportLog);
    }

    /**
     * 更新导入记录
     *
     * @param start  开始导入时间戳
     * @param flag   标记
     * @param status 状态
     */
    public void editLog(Long start, Long flag, Integer status) {
        long end = System.currentTimeMillis();

        update(q -> q
                .eq(ExcelImportLog::getId, flag)
                .set(ExcelImportLog::getStatus, status)
                .set(ExcelImportLog::getExecuteTime, (end - start) / 1000)
                .set(BaseEntity::getUpdateTime, new Date())
        );
    }

    @Override
    public Page<ExcelImportLog> getPage(Condition condition) {
        IWrapper<ExcelImportLog> wrapper = IWrapper.getWrapper(condition);
        wrapper.eq(ExcelImportLog::getBusinessType);
        wrapper.periodTime(ExcelImportLog::getCreateTime);
        wrapper.orderByDesc(ExcelImportLog::getId);

        return page(condition.getPage(), wrapper);
    }

    /**
     * 插入缓存信息
     *
     * @param flag                    缓存标记
     * @param excelProcessingProgress 缓存对象
     */
    private synchronized void setProgress(Long flag, ExcelProcessingProgress excelProcessingProgress) {
        bladeRedis.setEx(VRedisKeyConstant.EXCEL_FLAG_KEY + flag, excelProcessingProgress, 60L * 60 * 12);
    }

    /**
     * 验证并更新缓存信息
     *
     * @param flag       缓存标记
     * @param status     状态
     * @param percentage 进度
     */
    private synchronized void verificationAndUpdateProgress(Long flag, Integer status, Integer percentage) {
        ExcelProcessingProgress excelProcessingProgress = bladeRedis.get(VRedisKeyConstant.EXCEL_FLAG_KEY + flag);
        if (ObjectUtil.isNull(excelProcessingProgress) || excelProcessingProgress.getStatus().equals(6)) {
            throw new ExcelException("取消导入");
        }

        excelProcessingProgress.setStatus(status);
        excelProcessingProgress.setPercentage(percentage);
        bladeRedis.setEx(VRedisKeyConstant.EXCEL_FLAG_KEY + flag, excelProcessingProgress, 60L * 60 * 12);
    }

}
