package com.engine.salary.service.impl;

import com.cloudstore.dev.api.util.Util_DataCache;
import com.engine.core.impl.Service;
import com.engine.salary.entity.progress.ProgressDTO;
import com.engine.salary.service.ProgressService;
import com.engine.salary.util.JsonUtil;
import org.apache.commons.lang3.StringUtils;
import weaver.wechat.util.Utils;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 进度条
 * <p>Copyright: Copyright (c) 2022</p>
 * <p>Company: 泛微软件</p>
 *
 * @author qiantao
 * @version 1.0
 **/
public class ProgressServiceImpl extends Service implements ProgressService {


    @Override
    public void initProgress(String cacheKey, ProgressDTO progressDTO) {
        Util_DataCache.setObjVal(cacheKey, JsonUtil.toJsonString(progressDTO));
    }

    @Override
    public synchronized void getAndAddCalculatedQty(String cacheKey, Integer calculatedQuantity) {
        String resultStr = (String) Util_DataCache.getObjVal(cacheKey);
        if (StringUtils.isNotEmpty(resultStr)) {
            ProgressDTO progressDTO = JsonUtil.parseObject(resultStr, ProgressDTO.class);
            if (progressDTO == null || !progressDTO.isStatus()) {
                return;
            }
            Integer currentCalculatedQuantity = progressDTO.getCalculatedQuantity() + calculatedQuantity;
            progressDTO.setCalculatedQuantity(
                    currentCalculatedQuantity > progressDTO.getTotalQuantity() ? progressDTO.getTotalQuantity() : currentCalculatedQuantity);
            BigDecimal progress = BigDecimal.valueOf(progressDTO.getCalculatedQuantity())
                    .divide(BigDecimal.valueOf(progressDTO.getTotalQuantity()), 4, RoundingMode.HALF_DOWN);
            progressDTO.setProgress(progress.compareTo(BigDecimal.ONE) > 0 ? BigDecimal.ONE : progress);
            Util_DataCache.setObjVal(cacheKey, JsonUtil.toJsonString(progressDTO));
        }
    }

    @Override
    public synchronized void getAndAddCalculatedQty(String cacheKey, Integer calculatedQuantity, String message) {
        String resultStr = (String) Util_DataCache.getObjVal(cacheKey);
        if (StringUtils.isNotEmpty(resultStr)) {
            ProgressDTO progressDTO = JsonUtil.parseObject(resultStr, ProgressDTO.class);
            if (progressDTO == null || !progressDTO.isStatus()) {
                return;
            }
            Integer currentCalculatedQuantity = progressDTO.getCalculatedQuantity() + calculatedQuantity;
            progressDTO.setCalculatedQuantity(
                    currentCalculatedQuantity > progressDTO.getTotalQuantity() ? progressDTO.getTotalQuantity() : currentCalculatedQuantity);
            BigDecimal progress = BigDecimal.valueOf(progressDTO.getCalculatedQuantity())
                    .divide(BigDecimal.valueOf(progressDTO.getTotalQuantity()), 4, RoundingMode.HALF_DOWN);
            progressDTO.setProgress(progress.compareTo(BigDecimal.ONE) > 0 ? BigDecimal.ONE : progress);

            if (StringUtils.isNotBlank(message)) {
                progressDTO.setMessage(Utils.null2String(progressDTO.getMessage()) + message);
            }
            Util_DataCache.setObjVal(cacheKey, JsonUtil.toJsonString(progressDTO));
        }
    }

    @Override
    public void fail(String cacheKey, String message) {
        ProgressDTO progressDTO = new ProgressDTO();
        progressDTO.setStatus(false);
        progressDTO.setMessage(message);
        progressDTO.setCheckStatus(true);
        Util_DataCache.setObjVal(cacheKey, JsonUtil.toJsonString(progressDTO));
    }

    @Override
    public void finish(String cacheKey, boolean checkStatus) {
        String resultStr = (String) Util_DataCache.getObjVal(cacheKey);
        if (StringUtils.isNotEmpty(resultStr)) {
            ProgressDTO progressDTO = JsonUtil.parseObject(resultStr, ProgressDTO.class);
            if (progressDTO == null || !progressDTO.isStatus()) {
                return;
            }
            progressDTO.setTitle("操作完成");
            progressDTO.setCalculatedQuantity(progressDTO.getTotalQuantity());
            progressDTO.setProgress(BigDecimal.ONE);
            progressDTO.setCheckStatus(checkStatus);
            Util_DataCache.setObjVal(cacheKey, JsonUtil.toJsonString(progressDTO));
        }
    }

    @Override
    public void finish(String cacheKey, boolean checkStatus, String message) {
        String resultStr = (String) Util_DataCache.getObjVal(cacheKey);
        if (StringUtils.isNotEmpty(resultStr)) {
            ProgressDTO progressDTO = JsonUtil.parseObject(resultStr, ProgressDTO.class);
            if (progressDTO == null || !progressDTO.isStatus()) {
                return;
            }
            progressDTO.setTitle("操作完成");
            progressDTO.setCalculatedQuantity(progressDTO.getTotalQuantity());
            progressDTO.setProgress(BigDecimal.ONE);
            progressDTO.setCheckStatus(checkStatus);
            progressDTO.setMessage(message);
            Util_DataCache.setObjVal(cacheKey, JsonUtil.toJsonString(progressDTO));
        }
    }

    @Override
    public void updateProgress(String cacheKey, BigDecimal progress, boolean checkStatus) {
        String resultStr = (String) Util_DataCache.getObjVal(cacheKey);
        if (StringUtils.isNotEmpty(resultStr)) {
            ProgressDTO progressDTO = JsonUtil.parseObject(resultStr, ProgressDTO.class);
            if (progressDTO == null || !progressDTO.isStatus()) {
                return;
            }
            progressDTO.setTitle("处理中");
            progressDTO.setCalculatedQuantity(progressDTO.getTotalQuantity());
            progressDTO.setProgress(progress);
            progressDTO.setCheckStatus(checkStatus);
            Util_DataCache.setObjVal(cacheKey, JsonUtil.toJsonString(progressDTO));
        }
    }

    @Override
    public ProgressDTO getProgress(String cacheKey) {
        String resultStr = (String) Util_DataCache.getObjVal(cacheKey);
        if (StringUtils.isEmpty(resultStr)) {
            return null;
        }
        return JsonUtil.parseObject(resultStr, ProgressDTO.class);
    }

    @Override
    public ProgressDTO del(String cacheKey) {
        Util_DataCache.clearVal(cacheKey);
        ProgressDTO salaryAcctProgressDTO = new ProgressDTO();
        salaryAcctProgressDTO.setMessage("操作失败");
        salaryAcctProgressDTO.setStatus(false);
        return salaryAcctProgressDTO;
    }
}
