package com.fdkankan.scene.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fdkankan.common.constant.ErrorCode;
import com.fdkankan.common.constant.PayStatus;
import com.fdkankan.common.constant.SceneStatus;
import com.fdkankan.common.constant.ServerCode;
import com.fdkankan.common.exception.BusinessException;
import com.fdkankan.common.response.ResultData;
import com.fdkankan.common.util.DateExtUtil;
import com.fdkankan.platform.api.feign.PlatformGoodsClient;
import com.fdkankan.platform.api.feign.PlatformUserClient;
import com.fdkankan.platform.api.vo.Camera;
import com.fdkankan.platform.api.vo.UserIncrement;
import com.fdkankan.scene.entity.ScenePro;
import com.fdkankan.scene.service.ISceneEditService;
import com.fdkankan.scene.service.ISceneProService;
import com.fdkankan.scene.vo.BaseSceneParamVO;
import com.fdkankan.scene.vo.SceneAuthVO;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * TODO
 * </p>
 *
 * @author dengsixing
 * @since 2022/3/11
 **/
@Service
public class SceneEditServiceImpl implements ISceneEditService {

    @Autowired
    ISceneProService sceneProService;
    @Autowired
    PlatformGoodsClient platformGoodsClient;
    @Autowired
    PlatformUserClient platformUserClient;

    @Override
    public SceneAuthVO getAuth(BaseSceneParamVO param) throws Exception{

        ScenePro scenePro = sceneProService.getOne(
            new LambdaQueryWrapper<ScenePro>()
                .eq(ScenePro::getNum, param.getNum())
                .eq(ScenePro::getPayStatus,PayStatus.PAY.code())
                .in(ScenePro::getSceneStatus, SceneStatus.SUCCESS.code(), SceneStatus.NO_DISPLAY.code()));
        if(scenePro == null){
            throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
        }
        //判断该场景是否属于增值权益
        boolean isVip = false;
        //获取该相机是否有权益
        boolean isExpired  = false;
        ResultData<Camera> cameraResultData = platformGoodsClient.getCameraById(scenePro.getCameraId());
        if(!cameraResultData.getSuccess()){
            throw new Exception(ServerCode.FEIGN_REQUEST_FAILD.message());
        }
        Camera camera = cameraResultData.getData();
        if(camera != null){
            ResultData<UserIncrement> incrementResultData = platformUserClient
                .getUserIncrementByCameraId(camera.getId());
            if(!incrementResultData.getSuccess()){
                throw new Exception(ServerCode.FEIGN_REQUEST_FAILD.message());
            }
            UserIncrement userIncrement = incrementResultData.getData();
            if(userIncrement != null){
                if( userIncrement.getIsExpired().intValue() == 0){
                    isVip = true;
                }
                if(userIncrement.getIsExpired().intValue() == 1){
                    isExpired  = true;
                }
            }else{
                Date date = DateUtil.parse("2021-09-09 00:00:00", DateExtUtil.dateStyle);

                //非07批次的放开
                String pc = camera.getSnCode().substring(0,2);
                if(!pc.equals("07") ){
                    if(camera.getCreateTime()!=null && date.after(camera.getCreateTime())){
                        isVip = true;
                        isExpired  = false;
                    }
                }
            }
        }


        return SceneAuthVO.builder().isExpired(isExpired).isVip(isVip).build();
    }
}
