package com.ruoyi.system.service.enterprise.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.enterprise.SysUserEnterprise;
import com.ruoyi.system.domain.loan.LoanApplication;
import com.ruoyi.system.mapper.SysUserEnterpriseMapper;
import com.ruoyi.system.service.enterprise.ISysUserEnterpriseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
 * 用户企业信息Service业务层处理
 * 
 * @author boman
 * @date 2024-04-22
 */
@Service
public class SysUserEnterpriseServiceImpl implements ISysUserEnterpriseService
{
    @Autowired
    private SysUserEnterpriseMapper sysUserEnterpriseMapper;

    /**
     * 查询用户企业信息
     * 
     * @param userId 用户企业信息主键
     * @return 用户企业信息
     */
    @Override
    public List<SysUserEnterprise> selectSysUserEnterpriseByUserId(Long userId)
    {
        return sysUserEnterpriseMapper.selectSysUserEnterpriseByUserId(userId);
    }

    @Override
    public SysUserEnterprise selectSysUserEnterpriseByEnterpriseId(Long enterpriseId) {
        return sysUserEnterpriseMapper.selectSysUserEnterpriseByEnterpriseId(enterpriseId);
    }

    /**
     * 查询用户企业信息列表
     * 
     * @param sysUserEnterprise 用户企业信息
     * @return 用户企业信息
     */
    @Override
    public List<SysUserEnterprise> selectSysUserEnterpriseList(SysUserEnterprise sysUserEnterprise)
    {
                //判断是否是admin或者管理员manager
        List<SysRole> roles = SecurityUtils.getLoginUser().getUser().getRoles();
        String roleKey = "admin,manager";
        List<LoanApplication> loanApplications = new ArrayList<>();
       for (SysRole role : roles) {
            if (roleKey.contains(role.getRoleKey())) {
                sysUserEnterprise.setUserId(null);
            }
        }
        return sysUserEnterpriseMapper.selectSysUserEnterpriseList(sysUserEnterprise);
    }

    /**
     * 新增用户企业信息
     * 
     * @param sysUserEnterprise 用户企业信息
     * @return 结果
     */
    @Override
    public int insertSysUserEnterprise(SysUserEnterprise sysUserEnterprise)
    {
        sysUserEnterprise.setUserId(SecurityUtils.getUserId());
        sysUserEnterprise.setCreateTime(DateUtils.getNowDate());
        return sysUserEnterpriseMapper.insertSysUserEnterprise(sysUserEnterprise);
    }

    /**
     * 修改用户企业信息
     * 
     * @param sysUserEnterprise 用户企业信息
     * @return 结果
     */
    @Override
    public int updateSysUserEnterprise(SysUserEnterprise sysUserEnterprise)
    {
        sysUserEnterprise.setUpdateTime(DateUtils.getNowDate());
        return sysUserEnterpriseMapper.updateSysUserEnterprise(sysUserEnterprise);
    }

    /**
     * 批量删除用户企业信息
     * 
     * @param userIds 需要删除的用户企业信息主键
     * @return 结果
     */
    @Override
    public int deleteSysUserEnterpriseByUserIds(Long[] userIds)
    {
        return sysUserEnterpriseMapper.deleteSysUserEnterpriseByUserIds(userIds);
    }

    @Override
    public int deleteSysUserEnterpriseByEnterpriseIds(Long[] enterpriseIds) {
        return sysUserEnterpriseMapper.deleteSysUserEnterpriseByEnterpriseIds(enterpriseIds);
    }

    /**
     * 删除用户企业信息信息
     * 
     * @param userId 用户企业信息主键
     * @return 结果
     */
    @Override
    public int deleteSysUserEnterpriseByUserId(Long userId)
    {
        return sysUserEnterpriseMapper.deleteSysUserEnterpriseByUserId(userId);
    }
}
