package me.zhengjie.modules.dm.order.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.dm.order.domain.DmOrderItem;
import me.zhengjie.modules.dm.order.domain.DmOrderPayment;
import me.zhengjie.modules.dm.order.domain.DmOrderRecord;
import me.zhengjie.modules.dm.order.repository.DmOrderPaymentRepository;
import me.zhengjie.modules.dm.order.service.DmOrderPaymentService;
import me.zhengjie.modules.dm.order.service.dto.DmOrderPaymentDto;
import me.zhengjie.modules.dm.order.service.dto.DmOrderPaymentQueryCriteria;
import me.zhengjie.modules.dm.order.service.mapstruct.DmOrderPaymentMapper;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import me.zhengjie.utils.ValidationUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class DmOrderPaymentServiceImpl implements DmOrderPaymentService {
    private final DmOrderPaymentRepository dmOrderPaymentRepository;
    private final DmOrderPaymentMapper dmOrderPaymentMapper;

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

    @Override
    public List<DmOrderPaymentDto> queryAll(DmOrderPaymentQueryCriteria criteria) {
        return dmOrderPaymentMapper.toDto(dmOrderPaymentRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public DmOrderPaymentDto findById(Integer id) {
        DmOrderPayment dmOrderRecord = dmOrderPaymentRepository.findById(id).orElseGet(DmOrderPayment::new);
        ValidationUtil.isNull(dmOrderRecord.getId(),"DmOrderPayment","id",id);
        return dmOrderPaymentMapper.toDto(dmOrderRecord);
    }

    @Override
    public DmOrderPaymentDto create(DmOrderPayment resources) {
        return dmOrderPaymentMapper.toDto(dmOrderPaymentRepository.save(resources));
    }

    @Override
    public void update(DmOrderPayment resources) {
        DmOrderPayment dmOrderRecord = dmOrderPaymentRepository.findById(resources.getId()).orElseGet(DmOrderPayment::new);
        ValidationUtil.isNull( dmOrderRecord.getId(),"DmOrderPayment","id",resources.getId());
        dmOrderRecord.copy(resources);
        dmOrderPaymentRepository.save(dmOrderRecord);
    }

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

    @Override
    public void download(List<DmOrderPaymentDto> all, HttpServletResponse response) throws IOException {

    }
}
