package com.fdkankan.modeling.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.modeling.entity.Scene;
import com.fdkankan.modeling.entity.SceneExt;
import com.fdkankan.modeling.mapper.ISceneMapper;
import com.fdkankan.modeling.service.ISceneExtService;
import com.fdkankan.modeling.service.ISceneService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class SceneServiceImpl extends ServiceImpl<ISceneMapper, Scene> implements ISceneService {

    @Autowired
    private ISceneExtService sceneExtService;

    @Override
    public void updateTime(String projectNum, Long space, int payStatus) {
        Scene scene = this.findBySceneNum(projectNum);
        if(scene != null){
            scene.setCreateTime(new Date());
            scene.setSceneStatus(-2);
            scene.setPayStatus(payStatus);
            this.updateById(scene);
            SceneExt sceneExt = sceneExtService.getBySceneId(scene.getId());
            if(sceneExt != null){
                sceneExt.setSpace(space);
                sceneExtService.updateById(sceneExt);
            }
        }
    }

    @Override
    public void updateStatus(String projectNum, int i) {
        UpdateWrapper<Scene> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().set(Scene::getSceneStatus,i);
        updateWrapper.lambda().eq(Scene::getSceneCode,projectNum);
        this.update(updateWrapper);
    }

    @Override
    public Scene findBySceneNum(String projectNum) {
        QueryWrapper<Scene> sceneQueryWrapper = new QueryWrapper<>();
        sceneQueryWrapper.lambda().eq(Scene::getSceneCode,projectNum);
        sceneQueryWrapper.lambda().eq(Scene::getRecStatus,"A");
        List<Scene> list = this.list(sceneQueryWrapper);
        if(CollUtil.isEmpty(list)){
            return null;
        }
        return list.get(0);
    }
}
