package org.linlinjava.jz.db.service;

import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.linlinjava.jz.db.dao.JzDmChannelMapper;
import org.linlinjava.jz.db.domain.JzDmChannel;
import org.linlinjava.jz.db.domain.JzDmChannelExample;
import org.springframework.stereotype.Service;

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

/**
 * @author leo
 * 数据渠道 与来源不同，一个渠道可以对应多个来源。
 */
@Service
public class JzDmChannelService {

    @Resource
    private JzDmChannelMapper channelMapper;

    public List<JzDmChannel> querySelective(Integer page, Integer limit, String sort, String order) {
        JzDmChannelExample example = new JzDmChannelExample();
        JzDmChannelExample.Criteria criteria = JzDmChannelExample.newAndCreateCriteria();

        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        PageHelper.startPage(page, limit);
        return channelMapper.selectByExample(example);
    }

    public JzDmChannel add(JzDmChannel channel){
        channel.setCreateTime(LocalDateTime.now());
        channel.setUpdateTime(LocalDateTime.now());
        channelMapper.insertSelective(channel);
        return  channel;
    }

    public  List<JzDmChannel> all(){
        JzDmChannelExample example = new JzDmChannelExample();
        example.or().andDeletedEqualTo(false);
       return channelMapper.selectByExample(example);
    }

    public JzDmChannel queryById(int id){
        return channelMapper.selectByPrimaryKey(id);
    }

    public int update(JzDmChannel channel){
        channel.setUpdateTime(LocalDateTime.now());
        return channelMapper.updateByPrimaryKeySelective(channel);
    }

    public int delete(JzDmChannel channel){
        channel.setUpdateTime(LocalDateTime.now());
        channel.setDeleted(true);
        return channelMapper.updateByPrimaryKeySelective(channel);
    }
}
