package com.fjs.scenic.service.good.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.fjs.scenic.cyt.response.GetProductStockByOTAResponseBody;
import com.fjs.scenic.entity.good.Goods;
import com.fjs.scenic.entity.good.PriceRule;
import com.fjs.scenic.entity.good.StopTime;
import com.fjs.scenic.mapper.good.DateSurplusMapper;
import com.fjs.scenic.mapper.good.PriceRuleMapper;
import com.fjs.scenic.mapper.good.StopTimeMapper;
import com.fjs.scenic.service.good.StopTimeService;
import com.fjs.scenic.service.ticket.TicketService;
import com.fjs.scenic.ticket.utils.DateUtil;
import com.fjs.scenic.utils.ReturnResult;

/**
 * This class is used for ... 管理员
 *
 * @author lfj
 * @version 1.0
 * @CreateDate ${date} ${time}
 */
@Service
public class StopTimeServiceImpl  implements StopTimeService {

    @Autowired
    private StopTimeMapper stopTimeMapper;
    
    @Override
    public ReturnResult stopTiket(String startTime,String endTime) {
    	List<StopTime> selectAll = stopTimeMapper.selectAll();
    	int i= 0 ;
    	if(null==selectAll || selectAll.size()<1){
    		StopTime record = new StopTime();
    		record.setStartTime(startTime);
    		record.setEndTime(endTime);
    		i = stopTimeMapper.insertSelective(record);
    	}else{
    		StopTime record = new StopTime();
    		record.setStartTime(startTime);
    		record.setEndTime(endTime);
    		i = stopTimeMapper.updateByPrimaryKeySelective(record);
    	}
    	if(i<1){
    		return ReturnResult.error("操作失败");
    	}
        return ReturnResult.ok("操作成功");
    }

    @Override
    public ReturnResult openTiket() {
    	int i = stopTimeMapper.delete();
    	if(i<1){
    		return ReturnResult.ok("未设置停票时间");
    	}
        return ReturnResult.ok("取消暂停售票成功");
    }

	@Override
	public boolean isStopTime() throws Exception {
		List<StopTime> selectAll = stopTimeMapper.selectAll();
		if(null==selectAll || selectAll.size()<1){
			return true;//ReturnResult.ok("没有时间限制,随时可以买票");
		}
		StopTime stopTime = selectAll.get(0);
		SimpleDateFormat df = new SimpleDateFormat("HH:mm");//设置日期格式
		String now = df.format(new Date());
		if(stopTime.getStartTime()!=null && stopTime.getEndTime()!=null){
			String[] startTime = stopTime.getStartTime().split(":");
			String[] endTime = stopTime.getEndTime().split(":");
			if(Integer.parseInt(startTime[0])>Integer.parseInt(endTime[0])){
				boolean boolen1 = hourMinuteBetween(now, stopTime.getStartTime(), "24:00");
				boolean boolen2 = hourMinuteBetween(now, "00:00", stopTime.getEndTime());
				if(boolen1 || boolen2){
					return false;//ReturnResult.ok("停票时间");
				}
				return true;//ReturnResult.ok("可以买票");
			}else{
				boolean boolen = hourMinuteBetween(now, stopTime.getStartTime(), stopTime.getEndTime());
				if(boolen){
					return false;//ReturnResult.ok("停票时间");
				}
				return true;//ReturnResult.ok("可以买票");
			}
		}
		return true;//ReturnResult.error("时间为空");
	}
	
	
	/**
	 * 
	 * @param nowDate   要比较的时间
	 * @param startDate   开始时间
	 * @param endDate   结束时间
	 * @return   true在时间段内，false不在时间段内
	 * @throws Exception
	 */
	public static boolean hourMinuteBetween(String nowDate, String startDate, String endDate) throws Exception{
		
		SimpleDateFormat format = new SimpleDateFormat("HH:mm");
		
		Date now = format.parse(nowDate);
		Date start = format.parse(startDate);
		Date end = format.parse(endDate);
		
		long nowTime = now.getTime();
		long startTime = start.getTime();
		long endTime = end.getTime();
		
		return nowTime >= startTime && nowTime <= endTime;
	}



}
