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.UserInfoAudit;
import com.ruoyi.xq.domain.UserPictures;
import com.ruoyi.xq.dto.admin.useraudit.UserInfoAuditAdminVo;
import com.ruoyi.xq.dto.app.common.IdsReq;
import com.ruoyi.xq.enums.common.AuditEnum;
import com.ruoyi.xq.enums.user.UserInfoAuditTypeEnum;
import com.ruoyi.xq.enums.userinfo.UserHousingStatusEnum;
import com.ruoyi.xq.mapper.UserInfoAuditMapper;
import com.ruoyi.xq.service.UserInfoAuditService;
import com.ruoyi.xq.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 用户信息审核Service业务层处理
 *
 * @author 77
 * @date 2024-03-04
 */
@RequiredArgsConstructor
@Service
public class UserInfoAuditServiceImpl extends ServiceImpl<UserInfoAuditMapper,UserInfoAudit> implements UserInfoAuditService {

    @Autowired
    private UserService userService;

    @Override
    public UserInfoAudit getAuditingDataByUserIdAndType(Long userId, UserInfoAuditTypeEnum auditTypeEnum){
        return this.getOne(Wrappers.lambdaQuery(UserInfoAudit.class)
                .eq(UserInfoAudit::getInfoType, auditTypeEnum.getCode())
                .eq(UserInfoAudit::getUserId, userId)
                .eq(UserInfoAudit::getAuditStatus, AuditEnum.AUDITING.getCode())
                .last("limit 1"));
    }

    @Override
    public Page<UserInfoAuditAdminVo> pageAdmin(PageQuery pageQuery, UserInfoAuditAdminVo 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(UserInfoAudit.class)
                    .eq(UserInfoAudit::getId, id)
                    .eq(UserInfoAudit::getAuditStatus, AuditEnum.AUDITING.getCode())
                    .set(UserInfoAudit::getAuditStatus, AuditEnum.SUCCESS.getCode())
                    .set(UserInfoAudit::getAuditIp, ServletUtils.getClientIP())
                    .set(UserInfoAudit::getAuditOpId, LoginHelper.getUserId())
                    .set(UserInfoAudit::getAuditOpName, LoginHelper.getUsername())
                    .set(UserInfoAudit::getAuditTime, LocalDateTime.now())
                    .set(UserInfoAudit::getAuditRemark, "审核成功"));
            if(flag){
                UserInfoAudit userInfoAudit = this.getById(id);
                if(userInfoAudit.getInfoType().equals(UserInfoAuditTypeEnum.AVATAR.getCode())){
                    userService.update(Wrappers.lambdaUpdate(User.class)
                            .eq(User::getId, userInfoAudit.getUserId())
                            .set(User::getAvatar, userInfoAudit.getAuditContent()));
                }else if(userInfoAudit.getInfoType().equals(UserInfoAuditTypeEnum.REMARK.getCode())){
                    userService.update(Wrappers.lambdaUpdate(User.class)
                            .eq(User::getId, userInfoAudit.getUserId())
                            .set(User::getRemark, userInfoAudit.getAuditContent()));
                }

            }
        }
    }

    @Override
    public void auditFail(Long id, String auditRemark) {
        this.update(Wrappers.lambdaUpdate(UserInfoAudit.class)
                .eq(UserInfoAudit::getId, id)
                .eq(UserInfoAudit::getAuditStatus, AuditEnum.AUDITING.getCode())
                .set(UserInfoAudit::getAuditStatus, AuditEnum.FAIL.getCode())
                .set(UserInfoAudit::getAuditIp, ServletUtils.getClientIP())
                .set(UserInfoAudit::getAuditOpId, LoginHelper.getUserId())
                .set(UserInfoAudit::getAuditOpName, LoginHelper.getUsername())
                .set(UserInfoAudit::getAuditTime, LocalDateTime.now())
                .set(UserInfoAudit::getAuditRemark, auditRemark));
    }

}
