package com.fdkankan.scene.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fdkankan.common.constant.ErrorCode;
import com.fdkankan.common.constant.PayStatus;
import com.fdkankan.common.exception.BusinessException;
import com.fdkankan.common.response.ResultData;
import com.fdkankan.platform.api.feign.PlatformGoodsClient;
import com.fdkankan.scene.entity.SceneEditInfo;
import com.fdkankan.scene.entity.ScenePlus;
import com.fdkankan.scene.entity.ScenePlusExt;
import com.fdkankan.scene.entity.ScenePro;
import com.fdkankan.scene.entity.SceneProExt;
import com.fdkankan.scene.mapper.IScenePlusMapper;
import com.fdkankan.scene.service.ISceneEditInfoService;
import com.fdkankan.scene.service.IScenePlusExtService;
import com.fdkankan.scene.service.IScenePlusService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.scene.vo.SceneCheckKeyParamVO;
import java.util.Calendar;
import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 场景主表 服务实现类
 * </p>
 *
 * @author 
 * @since 2022-03-16
 */
@Slf4j
@Service
public class ScenePlusServiceImpl extends ServiceImpl<IScenePlusMapper, ScenePlus> implements IScenePlusService {

    @Autowired
    private IScenePlusExtService scenePlusExtService;
    @Autowired
    private PlatformGoodsClient platformGoodsClient;
    @Autowired
    private IScenePlusService scenePlusService;
    @Autowired
    ISceneEditInfoService sceneEditInfoService;

    @Override
    public ScenePlus getScenePlusByNum(String num) {
        return this.getOne(new LambdaQueryWrapper<ScenePlus>().eq(ScenePlus::getNum, num));
    }

    @Override
    public ResultData resetSpace(String num) throws Exception{
        ScenePlus scenePlus = this.getScenePlusByNum(num);
        ScenePlusExt scenePlusExt = scenePlusExtService.getScenePlusExtByPlusId(scenePlus.getId());
        if(Objects.isNull(scenePlus)){
            log.info("该场景不存在无法重置容量，场景码为：" + num);
            return ResultData.error(ErrorCode.FAILURE_CODE_5058);
        }
        if (scenePlusExt.getSceneScheme() < 4){
            log.info("该场景不属于八目相机无法重置容量，场景码为：" + num);
            return ResultData.error(ErrorCode.FAILURE_CODE_5057);
        }
        platformGoodsClient.updateCameraDetailByCameraIdAndSpace(scenePlus.getCameraId(), -scenePlusExt.getSpace());

        scenePlusExt.setSpace(Long.parseLong("0"));
        scenePlusExtService.updateById(scenePlusExt);

        return ResultData.ok();
    }

    @Override
    public ResultData checkKey(SceneCheckKeyParamVO param) throws Exception {
//        ScenePro scenePro = sceneProService.getOne(
//            new LambdaQueryWrapper<ScenePro>()
//                .eq(ScenePro::getNum, param.getNum())
//                .eq(ScenePro::getPayStatus, PayStatus.PAY.code()));
        ScenePlus scenePlus = scenePlusService.getOne(new LambdaQueryWrapper<ScenePlus>()
            .eq(ScenePlus::getNum, param.getNum())
            .eq(ScenePlus::getPayStatus, PayStatus.PAY.code()));
        if (Objects.isNull(scenePlus)) {
            throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
        }
        SceneEditInfo sceneEditInfo = sceneEditInfoService.getByScenePlusId(scenePlus.getId());

        if (Objects.isNull(sceneEditInfo)) {
            throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
        }

        if(!param.getPassword().equals(sceneEditInfo.getScenePassword())){
            throw new BusinessException(ErrorCode.FAILURE_CODE_5021);
        }

        return ResultData.ok();
    }
}
