package cn.lihongjie.coal.coalBlend.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.coalBlend.dto.CoalBlendDto;
import cn.lihongjie.coal.coalBlend.dto.CreateCoalBlendDto;
import cn.lihongjie.coal.coalBlend.dto.UpdateCoalBlendDto;
import cn.lihongjie.coal.coalBlend.entity.CoalBlendEntity;
import cn.lihongjie.coal.coalBlend.mapper.CoalBlendMapper;
import cn.lihongjie.coal.coalBlend.repository.CoalBlendRepository;
import cn.lihongjie.coal.coalWashingDailyAnalysis.mapper.RoundMapper;

import jakarta.annotation.PostConstruct;

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;
import org.springframework.util.StopWatch;

@Service
@Slf4j
@Transactional
public
class CoalBlendService extends BaseService<CoalBlendEntity, CoalBlendRepository> {

    @Autowired CoalBlendRepository repository;

    @Autowired CoalBlendMapper mapper;
    @Autowired ConversionService conversionService;
    @Autowired RoundMapper roundMapper;

    @PostConstruct
    public void init() {}

    public CoalBlendDto create(CreateCoalBlendDto request) {
        request.getCoals().forEach(x -> x.setId(null));

        CoalBlendEntity entity = mapper.toEntity(request);

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

    public CoalBlendDto update(UpdateCoalBlendDto request) {
        CoalBlendEntity 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 CoalBlendDto getById(String id) {

        CoalBlendEntity entity = repository.get(id);

        return mapper.toDto(entity);
    }

    public Page<CoalBlendDto> list(CommonQuery query) {

        Page<CoalBlendEntity> page =
                repository.findAll(
                        query.specification(conversionService),
                        PageRequest.of(
                                query.getPageNo(),
                                query.getPageSize(),
                                Sort.by(query.getOrders())));

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

    public CoalBlendDto blend(CreateCoalBlendDto dto) {

        StopWatch stopWatch = new StopWatch();

        stopWatch.start("toEntity");
        CoalBlendEntity entity = this.mapper.toEntity(dto);
        stopWatch.stop();
        stopWatch.start("blend");
        entity.blend();
        stopWatch.stop();
        stopWatch.start("toDto");
        CoalBlendDto blendDto = this.mapper.toDto(entity);

        stopWatch.stop();

        log.info(stopWatch.prettyPrint());
        return roundMapper.round(blendDto);
    }
}
