package com.fdkankan.scene.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.fdkankan.common.constant.RecStatus;
import com.fdkankan.common.constant.TbStatus;
import com.fdkankan.scene.entity.Scene3dfamily;
import com.fdkankan.scene.entity.SceneCooperation;
import com.fdkankan.scene.mapper.ISceneCooperationMapper;
import com.fdkankan.scene.service.ISceneCooperationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dengsixing
 * @since 2021-12-23
 */
@Service
public class SceneCooperationServiceImpl extends ServiceImpl<ISceneCooperationMapper, SceneCooperation> implements ISceneCooperationService {

    @Override
    public void updateRecStatus(String sceneNum) {
        this.update(new LambdaUpdateWrapper<SceneCooperation>()
                .eq(SceneCooperation::getSceneCode, sceneNum)
                .set(SceneCooperation::getUpdateTime, Calendar.getInstance().getTime())
                .set(SceneCooperation::getRecStatus, RecStatus.DISABLE.code()));
    }

    @Override
    public String findUserName(String sceneNum) {

        SceneCooperation sceneCooperation = this.findBySceneNum(sceneNum);
        if(Objects.isNull(sceneCooperation)) return null;

        return sceneCooperation.getUserName();
    }

    @Override
    public SceneCooperation findBySceneNum(String sceneNum) {
        List<SceneCooperation> list = this.list(new QueryWrapper<SceneCooperation>()
                .eq("tb_status", TbStatus.VALID.code())
                .eq("rec_status", RecStatus.VALID.code())
                .eq("scene_code", sceneNum)
                .orderByDesc("id"));
        if(CollUtil.isEmpty(list)){
            return null;
        }
        return list.get(0);
    }

}
