package com.ruoyi.cai.service.impl;

import cn.hutool.core.util.RandomUtil;
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.cai.domain.User;
import com.ruoyi.cai.domain.UserCameraAudit;
import com.ruoyi.cai.dto.admin.vo.UserCameraAuditAdminVo;
import com.ruoyi.cai.dto.app.vo.CameraAuditVo;
import com.ruoyi.cai.enums.AuditStatusEnum;
import com.ruoyi.cai.mapper.UserCameraAuditMapper;
import com.ruoyi.cai.service.UserCameraAuditService;
import com.ruoyi.cai.service.UserService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.BeanConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 自拍认证Service业务层处理
 *
 * @author 77
 * @date 2023-12-30
 */
@Service
public class UserCameraAuditServiceImpl extends ServiceImpl<UserCameraAuditMapper, UserCameraAudit> implements UserCameraAuditService {

    public static final Map<Integer,String> ACTION_TYPES = new HashMap<>();

    static {
        ACTION_TYPES.put(1,"images/audit/ic_1.jpg");
        ACTION_TYPES.put(2,"images/audit/ic_2.jpg");
        ACTION_TYPES.put(3,"images/audit/ic_3.jpg");
        ACTION_TYPES.put(4,"images/audit/ic_4.jpg");
    }

    @Autowired
    private UserService userService;

    @Override
    public UserCameraAudit getByUserId(Long userId){
        return this.getOne(Wrappers.lambdaQuery(UserCameraAudit.class).eq(UserCameraAudit::getUserId,userId).last("limit 1"));
    }

    @Override
    public CameraAuditVo cameraAudit(Long userId) {
        UserCameraAudit userCameraAudit = this.getByUserId(userId);
        CameraAuditVo vo;
        if(userCameraAudit != null){
            vo = BeanConvertUtil.convertTo(userCameraAudit, CameraAuditVo::new);
        } else {
            int actionType = RandomUtil.randomInt(0, 4);
            vo = new CameraAuditVo();
            vo.setUserId(userId);
            vo.setActionType(actionType);
            vo.setActionImage(ACTION_TYPES.get(actionType));
            vo.setAuditStatus(AuditStatusEnum.NONE.getCode());
        }
        User user = userService.getById(userId);
        if(user.getCameraStatus() == 1){
            vo.setAuditStatus(AuditStatusEnum.SUCCESS.getCode());
            vo.setAuditRemark(null);
        }
        return vo;
    }

    @Override
    public Boolean uploadCameraAudit(CameraAuditVo vo) {
        if(vo.getActionType() == null || ACTION_TYPES.get(vo.getActionType()) == null){
            throw new ServiceException("动作类型参数错误");
        }
        User user = userService.getById(vo.getUserId());
        if (user.getCameraStatus() == 1) {
            throw new ServiceException("您已经通过自拍认证,不能重复认证!");
        }
        UserCameraAudit userCameraAudit = this.getByUserId(vo.getUserId());
        if(userCameraAudit != null && userCameraAudit.getAuditStatus() == 2){
            throw new ServiceException("您的提交的自拍认证正在审核中请耐心等待!");
        }

        UserCameraAudit update = new UserCameraAudit();
        update.setUserId(vo.getUserId());
        update.setAuditStatus(AuditStatusEnum.AUDITING.getCode());
        update.setActionType(vo.getActionType());
        update.setActionImage(vo.getActionImage());
        if(userCameraAudit != null){
            update.setId(userCameraAudit.getId());
            update.setAuditCount(userCameraAudit.getAuditCount()+1);
        }
        update.setCreateTime(LocalDateTime.now());
        return this.saveOrUpdate(update);
    }

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