package com.fdkankan.scene.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.common.constant.CommonStatus;
import com.fdkankan.common.constant.ErrorCode;
import com.fdkankan.common.exception.BusinessException;
import com.fdkankan.scene.entity.SceneEditInfo;
import com.fdkankan.scene.entity.SceneEditInfoExt;
import com.fdkankan.scene.entity.ScenePlus;
import com.fdkankan.scene.mapper.ISceneEditInfoExtMapper;
import com.fdkankan.scene.service.ISceneEditInfoExtService;
import com.fdkankan.scene.service.ISceneEditInfoService;
import com.fdkankan.scene.service.IScenePlusService;
import java.util.Objects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2022-03-07
 */
@Service
public class SceneEditInfoExtServiceImpl extends ServiceImpl<ISceneEditInfoExtMapper, SceneEditInfoExt> implements ISceneEditInfoExtService {

    @Autowired
    private IScenePlusService scenePlusService;
    @Autowired
    private ISceneEditInfoService sceneEditInfoService;

    @Override
    public SceneEditInfoExt getBySceneProId(long sceneProId) {
        return this.getOne(new LambdaQueryWrapper<SceneEditInfoExt>().eq(SceneEditInfoExt::getSceneProId, sceneProId));
    }

    @Override
    public SceneEditInfoExt getByScenePlusId(long scenePlusId) {
        return this.getOne(new LambdaQueryWrapper<SceneEditInfoExt>().eq(SceneEditInfoExt::getScenePlusId, scenePlusId));
    }

    @Override
    public SceneEditInfoExt getByEditInfoId(long editInfoId) {
        return this.getOne(new LambdaQueryWrapper<SceneEditInfoExt>().eq(SceneEditInfoExt::getEditInfoId, editInfoId));
    }

    @Override
    public void updateToursByNum(String num, Integer tours) {
        ScenePlus scenePlus = scenePlusService.getScenePlusByNum(num);
        if(Objects.isNull(scenePlus)){
            throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
        }
        SceneEditInfo sceneEditInfo = sceneEditInfoService.getByScenePlusId(scenePlus.getId());
        sceneEditInfoService.upgradeVersionById(sceneEditInfo.getId());
        SceneEditInfoExt sceneEditInfoExt = this.getByEditInfoId(sceneEditInfo.getId());
        sceneEditInfoExt.setTours(tours);
        this.updateById(sceneEditInfoExt);
    }
}
