package org.linlinjava.jz.db.service;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import org.linlinjava.jz.db.dao.JzDmLabelMapper;
import org.linlinjava.jz.db.domain.JzDmLabel;
import org.linlinjava.jz.db.domain.JzDmLabelExample;
import org.linlinjava.jz.db.util.LabelTypeConstant;
import org.springframework.stereotype.Service;

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

/**
 * @author leo
 * 客户标签管理
 */
@Service
public class JzDmLabelService {

    @Resource
    private JzDmLabelMapper labelSettingMapper;


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

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

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

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

    public  List<JzDmLabel> all(){
        JzDmLabelExample example = new JzDmLabelExample();
        example.or().andDeletedEqualTo(false);
        return labelSettingMapper.selectByExample(example);
    }

    public JzDmLabel findById(Integer id) {
        return labelSettingMapper.selectByPrimaryKey(id);
    }

    public JzDmLabel add(JzDmLabel setting) {
        setting.setLabelTb(getTable(setting.getLabelType()));
        setting.setCreateTime(LocalDateTime.now());
        setting.setUpdateTime(LocalDateTime.now());
        labelSettingMapper.insertSelective(setting);
        return  setting;
    }

    String getTable(short type) {
        return LabelTypeConstant.GetTable(type);
    }

    public JzDmLabel update(JzDmLabel setting) {
        setting.setLabelTb(getTable(setting.getLabelType()));
        setting.setUpdateTime(LocalDateTime.now());
        labelSettingMapper.updateByPrimaryKeySelective(setting);
        return  setting;
    }

}
