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.UserPictures;
import com.ruoyi.xq.dto.admin.user.UserPicturesAdminVo;
import com.ruoyi.xq.enums.common.AuditEnum;
import com.ruoyi.xq.mapper.UserPicturesMapper;
import com.ruoyi.xq.service.UserPicturesService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 用户相册管理Service业务层处理
 *
 * @author 77
 * @date 2024-03-04
 */
@Service
public class UserPicturesServiceImpl extends ServiceImpl<UserPicturesMapper,UserPictures> implements UserPicturesService {

    @Override
    public List<UserPictures> listByUserIdAuditingAndSuccess(Long userId) {
        return this.list(Wrappers.lambdaQuery(UserPictures.class)
                .eq(UserPictures::getUserId, userId)
                .in(UserPictures::getAuditStatus, AuditEnum.AUDITING.getCode(), AuditEnum.SUCCESS.getCode()));
    }

    @Override
    public List<UserPictures> listByUserIdSuccess(Long userId) {
        return this.list(Wrappers.lambdaQuery(UserPictures.class)
                .eq(UserPictures::getUserId, userId)
                .in(UserPictures::getAuditStatus, AuditEnum.AUDITING.getCode(), AuditEnum.SUCCESS.getCode()));
    }

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

    @Override
    public void auditSuccess(String ids) {
        String[] idArray = ids.split(",");
        for (String id : idArray) {
            this.update(Wrappers.lambdaUpdate(UserPictures.class)
                    .eq(UserPictures::getId, id)
                    .eq(UserPictures::getAuditStatus, AuditEnum.AUDITING.getCode())
                    .set(UserPictures::getAuditStatus, AuditEnum.SUCCESS.getCode())
                    .set(UserPictures::getAuditIp, ServletUtils.getClientIP())
                    .set(UserPictures::getAuditOpId, LoginHelper.getUserId())
                    .set(UserPictures::getAuditOpName, LoginHelper.getUsername())
                    .set(UserPictures::getAuditTime, LocalDateTime.now())
                    .set(UserPictures::getAuditRemark, "审核成功"));
        }
    }

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