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.BssPlatform;
import com.cwhelp.modules.business.repository.BssPlatformRepository;
import com.cwhelp.modules.business.service.BssPlatformService;
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.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * @author yan.y
 * @date 2019/07/28
 */
@Service
public class BssPlatformServiceImpl implements BssPlatformService {

    @Autowired
    private BssPlatformRepository bssPlatformRepository;

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

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

    /**
     * 保存数据
     * @param bssPlatform 实体对象
     */
    @Override
    public BssPlatform save(BssPlatform bssPlatform) {
        return bssPlatformRepository.save(bssPlatform);
    }

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

    /**
     * 获取部门列表数据
     * @param example 查询实例
     * @param sort 排序对象
     */
    @Override
    public List<BssPlatform> getListByExample(Example<BssPlatform> example, Sort sort) {
        return bssPlatformRepository.findAll(example, sort);
    }

    @Override
    public boolean repeatByContactTel(BssPlatform bssPlatform) {
        Long id = bssPlatform.getId() != null ? bssPlatform.getId() : Long.MIN_VALUE;
        return bssPlatformRepository.findByContactTelAndIdNot(bssPlatform.getContactTel(), id) != null;
    }

    @Override
    public boolean repeatByContactCard(BssPlatform bssPlatform) {
        Long id = bssPlatform.getId() != null ? bssPlatform.getId() : Long.MIN_VALUE;
        return bssPlatformRepository.findByContactCardAndIdNot(bssPlatform.getContactCard(), id) != null;
    }

    @Override
    public boolean repeatByEmail(BssPlatform bssPlatform) {
        Long id = bssPlatform.getId() != null ? bssPlatform.getId() : Long.MIN_VALUE;
        return bssPlatformRepository.findByEmailAndIdNot(bssPlatform.getEmail(), id) != null;
    }
}