package org.linlinjava.jz.db.service;

import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.linlinjava.jz.db.dao.JzDepartmentMapper;
import org.linlinjava.jz.db.domain.JzDepartment;
import org.linlinjava.jz.db.domain.JzDepartmentExample;
import org.springframework.stereotype.Service;

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

/**
 * 公司部门管理
 */
@Service
public class JzDepartmentService {

    @Resource
    private JzDepartmentMapper departmentMapper;

    public List<JzDepartment> querySelective(String name, Integer page, Integer limit, String sort, String order) {
        JzDepartmentExample example = new JzDepartmentExample();
        JzDepartmentExample.Criteria criteria = example.createCriteria();

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

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

        PageHelper.startPage(page, limit);
        return departmentMapper.selectByExample(example);
    }

    public int add(JzDepartment department) {
        department.setCreateTime(LocalDateTime.now());
        department.setUpdateTime(LocalDateTime.now());
        return departmentMapper.insertSelective(department);
    }

    public int deleteById(JzDepartment department) {
        department.setUpdateTime(LocalDateTime.now());
        department.setDeleted(true);
        return departmentMapper.updateByPrimaryKeySelective(department);
    }

    public int updateById(JzDepartment department) {
        department.setUpdateTime(LocalDateTime.now());
        return departmentMapper.updateByPrimaryKeySelective(department);
    }

    public JzDepartment findById(Integer id) {
        return departmentMapper.selectByPrimaryKey(id);

    }

    public List<JzDepartment> queryAll(){
        JzDepartmentExample example = new JzDepartmentExample();
        example.or().andDeletedEqualTo(false);
        return departmentMapper.selectByExample(example);
    }

}
