package com.cwhelp.modules.business.service.impl;

import com.cwhelp.common.data.PageSort;
import com.cwhelp.common.enums.StatusEnum;
import com.cwhelp.modules.business.domain.BssDept;
import com.cwhelp.modules.business.repository.BssDeptRepository;
import com.cwhelp.modules.business.service.BssDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author yan.y
 * @date 2019/07/29
 */
@Service
public class BssDeptServiceImpl implements BssDeptService {

    @Autowired
    private BssDeptRepository bssDeptRepository;

    /**
     * 根据ID查询数据
     * @param id 主键ID
     */
    @Override
    @Transactional
    public BssDept getById(Long id) {
        return bssDeptRepository.findById(id).orElse(null);
    }

    /**
     * 获取分页列表数据
     * @param example 查询实例
     * @return 返回分页数据
     */
    @Override
    public Page<BssDept> getPageList(Example<BssDept> example) {
        // 创建分页对象
        PageRequest page = PageSort.pageRequest();
        return bssDeptRepository.findAll(example, page);
    }

    /**
     * 保存数据
     * @param bssDept 实体对象
     */
    @Override
    public BssDept save(BssDept bssDept) {
        return bssDeptRepository.save(bssDept);
    }

    /**
     * 状态(启用，冻结，删除)/批量状态处理
     */
    @Override
    @Transactional
    public Boolean updateStatus(StatusEnum statusEnum, List<Long> idList) {
        return bssDeptRepository.updateStatus(statusEnum.getCode(), idList) > 0;
    }
}