package com.kym.service.admin.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.kym.common.utils.IDGenerator;
import com.kym.entity.admin.StationWhitelistLog;
import com.kym.mapper.admin.StationWhitelistLogMapper;
import com.kym.service.admin.StationWhitelistLogService;
import org.springframework.stereotype.Service;

@Service
@DS("db-admin")
public class StationWhitelistLogServiceImpl extends MPJBaseServiceImpl<StationWhitelistLogMapper, StationWhitelistLog> implements StationWhitelistLogService {

    private final IDGenerator idGenerator = new IDGenerator();

    @Override
    public void logAdd(String stationId, String stationName, Long userId, String userPhone, Long operatorId, String operatorName) {
        StationWhitelistLog log = new StationWhitelistLog();
        log.setId(idGenerator.nextId());
        log.setStationId(stationId);
        log.setStationName(stationName);
        log.setUserId(userId);
        log.setUserPhone(userPhone);
        log.setOperationType(StationWhitelistLog.OPERATION_TYPE_添加);
        log.setOperationDesc("添加用户到白名单");
        log.setOperatorId(operatorId);
        log.setOperatorName(operatorName);
        save(log);
    }

    @Override
    public void logBatchAdd(String stationId, String stationName, int count, Long operatorId, String operatorName) {
        StationWhitelistLog log = new StationWhitelistLog();
        log.setId(idGenerator.nextId());
        log.setStationId(stationId);
        log.setStationName(stationName);
        log.setOperationType(StationWhitelistLog.OPERATION_TYPE_批量添加);
        log.setOperationDesc("批量添加" + count + "个用户到白名单");
        log.setOperatorId(operatorId);
        log.setOperatorName(operatorName);
        save(log);
    }

    @Override
    public void logRemove(String stationId, String stationName, Long userId, String userPhone, Long operatorId, String operatorName) {
        StationWhitelistLog log = new StationWhitelistLog();
        log.setId(idGenerator.nextId());
        log.setStationId(stationId);
        log.setStationName(stationName);
        log.setUserId(userId);
        log.setUserPhone(userPhone);
        log.setOperationType(StationWhitelistLog.OPERATION_TYPE_删除);
        log.setOperationDesc("从白名单移除用户");
        log.setOperatorId(operatorId);
        log.setOperatorName(operatorName);
        save(log);
    }

    @Override
    public void logBatchRemove(String stationId, String stationName, int count, Long operatorId, String operatorName) {
        StationWhitelistLog log = new StationWhitelistLog();
        log.setId(idGenerator.nextId());
        log.setStationId(stationId);
        log.setStationName(stationName);
        log.setOperationType(StationWhitelistLog.OPERATION_TYPE_批量删除);
        log.setOperationDesc("批量移除" + count + "个用户");
        log.setOperatorId(operatorId);
        log.setOperatorName(operatorName);
        save(log);
    }

    @Override
    public void logStatusChange(String stationId, String stationName, Long userId, String userPhone, Integer status, Long operatorId, String operatorName) {
        StationWhitelistLog log = new StationWhitelistLog();
        log.setId(idGenerator.nextId());
        log.setStationId(stationId);
        log.setStationName(stationName);
        log.setUserId(userId);
        log.setUserPhone(userPhone);
        log.setOperationType(status == 1 ? StationWhitelistLog.OPERATION_TYPE_启用 : StationWhitelistLog.OPERATION_TYPE_禁用);
        log.setOperationDesc(status == 1 ? "启用白名单用户" : "禁用白名单用户");
        log.setOperatorId(operatorId);
        log.setOperatorName(operatorName);
        save(log);
    }

    @Override
    public void logConfigChange(String stationId, String stationName, Integer enabled, Long operatorId, String operatorName) {
        StationWhitelistLog log = new StationWhitelistLog();
        log.setId(idGenerator.nextId());
        log.setStationId(stationId);
        log.setStationName(stationName);
        log.setOperationType(StationWhitelistLog.OPERATION_TYPE_配置变更);
        log.setOperationDesc(enabled == 1 ? "启用站点白名单功能" : "禁用站点白名单功能");
        log.setOperatorId(operatorId);
        log.setOperatorName(operatorName);
        save(log);
    }
}
