package com.lesingle.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lesingle.common.enums.ErrorCode;
import com.lesingle.common.exception.BusinessException;
import com.lesingle.common.result.PageResult;
import com.lesingle.modules.sys.dto.CreatePermissionDto;
import com.lesingle.modules.sys.entity.SysPermission;
import com.lesingle.modules.sys.mapper.SysPermissionMapper;
import com.lesingle.modules.sys.service.ISysPermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionMapper, SysPermission>
        implements ISysPermissionService {

    @Override
    public SysPermission createPermission(CreatePermissionDto dto, Long tenantId) {
        log.info("开始创建权限，编码：{}，租户：{}", dto.getCode(), tenantId);

        SysPermission perm = new SysPermission();
        perm.setTenantId(tenantId);
        perm.setName(dto.getName());
        perm.setCode(dto.getCode());
        perm.setResource(dto.getResource());
        perm.setAction(dto.getAction());
        perm.setDescription(dto.getDescription());
        save(perm);

        log.info("权限创建成功，ID：{}", perm.getId());
        return perm;
    }

    @Override
    public PageResult<SysPermission> findAll(Long page, Long pageSize, Long tenantId) {
        IPage<SysPermission> result = baseMapper.selectPage(
                new Page<>(page, pageSize),
                new LambdaQueryWrapper<SysPermission>()
                        .eq(tenantId != null, SysPermission::getTenantId, tenantId)
                        .eq(SysPermission::getValidState, 1)
                        .orderByDesc(SysPermission::getCreateTime)
        );
        return PageResult.from(result);
    }

    @Override
    public SysPermission findDetail(Long id, Long tenantId) {
        SysPermission perm = getById(id);
        if (perm == null) {
            throw BusinessException.of(ErrorCode.NOT_FOUND, "权限不存在");
        }
        return perm;
    }

    @Override
    public SysPermission updatePermission(Long id, CreatePermissionDto dto, Long tenantId) {
        log.info("开始更新权限，ID：{}", id);
        findDetail(id, tenantId);

        SysPermission perm = new SysPermission();
        perm.setId(id);
        if (dto.getName() != null) perm.setName(dto.getName());
        if (dto.getCode() != null) perm.setCode(dto.getCode());
        if (dto.getResource() != null) perm.setResource(dto.getResource());
        if (dto.getAction() != null) perm.setAction(dto.getAction());
        if (dto.getDescription() != null) perm.setDescription(dto.getDescription());
        updateById(perm);

        return getById(id);
    }

    @Override
    public void removePermission(Long id, Long tenantId) {
        log.info("删除权限，ID：{}", id);
        findDetail(id, tenantId);
        removeById(id);
    }
}
