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

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.enterprise.SysUserEnterprise;
import com.ruoyi.system.domain.idcard.SysUserIdcard;
import com.ruoyi.system.domain.loan.LoanApplication;
import com.ruoyi.system.mapper.LoanApplicationMapper;
import com.ruoyi.system.mapper.SysUserEnterpriseMapper;
import com.ruoyi.system.mapper.SysUserIdcardMapper;
import com.ruoyi.system.service.idcard.ISysUserIdcardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.SystemEnvironmentPropertySource;
import org.springframework.stereotype.Service;


/**
 * 用户身份证信息Service业务层处理
 * 
 * @author boman
 * @date 2024-04-22
 */
@Service
public class SysUserIdcardServiceImpl implements ISysUserIdcardService
{
    @Autowired
    private SysUserIdcardMapper sysUserIdcardMapper;
    @Autowired
    private SysUserEnterpriseMapper sysUserEnterpriseMapper;
    @Autowired
    private LoanApplicationMapper loanApplicationMapper;

    /**
     * 查询用户身份证信息
     * 
     * @param userId 用户身份证信息主键
     * @return 用户身份证信息
     */
    @Override
    public SysUserIdcard selectSysUserIdcardByUserId(Long userId)
    {
        SysUserIdcard sysUserIdcard = sysUserIdcardMapper.selectSysUserIdcardByUserId(userId);
        List<SysUserEnterprise> sysUserEnterpriseList = sysUserEnterpriseMapper.selectSysUserEnterpriseByUserId(userId);
        if (sysUserEnterpriseList != null && sysUserEnterpriseList.size() > 0){
            sysUserIdcard.setSysUserEnterpriseNum(sysUserEnterpriseList.size());
            sysUserIdcard.setSysUserEnterpriseList(sysUserEnterpriseList);
        }
        LoanApplication loanApplication =  new LoanApplication();
        loanApplication.setUserId(userId);
        List<LoanApplication> loanApplications = loanApplicationMapper.selectLoanApplicationList(loanApplication);
        if (loanApplications != null && loanApplications.size() > 0){
            sysUserIdcard.setLoanApplicationNum(loanApplications.size());
            sysUserIdcard.setLoanApplicationList(loanApplications);
        }
        return sysUserIdcard;
    }

    /**
     * 查询用户身份证信息列表
     * 
     * @param sysUserIdcard 用户身份证信息
     * @return 用户身份证信息
     */
    @Override
    public List<SysUserIdcard> selectSysUserIdcardList(SysUserIdcard sysUserIdcard)
    {
        List<SysUserIdcard> sysUserIdCards = sysUserIdcardMapper.selectSysUserIdcardList(sysUserIdcard);
        //查询个人名下企业和申请贷款次数
        if (sysUserIdCards != null && sysUserIdCards.size() > 0){
            for (SysUserIdcard userIdCard : sysUserIdCards) {
                Long userId = userIdCard.getUserId();
                List<SysUserEnterprise> sysUserEnterpriseList = sysUserEnterpriseMapper.selectSysUserEnterpriseByUserId(userId);
                userIdCard.setSysUserEnterpriseNum(sysUserEnterpriseList.size());
                LoanApplication loanApplication =  new LoanApplication();
                loanApplication.setUserId(userId);
                List<LoanApplication> loanApplications = loanApplicationMapper.selectLoanApplicationList(loanApplication);
                userIdCard.setLoanApplicationNum(loanApplications.size());
            }
        }
        return sysUserIdCards;
    }

    /**
     * 新增用户身份证信息
     * 
     * @param sysUserIdcard 用户身份证信息
     * @return 结果
     */
    @Override
    public int insertSysUserIdcard(SysUserIdcard sysUserIdcard)
    {
        sysUserIdcard.setUserId(sysUserIdcard.getUserId());
        sysUserIdcard.setCreateTime(DateUtils.getNowDate());
        return sysUserIdcardMapper.insertSysUserIdcard(sysUserIdcard);
    }

    /**
     * 修改用户身份证信息
     * 
     * @param sysUserIdcard 用户身份证信息
     * @return 结果
     */
    @Override
    public int updateSysUserIdcard(SysUserIdcard sysUserIdcard)
    {
        sysUserIdcard.setUpdateTime(DateUtils.getNowDate());
        return sysUserIdcardMapper.updateSysUserIdcard(sysUserIdcard);
    }

    /**
     * 批量删除用户身份证信息
     * 
     * @param userIds 需要删除的用户身份证信息主键
     * @return 结果
     */
    @Override
    public int deleteSysUserIdcardByUserIds(Long[] userIds)
    {
        return sysUserIdcardMapper.deleteSysUserIdcardByUserIds(userIds);
    }

    /**
     * 删除用户身份证信息信息
     * 
     * @param userId 用户身份证信息主键
     * @return 结果
     */
    @Override
    public int deleteSysUserIdcardByUserId(Long userId)
    {
        return sysUserIdcardMapper.deleteSysUserIdcardByUserId(userId);
    }
}
