package cn.lihongjie.coal.deviceCategory.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.JpaUtils;
import cn.lihongjie.coal.common.ReflectUtils;
import cn.lihongjie.coal.common.TreeUtils;
import cn.lihongjie.coal.dbFunctions.DbFunctionService;
import cn.lihongjie.coal.deviceCategory.dto.CreateDeviceCategoryDto;
import cn.lihongjie.coal.deviceCategory.dto.DeviceCategoryDto;
import cn.lihongjie.coal.deviceCategory.dto.DeviceCategoryTreeDto;
import cn.lihongjie.coal.deviceCategory.dto.UpdateDeviceCategoryDto;
import cn.lihongjie.coal.deviceCategory.entity.DeviceCategoryEntity;
import cn.lihongjie.coal.deviceCategory.mapper.DeviceCategoryMapper;
import cn.lihongjie.coal.deviceCategory.repository.DeviceCategoryRepository;
import cn.lihongjie.coal.exception.BizException;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.collections4.CollectionUtils;
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.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

@Service
@Slf4j
@Transactional
public class DeviceCategoryService
        extends BaseService<DeviceCategoryEntity, DeviceCategoryRepository> {
    @Autowired private DeviceCategoryRepository repository;

    @Autowired private DeviceCategoryMapper mapper;

    @Autowired private ConversionService conversionService;

    @Autowired private DbFunctionService dbFunctionService;

    public DeviceCategoryDto create(CreateDeviceCategoryDto request) {
        DeviceCategoryEntity entity = mapper.toEntity(request);

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

    public DeviceCategoryDto update(UpdateDeviceCategoryDto request) {
        DeviceCategoryEntity entity = this.repository.get(request.getId());
        this.mapper.updateEntity(entity, request);

        this.repository.save(entity);

        return getById(entity.getId());
    }

    public void delete(IdRequest request) {

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

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

    public DeviceCategoryDto getById(String id) {
        DeviceCategoryEntity entity = repository.get(id);

        return mapper.toDto(entity);
    }

    public Page<DeviceCategoryDto> list(CommonQuery query) {
        Specification specification = query.specification(conversionService);

//        if (!BooleanUtils.isTrue(Ctx.currentUser().getOrgAdmin())) {
//
//            //
//            specification =
//                    specification.and(
//                            (root, query1, cb) -> {
//                                Join users = root.join("users", JoinType.LEFT);
//                                return cb.or(
//                                        cb.isNull(users.get("id")),
//                                        cb.equal(users.get("id"), Ctx.currentUser().getId()));
//                            });
//        }

        Page<DeviceCategoryEntity> page =
                repository.findAll(
                        specification,
                        PageRequest.of(
                                query.getPageNo(),
                                query.getPageSize(),
                                Sort.by(query.getOrders())));

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

    public List<DeviceCategoryTreeDto> getRoots(CommonQuery request) {

        if (CollectionUtils.isEmpty(request.getItems())) {
            List<DeviceCategoryEntity> roots =
                    this.repository.findAll(
                            (root, query, criteriaBuilder) ->
                                    criteriaBuilder.isNull(root.get("parent")));
            List<DeviceCategoryTreeDto> collect =
                    roots.stream()
                            .map(de -> this.mapper.toTreeDto(de))
                            .collect(Collectors.toList());

            return addDeviceIds(collect);
        } else {
            Page<DeviceCategoryEntity> page =
                    repository.findAll(
                            request.specification(conversionService),
                            PageRequest.of(
                                    request.getPageNo(),
                                    request.getPageSize(),
                                    Sort.by(request.getOrders())));

            List<String> selfAndParentIds =
                    this.dbFunctionService.selfAndParentIds(
                            dbFunctionService.entityToTableName(DeviceCategoryEntity.class),
                            page.stream().map(x -> x.getId()).collect(Collectors.toList()),
                            true);

            List<DeviceCategoryTreeDto> selfAndParent =
                    addDeviceIds(
                            this.findAllByIds(selfAndParentIds).stream()
                                    .map(x -> (this.mapper.toTreeDtoExcludeChildren(x)))
                                    .collect(Collectors.toList()));

            return (StreamSupport.stream(
                            TreeUtils.buildTreeFromList(
                                            selfAndParent,
                                            DeviceCategoryTreeDto::getId,
                                            x -> x.getParent(),
                                            (x, y) -> {
                                                if (x.getChildren() == null) {
                                                    x.setChildren(new ArrayList<>());
                                                }
                                                x.getChildren().add(y);
                                                return null;
                                            })
                                    .spliterator(),
                            false)
                    .collect(Collectors.toList()));
        }
    }

    private List<DeviceCategoryTreeDto> addDeviceIds(List<DeviceCategoryTreeDto> collect) {

        var dtos = TreeUtils.treeToList(collect);

        var maps = repository.deviceIds(ReflectUtils.idList(dtos));

        JpaUtils.mergeMapToPojo(dtos, maps, conversionService);
        return collect;
    }

    public List<DeviceCategoryTreeDto> getTreeByIds(IdRequest request) {
        if (request.getIds().isEmpty()) {
            return new ArrayList<>();
        }
        var roots =
                this.repository.findAll(
                        (root, query, criteriaBuilder) -> root.get("id").in(request.getIds()));
        return roots.stream().map(de -> this.mapper.toTreeDto(de)).collect(Collectors.toList());
    }
}
