package cn.pluss.platform.jft.impl;

import cn.pluss.platform.entity.JftSmallClass;
import cn.pluss.platform.jft.JftSmallClassService;
import cn.pluss.platform.mapper.JftSmallClassMapper;
import cn.pluss.platform.util.Base64Util;
import cn.pluss.platform.util.OssOperatUtil;
import cn.pluss.platform.util.StringUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

/**
 * <p>
 * 缴费通小课堂信息表 服务实现类
 * </p>
 *
 * @author bzg
 * @since 2022-07-06
 */
@Service
public class JftSmallClassServiceImpl extends ServiceImpl<JftSmallClassMapper, JftSmallClass> implements JftSmallClassService {

    @Override
    public boolean saveOrUpdate(JftSmallClass entity) {
        if(StringUtil.isNotEmpty(entity.getPhotoBaseStr())){
            String photoBaseStr = entity.getPhotoBaseStr();
            if(photoBaseStr.contains(",")){
                photoBaseStr = photoBaseStr.substring( photoBaseStr.indexOf( "," ) + 1 );
            }
            InputStream is = Base64Util.base64ToInputStream(photoBaseStr);
            String url = OssOperatUtil.upload(is, StringUtil.genUuid(), "jpg");
            entity.setPhoto(url);
        }
        if(entity.getId() != null && StringUtil.isNotEmpty(entity.getPhotoBaseStr())){
            JftSmallClass smallClass = baseMapper.selectById(entity.getId());
            if(smallClass != null && StringUtil.isNotEmpty(smallClass.getPhoto())){
                OssOperatUtil.deleteUrlFile(smallClass.getPhoto());
            }
        }
        return super.saveOrUpdate(entity);
    }

    @Override
    public boolean removeById(Serializable id) {
        JftSmallClass jftSmallClass = baseMapper.selectById(id);
        if(jftSmallClass != null && StringUtil.isNotEmpty(jftSmallClass.getPhoto())){
            OssOperatUtil.deleteUrlFile(jftSmallClass.getPhoto());
        }
        return super.removeById(id);
    }
}
