package com.fjs.scenic.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fjs.scenic.cyt.response.GetProductStockByOTAResponseBody;
import com.fjs.scenic.entity.good.PriceRule;
import com.fjs.scenic.entity.order.PlayMan;
import com.fjs.scenic.entity.user.LinkMan;
import com.fjs.scenic.mapper.good.PriceRuleMapper;
import com.fjs.scenic.mapper.user.LinkManMapper;
import com.fjs.scenic.service.ord.OrdersService;
import com.fjs.scenic.service.ticket.OtaOrderService;
import com.fjs.scenic.service.ticket.TicketService;
import com.fjs.scenic.utils.ReturnResult;

import io.swagger.annotations.ApiOperation;


@RestController
@RequestMapping("/a")
public class TestController {
	
	private Logger log = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private TicketService ticketService;
	@Autowired
	private OrdersService ordersService;
	@Autowired
	private PriceRuleMapper priceRuleMapper;
	@Autowired
	private RedisTemplate<String, Object> redisTemplate;
	
	
	@GetMapping("/add")
	public ReturnResult add(Integer priceRuleId){
		PriceRule priceRule = priceRuleMapper.selectByPrimaryKey(priceRuleId);
        if (priceRule == null) {
        	return ReturnResult.build(500, "此时间无发售票");
        }
		ReturnResult r = ticketService.getCytProductStock(priceRule);
        if(r.getStatus()==200){
        	redisTemplate.opsForValue().set("priceRule", r.getData(), 1, TimeUnit.MINUTES);
        }
        return r;
	}
	
	
	@GetMapping("/del")
	public void del(){
		Set<String> keys = redisTemplate.keys("priceRule:" + "*");
        redisTemplate.delete(keys);
	}
	
	@GetMapping("/d")
	public void d(){
        redisTemplate.delete("priceRule:" + "2019-07-27");
	}
	
	@GetMapping("/expire")
	public void expire(){
		redisTemplate.expire("priceRule", 1, TimeUnit.MINUTES);
	}
	
	@GetMapping("/index")
	public ReturnResult getOrder(Integer priceRuleId){
		GetProductStockByOTAResponseBody.ProductstockInfos string = (GetProductStockByOTAResponseBody.ProductstockInfos)redisTemplate.opsForValue().get("priceRule");
    	if(string!=null){
    		//redisTemplate.expire("priceRule", 1, TimeUnit.MINUTES);
    		return ReturnResult.ok(string);
    	}
        PriceRule priceRule = priceRuleMapper.selectByPrimaryKey(priceRuleId);
        if (priceRule == null) {
        	return ReturnResult.build(500, "此时间无发售票");
        }
        ReturnResult r = ticketService.getCytProductStock(priceRule);
        if(r.getStatus()==200){
        	redisTemplate.opsForValue().set("priceRule", r.getData(), 1, TimeUnit.MINUTES);
        }
        return r;
	}
	
	
    /**
     * 模拟支付
     * @param request
     * @return
     */
   @ApiOperation(value = "获取菜单", hidden = false)
    @PostMapping("/testPayNitice/{orderNo}")
    public ReturnResult testPayNitice(@PathVariable("orderNo")String orderNo) throws Exception{
    	return ordersService.pay(orderNo);
    }

}
