package org.linlinjava.jz.db.service;

import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.linlinjava.jz.db.dao.JzDmLabelProfileMapper;
import org.linlinjava.jz.db.domain.JzDmLabelProfile;
import org.linlinjava.jz.db.domain.JzDmLabelProfileExample;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author leo
 * 标签画像 多个标签得集合，算一个画像
 */
@Service
public class JzDmLabelProfileService {

    @Resource
    private JzDmLabelProfileMapper labelProfileMapper;


    public List<JzDmLabelProfile> querySelective(String name, Integer page, Integer size, String sort, String order) {
        JzDmLabelProfileExample example = new JzDmLabelProfileExample();
        JzDmLabelProfileExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, size);
        return labelProfileMapper.selectByExample(example);
    }


    public JzDmLabelProfile findById(Integer id) {
        return labelProfileMapper.selectByPrimaryKey(id);
    }

    public JzDmLabelProfile add(JzDmLabelProfile setting) {
        setting.setCreateTime(LocalDateTime.now());
        setting.setUpdateTime(LocalDateTime.now());
        labelProfileMapper.insertSelective(setting);
        return  setting;
    }

    public JzDmLabelProfile update(JzDmLabelProfile setting) {
        setting.setUpdateTime(LocalDateTime.now());
        labelProfileMapper.updateByPrimaryKeySelective(setting);
        return  setting;
    }
}
