package shop.alien.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import shop.alien.entity.store.LifeSysRole;
import shop.alien.mapper.LifeSysRoleMapper;
import shop.alien.store.service.LifeSysRoleService;

import java.util.List;

/**
 * 平台角色信息表 服务实现类
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional
public class LifeSysRoleServiceImpl extends ServiceImpl<LifeSysRoleMapper, LifeSysRole> implements LifeSysRoleService {

    private final LifeSysRoleMapper lifeSysRoleMapper;

    @Override
    public IPage<LifeSysRole> getRolePage(int page, int size, String roleName, String description, String status) {
        LambdaQueryWrapper<LifeSysRole> queryWrapper = new LambdaQueryWrapper<>();
        
        // 角色名称模糊查询
        if (StringUtils.hasText(roleName)) {
            queryWrapper.like(LifeSysRole::getRoleName, roleName);
        }
        // 角色描述模糊查询
        if (StringUtils.hasText(description)) {
            queryWrapper.like(LifeSysRole::getDescription, description);
        }
        
        // 角色状态查询
        if (StringUtils.hasText(status)) {
            queryWrapper.eq(LifeSysRole::getStatus, status);
        }
        
        // 按显示顺序和创建时间排序
        queryWrapper.orderByAsc(LifeSysRole::getRoleSort);
        queryWrapper.orderByDesc(LifeSysRole::getCreatedTime);

        return lifeSysRoleMapper.selectPage(new Page<>(page, size), queryWrapper);
    }

    @Override
    public LifeSysRole getRoleById(Long roleId) {
        return lifeSysRoleMapper.selectById(roleId);
    }

    @Override
    public boolean saveRole(LifeSysRole role) {
        // 校验必填字段
        if (!StringUtils.hasText(role.getRoleName())) {
            log.error("角色名称不能为空");
            return false;
        }
        // 设置默认值
        if (role.getRoleSort() == null) {
            role.setRoleSort(0);
        }
        if (!StringUtils.hasText(role.getStatus())) {
            role.setStatus("0"); // 默认正常
        }
        if (!StringUtils.hasText(role.getDelFlag())) {
            role.setDelFlag("0"); // 默认未删除
        }
        int result = lifeSysRoleMapper.insert(role);
        return result > 0;
    }

    @Override
    public boolean updateRole(LifeSysRole role) {
        if (role.getRoleId() == null) {
            log.error("角色ID不能为空");
            return false;
        }

        int result = lifeSysRoleMapper.updateById(role);
        return result > 0;
    }

    @Override
    public boolean deleteRole(Long roleId) {
        // 逻辑删除
        LambdaUpdateWrapper<LifeSysRole> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(LifeSysRole::getRoleId, roleId);
        updateWrapper.set(LifeSysRole::getDelFlag, "2"); // 2代表删除
        
        int result = lifeSysRoleMapper.update(null, updateWrapper);
        return result > 0;
    }

    @Override
    public boolean updateStatus(Long roleId, String status) {
        if (roleId == null) {
            log.error("角色ID不能为空");
            return false;
        }
        if (!StringUtils.hasText(status)) {
            log.error("角色状态不能为空");
            return false;
        }
        
        LambdaUpdateWrapper<LifeSysRole> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(LifeSysRole::getRoleId, roleId);
        updateWrapper.set(LifeSysRole::getStatus, status);
        
        int result = lifeSysRoleMapper.update(null, updateWrapper);
        return result > 0;
    }

    @Override
    public List<LifeSysRole> getAllNormalRoles() {
        LambdaQueryWrapper<LifeSysRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LifeSysRole::getStatus, "0"); // 正常状态
        queryWrapper.eq(LifeSysRole::getDelFlag, "0"); // 未删除
        queryWrapper.orderByAsc(LifeSysRole::getRoleSort);
        
        return lifeSysRoleMapper.selectList(queryWrapper);
    }

    @Override
    public List<LifeSysRole> getRoleByUserId(Long userId) {
        QueryWrapper<LifeSysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("lsur.user_id", userId);
        return lifeSysRoleMapper.getRoleByUserId(queryWrapper);
    }
}

