package cn.ysk.cashier.service.impl.shopimpl;

import cn.ysk.cashier.dto.shop.TbShopVideoDto;
import cn.ysk.cashier.dto.shop.TbShopVideoQueryCriteria;
import cn.ysk.cashier.mapper.shop.TbShopVideoMapper;
import cn.ysk.cashier.pojo.shop.TbShopVideo;
import cn.ysk.cashier.repository.shop.TbShopVideoRepository;
import cn.ysk.cashier.service.shop.TbShopVideoService;
import cn.ysk.cashier.utils.PageUtil;
import cn.ysk.cashier.utils.QueryHelp;
import cn.ysk.cashier.utils.ValidationUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class TbShopVideoServiceImpl implements TbShopVideoService {

    private final TbShopVideoRepository tbShopVideoRepository;
    private final TbShopVideoMapper tbShopVideoMapper;


    @Override
    public Map<String, Object> queryAllPage(TbShopVideoQueryCriteria criteria) {
        Pageable pageable = PageRequest.of(criteria.getPage(), criteria.getPageSize(), Sort.by("createdTime"));
        Page<TbShopVideo> page = tbShopVideoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        return PageUtil.toPage(page.map(tbShopVideoMapper::toDto));
    }

    @Override
    public List<TbShopVideoDto> queryAll(TbShopVideoQueryCriteria criteria) {
        return tbShopVideoMapper.toDto(tbShopVideoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
    }

    @Override
    @Transactional
    public TbShopVideoDto findById(Integer id) {
        TbShopVideo tbShopVideo = tbShopVideoRepository.findById(id).orElseGet(TbShopVideo::new);
        ValidationUtil.isNull(tbShopVideo.getId(), "TbShopVideo", "id", id);
        return tbShopVideoMapper.toDto(tbShopVideo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TbShopVideoDto create(TbShopVideo resources) {
        resources.setCreatedTime(new Timestamp(System.currentTimeMillis()));
        return tbShopVideoMapper.toDto(tbShopVideoRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(TbShopVideo resources) {
        TbShopVideo tbShopVideo = tbShopVideoRepository.findById(resources.getId()).orElseGet(TbShopVideo::new);
        ValidationUtil.isNull(tbShopVideo.getId(), "TbShopVideo", "id", resources.getId());
        tbShopVideo.copy(resources);
        tbShopVideo.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        tbShopVideoRepository.save(tbShopVideo);
    }

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