package cn.lihongjie.coal.saleOrderReport.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.saleOrderReport.dto.CreateSaleOrderReportDto;
import cn.lihongjie.coal.saleOrderReport.dto.SaleOrderReportDto;
import cn.lihongjie.coal.saleOrderReport.dto.UpdateSaleOrderReportDto;
import cn.lihongjie.coal.saleOrderReport.entity.SaleOrderReportEntity;
import cn.lihongjie.coal.saleOrderReport.mapper.SaleOrderReportMapper;
import cn.lihongjie.coal.saleOrderReport.repository.SaleOrderReportRepository;

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 SaleOrderReportService
        extends BaseService<SaleOrderReportEntity, SaleOrderReportRepository> {
    @Autowired private SaleOrderReportRepository repository;

    @Autowired private SaleOrderReportMapper mapper;

    @Autowired private ConversionService conversionService;

    @Autowired private DbFunctionService dbFunctionService;

    public SaleOrderReportDto create(CreateSaleOrderReportDto request) {
        SaleOrderReportEntity entity = mapper.toEntity(request);

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

    public SaleOrderReportDto update(UpdateSaleOrderReportDto request) {
        SaleOrderReportEntity 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("部分数据已归档，无法编辑或删除");
        }
        boolean linked = this.repository.isLinked(request.getIds());

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

    public SaleOrderReportDto getById(String id) {
        SaleOrderReportEntity entity = repository.get(id);

        return mapper.toDto(entity);
    }

    public Page<SaleOrderReportDto> list(CommonQuery query) {
        Page<SaleOrderReportEntity> 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);
    }
}
