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.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.mapper.good.DateSurplusMapper;
import com.fjs.scenic.mapper.good.GoodsMapper;
import com.fjs.scenic.mapper.good.PriceRuleMapper;
import com.fjs.scenic.service.common.impl.BaseServiceImpl;
import com.fjs.scenic.service.good.GoodsService;
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 GoodsServiceImpl extends BaseServiceImpl<Goods> implements GoodsService {
	
	private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private PriceRuleMapper priceRuleMapper;
    @Autowired
    private DateSurplusMapper dateSurplusMapper;
    @Autowired
    private TicketService ticketService;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    
    @Override
    public ReturnResult goodsOrderPrice(Integer goodsId) {
        List<Date> dates = DateUtil.getDatesBetweenTwoDateByNow();
        List<Map<String, Object>> res = new ArrayList<>();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        List<PriceRule> priceRules = priceRuleMapper.selectPriceRulesByGoodsId(goodsId);
        for (Date date : dates) {
            Map<String, Object> map = new HashMap<>();
            map.put("date", format.format(date));
            map.put("price", "");
            map.put("repertory", "0");
            map.put("played", false);
            for (PriceRule priceRule : priceRules) {
                if (DateUtil.isSameDateByTime(priceRule.getDate(), date)) {
                    map.put("price", (double) priceRule.getSellPrice() / 100);
                    //当没有配置库存时，前端不显示有票
                    map.put("repertory", (priceRule.getRepertory() == null) ? "" : "有票");
                    map.put("priceRuleId", priceRule.getId());
                    //当库存==0时,该日期无法买票
                    map.put("played", (priceRule.getRepertory() != null && priceRule.getRepertory() == 0) ? false : true);
                }
            }
            res.add(map);
        }
        return ReturnResult.ok(res);
    }

    @Override
    public ReturnResult goodsDateList(Integer priceRuleId) {
        List<Map<String, String>> dateList = dateSurplusMapper.getDateListByPriceRuleId(priceRuleId);
        return ReturnResult.ok(dateList);
    }

    @Override
    public ReturnResult getGoodsStock(Integer priceRuleId) {
        PriceRule priceRule = priceRuleMapper.selectByPrimaryKey(priceRuleId);
        if (priceRule == null) {
        	return ReturnResult.build(500, "此时间无发售票");
        }
        Goods goods = goodsMapper.selectByGoodsId(priceRule.getGoodsId());
        if(goods==null){
        	return ReturnResult.build(500, "此时间无发售票");
        }
        if(goods.getSortType().equals("0")){
        	return ReturnResult.ok();
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(priceRule.getDate());
        GetProductStockByOTAResponseBody.ProductstockInfos string = (GetProductStockByOTAResponseBody.ProductstockInfos)redisTemplate.opsForValue().get("priceRule:"+format+goods.getDoorType());
    	if(string!=null){
    		log.info("缓存中获取分时库存，priceRuleId={},stok={}",priceRuleId,string);
    		//redisTemplate.expire("priceRule:"+format, 1, TimeUnit.MINUTES);
    		return ReturnResult.ok(string);
    	}
        ReturnResult r = ticketService.getCytProductStock(priceRule);
        log.info("接口获取分时库存，priceRuleId={},stok={}",priceRuleId,r.getData());
        if(r.getStatus()==200){
        	redisTemplate.opsForValue().set("priceRule:"+format+goods.getDoorType(), r.getData(), 1, TimeUnit.MINUTES);
        }
        return r;
    }

    /**
     * 暂停或恢复售票
     * @param startDate
     * @param endDate
     * @param saleTicketType 1恢复售票0暂停售票
     * @return
     */
    @Override
    public int saleOrRecoverSaleTicket(Date startDate, Date endDate, int saleTicketType) {
        return priceRuleMapper.saleOrRecoverSaleTicket(startDate, endDate, saleTicketType);
    }

}
