package cn.lihongjie.coal.excelTemplate.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.excelTemplate.dto.CreateExcelTemplateDto;
import cn.lihongjie.coal.excelTemplate.dto.ExcelTemplateDto;
import cn.lihongjie.coal.excelTemplate.dto.UpdateExcelTemplateDto;
import cn.lihongjie.coal.excelTemplate.entity.ExcelTemplateEntity;
import cn.lihongjie.coal.excelTemplate.mapper.ExcelTemplateMapper;
import cn.lihongjie.coal.excelTemplate.repository.ExcelTemplateRepository;

import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;

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.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
@Transactional
public class ExcelTemplateService
        extends BaseService<ExcelTemplateEntity, ExcelTemplateRepository> {
    @Autowired private ExcelTemplateRepository repository;

    @Autowired private ExcelTemplateMapper mapper;

    @Autowired private ConversionService conversionService;

    public ExcelTemplateDto create(CreateExcelTemplateDto request) {
        ExcelTemplateEntity entity = mapper.toEntity(request);

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

    public ExcelTemplateDto update(UpdateExcelTemplateDto request) {
        ExcelTemplateEntity 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 ExcelTemplateDto getById(String id) {
        ExcelTemplateEntity entity = repository.get(id);

        return mapper.toDto(entity);
    }

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

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

    public ExcelTemplateEntity getByCode(String templateCode) {

        return repository.findOne(new Specification<ExcelTemplateEntity>() {
            @Override
            public Predicate toPredicate(Root<ExcelTemplateEntity> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                return criteriaBuilder.equal(root.get("code"), templateCode);
            }
        }).orElse(null);
    }
}
