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.PhotoAlbum;
import com.ym.mec.biz.dal.enums.YesOrNoEnum;
import com.ym.mec.biz.dal.page.PhotoQueryInfo;
import com.ym.mec.biz.service.PhotoAlbumService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.page.PageInfo;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import com.ym.mec.util.collection.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class PhotoAlbumServiceImpl extends BaseServiceImpl<Integer, PhotoAlbum>  implements PhotoAlbumService {
	
	@Autowired
	private PhotoAlbumDao photoAlbumDao;
	@Autowired
	private PhotoDao photoDao;

	@Override
	public BaseDAO<Integer, PhotoAlbum> getDAO() {
		return photoAlbumDao;
	}

    @Override
    public PageInfo<PhotoAlbum> queryAlbumPage(PhotoQueryInfo queryInfo) {
		PageInfo<PhotoAlbum> photoAlbumPageInfo = queryPage(queryInfo);
		List<PhotoAlbum> rows = photoAlbumPageInfo.getRows();
		if(rows.size() > 0){
			List<Integer> albumIds = rows.stream().map(e -> e.getId()).collect(Collectors.toList());
			Map<Integer, Long> totalNumMap = MapUtil.convertIntegerMap(photoAlbumDao.countTotalNumByAlbumIds(albumIds,queryInfo.getClientShow()));
			for (PhotoAlbum photoAlbum : rows) {
				photoAlbum.setPhotoNum(totalNumMap.get(photoAlbum.getId()));
			}
		}
		return photoAlbumPageInfo;
    }

    @Override
    public void batchUpdate(List<PhotoAlbum> photoAlbumList) {
		photoAlbumDao.batchUpdate(photoAlbumList);
    }

    @Override
	@Transactional(rollbackFor = Exception.class)
    public void batchDelete(String ids) {
		photoDao.deleteByAlbumIds(ids);
		photoAlbumDao.deleteByIds(ids);
    }
}