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.helper.LoginHelper;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.xq.domain.User;
import com.ruoyi.xq.domain.UserAuth;
import com.ruoyi.xq.domain.UserAuthAudit;
import com.ruoyi.xq.dto.admin.userauth.UserAuthAuditAdminVo;
import com.ruoyi.xq.enums.common.AuditEnum;
import com.ruoyi.xq.enums.user.UserAuthTypeEnum;
import com.ruoyi.xq.mapper.UserAuthAuditMapper;
import com.ruoyi.xq.service.UserAuthAuditService;
import com.ruoyi.xq.service.UserAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 用户认证审核Service业务层处理
 *
 * @author 77
 * @date 2024-03-04
 */
@Service
public class UserAuthAuditServiceImpl extends ServiceImpl<UserAuthAuditMapper,UserAuthAudit> implements UserAuthAuditService {
    @Autowired
    private UserAuthService userAuthService;

    @Override
    public void saveAuthAudit(User user, UserAuthTypeEnum userAuthType, String pic, String remark) {
        UserAuthAudit userAuthAudit = new UserAuthAudit();
        userAuthAudit.setUserId(user.getId());
        userAuthAudit.setUsercode(user.getUsercode());
        userAuthAudit.setAuthType(userAuthType.getCode());
        userAuthAudit.setAuthPic(pic);
        userAuthAudit.setAuthRemark(remark);
        userAuthAudit.setAuditStatus(AuditEnum.AUDITING.getCode());
        this.save(userAuthAudit);
    }

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

    @Override
    public void auditSuccess(String ids) {
        String[] idArray = ids.split(",");
        for (String id : idArray) {
            boolean flag = this.update(Wrappers.lambdaUpdate(UserAuthAudit.class)
                    .eq(UserAuthAudit::getId, id)
                    .eq(UserAuthAudit::getAuditStatus, AuditEnum.AUDITING.getCode())
                    .set(UserAuthAudit::getAuditStatus, AuditEnum.SUCCESS.getCode())
                    .set(UserAuthAudit::getAuditIp, ServletUtils.getClientIP())
                    .set(UserAuthAudit::getAuditOpId, LoginHelper.getUserId())
                    .set(UserAuthAudit::getAuditOpName, LoginHelper.getUsername())
                    .set(UserAuthAudit::getAuditTime, LocalDateTime.now())
                    .set(UserAuthAudit::getAuditRemark, "审核成功"));
            if(flag){
                UserAuthAudit userInfoAudit = this.getById(id);
                if(userInfoAudit.getAuthType().equals(UserAuthTypeEnum.CAR.getCode())){
                    userAuthService.update(Wrappers.lambdaUpdate(UserAuth.class)
                            .eq(UserAuth::getUserId, userInfoAudit.getUserId())
                            .set(UserAuth::getCarAuth, AuditEnum.SUCCESS.getCode())
                            .set(UserAuth::getCarPic, userInfoAudit.getAuthPic()));
                    userAuthService.checkAuthNum(userInfoAudit.getUserId());
                }else if(userInfoAudit.getAuthType().equals(UserAuthTypeEnum.JOB.getCode())){
                    userAuthService.update(Wrappers.lambdaUpdate(UserAuth.class)
                            .eq(UserAuth:: getUserId, userInfoAudit.getUserId())
                            .set(UserAuth:: getJobAuth, AuditEnum.SUCCESS.getCode())
                            .set(UserAuth:: getJobPic, userInfoAudit.getAuthPic())
                            .set(UserAuth:: getJobAuthType, userInfoAudit.getAuthOther()));
                    userAuthService.checkAuthNum(userInfoAudit.getUserId());
                }else if(userInfoAudit.getAuthType().equals(UserAuthTypeEnum.EDUCATION.getCode())){
                    userAuthService.update(Wrappers.lambdaUpdate(UserAuth.class)
                            .eq(UserAuth:: getUserId, userInfoAudit.getUserId())
                            .set(UserAuth:: getEducationAuth, AuditEnum.SUCCESS.getCode())
                            .set(UserAuth:: getEducationPic, userInfoAudit.getAuthPic()));
                    userAuthService.checkAuthNum(userInfoAudit.getUserId());
                }else if(userInfoAudit.getAuthType().equals(UserAuthTypeEnum.HOUSE.getCode())){
                    userAuthService.update(Wrappers.lambdaUpdate(UserAuth.class)
                            .eq(UserAuth:: getUserId, userInfoAudit.getUserId())
                            .set(UserAuth:: getHouseAuth, AuditEnum.SUCCESS.getCode())
                            .set(UserAuth:: getHousePic, userInfoAudit.getAuthPic()));
                    userAuthService.checkAuthNum(userInfoAudit.getUserId());
                }else if(userInfoAudit.getAuthType().equals(UserAuthTypeEnum.MARRIAGE.getCode())){
                    userAuthService.update(Wrappers.lambdaUpdate(UserAuth.class)
                            .eq(UserAuth:: getUserId, userInfoAudit.getUserId())
                            .set(UserAuth:: getMarriageAuth, AuditEnum.SUCCESS.getCode())
                            .set(UserAuth:: getMarriagePic, userInfoAudit.getAuthPic()));
                    userAuthService.checkAuthNum(userInfoAudit.getUserId());
                }else if(userInfoAudit.getAuthType().equals(UserAuthTypeEnum.SINGLE.getCode())){
                    userAuthService.update(Wrappers.lambdaUpdate(UserAuth.class)
                            .eq(UserAuth:: getUserId, userInfoAudit.getUserId())
                            .set(UserAuth:: getSinglePersonAuth, AuditEnum.SUCCESS.getCode()));
                    userAuthService.checkAuthNum(userInfoAudit.getUserId());
                }
            }
        }
    }

    @Override
    public void auditFail(Long id, String auditRemark) {
        boolean update = this.update(Wrappers.lambdaUpdate(UserAuthAudit.class)
                .eq(UserAuthAudit::getId, id)
                .eq(UserAuthAudit::getAuditStatus, AuditEnum.AUDITING.getCode())
                .set(UserAuthAudit::getAuditStatus, AuditEnum.FAIL.getCode())
                .set(UserAuthAudit::getAuditIp, ServletUtils.getClientIP())
                .set(UserAuthAudit::getAuditOpId, LoginHelper.getUserId())
                .set(UserAuthAudit::getAuditOpName, LoginHelper.getUsername())
                .set(UserAuthAudit::getAuditTime, LocalDateTime.now())
                .set(UserAuthAudit::getAuditRemark, auditRemark));
        if(update){
            UserAuthAudit userInfoAudit = this.getById(id);
            if(userInfoAudit.getAuthType().equals(UserAuthTypeEnum.CAR.getCode())){
                userAuthService.update(Wrappers.lambdaUpdate(UserAuth.class)
                        .eq(UserAuth:: getUserId, userInfoAudit.getUserId())
                        .in(UserAuth::getCarAuth, AuditEnum.AUDITING.getCode(), AuditEnum.NO.getCode())
                        .set(UserAuth:: getCarAuth, AuditEnum.FAIL.getCode()));
            }else if(userInfoAudit.getAuthType().equals(UserAuthTypeEnum.JOB.getCode())){
                userAuthService.update(Wrappers.lambdaUpdate(UserAuth.class)
                        .eq(UserAuth:: getUserId, userInfoAudit.getUserId())
                        .in(UserAuth::getJobAuth, AuditEnum.AUDITING.getCode(), AuditEnum.NO.getCode())
                        .set(UserAuth:: getJobAuth, AuditEnum.FAIL.getCode()));
            }else if(userInfoAudit.getAuthType().equals(UserAuthTypeEnum.EDUCATION.getCode())){
                userAuthService.update(Wrappers.lambdaUpdate(UserAuth.class)
                        .eq(UserAuth:: getUserId, userInfoAudit.getUserId())
                        .in(UserAuth::getEducationAuth, AuditEnum.AUDITING.getCode(), AuditEnum.NO.getCode())
                        .set(UserAuth:: getEducationAuth, AuditEnum.FAIL.getCode()));
            }else if(userInfoAudit.getAuthType().equals(UserAuthTypeEnum.HOUSE.getCode())){
                userAuthService.update(Wrappers.lambdaUpdate(UserAuth.class)
                        .eq(UserAuth:: getUserId, userInfoAudit.getUserId())
                        .in(UserAuth::getHouseAuth, AuditEnum.AUDITING.getCode(), AuditEnum.NO.getCode())
                        .set(UserAuth:: getHouseAuth, AuditEnum.FAIL.getCode()));
            }else if(userInfoAudit.getAuthType().equals(UserAuthTypeEnum.MARRIAGE.getCode())){
                userAuthService.update(Wrappers.lambdaUpdate(UserAuth.class)
                        .eq(UserAuth:: getUserId, userInfoAudit.getUserId())
                        .in(UserAuth::getMarriageAuth, AuditEnum.AUDITING.getCode(), AuditEnum.NO.getCode())
                        .set(UserAuth:: getMarriageAuth, AuditEnum.FAIL.getCode()));
            }else if(userInfoAudit.getAuthType().equals(UserAuthTypeEnum.SINGLE.getCode())){
                userAuthService.update(Wrappers.lambdaUpdate(UserAuth.class)
                        .eq(UserAuth:: getUserId, userInfoAudit.getUserId())
                        .in(UserAuth::getSinglePersonAuth, AuditEnum.AUDITING.getCode(), AuditEnum.NO.getCode())
                        .set(UserAuth:: getSinglePersonAuth, AuditEnum.FAIL.getCode()));
            }

        }
    }
}
