/**
 * Copyright &copy; 2021-2026 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.sys.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeeplus.center.enums.ServiceAliasEnum;
import com.jeeplus.sys.domain.*;
import com.jeeplus.sys.mapper.SysUserFailedLogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class SysUserFailedLogService extends ServiceImpl<SysUserFailedLogMapper, SysUserFailedLog> {

	@Autowired
	private SysUserFailedLogMapper sysUserFailedLogMapper;

	public String saveFailedLog(String userId,String belongService) {
		SysUserFailedLog sysUserFailedLog = new SysUserFailedLog();
		sysUserFailedLog.setUserId(userId);
		sysUserFailedLog.setBelongService(belongService);
		sysUserFailedLogMapper.insert(sysUserFailedLog);
		return sysUserFailedLog.getId();
	}

	/**
	 * 在切换用户所属部门时，清除之前所属子系统数据失败记录
	 */
	public String deleteFailedLog(String userId,String belongService) {
		SysUserFailedLog sysUserFailedLog = new SysUserFailedLog();
		sysUserFailedLog.setUserId(userId);
		sysUserFailedLog.setBelongService(belongService);
		sysUserFailedLog.setIsSuccess("1"); // 清除操作标记为失败
		sysUserFailedLogMapper.insert(sysUserFailedLog);
		return sysUserFailedLog.getId();
	}

	/**
	 * 在删除花名册数据时，删除子系统花名册数据失败记录
	 */
	public String removeFailedLog(String userId,String belongService) {
		SysUserFailedLog sysUserFailedLog = new SysUserFailedLog();
		sysUserFailedLog.setUserId(userId);
		sysUserFailedLog.setBelongService(belongService);
		sysUserFailedLog.setIsSuccess("2"); // 删除数据操作标记为失败
		sysUserFailedLogMapper.insert(sysUserFailedLog);
		return sysUserFailedLog.getId();
	}

	public void deleteByUserAndService(String userId,String belongService) {
		sysUserFailedLogMapper.delete(new LambdaQueryWrapper<SysUserFailedLog>()
				.eq(SysUserFailedLog::getUserId,userId)
				.eq(SysUserFailedLog::getBelongService,belongService)
		);
	}

	public void finishById(String id) {
		sysUserFailedLogMapper.finishById(id);
	}

}
