package com.ym.mec.biz.service.impl;

import com.ym.mec.biz.dal.dao.PhotoAlbumDao;
import com.ym.mec.biz.dal.dao.PhotoDao;
import com.ym.mec.biz.dal.entity.Photo;
import com.ym.mec.biz.dal.entity.PhotoAlbum;
import com.ym.mec.biz.dal.enums.YesOrNoEnum;
import com.ym.mec.biz.dal.page.PhotoQueryInfo;
import com.ym.mec.biz.service.PhotoService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.exception.BizException;
import com.ym.mec.common.page.PageInfo;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class PhotoServiceImpl extends BaseServiceImpl<Integer, Photo>  implements PhotoService {
	
	@Autowired
	private PhotoDao photoDao;
	@Autowired
	private PhotoAlbumDao photoAlbumDao;

	@Override
	public BaseDAO<Integer, Photo> getDAO() {
		return photoDao;
	}

    @Override
    public int del(String ids) {
		return photoDao.deleteByIds(ids);
    }

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int batchAdd(List<Photo> photoList) {
		if(photoList == null ||photoList.size() == 0){
			throw new BizException("请录入照片");
		}
		Photo photo = photoList.get(0);
		Integer photoAlbumId = photo.getPhotoAlbumId();
		String musicGroupId = photo.getMusicGroupId();
		if(photoAlbumId == null && StringUtils.isEmpty(musicGroupId)){
			throw new BizException("参数校验失败");
		}
		//如果相册编号是空，那么插入证书相册
		if(photoAlbumId == null){
			PhotoAlbum photoAlbum = photoAlbumDao.findByTypeAndGroupId(musicGroupId,"SHOW");
			if(photoAlbum == null){
				photoAlbum = new PhotoAlbum();
				photoAlbum.setMusicGroupId(musicGroupId);
				photoAlbum.setName("获奖证书");
				photoAlbum.setClientShow(YesOrNoEnum.YES);
				photoAlbum.setCoverUrl("https://daya.ks3-cn-beijing.ksyun.com/202107/Sdmx7vx.png");
				photoAlbum.setType("SHOW");
				photoAlbumDao.insert(photoAlbum);
			}
			photoAlbumId = photoAlbum.getId();
		}
		for (Photo e : photoList) {
			e.setPhotoAlbumId(photoAlbumId);
		}
		return photoDao.batchInsert(photoList);
	}

    @Override
    public int batchUpdate(List<Photo> photoList) {
		return photoDao.batchUpdate(photoList);
    }

    @Override
    public PageInfo<Photo> queryPhotoPage(PhotoQueryInfo queryInfo) {
		Integer photoAlbumId = queryInfo.getPhotoAlbumId();
		if(photoAlbumId == null){
			String type = queryInfo.getType();
			if(!"SHOW".equals(type)){
				throw new BizException("参数校验失败");
			}
			PhotoAlbum photoAlbum = photoAlbumDao.findByTypeAndGroupId(queryInfo.getMusicGroupId(), type);
			if(photoAlbum == null){
				photoAlbum = new PhotoAlbum();
				photoAlbum.setMusicGroupId(queryInfo.getMusicGroupId());
				photoAlbum.setName("获奖证书");
				photoAlbum.setClientShow(YesOrNoEnum.YES);
				photoAlbum.setType("SHOW");
				photoAlbumDao.insert(photoAlbum);
			}
			queryInfo.setPhotoAlbumId(photoAlbum.getId());
		}
        return queryPage(queryInfo);
    }
}