package com.yn.bftl.thirdparty.modules.log.service.impl;

import com.alibaba.fastjson.JSON;
import com.yn.bftl.common.modules.company.entity.Company;
import com.yn.bftl.common.modules.log.entity.*;
import com.yn.bftl.thirdparty.modules.log.repository.*;
import com.yn.bftl.thirdparty.modules.log.abstracts.AbstractLogService;
import com.yn.bftl.thirdparty.modules.log.service.LoggerService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class LoggerServiceImplLogService extends AbstractLogService implements LoggerService {

    private static final Log log = LogFactory.getLog(LoggerServiceImplLogService.class);

    @Resource
    PayLogRepository payLogRepository;

    @Resource
    CommissionLogRepository commissionLogRepository;

    @Resource
    SeparateLogRepository separateLogRepository;

    @Resource
    UserLogRepository userLogRepository;

    @Resource
    OrderLogRepository orderLogRepository;

    @Resource
    OperationLogRepository operationLogRepository;

    @Resource
    UnionpayLogRepository unionpayLogRepository;

    @Resource
    RechargeTransferLogRepository rechargeTransferLogRepository;

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public void pay(String message, Map<String, Object> attachment, String className, String functionName, String logChildType) {
        String logType = "pay";
        writeLog(message, logType, logChildType, attachment, className, functionName);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public void commission(String message, Map<String, Object> attachment, String className, String functionName, String logChildType) {
        String logType = "commission";
        writeLog(message, logType, logChildType, attachment, className, functionName);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public void separate(String message, Map<String, Object> attachment, String className, String functionName, String logChildType) {
        String logType = "separate";
        writeLog(message, logType, logChildType, attachment, className, functionName);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public void user(String message, Map<String, Object> attachment, String className, String functionName, String logChildType) {
        String logType = "user";
        writeLog(message, logType, logChildType, attachment, className, functionName);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public void order(String message, Map<String, Object> attachment, String className, String functionName, String logChildType) {
        String logType = "order";
        writeLog(message, logType, logChildType, attachment, className, functionName);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public void operation(String message, Map<String, Object> attachment, String className, String functionName, String logChildType) {
        String logType = "operation";
        writeLog(message, logType, logChildType, attachment, className, functionName);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public void unionpay(String message, Map<String, Object> attachment, String className, String functionName, String logChildType) {
        UnionpayLog unionpayLog = new UnionpayLog();
        unionpayLog.setMessage(message);
        unionpayLog.setLogType(logChildType);
        unionpayLog.setAttachment(JSON.toJSONString(attachment));
        unionpayLog.setClassName(className);
        unionpayLog.setFunctionName(functionName);
        unionpayLogRepository.save(unionpayLog);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public void unionpay(String message, String attachmentStr, String className, String functionName, String logChildType) {
        UnionpayLog unionpayLog = new UnionpayLog();
        unionpayLog.setMessage(message);
        unionpayLog.setLogType(logChildType);
        unionpayLog.setAttachment(attachmentStr);
        unionpayLog.setClassName(className);
        unionpayLog.setFunctionName(functionName);
        unionpayLogRepository.save(unionpayLog);
    }

    @Override
    public void rechargeTransfer(String message, Object params, String className, String functionName, String logType, Company company) {
        RechargeTransferLog rechargeTransferLog = new RechargeTransferLog();
        rechargeTransferLog.setMessage(message);
        rechargeTransferLog.setLogType(logType);
        rechargeTransferLog.setAttachment(JSON.toJSONString(params));
        rechargeTransferLog.setClassName(className);
        rechargeTransferLog.setFunctionName(functionName);
        rechargeTransferLog.setCompany(company);
        rechargeTransferLogRepository.save(rechargeTransferLog);
    }

    /**
     * 所有的日志方法调用
     *
     * @param message
     * @param logType
     * @param logChildType
     * @param attachment
     * @param className
     * @param functionName
     */
    private void writeLog(String message, String logType, String logChildType, Map<String, Object> attachment, String className, String functionName) {
        if (message == null) {
            return;
        }
        if (className == null) {
            className = "UndefinedClass";
        }
        if (functionName == null) {
            functionName = "UndefinedFunction";
        }

        String json = JSON.toJSONString(attachment);
        try {
            switch (logType) {
                case "pay":
                    PayLog payLog = new PayLog();
                    payLog.setMessage(message);
                    payLog.setLogType(logChildType);
                    payLog.setAttachment(json);
                    payLog.setClassName(className);
                    payLog.setFunctionName(functionName);
                    payLog.setCompany(getCompany());
                    payLogRepository.save(payLog);
                    break;
                case "commission":
                    CommissionLog commissionLog = new CommissionLog();
                    commissionLog.setMessage(message);
                    commissionLog.setLogType(logChildType);
                    commissionLog.setAttachment(json);
                    commissionLog.setClassName(className);
                    commissionLog.setFunctionName(functionName);
                    commissionLog.setCompany(getCompany());
                    commissionLogRepository.save(commissionLog);
                    break;
                case "separate":
                    SeparateLog separateLog = new SeparateLog();
                    separateLog.setMessage(message);
                    separateLog.setLogType(logChildType);
                    separateLog.setAttachment(json);
                    separateLog.setClassName(className);
                    separateLog.setFunctionName(functionName);
                    separateLog.setCompany(getCompany());
                    separateLogRepository.save(separateLog);
                    break;
                case "user":
                    UserLog userLog = new UserLog();
                    userLog.setMessage(message);
                    userLog.setLogType(logChildType);
                    userLog.setAttachment(json);
                    userLog.setClassName(className);
                    userLog.setFunctionName(functionName);
                    userLog.setCompany(getCompany());
                    userLogRepository.save(userLog);
                    break;
                case "order":
                    OrderLog orderLog = new OrderLog();
                    orderLog.setMessage(message);
                    orderLog.setLogType(logChildType);
                    orderLog.setAttachment(json);
                    orderLog.setClassName(className);
                    orderLog.setFunctionName(functionName);
                    orderLog.setCompany(getCompany());
                    orderLogRepository.save(orderLog);
                    break;
                case "operation":
                    OperationLog operationLog = new OperationLog();
                    operationLog.setMessage(message);
                    operationLog.setLogType(logChildType);
                    operationLog.setAttachment(json);
                    operationLog.setClassName(className);
                    operationLog.setFunctionName(functionName);
                    operationLog.setCompany(getCompany());
                    operationLogRepository.save(operationLog);
                    break;
                case "unionpay":
                    UnionpayLog unionpayLog = new UnionpayLog();
                    unionpayLog.setMessage(message);
                    unionpayLog.setLogType(logChildType);
                    unionpayLog.setAttachment(json);
                    unionpayLog.setClassName(className);
                    unionpayLog.setFunctionName(functionName);
                    unionpayLog.setCompany(getCompany());
                    unionpayLogRepository.save(unionpayLog);
                    break;
                case "":
                    // 其他
                    break;
                default:
                    break;
            }
        } catch (Exception ignored) {
        }
    }
}
