/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.cku.oa.show.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.cku.oa.finance.service.SaPaymentDetailTotalService;
import com.cku.oa.show.dao.MainShowsDao;
import com.cku.oa.show.dao.ShowApplyDao;
import com.cku.oa.show.entity.MainShows;
import com.cku.oa.show.entity.ShowApply;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;

/**
 * 赛事子表Service
 * 
 * @author lgl
 * @version 2016-07-11
 */
@Service
@Transactional(readOnly = true)
public class MainShowsService extends CrudService<MainShowsDao, MainShows> {

	@Autowired
	private SaPaymentDetailTotalService saPaymentDetailTotalService;

	@Autowired
	private ShowApplyDao showApplyDao;

	public MainShows get(String id) {
		return super.get(id);
	}

	public List<MainShows> findAllMainShows() {
		return dao.findAllMainShows();
	}

	@Transactional(readOnly = false)
	public void save(MainShows mainShows) {
		super.save(mainShows);
	}

	/**
	 * 获取可以报名的主赛事
	 * 
	 * @return
	 */
	public List<MainShows> findUnCloseMainShows(String showLocation, String mainShowName, Date closingTime,
			Integer pageNo, Integer pageSize) {
		return dao.findUnCloseMainShows(showLocation, mainShowName, closingTime, pageNo, pageSize);
	}

	/**
	 * 
	 * @description: 定时任务执行方法，每天15点01分，把已过赛事报名时间的赛事标记为关闭报名状态
	 * @author: laiguanglong
	 * @date: 2016年10月31日 下午2:48:26
	 */
	@Transactional(readOnly = false)
	public int updateRegisterFlag() {
		return dao.updateRegisterFlag();
	}

	/**
	 * 删除图片
	 * 
	 * @Author chaixueteng
	 * @2017年8月28日上午10:34:43
	 */
	@Transactional(readOnly = false)
	public void deleteCkuImage(String mainShowId, String type) {
		MainShows mainShows = new MainShows();
		mainShows.setId(mainShowId);
		if ("handerImage".equals(type)) {
			mainShows.setHanderImage(type);
		} else if ("scheduleImage".equals(type)) {
			mainShows.setScheduleImage(type);
		} else {
			mainShows.setThumbnailImage(type);
		}
		mainShows.preUpdate();
		dao.deleteImage(mainShows);
	}

	@Transactional(readOnly = false)
	public int showEndReceive(){
		// 查询今日结束的比赛
		List<MainShows> shows = dao.findAllEndShow();
		int count = 0;
		for (MainShows show : shows) {
			ShowApply showApply = new ShowApply();
			showApply.setMainShowId(show.getId());
			List<ShowApply> applys = showApplyDao.findList(showApply);
			for (ShowApply apply : applys) {
				if(StringUtils.isNotEmpty(apply.getRunningNumber())) {
					if(saPaymentDetailTotalService.financeConfirmTime(apply.getRunningNumber()) > 0) {
						count ++;
					}
				}
				if(StringUtils.isNotEmpty(apply.getPhoneApplyRunningNumber())) {
					if(saPaymentDetailTotalService.financeConfirmTime(apply.getPhoneApplyRunningNumber()) > 0) {
						count ++;
					}
				}
			}
		}
		if(!CollectionUtils.isEmpty(shows)) {
			dao.updateShowStatus(shows);
		}
		return count;
	}
}