package com.ruoyi.system.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
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.common.utils.StringUtils;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
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;

    @Autowired
    private SysUserRoleMapper userRoleMapper;

    /**
     * 查询注册申请
     *
     * @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, Long[] roleIds) {
        SysRegisterRequest request = sysRegisterRequestMapper.selectSysRegisterRequestByRequestId(requestId);
        if (request == null) {
            throw new RuntimeException("注册申请不存在");
        }

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

        if (approve) {
            request.setStatus("1"); // 人工审核通过
            // 删除用户与角色关联
            userRoleMapper.deleteUserRoleByUserId(user.getUserId());
            // 新增用户与角色管理
            if (StringUtils.isNotEmpty(roleIds))
            {
                // 新增用户与角色管理
                List<SysUserRole> list = new ArrayList<SysUserRole>(roleIds.length);
                for (Long roleId : roleIds)
                {
                    SysUserRole ur = new SysUserRole();
                    ur.setUserId(user.getUserId());
                    ur.setRoleId(roleId);
                    list.add(ur);
                }
                userRoleMapper.batchUserRole(list);
            }

        } else {
            request.setStatus("2"); // 已驳回
            user.setDelFlag("2"); // 设置为删除

            // TODO 发送邮箱告知注册审核被驳回
        }
        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);
    }
}
