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

import com.fdkankan.common.constant.Constant;
import com.fdkankan.platform.goods.entity.CameraDetail;
import com.fdkankan.platform.goods.entity.CameraSpace;
import com.fdkankan.platform.goods.service.ICameraDetailService;
import com.fdkankan.platform.goods.service.ICameraSpaceService;
import com.fdkankan.platform.order.entity.CommerceOrder;
import com.fdkankan.platform.order.service.ICommerceOrderService;
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;

@Log4j2
@Component
public class CommerceOrderImpl implements OrderStrategy {
    @Autowired
    private ICameraSpaceService cameraSpaceService;
    @Autowired
    private ICameraDetailService cameraDetailService;
    @Autowired
    private ICommerceOrderService commerceOrderService;
    @Autowired
    private SceneUserSceneClient sceneUserSceneClient;

    @Override
    public void handleOrder(String orderSn, String tradeNo, String openId, int paymentTypeName, Long spaceId) throws Exception {
        CommerceOrder commerceOrder = commerceOrderService.getByOrderSn(orderSn);
        if (commerceOrder == null) {
            log.error("找不到订单：" + orderSn);
            throw new Exception("找不到订单，out_trade_no错误");
        }
        // 更新扩容订单
        boolean result = commerceOrderService.paySuccessCommerceOrder(orderSn, tradeNo, paymentTypeName);
        if (!result){
            log.error("更新商业订单失败");
            throw new Exception("更新商业订单失败");
        }
        String expansion = Constant.DEADLINE_SPACE_VALUE_YEAR;
        CameraDetail detailEntity = cameraDetailService.getByCameraId(commerceOrder.getCameraId());
        if (detailEntity.getCameraType() < 2){
            detailEntity.setCameraType(2);
            detailEntity.setTotalSpace(detailEntity.getTotalSpace() + Long.parseLong(expansion));
            cameraDetailService.updateById(detailEntity);
        }
        CameraSpace dbEntity = cameraSpaceService.getByUserIdAndCameraId(commerceOrder.getUserId(), commerceOrder.getCameraId());
        if (dbEntity == null){
            DateTime date = new DateTime();
            if (commerceOrder.getCommerceType() == 0){
                date = date.plusYears(commerceOrder.getCount());
            }else {
                date = date.plusDays(commerceOrder.getCount() * 31);
            }
            CameraSpace cameraSpaceEntity = new CameraSpace();
            cameraSpaceEntity.setOrderSn(orderSn);
            cameraSpaceEntity.setUserId(commerceOrder.getUserId());
            cameraSpaceEntity.setCameraId(commerceOrder.getCameraId());
            cameraSpaceEntity.setSpace(Long.parseLong(expansion));
            cameraSpaceEntity.setSpaceType(1);
            cameraSpaceEntity.setSpaceStartTime(new Date());
            cameraSpaceEntity.setSpaceEndTime(date.toDate());
            cameraSpaceService.save(cameraSpaceEntity);
        }else{
            DateTime oldEndTime = new DateTime(dbEntity.getSpaceEndTime().getTime());
            DateTime date;
            if (commerceOrder.getCommerceType() == 0){
                date = oldEndTime.plusYears(commerceOrder.getCount());
            }else {
                date = oldEndTime.plusDays(commerceOrder.getCount() * 31);
            }
            dbEntity.setSpaceEndTime(date.toDate());
            cameraSpaceService.updateById(dbEntity);
        }
        sceneUserSceneClient.unlockBeyondSpaceScenes(Long.valueOf(Constant.DEADLINE_SPACE_VALUE_YEAR), commerceOrder.getCameraId());
    }

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

}
