package com.zhiqiyun.open.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhiqiyun.open.core.enmus.BzDataType;
import com.zhiqiyun.open.core.mapper.bz.BaseDataInfoMapper;
import com.zhiqiyun.open.core.mapper.bz.SceneryInfoMapper;
import com.zhiqiyun.open.core.models.bz.BaseDataInfo;
import com.zhiqiyun.open.core.models.bz.SceneryInfo;
import com.zhiqiyun.open.core.service.SceneryInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;

@Service
public class SceneryInfoServiceImpl extends ServiceImpl<SceneryInfoMapper, SceneryInfo> implements SceneryInfoService {

    @Autowired
    private BaseDataInfoMapper baseDataInfoMapper;

    @Override
    public <E extends IPage<SceneryInfo>> E page(E page, Wrapper<SceneryInfo> queryWrapper) {
        E p = this.getBaseMapper().selectPage(page, queryWrapper);
        for (SceneryInfo entity : p.getRecords()) {
            BaseDataInfo baseDataInfo = this.baseDataInfoMapper.selectById(entity.getId());
            entity.setBaseDataInfo(baseDataInfo);
        }
        return p;
    }

    @Override
    @Transactional
    public boolean save(SceneryInfo entity) {
        BaseDataInfo baseDataInfo = entity.getBaseDataInfo();
        baseDataInfo.setId(entity.getId());
        baseDataInfo.setDataType(BzDataType.SCENERY);

        this.baseDataInfoMapper.insert(baseDataInfo);
        return super.save(entity);
    }

    @Override
    @Transactional
    public boolean updateById(SceneryInfo entity) {

        BaseDataInfo baseDataInfo = entity.getBaseDataInfo();
        baseDataInfo.setId(entity.getId());
        baseDataInfo.setDataType(BzDataType.SCENERY);

        this.baseDataInfoMapper.updateById(baseDataInfo);
        return super.updateById(entity);
    }

    @Override
    @Transactional
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        this.baseDataInfoMapper.deleteBatchIds(idList);
        return super.removeByIds(idList);
    }
}
