package com.ruoyi.xq.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.xq.domain.User;
import com.ruoyi.xq.domain.UserAuth;
import com.ruoyi.xq.dto.admin.user.UserAuthAdminVo;
import com.ruoyi.xq.dto.admin.userauth.UserAuthAdminInfoVo;
import com.ruoyi.xq.enums.common.AuditEnum;
import com.ruoyi.xq.mapper.UserAuthMapper;
import com.ruoyi.xq.service.UserAuthService;
import com.ruoyi.xq.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户认证管理Service业务层处理
 *
 * @author 77
 * @date 2024-03-04
 */
@Service
public class UserAuthServiceImpl extends ServiceImpl<UserAuthMapper,UserAuth> implements UserAuthService {

    @Autowired
    private UserService userService;

    @Override
    public UserAuth getByUserId(Long userId) {
        return this.getOne(Wrappers.lambdaQuery(UserAuth.class)
                .eq(UserAuth::getUserId, userId));
    }

    @Override
    public Page<UserAuthAdminVo> pageAdmin(PageQuery pageQuery, UserAuthAdminVo bo) {
        return baseMapper.pageAdmin(pageQuery.build(), bo);
    }

    @Override
    public UserAuthAdminInfoVo getAdminById(Long id) {
        UserAuthAdminInfoVo vo = new UserAuthAdminInfoVo();
        UserAuth userAuth = this.getById(id);
        User user = userService.getById(userAuth.getUserId());
        vo.setUser(user);
        vo.setUserAuth(userAuth);
        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateUserAuth(UserAuth bo) {
        UserAuth update = new UserAuth();
        update.setId(bo.getId());
//        update.setAlreadyAuthNum();
        update.setCardNumAuth(bo.getCardNumAuth());
        update.setSinglePersonAuth(bo.getSinglePersonAuth());
        update.setEducationAuth(bo.getEducationAuth());
        update.setJobAuth(bo.getJobAuth());
        update.setCarAuth(bo.getCarAuth());
        update.setHouseAuth(bo.getHouseAuth());
        update.setMarriageAuth(bo.getMarriageAuth());
        this.updateById(update);
        UserAuthService userAuthService = SpringUtils.getBean(UserAuthService.class);
        userAuthService.checkAuthNum(bo.getUserId());
    }

    @Override
    public void checkAuthNum(Long userId){
        UserAuth userAuth = this.getByUserId(userId);
        int i = 0;
        if(userAuth.getCarAuth().equals(AuditEnum.SUCCESS.getCode())){
            i++;
        }
        if(userAuth.getMarriageAuth().equals(AuditEnum.SUCCESS.getCode())){
            i++;
        }
        if(userAuth.getEducationAuth().equals(AuditEnum.SUCCESS.getCode())){
            i++;
        }
        if(userAuth.getJobAuth().equals(AuditEnum.SUCCESS.getCode())){
            i++;
        }
        if(userAuth.getPhoneAuth().equals(AuditEnum.SUCCESS.getCode())){
            i++;
        }
        if(userAuth.getSinglePersonAuth().equals(AuditEnum.SUCCESS.getCode())){
            i++;
        }
        if(userAuth.getHouseAuth().equals(AuditEnum.SUCCESS.getCode())){
            i++;
        }
        if(userAuth.getCardNumAuth().equals(AuditEnum.SUCCESS.getCode())){
            i++;
        }
        this.update(Wrappers.lambdaUpdate(UserAuth.class).eq(UserAuth::getId, userAuth.getId())
                .set(UserAuth::getAlreadyAuthNum, i));
    }
}
