package com.liquidnet.service.consumer.service.Utils;

import com.liquidnet.common.cache.redis.util.RedisUtil;
import com.liquidnet.service.kylin.constant.KylinRedisConst;
import com.liquidnet.service.kylin.dto.vo.middle.KylinTicketTimesVo;
import com.liquidnet.service.kylin.dto.vo.middle.KylinTicketVo;
import com.liquidnet.service.kylin.dto.vo.mongo.KylinOrderTicketVo;
import com.liquidnet.service.kylin.dto.vo.mongo.KylinPerformanceVo;
import com.liquidnet.service.kylin.dto.vo.returns.KylinOrderListVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Component
public class KylinUtils {

    @Autowired
    private RedisUtil redisUtil;
    /**
     * @param userId  用户id
     * @param type    1新增 2修改
     * @param orderId 订单id [需要新增或者修改的订单id]
     */
    public Boolean resetOrderListVo(String userId, Integer type, String orderId, KylinOrderTicketVo dataSingle) {
        List<KylinOrderListVo> vo = new ArrayList<>();
        List<KylinOrderListVo> redisVo = new ArrayList();
        KylinOrderListVo voItem = new KylinOrderListVo();
        KylinOrderTicketVo data;
        if (dataSingle == null) {
            data = (KylinOrderTicketVo)redisUtil.get(KylinRedisConst.ORDER + orderId);
        } else {
            data = dataSingle;
        }
        BeanUtils.copyProperties(data, voItem);
        List<KylinOrderListVo> redisData = (List<KylinOrderListVo>) redisUtil.get(KylinRedisConst.ORDER_LIST + userId);
        if(type == 1) {
            redisVo.add(voItem);
            if (redisData.size() > 0) {
                if (redisData.get(0).getOrderTicketsId().equals(orderId)) {
                    redisData.remove(0);
                }
            }
        }

        redisVo.addAll(redisData);

        switch (type) {
            case 1:
                for (int i = 0; i < redisVo.size(); i++) {
                    if (i == 40) {
                        break;
                    }
                    if (i == 0) {
                        vo.add(voItem);
                    } else {
                        vo.add(redisVo.get(i));
                    }
                }
                if (redisVo.size() == 0) {
                    vo.add(voItem);
                }
                redisUtil.set(KylinRedisConst.ORDER_LIST + userId,vo);
                return true;
            case 2:
                for (int i = 0; i < redisVo.size(); i++) {
                    if (i == 40) {
                        break;
                    }
                    if (redisVo.get(i).getOrderTicketsId().equals(orderId)) {
                        vo.add(voItem);
                    } else {
                        vo.add(redisVo.get(i));
                    }
                }
                redisUtil.set(KylinRedisConst.ORDER_LIST + userId,vo);
                return true;
            default:
                return false;
        }
    }

    public void changeBuyInfo(String userId, String idCard, String performanceId, String ticketId, int buyCount) {
        String redisKey;
        KylinPerformanceVo vo = (KylinPerformanceVo) redisUtil.get(KylinRedisConst.PERFORMANCES + performanceId);
        int isTrueName = vo.getIsTrueName();
        int ticketType = 0;
        String useTime = "";
        HashMap<String, ArrayList<String>> allTicketId = new HashMap<>();
        for (int x = 0; x < vo.getTicketTimeList().size(); x++) {
            KylinTicketTimesVo timeItem = vo.getTicketTimeList().get(x);
            ArrayList<String> ticketList = new ArrayList<>();
            for (int y = 0; y < timeItem.getTicketList().size(); y++) {
                KylinTicketVo ticketItem = timeItem.getTicketList().get(y);
                if (ticketItem.getType() == 1) {
                    ticketList.add(ticketItem.getTicketsId());
                }
                if (ticketItem.getTicketsId().equals(ticketId)) {
                    useTime = ticketItem.getUseStart();
                    ticketType = ticketItem.getType();
                }
            }
            allTicketId.put(timeItem.getUseStart(), ticketList);
        }

        if (0 == isTrueName) {
            redisKey = KylinRedisConst.USERID_BUY_INFO + userId;
        } else {
            redisKey = KylinRedisConst.IDCARD_BUY_INFO + idCard;
        }

        String performanceIdKey = redisKey + ":" + KylinRedisConst.PERFORMANCE_ID + ":" + performanceId;
        String ticketIdKey = redisKey + ":" + KylinRedisConst.TICKET_ID + ":" + ticketId;
        String ticketUseTimeKey = ticketIdKey + ":" + KylinRedisConst.USE_TIME;
        redisUtil.set(ticketUseTimeKey, useTime);
        if (buyCount > 0) {
            redisUtil.incr(ticketIdKey, buyCount);
            if (1 == ticketType) {
                int count = 0;
                for (int i = 0; i < allTicketId.get(useTime).size(); i++) {
                    String item = allTicketId.get(useTime).get(i);
                    if (allTicketId.get(useTime).size() == 1) {
                        redisUtil.incr(performanceIdKey, buyCount);
                        break;
                    }
                    if (null == redisUtil.get(redisKey + ":" + KylinRedisConst.TICKET_ID + ":" + item)) {
                        redisUtil.set(redisKey + ":" + KylinRedisConst.TICKET_ID + ":" + item, 0);
                    }
                    if (i == 0) {
                        count = (int) redisUtil.get(redisKey + ":" + KylinRedisConst.TICKET_ID + ":" + item);
                    } else {
                        if (count != (int) redisUtil.get(redisKey + ":" + KylinRedisConst.TICKET_ID + ":" + item)) {
                            redisUtil.incr(performanceIdKey, buyCount);
                            break;
                        }
                    }
                }
            } else {
                redisUtil.incr(performanceIdKey, buyCount);
            }
        } else {
            redisUtil.decr(ticketIdKey, Math.abs(buyCount));
            if (1 == ticketType) {
                int count = 0;
                for (int i = 0; i < allTicketId.get(useTime).size(); i++) {
                    String item = allTicketId.get(useTime).get(i);
                    if (allTicketId.get(useTime).size() == 1) {
                        redisUtil.decr(performanceIdKey, Math.abs(buyCount));
                        break;
                    }
                    if (null == redisUtil.get(redisKey + ":" + KylinRedisConst.TICKET_ID + ":" + item)) {
                        redisUtil.set(redisKey + ":" + KylinRedisConst.TICKET_ID + ":" + item, 0);
                    }
                    if (i == 0) {
                        count = (int) redisUtil.get(redisKey + ":" + KylinRedisConst.TICKET_ID + ":" + item);
                    } else {
                        if (count != (int) redisUtil.get(redisKey + ":" + KylinRedisConst.TICKET_ID + ":" + item)) {
                            redisUtil.decr(performanceIdKey, Math.abs(buyCount));
                            break;
                        }
                    }
                }
            } else {
                redisUtil.decr(performanceIdKey, Math.abs(buyCount));
            }
        }
    }
}
