package cn.lihongjie.coal.meterDayLog.service;

import cn.lihongjie.coal.base.dto.CommonQuery;
import cn.lihongjie.coal.base.dto.IdRequest;
import cn.lihongjie.coal.base.service.BaseService;
import cn.lihongjie.coal.meterDayLog.dto.CreateMeterDayLogDto;
import cn.lihongjie.coal.meterDayLog.dto.MeterDayLogDto;
import cn.lihongjie.coal.meterDayLog.dto.UpdateMeterDayLogDto;
import cn.lihongjie.coal.meterDayLog.entity.MeterDayLogEntity;
import cn.lihongjie.coal.meterDayLog.mapper.MeterDayLogMapper;
import cn.lihongjie.coal.meterDayLog.repository.MeterDayLogRepository;
import cn.lihongjie.coal.meterMonthLog.service.MeterMonthLogService;

import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.ConversionService;
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;

@Service
@Slf4j
@Transactional
public
class MeterDayLogService extends BaseService<MeterDayLogEntity, MeterDayLogRepository> {
    @Autowired MeterMonthLogService meterMonthLogService;
  @Autowired
  private MeterDayLogRepository repository;
  @Autowired
  private MeterDayLogMapper mapper;
  @Autowired
  private ConversionService conversionService;
    @PersistenceContext
    private EntityManager entityManager;

  public MeterDayLogDto create(CreateMeterDayLogDto request) {
    MeterDayLogEntity entity = mapper.toEntity(request);


        this.repository.save(entity);
        return getById(entity.getId())
        ;
  }

  public MeterDayLogDto update(UpdateMeterDayLogDto request) {
    MeterDayLogEntity entity = this.repository.get(request.getId());
        this.mapper.updateEntity(entity, request);

        this.repository.save(entity);

        return getById(entity.getId())
        ;
  }

  public void delete(IdRequest request) {
    this.repository.deleteAllById(request.getIds())
        ;
  }

  public MeterDayLogDto getById(String id) {
    MeterDayLogEntity entity = repository.get(id);


        return mapper.toDto(entity)
        ;
  }

  public Page<MeterDayLogDto> list(CommonQuery query) {
     Page<MeterDayLogEntity> page = repository.findAll(query.specification(conversionService), PageRequest.of(query.getPageNo(), query.getPageSize(), Sort.by(query.getOrders())));


        return page.map(this.mapper::toDto)
        ;
  }

}
