package cn.lihongjie.coal.website.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.website.dto.CreateWebsiteDto;
import cn.lihongjie.coal.website.dto.UpdateWebsiteDto;
import cn.lihongjie.coal.website.dto.WebsiteDto;
import cn.lihongjie.coal.website.entity.WebsiteEntity;
import cn.lihongjie.coal.website.mapper.WebsiteMapper;
import cn.lihongjie.coal.website.repository.WebsiteRepository;

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 WebsiteService extends BaseService<WebsiteEntity, WebsiteRepository> {
    @Autowired private WebsiteRepository repository;

    @Autowired private WebsiteMapper mapper;

    @Autowired private ConversionService conversionService;

    @Autowired private DbFunctionService dbFunctionService;

    public WebsiteDto create(CreateWebsiteDto request) {
        WebsiteEntity entity = mapper.toEntity(request);

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

    public WebsiteDto update(UpdateWebsiteDto request) {
        WebsiteEntity 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 WebsiteDto getById(String id) {
        WebsiteEntity entity = repository.get(id);

        return mapper.toDto(entity);
    }

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