package com.szxgl.nft.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.szxgl.nft.entity.NftPhotoDO;
import com.szxgl.nft.entity.UserNftPhoto;
import com.szxgl.nft.mapper.NftPhotoMapper;
import com.szxgl.nft.mapper.UserNftPhotoMapper;
import com.szxgl.nft.service.NftPhotoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Transient;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class NftPhotoServiceImpl implements NftPhotoService {

    @Autowired
    private NftPhotoMapper nftPhotoMapper;

    @Autowired
    private UserNftPhotoMapper userNftPhotoMapper;

    @Override
    public UserNftPhoto getNftPhoto(String phone) {
        QueryWrapper<UserNftPhoto> wrapper = new QueryWrapper<>();
        wrapper.eq("phone",phone);
        return userNftPhotoMapper.selectOne(wrapper);
    }

    @Override
    @Transient
    public void addPhonePaintingHash(String openid,String seed,Integer flag) {
        UserNftPhoto userNftPhoto = getNftPhoto(seed);
        //非定向人群
        if(userNftPhoto == null ){
            //随机获取图片id（图片没有被占用）
            Long id = nftPhotoMapper.getRandomHash(flag);
            //库存充裕
            if(id != null ){
                //图片绑定手机号
                nftPhotoMapper.addNftPhoto(id, seed);
            }
        }
    }

    @Override
    public Map<String,Object> getNftPhotoUrl(String openid) {
        return nftPhotoMapper.getNftPhotoUrl(openid);
    }

    @Override
    public Map<String, Object> getPhoneAndHash(String openid) {
        return nftPhotoMapper.getPhoneAndHash(openid);
    }

    @Override
    public int getNftCount() {
        return nftPhotoMapper.getNftCount();
    }

    @Override
    public String getStatus(String openid) {
        return nftPhotoMapper.getStatus(openid);
    }

}
