package com.yonge.cooleshow.cms.service.impl;

import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.cms.controller.queryinfo.NewsInformationQueryInfo;
import com.yonge.cooleshow.cms.dal.dao.SysNewsInformationDao;
import com.yonge.cooleshow.cms.dal.entity.NewsStatusEnum;
import com.yonge.cooleshow.cms.dal.entity.SysNewsInformation;
import com.yonge.cooleshow.cms.dto.SysNewsInformationDto;
import com.yonge.cooleshow.cms.service.SysNewsInformationService;
import com.yonge.toolset.base.exception.BizException;
import com.yonge.toolset.mybatis.dal.BaseDAO;
import com.yonge.toolset.base.page.PageInfo;
import com.yonge.toolset.base.page.QueryInfo;
import com.yonge.toolset.mybatis.service.impl.BaseServiceImpl;
import com.yonge.toolset.utils.collection.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

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

	@Autowired
	private SysNewsInformationDao sysNewsInformationDao;

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

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

	@Override
	@Transactional
	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.getCode().toString());
		
		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);

		/*boolean isNewer = false;
		Date date = new Date();
		Date startDate = queryInfo.getDate();
		Date endDate = null;
		if (startDate == null) {
			try {
				String startDateStr = "2020-05-01";
				startDate = sdf.parse(startDateStr);
				endDate = sdf.parse("2021-02-01");
			} catch (ParseException e) {
				throw new RuntimeException(e);
			}
		}
		if (queryInfo.getTenantId() == 1 && date.after(startDate) && date.before(endDate)) {
			if (user != null && "18696158274".equals(user.getPhone()) == false) {
				isNewer = webFeignService.isPurchasedPracticeCourse(user.getId()) == false;
				SysNewsInformation info = new SysNewsInformation();
				info.setOrder(1);
				info.setCreateTime(date);
				info.setUpdateTime(date);
				if (isNewer) {
					info.setLinkUrl(serverContextPath + "/#/auditionActive");
					info.setCoverImage("https://daya-online.oss-cn-beijing.aliyuncs.com/tempActivity/3_1.png");
				} else {
					info.setLinkUrl(serverContextPath + "/#/auditionActive");
					info.setCoverImage("https://daya-online.oss-cn-beijing.aliyuncs.com/tempActivity/3_2.png");
				}
				pageInfos.getRows().add(info);
			}
		}*/
		homeList.put("banner",pageInfos);
		
		queryInfo.setMemo(memo);
		queryInfo.setType(6);
		MapUtil.populateMap(params, queryInfo);
		count = sysNewsInformationDao.queryHomeCount(params);
		if(count == 0){
			queryInfo.setMemo(null);
		}
		homeList.put("app",queryHomePage(queryInfo));
		
		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));
		return homeList;
	}
	
	@Override
	public SysNewsInformationDto queryById(Long id) {
		return sysNewsInformationDao.queryById(id);
	}

	private PageInfo<SysNewsInformation> queryHomePage(QueryInfo queryInfo) {
		PageInfo<SysNewsInformation> pageInfo = new PageInfo<>(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<SysNewsInformation>();
		}
		pageInfo.setRows(dataList);
		return pageInfo;
	}

	@Override
	@Transactional
	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;
	}

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

    @Override
	@Transactional
    public boolean updateStatus(Long id) {
		SysNewsInformationDto sysNewsInformationDto = sysNewsInformationDao.queryById(id);
		if (sysNewsInformationDto.getType() != 2) {
			sysNewsInformationDao.updateStatus(id);
			return true;
		}

		if (sysNewsInformationDto.getStatus().getCode().equals(NewsStatusEnum.SHOW.getCode())) {
			sysNewsInformationDao.updateStatus(id);
			return true;
		}

		Integer count = sysNewsInformationDao.selectByVersionAndStatus(null,NewsStatusEnum.SHOW.getCode());

		if (count >0) {
			throw  new BizException("有正在启用的广告");
		}
		sysNewsInformationDao.updateStatus(id);
		return true;
    }

	@Override
	public String selectAppVersionInfo(String platform, String version) {
		return sysNewsInformationDao.selectAppVersionInfo(platform, version);
	}
}
