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

import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.cms.controller.queryinfo.NewsInformationQueryInfo;
import com.ym.mec.cms.dal.dao.SysNewsInformationDao;
import com.ym.mec.cms.dal.entity.NewsStatusEnum;
import com.ym.mec.cms.dal.entity.SysNewsInformation;
import com.ym.mec.cms.dto.SysNewsInformationDto;
import com.ym.mec.cms.service.SysNewsInformationService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.page.PageInfo;
import com.ym.mec.common.page.QueryInfo;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import com.ym.mec.util.collection.MapUtil;
import com.ym.mec.web.WebFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

@Service
public class SysNewsInformationServiceImpl extends BaseServiceImpl<Long, SysNewsInformation> implements SysNewsInformationService {

	@Autowired
	private SysNewsInformationDao sysNewsInformationDao;
	@Autowired
	private WebFeignService webFeignService;

	@Override
	public BaseDAO<Long, SysNewsInformation> getDAO() {
		return sysNewsInformationDao;
	}

    @Override
    public List<SysNewsInformation> queryBottomInfo(Integer subType) {
        return sysNewsInformationDao.queryBottomInfo(subType);
    }

	@Override
	public boolean deleteWithLogical(Long id) {
		sysNewsInformationDao.deleteWithLogical(id);
		return true;
	}

	@Override
	public Map<String,PageInfo<SysNewsInformation>> getHomeList(SysUser user, NewsInformationQueryInfo queryInfo) {
		//1精彩活动，2热门资讯,3banner类型
		Map<String,PageInfo<SysNewsInformation>> homeList = new HashMap<>();
		queryInfo.setStatus(NewsStatusEnum.SHOW);
		
		if (user == null || user.getId() == null) {
			queryInfo.setOrganId(43);
		} else {
			queryInfo.setOrganId(user.getOrganId());
		}
		
		String memo = queryInfo.getMemo();
		
		Map<String, Object> params = new HashMap<String, Object>();

		queryInfo.setType(3);
		MapUtil.populateMap(params, queryInfo);
		int count = sysNewsInformationDao.queryHomeCount(params);
		if(count == 0){
			queryInfo.setMemo(null);
		}
		PageInfo<SysNewsInformation> pageInfos = queryHomePage(queryInfo);
		homeList.put("banner",pageInfos);
		
		queryInfo.setMemo(null);
		queryInfo.setType(1);
		homeList.put("active",queryHomePage(queryInfo));
		queryInfo.setType(2);
		homeList.put("hot",queryHomePage(queryInfo));
		
		queryInfo.setType(5);
		homeList.put("flash",queryHomePage(queryInfo));
		
		queryInfo.setMemo(memo);
		queryInfo.setType(6);
		queryInfo.setTenantId(-1);
		MapUtil.populateMap(params, queryInfo);
		count = sysNewsInformationDao.queryHomeCount(params);
		if(count == 0){
			queryInfo.setMemo(null);
		}
		PageInfo<SysNewsInformation> app = queryHomePage(queryInfo);
		if(!CollectionUtils.isEmpty(app.getRows()) && user != null && user.getId() != null){
			for (SysNewsInformation row : app.getRows()) {
				if(row.getTitle().equals("进阶课堂")){
					try {
						//当前学员是否有可购买vip课
						row.setRedDot(webFeignService.queryVipPracticeGroups());
					}catch (Exception e){
						e.printStackTrace();
					}
				}
			}
		}
		homeList.put("app",app);
		return homeList;
	}
	
	@Override
	public SysNewsInformationDto queryById(Long id) {
		return sysNewsInformationDao.queryById(id);
	}

	private PageInfo<SysNewsInformation> queryHomePage(QueryInfo queryInfo) {
		PageInfo<SysNewsInformation> pageInfo = new PageInfo<SysNewsInformation>(queryInfo.getPage(), queryInfo.getRows());
		Map<String, Object> params = new HashMap<String, Object>();
		MapUtil.populateMap(params, queryInfo);
		
		List<SysNewsInformation> dataList = null;
		int count = sysNewsInformationDao.queryHomeCount(params);
		if (count > 0) {
			pageInfo.setTotal(count);
			params.put("offset", pageInfo.getOffset());
			dataList = sysNewsInformationDao.queryHomePage(params);
		}
		if (count == 0) {
			dataList = new ArrayList<>();
		}
		pageInfo.setRows(dataList);
		return pageInfo;
	}

	@Override
	public boolean autoUpdateStatus() {
		List<SysNewsInformation> list = sysNewsInformationDao.queryNeedUpdateStatusList();
		if(list != null && list.size() > 0){
			Date now = new Date();
			for(SysNewsInformation news : list){
				if(news.getStatus() == NewsStatusEnum.SHOW){
					news.setStatus(NewsStatusEnum.HIDDEN);
				}else{
					news.setStatus(NewsStatusEnum.SHOW);
				}
				news.setUpdateTime(now);
				sysNewsInformationDao.update(news);
			}
		}
		return true;
	}

}
