package cn.lihongjie.coal.empSalaryBatch.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.common.ArchiveUtils;
import cn.lihongjie.coal.empSalaryBatch.dto.CreateEmpSalaryBatchDto;
import cn.lihongjie.coal.empSalaryBatch.dto.EmpSalaryBatchDto;
import cn.lihongjie.coal.empSalaryBatch.dto.UpdateEmpSalaryBatchDto;
import cn.lihongjie.coal.empSalaryBatch.entity.EmpSalaryBatchEntity;
import cn.lihongjie.coal.empSalaryBatch.mapper.EmpSalaryBatchMapper;
import cn.lihongjie.coal.empSalaryBatch.repository.EmpSalaryBatchRepository;
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 EmpSalaryBatchService
        extends BaseService<EmpSalaryBatchEntity, EmpSalaryBatchRepository> {
    @Autowired private EmpSalaryBatchRepository repository;

    @Autowired private EmpSalaryBatchMapper mapper;

    @Autowired private ConversionService conversionService;

    public EmpSalaryBatchDto create(CreateEmpSalaryBatchDto request) {
        EmpSalaryBatchEntity entity = mapper.toEntity(request);

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

    public EmpSalaryBatchDto update(UpdateEmpSalaryBatchDto request) {
        EmpSalaryBatchEntity entity = this.repository.get(request.getId());
        ArchiveUtils.checkArchiveStatus(
                entity,
                EmpSalaryBatchEntity::getArchiveStatus,
                x -> "0",
                (e, actual, expected) -> {
                    throw new BizException("数据 " + "已归档，无法编辑");
                });
        this.mapper.updateEntity(entity, request);

        this.repository.save(entity);

        return getById(entity.getId());
    }

    public void delete(IdRequest request) {
        ArchiveUtils.checkArchiveStatus(
                this.repository::findAllById,
                request.getIds(),
                EmpSalaryBatchEntity::getArchiveStatus,
                x -> "0",
                (e, actual, expected) -> {
                    throw new BizException("数据 " + "已归档，无法删除");
                });
        this.repository.deleteAllById(request.getIds());
    }

    public EmpSalaryBatchDto getById(String id) {
        EmpSalaryBatchEntity entity = repository.get(id);

        return mapper.toDto(entity);
    }

    public Page<EmpSalaryBatchDto> list(CommonQuery query) {
        Page<EmpSalaryBatchEntity> 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.findAllById(dto.getIds()).stream()
                .peek(
                        x ->
                                ArchiveUtils.checkArchiveStatus(
                                        x,
                                        EmpSalaryBatchEntity::getArchiveStatus,
                                        y -> "0",
                                        (e, actual, expected) -> {
                                            throw new BizException("数据 " + "已归档，无法再次归档");
                                        }))
                .peek(x -> x.setArchiveStatus("1"))
                .forEach(this.repository::save);
    }

    public void unarchive(IdRequest dto) {
        this.repository.findAllById(dto.getIds()).stream()
                .peek(
                        x ->
                                ArchiveUtils.checkArchiveStatus(
                                        x,
                                        EmpSalaryBatchEntity::getArchiveStatus,
                                        y -> "1",
                                        (e, actual, expected) -> {
                                            throw new BizException("数据" + "未归档，无法取消归档");
                                        }))
                .peek(x -> x.setArchiveStatus("0"))
                .forEach(this.repository::save);
    }
}
