package com.ruoyi.system.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysRegisterRequestMapper;
import com.ruoyi.system.domain.SysRegisterRequest;
import com.ruoyi.system.service.ISysRegisterRequestService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 注册申请Service业务层处理
 *
 * @author liangwen
 * @date 2025-08-26
 */
@Service
public class SysRegisterRequestServiceImpl implements ISysRegisterRequestService
{
    @Autowired
    private SysRegisterRequestMapper sysRegisterRequestMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询注册申请
     *
     * @param requestId 注册申请主键
     * @return 注册申请
     */
    @Override
    public SysRegisterRequest selectSysRegisterRequestByRequestId(Long requestId)
    {
        return sysRegisterRequestMapper.selectSysRegisterRequestByRequestId(requestId);
    }

    /**
     * 查询注册申请列表
     *
     * @param sysRegisterRequest 注册申请
     * @return 注册申请
     */
    @Override
    public List<SysRegisterRequest> selectSysRegisterRequestList(SysRegisterRequest sysRegisterRequest)
    {
        return sysRegisterRequestMapper.selectSysRegisterRequestList(sysRegisterRequest);
    }

    /**
     * 新增注册申请
     *
     * @param sysRegisterRequest 注册申请
     * @return 结果
     */
    @Override
    public int insertSysRegisterRequest(SysRegisterRequest sysRegisterRequest)
    {
        sysRegisterRequest.setCreateTime(DateUtils.getNowDate());
        return sysRegisterRequestMapper.insertSysRegisterRequest(sysRegisterRequest);
    }

    /**
     * 修改注册申请
     *
     * @param sysRegisterRequest 注册申请
     * @return 结果
     */
    @Override
    public int updateSysRegisterRequest(SysRegisterRequest sysRegisterRequest)
    {
        sysRegisterRequest.setUpdateTime(DateUtils.getNowDate());
        return sysRegisterRequestMapper.updateSysRegisterRequest(sysRegisterRequest);
    }

    /**
     * 批量删除注册申请
     *
     * @param requestIds 需要删除的注册申请主键
     * @return 结果
     */
    @Override
    public int deleteSysRegisterRequestByRequestIds(Long[] requestIds)
    {
        return sysRegisterRequestMapper.deleteSysRegisterRequestByRequestIds(requestIds);
    }

    /**
     * 删除注册申请信息
     *
     * @param requestId 注册申请主键
     * @return 结果
     */
    @Override
    public int deleteSysRegisterRequestByRequestId(Long requestId)
    {
        return sysRegisterRequestMapper.deleteSysRegisterRequestByRequestId(requestId);
    }

    @Transactional
    @Override
    public void approve(Long requestId, boolean approve, String remark) {
        SysRegisterRequest request = sysRegisterRequestMapper.selectSysRegisterRequestByRequestId(requestId);
        if (request == null) {
            throw new RuntimeException("申请不存在");
        }

        SysUser user = sysUserMapper.selectUserById(request.getUserId());
        if (user == null) {
            new RuntimeException("用户不存在");
        }

        if (approve) {
            request.setStatus("1");
            user.setStatus("0");
        } else {
            request.setStatus("2");
            user.setDelFlag("2"); // 删除
        }
        request.setRemark(remark);
        request.setUpdateTime(DateUtils.getNowDate());
        request.setUpdateBy(SecurityUtils.getUsername());
        user.setUpdateTime(DateUtils.getNowDate());
        user.setUpdateBy(SecurityUtils.getUsername());

        sysRegisterRequestMapper.updateSysRegisterRequest(request);
        sysUserMapper.updateUser(user);
    }
}
