package cn.lihongjie.coal.deviceSupplier.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.deviceSupplier.dto.CreateDeviceSupplierDto;
import cn.lihongjie.coal.deviceSupplier.dto.DeviceSupplierDto;
import cn.lihongjie.coal.deviceSupplier.dto.UpdateDeviceSupplierDto;
import cn.lihongjie.coal.deviceSupplier.entity.DeviceSupplierEntity;
import cn.lihongjie.coal.deviceSupplier.mapper.DeviceSupplierMapper;
import cn.lihongjie.coal.deviceSupplier.repository.DeviceSupplierRepository;
import cn.lihongjie.coal.exception.BizException;

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 DeviceSupplierService
        extends BaseService<DeviceSupplierEntity, DeviceSupplierRepository> {
    @Autowired private DeviceSupplierRepository repository;

    @Autowired private DeviceSupplierMapper mapper;

    @Autowired private ConversionService conversionService;

    public DeviceSupplierDto create(CreateDeviceSupplierDto request) {
        DeviceSupplierEntity entity = mapper.toEntity(request);

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

    public DeviceSupplierDto update(UpdateDeviceSupplierDto request) {
        DeviceSupplierEntity entity = this.repository.get(request.getId());
        if (this.repository.containArchived(request.getId())){
            throw new BizException("部分数据已归档，无法编辑或删除");
        }
        this.mapper.updateEntity(entity, request);

        this.repository.save(entity);

        return getById(entity.getId());
    }

    public void delete(IdRequest request) {
        if (this.repository.containArchived(request)){
            throw new BizException("部分数据已归档，无法编辑或删除");
        }

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

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

    public DeviceSupplierDto getById(String id) {
        DeviceSupplierEntity entity = repository.get(id);

        return mapper.toDto(entity);
    }

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

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

    public void archive(IdRequest dto) {
        this.repository.archive(dto);
    }

    public void unarchive(IdRequest dto) {
        this.repository.unArchive(dto);
    }
}
