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

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.inkasso.adapter.util.BeanUtilsProxy;
import com.inkasso.auth.domain.PermissionDO;
import com.inkasso.factoring.system.permission.dao.PermissionDAO;
import com.inkasso.factoring.system.permission.model.dto.PermissionDTO;
import com.inkasso.factoring.system.permission.model.request.AddPermissionRequest;
import com.inkasso.factoring.system.permission.model.request.EditPermissionRequest;
import com.inkasso.factoring.system.permission.service.PermissionService;
import com.inkasso.adapter.exception.ServiceException;
import com.inkasso.adapter.exception.enums.ExceptionEnum;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author 张超
 * @date 2023-08-31
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionDAO, PermissionDO> implements PermissionService {


    @Override
    public void addPermission(AddPermissionRequest addPermissionRequest) {
        PermissionDO permission = BeanUtilsProxy.copyPropertiesIgnoreNull(addPermissionRequest, PermissionDO.class);
        this.baseMapper.insert(permission);
    }

    @Override
    public void editPermission(EditPermissionRequest editPermissionRequest) {

        PermissionDO permission = super.getById(editPermissionRequest.getId());
        if (permission == null) {
            throw new ServiceException(400, ExceptionEnum.SYSTEM_DATE_ERROR);
        }
        BeanUtilsProxy.copyPropertiesNotIgnoreNull(editPermissionRequest, permission);
        this.updateById(permission);
    }

    @Override
    public PermissionDTO getPermissionDtoById(Long id) {
        PermissionDO permission = super.getById(id);
        PermissionDTO permissionDto = BeanUtilsProxy.copyProperties(permission, PermissionDTO.class);
        return permissionDto;
    }

    @Override
    public void deletePermission(Long id) {

        this.removeById(id);
    }


    @Override
    public boolean checkPermission(String permissionCode, Long userId) {

        return false;
    }

    @Override
    public List<PermissionDTO> getByResourceIds(List<Long> resourceIds) {
        if(CollectionUtils.isEmpty(resourceIds)){
            return null;
        }
        return this.baseMapper.getByResourceIds(resourceIds);
    }
}

