package com.bjtdba.userCenter.service.impl;

import com.bjtdba.entity.poster.SharePosters;
import com.bjtdba.entity.userCenter.SharePostersClassify;
import com.bjtdba.userCenter.dao.SharePostersClassifyDao;
import com.bjtdba.userCenter.dao.SharePostersDao;
import com.bjtdba.userCenter.service.SharePostersService;
import com.bjtdba.utils.ResponseUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SharePostersServiceImpl implements SharePostersService {

    @Autowired
    private SharePostersClassifyDao sharePostersClassifyDao;

    @Autowired
    private SharePostersDao sharePostersDao;

    @Override
    public Object selectPosters() {
        Map<String, Object> data = new HashMap<>();
        List<SharePostersClassify> sharePostersClassifyList = sharePostersClassifyDao.selectPosters();
        if(sharePostersClassifyList.size()==0){
            sharePostersClassifyList = sharePostersClassifyDao.selectPosters();
        }
        data.put("sharePostersClassifyList",sharePostersClassifyList);
        return ResponseUtil.ok(data);
    }

    @Override
    public Object selectImgesList(int pageNum,int pageSize,String classify, String type) {
        Map<String, Object> data = new HashMap<>();
        PageHelper.startPage(pageNum,pageSize);
        List<SharePosters> sharePostersList = sharePostersDao.selectSharePostersById(classify,type);
        PageInfo<SharePosters> pageInfo = null;
        pageInfo = new PageInfo<SharePosters>(sharePostersList);
        if(pageInfo.getPages()<pageNum){
            PageHelper.startPage(0,0);
            List<SharePosters> sharePosterss = new ArrayList<>();
            pageInfo = new PageInfo<>(sharePosterss);
        }
        data.put("pageInfo",pageInfo);
        return ResponseUtil.ok(data);
    }
}
