package cn.lihongjie.coal.weightDeviceDataSelect.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.weightDeviceDataSelect.dto.CreateWeightDeviceDataSelectDto;
import cn.lihongjie.coal.weightDeviceDataSelect.dto.UpdateWeightDeviceDataSelectDto;
import cn.lihongjie.coal.weightDeviceDataSelect.dto.WeightDeviceDataSelectDto;
import cn.lihongjie.coal.weightDeviceDataSelect.entity.WeightDeviceDataSelectEntity;
import cn.lihongjie.coal.weightDeviceDataSelect.mapper.WeightDeviceDataSelectMapper;
import cn.lihongjie.coal.weightDeviceDataSelect.repository.WeightDeviceDataSelectRepository;

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 WeightDeviceDataSelectService
        extends BaseService<WeightDeviceDataSelectEntity, WeightDeviceDataSelectRepository> {
    @Autowired private WeightDeviceDataSelectRepository repository;

    @Autowired private WeightDeviceDataSelectMapper mapper;

    @Autowired private ConversionService conversionService;

    @Autowired private DbFunctionService dbFunctionService;

    public WeightDeviceDataSelectDto create(CreateWeightDeviceDataSelectDto request) {
        WeightDeviceDataSelectEntity entity = mapper.toEntity(request);

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

    public WeightDeviceDataSelectDto update(UpdateWeightDeviceDataSelectDto request) {
        WeightDeviceDataSelectEntity entity = this.repository.get(request.getId());
        this.mapper.updateEntity(entity, request);

        this.repository.save(entity);

        return getById(entity.getId());
    }

    public void delete(IdRequest request) {
        boolean linked = this.repository.isLinked(request.getIds());

        if (linked) {
            throw new BizException("数据已被关联，无法删除");
        }

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

    public WeightDeviceDataSelectDto getById(String id) {
        WeightDeviceDataSelectEntity entity = repository.get(id);

        return mapper.toDto(entity);
    }

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

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