package com.fdkankan.ucenter.pay.strategy.impl;

import com.fdkankan.ucenter.entity.CameraDetail;
import com.fdkankan.ucenter.entity.CameraSpace;
import com.fdkankan.ucenter.entity.ExpansionOrder;
import com.fdkankan.ucenter.pay.strategy.OrderStrategy;
import com.fdkankan.ucenter.service.ICameraDetailService;
import com.fdkankan.ucenter.service.ICameraSpaceService;
import com.fdkankan.ucenter.service.IExpansionOrderService;
import com.fdkankan.ucenter.service.ISceneProService;
import lombok.extern.log4j.Log4j2;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Log4j2
@Component
public class ExpansionOrderImpl implements OrderStrategy {
    @Autowired
    private IExpansionOrderService expansionOrderService;
    @Autowired
    private ICameraSpaceService cameraSpaceService;
    @Autowired
    private ICameraDetailService cameraDetailService;
    @Autowired
    private ISceneProService sceneProService;

    @Override
    public void handleOrder(String orderSn, String tradeNo, String openId, int paymentTypeName, Long spaceId) throws Exception {
        ExpansionOrder expansionOrder = expansionOrderService.getByOrderSnNoPay(orderSn);
        if (expansionOrder == null) {
            log.error("找不到订单：" + orderSn);
            throw new Exception("找不到订单，out_trade_no错误");
        }
        //1表示充值，0表示系统赠送，2表示升级套餐
        int status = 1;
        if(spaceId == null){
            status = 2;
        }
        // 更新扩容订单
        boolean result = expansionOrderService.paySuccessExpansionOrder(orderSn, tradeNo, paymentTypeName, status);
        if (!result){
            log.error("更新扩容订单失败");
            throw new Exception("更新扩容订单失败");
        }

        CameraSpace cameraSpaceEntity = new CameraSpace();
        if(spaceId == null){
            //新的容量套餐方案
            cameraSpaceService.removeByCameraId(expansionOrder.getCameraId());
            cameraSpaceEntity = cameraSpaceService.saveByExpansionOrder(expansionOrder);
            CameraDetail detailEntity = cameraDetailService.getByCameraId(expansionOrder.getCameraId());
            detailEntity.setTotalSpace(cameraSpaceEntity.getSpace());
            cameraDetailService.updateById(detailEntity);
            sceneProService.lockOrUnLockBySpace(detailEntity,detailEntity.getCameraId() );
        }else {
            cameraSpaceEntity = cameraSpaceService.getById(spaceId);
            DateTime date = null;
            if(expansionOrder.getMonth() == 12){
                date = new DateTime(cameraSpaceEntity.getSpaceEndTime()).plusYears(1);
                if(cameraSpaceEntity.getSpaceEndTime().getYear() % 4 == 0){
                    date.plusDays(1);
                }
            }else {
                date = new DateTime(cameraSpaceEntity.getSpaceEndTime()).plusDays(expansionOrder.getMonth() * 31);
            }
            cameraSpaceEntity.setSpaceEndTime(date.toDate());
            cameraSpaceEntity.setOrderSn(cameraSpaceEntity.getOrderSn() + "," + orderSn);
            cameraSpaceService.updateById(cameraSpaceEntity);
        }
    }

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