package com.yn.bftl.thirdparty.modules.contract.abstracts;

import com.yn.bftl.common.modules.meta.entity.MetaFile;
import com.yn.bftl.thirdparty.common.redis.ContractLockRedis;
import com.yn.bftl.thirdparty.common.service.BaseMemberService;
import com.yn.bftl.thirdparty.modules.log.service.LoggerService;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 契约锁业务处理抽象类
 *
 * @author chendehuihuo
 * @date 2022/7/20 11:31
 */
public class AbstractContractLockService extends BaseMemberService {

    @Resource
    private ContractLockRedis contractLockRedis;
    @Resource
    LoggerService loggerService;

    protected Map<String, Object> logMap = new HashMap<>();

    @Value("${file.urlPath}")
    public String urlPath;

    protected String selectPic(MetaFile file){
        if(file == null){
            return "";
        }
        // 这里存在数据库数据缺失，导致服务异常
        try {
            return file.getOuterChainPath() == null ? (urlPath + file.getFilePath()) : file.getOuterChainPath();
        } catch (Exception e) {
            return "";
        }
    }

    protected String redisKey(String companyId) {
        return contractLockRedis.redisMemberUserKey + "lock:" + companyId;
    }

    protected ContractLockRedis lockRedis() {
        return contractLockRedis.getInstance();
    }

    /**
     * 记录用户接口调用日志
     *
     * @param message
     * @return BaseAllinpayServiceAbstract
     * @author chendehuihuo
     * @date 2021-11-16
     */
    protected void writeUserLog(String message) {
        //获取被调用者的类名
        String classname = Thread.currentThread().getStackTrace()[2].getClassName();
        //获取被调用者的方法名
        String functionName = Thread.currentThread().getStackTrace()[2].getMethodName();
        message = "[记录]" + functionName + message;
        loggerService.user(message, logMap, classname, functionName, "user:" + classname + ":" + functionName);
    }
}
