package com.xly.erp.module.usr.mapper;

import com.xly.erp.module.usr.vo.StaffSearchVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface StaffMapper {

    @Select("SELECT 1 FROM tStaff WHERE iIncrement = #{id} AND bDeleted = 0 LIMIT 1")
    Integer findActiveStaffFlag(@Param("id") Integer id);

    default boolean existsActiveById(Integer id) {
        return findActiveStaffFlag(id) != null;
    }

    @Select("""
            <script>
            SELECT iIncrement, sStaffNo, sStaffName, sDepartment
              FROM tStaff
             WHERE bDeleted = 0
            <if test='keyword != null and keyword != ""'>
               AND (sStaffName LIKE CONCAT('%', #{keyword}, '%')
                 OR sStaffNo   LIKE CONCAT('%', #{keyword}, '%'))
            </if>
             ORDER BY sStaffName ASC
             LIMIT #{limit}
            </script>
            """)
    List<StaffSearchVO> searchActive(@Param("keyword") String keyword, @Param("limit") Integer limit);
}
