package cn.qinys.platform.upms.api.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.qinys.platform.entity.upms.UpmsUser;
import cn.qinys.platform.upms.api.resp.UserPageResp;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;


/**
 * 用户表(UpmsUser)表数据库访问层
 *
 * @author makejava
 * @since 2024-02-26 11:17:59
 */
@Repository
@Mapper
public interface UpmsUserMapper extends BaseMapper<UpmsUser> {

    /**
     * 用户分页
     *
     * @param page
     * @param wrapper
     * @return
     */
    @Select("SELECT u.*, t.name AS tenantName FROM upms_user u LEFT JOIN upms_tenant t ON u.tenant_id = t.id ${ew.customSqlSegment}")
    Page<UserPageResp> selectUserPage(Page<UpmsUser> page, @Param(Constants.WRAPPER) Wrapper<UpmsUser> wrapper);

    /**
     * 通过用户名查找
     *
     * @param username
     * @return
     */
    @Select("SELECT * FROM upms_user WHERE username = #{username} LIMIT 1")
    UpmsUser selectByUsername(@Param("username") String username);

    /**
     * 更新锁定状态
     *
     * @param userId
     * @param locked
     */
    @Update("UPDATE upms_user SET locked = #{locked}, update_time = NOW() WHERE id = #{userId}")
    void updateLockStatus(@Param("userId") String userId, @Param("locked") String locked);

    /**
     * 删除
     *
     * @param tenantId
     */
    @Update("UPDATE upms_user SET deleted = 1, update_time = NOW() WHERE tenant_id = #{tenantId}")
    void deleteByTenantId(@Param("tenantId") String tenantId);

}

