package com.fdkankan.scene.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fdkankan.common.constant.RecStatus;
import com.fdkankan.common.constant.TbStatus;
import com.fdkankan.scene.entity.ScenePro;
import com.fdkankan.scene.entity.SceneProEdit;
import com.fdkankan.scene.entity.SceneProEditExt;
import com.fdkankan.scene.mapper.ISceneProEditExtMapper;
import com.fdkankan.scene.service.ISceneProEditExtService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.scene.service.ISceneProEditService;
import com.fdkankan.scene.service.ISceneProService;
import com.fdkankan.common.response.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * pro场景编辑数据表 服务实现类
 * </p>
 *
 * @author dengsixing
 * @since 2021-12-23
 */
@Service
public class SceneProEditExtServiceImpl extends ServiceImpl<ISceneProEditExtMapper, SceneProEditExt> implements ISceneProEditExtService {

    @Autowired
    ISceneProService sceneProService;
    @Autowired
    ISceneProEditService sceneProEditService;

    @Override
    public SceneProEditExt getByProEditId(Long proEditId) {
        return this.getOne(new QueryWrapper<SceneProEditExt>().eq("pro_edit_id", proEditId));
    }

    @Override
    public ResultData updateimageVersion(String sceneNum) throws Exception {

        List<ScenePro> sceneProList = sceneProService.list(
                new LambdaQueryWrapper<ScenePro>()
                        .select(ScenePro::getId)
                        .eq(ScenePro::getTbStatus, TbStatus.VALID.code())
                        .eq(ScenePro::getNum, sceneNum));
        if(CollUtil.isEmpty(sceneProList)){
            ResultData.ok(0);
        }

        List<Long> sceneProIds = sceneProList.stream().map(scenePro -> {
            return scenePro.getId();
        }).collect(Collectors.toList());

        List<SceneProEditExt> extList = this.list(new LambdaQueryWrapper<SceneProEditExt>()
                .in(SceneProEditExt::getProId, sceneProIds));
        if(CollUtil.isEmpty(extList)){
            ResultData.ok(0);
        }
        extList.stream().forEach(ext -> {
            ext.setImagesVersion(ext.getImagesVersion()+1);
        });

        this.updateBatchById(extList);

        return ResultData.ok(extList.size());
    }

    @Override
    public SceneProEditExt findEdit(ScenePro scenePro) {
        SceneProEdit edit = sceneProEditService.getOne(
                new LambdaQueryWrapper<SceneProEdit>()
                        .select(SceneProEdit::getId)
                        .eq(SceneProEdit::getProId, scenePro.getId())
                        .eq(SceneProEdit::getTbStatus, TbStatus.VALID.code()));

        return this.getOne(new LambdaQueryWrapper<SceneProEditExt>().eq(SceneProEditExt::getProEditId, edit.getId()));
    }
}
