package com.inkasso.factoring.system.resource.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.inkasso.adapter.tree.TreeUtils;
import com.inkasso.adapter.util.BeanUtilsProxy;
import com.inkasso.factoring.system.resource.dao.ResourceDAO;
import com.inkasso.factoring.system.resource.entity.ResourceDO;
import com.inkasso.factoring.system.resource.model.dto.ResourceDTO;
import com.inkasso.factoring.system.resource.model.dto.ResourceTreeDTO;
import com.inkasso.factoring.system.resource.model.request.AddResourceRequest;
import com.inkasso.factoring.system.resource.model.request.EditResourceRequest;
import com.inkasso.factoring.system.resource.service.ResourceService;
import com.inkasso.adapter.exception.ServiceException;
import com.inkasso.adapter.exception.enums.ExceptionEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 张超
 * @date 2023-08-19
 */
@Service
public class ResourceServiceImpl extends ServiceImpl<ResourceDAO, ResourceDO> implements ResourceService {

    @Autowired
    private TreeUtils treeUtils;

    @Override
    public void addResource(AddResourceRequest addResourceRequest) {
        ResourceDO resource = BeanUtilsProxy.copyPropertiesIgnoreNull(addResourceRequest, ResourceDO.class);
        this.baseMapper.insert(resource);
    }

    @Override
    public void editResource(EditResourceRequest editResourceRequest) {

        ResourceDO resource = super.getById(editResourceRequest.getId());
        if (resource == null) {
            throw new ServiceException(400, ExceptionEnum.SYSTEM_DATE_ERROR);
        }
        BeanUtilsProxy.copyPropertiesNotIgnoreNull(editResourceRequest, resource);
        this.updateById(resource);
    }

    @Override
    public ResourceDTO getById(Long id) {
        ResourceDO resource = super.getById(id);
        ResourceDTO resourceDto = BeanUtilsProxy.copyProperties(resource, ResourceDTO.class);
        return resourceDto;
    }

    @Override
    public void deleteResource(Long id) {

        this.removeById(id);
    }


    @Override
    public List<ResourceDTO> getResourceById(Long userId, Long tenantId) {
        if (tenantId == null || userId == null) {
            return null;
        }
        List<ResourceDTO> resource = this.baseMapper.getResourceById(userId, tenantId);
        return resource;
    }

    /**
     * 查询 资源 树结构
     *
     * @param tenantId
     * @return
     */
    @Override
    public List<ResourceTreeDTO> getResourceTree(Long tenantId) {
        LambdaQueryWrapper<ResourceDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ResourceDO::getTenantId, tenantId).or().isNull(ResourceDO::getTenantId);
        List<ResourceDO> resourceDOS = this.baseMapper.selectList(queryWrapper);
        List<ResourceTreeDTO> resourceTreeDTOS = BeanUtilsProxy.copyPropertiesList(resourceDOS, ResourceTreeDTO.class);
        List<ResourceTreeDTO> list = treeUtils.buidTree(resourceTreeDTOS, 0L);
        return list;
    }

    /**
     *
     * @param roleId
     * @return
     */
    @Override
    public List<ResourceDTO> getResourceByRoleId(Long roleId) {

        return this.baseMapper.getResourceByRoleId(roleId);
    }


}

