package cn.lihongjie.coal.weightDeviceSupplier.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.dbFunctions.DbFunctionService;
import cn.lihongjie.coal.exception.BizException;
import cn.lihongjie.coal.weightDeviceSupplier.dto.CreateWeightDeviceSupplierDto;
import cn.lihongjie.coal.weightDeviceSupplier.dto.UpdateWeightDeviceSupplierDto;
import cn.lihongjie.coal.weightDeviceSupplier.dto.WeightDeviceSupplierDto;
import cn.lihongjie.coal.weightDeviceSupplier.entity.WeightDeviceSupplierEntity;
import cn.lihongjie.coal.weightDeviceSupplier.mapper.WeightDeviceSupplierMapper;
import cn.lihongjie.coal.weightDeviceSupplier.repository.WeightDeviceSupplierRepository;

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 WeightDeviceSupplierService
        extends BaseService<WeightDeviceSupplierEntity, WeightDeviceSupplierRepository> {
    @Autowired private WeightDeviceSupplierRepository repository;

    @Autowired private WeightDeviceSupplierMapper mapper;

    @Autowired private ConversionService conversionService;

    @Autowired private DbFunctionService dbFunctionService;

    public WeightDeviceSupplierDto create(CreateWeightDeviceSupplierDto request) {
        WeightDeviceSupplierEntity entity = mapper.toEntity(request);

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

    public WeightDeviceSupplierDto update(UpdateWeightDeviceSupplierDto request) {
        WeightDeviceSupplierEntity entity = this.repository.get(request.getId());
        this.mapper.updateEntity(entity, request);

        this.repository.save(entity);

        return getById(entity.getId());
    }

    public void delete(IdRequest request) {
        if (this.repository.isLinked(request.getIds())) {
            throw new BizException("部分数据已被关联，无法删除");
        }

        this.repository.deleteAllById(request.getIds());
    }

    public WeightDeviceSupplierDto getById(String id) {
        WeightDeviceSupplierEntity entity = repository.get(id);

        return mapper.toDto(entity);
    }

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

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