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

import com.inkasso.adapter.exception.enums.ExceptionEnum;
import com.inkasso.adapter.service.KafubaoServiceImpl;
import com.inkasso.adapter.util.BeanUtilsProxy;
import com.inkasso.factoring.system.role.dao.RoleDAO;
import com.inkasso.factoring.system.role.entity.RoleDO;
import com.inkasso.factoring.system.role.model.dto.RoleDTO;
import com.inkasso.factoring.system.role.model.request.AddRoleRequest;
import com.inkasso.factoring.system.role.model.request.EditRoleRequest;
import com.inkasso.factoring.system.role.service.RoleService;
import com.inkasso.adapter.exception.ServiceException;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author 张超
 * @date 2023-08-15
 */
@Service
public class RoleServiceImpl extends KafubaoServiceImpl<RoleDAO, RoleDO,RoleDTO> implements RoleService {


    @Override
    public void addRole(AddRoleRequest addRoleRequest) {
        RoleDO role = BeanUtilsProxy.copyPropertiesIgnoreNull(addRoleRequest, RoleDO.class);
        this.baseMapper.insert(role);
    }

    @Override
    public void editRole(EditRoleRequest editRoleRequest) {

        RoleDO role = super.getById(editRoleRequest.getId());
        if (role == null) {
            throw new ServiceException(400, ExceptionEnum.SYSTEM_DATE_ERROR);
        }
        BeanUtilsProxy.copyPropertiesNotIgnoreNull(editRoleRequest, role);
        this.updateById(role);
    }

    @Override
    public RoleDTO getRoleDtoById(Long id) {
        RoleDO role = super.getById(id);
        RoleDTO roleDto = BeanUtilsProxy.copyProperties(role, RoleDTO.class);
        return roleDto;
    }

    @Override
    public void deleteRole(Long id) {

        this.removeById(id);
    }

    @Override
    public List<RoleDTO> getByUserIdAndTenantId(Long userId, Long tenantId) {

        List<RoleDO> roles = this.baseMapper.getByUserIdAndTenantId(userId, tenantId);
        if(!CollectionUtils.isEmpty(roles)){
            return BeanUtilsProxy.copyPropertiesList(roles,RoleDTO.class);
        }
        return null;
    }
}

