package io.renren.modules.generator.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.datasource.annotation.DataSource;
import io.renren.modules.generator.dao.AlgorithmDao;
import io.renren.modules.generator.dao.ModelDao;
import io.renren.modules.generator.entity.AlgorithmEntity;
import io.renren.modules.generator.entity.ImagesEntity;
import io.renren.modules.generator.entity.ModelEntity;
import io.renren.modules.generator.service.AlgorithmService;
import io.renren.modules.generator.service.ModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @Description
 * @Author chenrj.xdu@gmail.com
 * @Version V1.0.0
 * @Since 1.0
 * @Date 2021/11/24
 */
@Service
@DataSource("slave2")
public class ModelServiceImpl extends ServiceImpl<ModelDao, ModelEntity> implements ModelService {

    @Autowired
    ModelDao modelDao;

    public boolean updateById(ModelEntity model) {
        if (modelDao.updateById(model) == 1) {
            return true;
        }
        return false;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String categoryId = (String) params.get("categoryId");
        String modelName = (String) params.get("modelName");
        IPage<ModelEntity> page = this.page(
                new Query<ModelEntity>().getPage(params),
                new QueryWrapper<ModelEntity>()
                        .eq(StringUtils.isNotBlank(categoryId), "category_id", categoryId)
                        .like(StringUtils.isNotBlank(modelName), "model_name", modelName)
                        .eq(StringUtils.isNotBlank(categoryId), "category_id", categoryId)
                        .like(StringUtils.isNotBlank(modelName), "model_name", modelName)
        );

        return new PageUtils(page);
    }

    @Override
    public int updateConstructMessage(ModelEntity model) {
        return modelDao.updateConstructMessage(model);
    }


}
