package com.fdkankan.platform.user.strategy.impl;

import com.fdkankan.platform.order.entity.IncrementOrder;
import com.fdkankan.platform.order.service.IIncrementOrderService;
import com.fdkankan.platform.user.entity.User;
import com.fdkankan.platform.user.entity.UserIncrement;
import com.fdkankan.platform.user.service.IUserIncrementService;
import com.fdkankan.platform.user.service.IUserService;
import com.fdkankan.platform.user.strategy.OrderStrategy;
import com.fdkankan.scene.api.feign.SceneUserSceneClient;
import lombok.extern.log4j.Log4j2;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.UUID;

@Log4j2
@Component
public class IncrementOrderImpl implements OrderStrategy {
    @Autowired
    private IIncrementOrderService incrementOrderService;
    @Autowired
    private IUserIncrementService userIncrementService;

    @Autowired
    private IUserService userService;
    @Autowired
    private SceneUserSceneClient sceneUserSceneClient;

    @Override
    public void handleOrder(String orderSn, String tradeNo, String openId, int paymentTypeName, Long spaceId) throws Exception {
        IncrementOrder responseIncrementOrder = incrementOrderService.getByOrderSn(orderSn);
        if (responseIncrementOrder == null) {
            log.error("找不到订单：" + orderSn);
            throw new Exception("找不到订单，out_trade_no错误");
        }
        // 更新增值权益订单
        boolean result = incrementOrderService.paySuccessIncrementOrder(orderSn, tradeNo, paymentTypeName);
        if (!result){
            log.error("更新增值权益订单失败");
            throw new Exception("更新增值权益订单失败");
        }
        UserIncrement userIncrementEntity = new UserIncrement();
        if(responseIncrementOrder.getIncrementId() == null){

            //新的增值权益套餐方案
            DateTime date = new DateTime().plusYears(1);

            if(responseIncrementOrder.getCount() > 0){
                for(int i = 0, len = responseIncrementOrder.getCount(); i < len; i++){
                    userIncrementEntity = new UserIncrement();
                    userIncrementEntity.setKeyWord(UUID.randomUUID().toString().replace("-", ""));
                    userIncrementEntity.setUserId(responseIncrementOrder.getUserId());
                    userIncrementEntity.setOrderSn(orderSn);
                    userIncrementEntity.setIncrementStartTime(new Date());
                    userIncrementEntity.setIncrementEndTime(date.toDate());
                    userIncrementEntity.setIsExpired(0);
                    userIncrementService.save(userIncrementEntity);
                }
            }
        }else {
            userIncrementEntity = userIncrementService.getById(responseIncrementOrder.getIncrementId());
            DateTime date = null;
            Date nowDate = new Date();
            if(userIncrementEntity.getIncrementEndTime().getTime() > nowDate.getTime()){
                date = new DateTime(userIncrementEntity.getIncrementEndTime()).plusYears(1);
                if(userIncrementEntity.getIncrementEndTime().getYear() % 4 == 0){
                    date.plusDays(1);
                }
            }else {
                date = new DateTime(nowDate).plusYears(1);
                if(nowDate.getYear() % 4 == 0){
                    date.plusDays(1);
                }
            }
            userIncrementEntity.setIncrementEndTime(date.toDate());
            userIncrementEntity.setOrderSn(userIncrementEntity.getOrderSn() + "," + orderSn);
            userIncrementEntity.setCreateTime(new Date());
            userIncrementEntity.setIsExpired(0);
            userIncrementService.updateById(userIncrementEntity);

            //解封所有场景
            if(userIncrementEntity.getUserId() != null && userIncrementEntity.getCameraId() != null){
                sceneUserSceneClient.unlockBeyondSpaceScenes(null, userIncrementEntity.getCameraId());
            }
        }

        User userEntity = userService.getById(userIncrementEntity.getUserId());
        if(userEntity != null){
            userEntity.setDownloadNumTotal(userEntity.getDownloadNumTotal() + responseIncrementOrder.getCount() * 10);
            userService.updateById(userEntity);
        }
    }

    @Override
    public String getType() {
        return "increment";
    }
}
