package cn.ysk.cashier.service.impl;
import cn.ysk.cashier.dto.TbPlatformDictTypeDto;
import cn.ysk.cashier.dto.TbPlatformDictTypeQueryCriteria;
import cn.ysk.cashier.mapper.TbPlatformDictTypeMapper;
import cn.ysk.cashier.pojo.TbPlatformDictType;
import cn.ysk.cashier.utils.PageUtil;
import cn.ysk.cashier.utils.QueryHelp;
import cn.ysk.cashier.utils.ValidationUtil;
import lombok.RequiredArgsConstructor;
import cn.ysk.cashier.repository.TbPlatformDictTypeRepository;
import cn.ysk.cashier.service.TbPlatformDictTypeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;

/**
 * @website https://eladmin.vip
 * @description 服务实现
 * @author ww
 * @date 2024-05-11
 **/
@Service
@RequiredArgsConstructor
public class TbPlatformDictTypeServiceImpl implements TbPlatformDictTypeService {

    private final TbPlatformDictTypeRepository tbPlatformDictTypeRepository;
    private final TbPlatformDictTypeMapper tbPlatformDictTypeMapper;

    @Override
    public Map<String,Object> queryAll(TbPlatformDictTypeQueryCriteria criteria, Pageable pageable){
        Page<TbPlatformDictType> page = tbPlatformDictTypeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(tbPlatformDictTypeMapper::toDto));
    }

    @Override
    public List<TbPlatformDictTypeDto> queryAll(TbPlatformDictTypeQueryCriteria criteria){
        return tbPlatformDictTypeMapper.toDto(tbPlatformDictTypeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public TbPlatformDictTypeDto findById(Integer id) {
        TbPlatformDictType tbPlatformDictType = tbPlatformDictTypeRepository.findById(id).orElseGet(TbPlatformDictType::new);
        ValidationUtil.isNull(tbPlatformDictType.getId(),"TbPlatformDictType","id",id);
        return tbPlatformDictTypeMapper.toDto(tbPlatformDictType);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TbPlatformDictTypeDto create(TbPlatformDictType resources) {
        return tbPlatformDictTypeMapper.toDto(tbPlatformDictTypeRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(TbPlatformDictType resources) {
        TbPlatformDictType tbPlatformDictType = tbPlatformDictTypeRepository.findById(resources.getId()).orElseGet(TbPlatformDictType::new);
        ValidationUtil.isNull( tbPlatformDictType.getId(),"TbPlatformDictType","id",resources.getId());
        tbPlatformDictType.copy(resources);
        tbPlatformDictTypeRepository.save(tbPlatformDictType);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            tbPlatformDictTypeRepository.deleteById(id);
        }
    }
}