package com.fs.hospital.mapper;

import java.util.List;

import com.fs.hospital.domain.HospitalUser;
import com.fs.hospital.vo.HospitalUserListHVO;
import com.fs.hospital.vo.HospitalUserHVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * 员工信息Mapper接口
 *
 * @author fs
 * @date 2024-02-26
 */
public interface HospitalUserMapper
{
    /**
     * 查询员工信息
     *
     * @param userId 员工信息主键
     * @return 员工信息
     */
    public HospitalUser selectHospitalUserByUserId(Long userId);

    /**
     * 查询员工信息列表
     *
     * @param hospitalUser 员工信息
     * @return 员工信息集合
     */
    public List<HospitalUser> selectHospitalUserList(HospitalUser hospitalUser);

    /**
     * 新增员工信息
     *
     * @param hospitalUser 员工信息
     * @return 结果
     */
    public int insertHospitalUser(HospitalUser hospitalUser);

    /**
     * 修改员工信息
     *
     * @param hospitalUser 员工信息
     * @return 结果
     */
    public int updateHospitalUser(HospitalUser hospitalUser);

    /**
     * 删除员工信息
     *
     * @param userId 员工信息主键
     * @return 结果
     */
    public int deleteHospitalUserByUserId(Long userId);

    /**
     * 批量删除员工信息
     *
     * @param userIds 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteHospitalUserByUserIds(Long[] userIds);

    @Select("select count(1) from hospital_user where user_name=#{userName}")
    int checkUserName(String userName);
    @Select("select * from hospital_user WHERE user_name=#{username}")
    HospitalUser selectHospitalUserByUserName(String username);
    @Select("select count(1) from hospital_user  where user_name=#{userName} and is_del=0  ")
    int checkUserNameUnique(String userName);

    @Select("select u.* from hospital_user u where u.dept_id in ( SELECT dept_id FROM hospital_dept WHERE dept_id = #{maps.deptId} or find_in_set( #{maps.deptId} , ancestors ) ) ${maps.params.dataScope}")
    List<HospitalUser> getUserListByDeptId(@Param("maps") HospitalUser user);

    @Select("select count(1) from hospital_user  where hospital_id=#{hospitalId} and is_del=0  ")
    Integer selectHospitalUserCountByHospitalId(Long hospitalId);
    @Update("update hospital_user set password=#{password} where user_id=#{userId}")
    int resetUserPwdByUserId(@Param("userId")Long userId, @Param("password") String encryptPassword);

    @Select({"<script> " +
            "select u.user_id, u.dept_id, u.nick_name, u.user_name, u.email, u.avatar, u.mobile, u.password, u.sex, u.status, u.is_del, u.login_ip, u.login_date, u.create_by, u.create_time, u.remark, d.dept_name, d.leader from hospital_user u\n" +
            "        left join hospital_dept d on u.dept_id = d.dept_id\n" +
            "        where u.is_del = 0 and u.status=0\n" +
            "        <if test=\"hospitalId != null  \">\n" +
            "            AND u.hospital_id = #{hospitalId}\n" +
            "        </if>\n" +
            "        <if test=\"nickName != null and nickName != ''\">\n" +
            "            AND u.nick_name like concat( #{nickName}, '%')\n" +
            "        </if>\n" +
            "        <if test=\"mobile != null and mobile != ''\">\n" +
            "            AND u.mobile like concat( #{mobile}, '%')\n" +
            "        </if>\n" +
            "        <if test=\"beginTime != null and beginTime != ''\"><!-- 开始时间检索 -->\n" +
            "            AND date_format(u.create_time,'%y%m%d') &gt;= date_format(#{beginTime},'%y%m%d')\n" +
            "        </if>\n" +
            "        <if test=\"endTime != null and endTime != ''\"><!-- 结束时间检索 -->\n" +
            "            AND date_format(u.create_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')\n" +
            "        </if>\n" +
            "        <if test=\"deptId != null and deptId != 0\">\n" +
            "            AND (u.dept_id = #{deptId} OR u.dept_id IN ( SELECT t.dept_id FROM hospital_dept t WHERE find_in_set(#{deptId}, ancestors) ))\n" +
            "        </if>"+
            "</script>"})
    List<HospitalUserHVO> selectMyUserList(HospitalUser user);

    int resetUserPwd(@Param("userName")String userName,  @Param("password")String encryptPassword);

    int updateUserAvatar(@Param("userName")String username, @Param("avatar") String avatar);
    @Select({"<script> " +
            "select u.*,d.dept_name from hospital_user u LEFT JOIN hospital_dept d ON d.dept_id=u.dept_id"+
            " <where>\n" +
            "            <if test=\"deptId != null \"> and u.dept_id = #{deptId}</if>\n" +
            "            <if test=\"userName != null  and userName != ''\"> and u.user_name like concat(#{userName}, '%')</if>\n" +
            "            <if test=\"nickName != null  and nickName != ''\"> and u.nick_name like concat( #{nickName}, '%')</if>\n" +
            "            <if test=\"userType != null  and userType != ''\"> and u.user_type = #{userType}</if>\n" +
            "            <if test=\"email != null  and email != ''\"> and u.email = #{email}</if>\n" +
            "            <if test=\"mobile != null  and mobile != ''\"> and u.mobile = #{mobile}</if>\n" +
            "            <if test=\"sex != null  and sex != ''\"> and u.sex = #{sex}</if>\n" +
            "            <if test=\"avatar != null  and avatar != ''\"> and u.avatar = #{avatar}</if>\n" +
            "            <if test=\"password != null  and password != ''\"> and u.password = #{password}</if>\n" +
            "            <if test=\"status != null \"> and u.status = #{status}</if>\n" +
            "            <if test=\"isDel != null \"> and u.is_del = #{isDel}</if>\n" +
            "            <if test=\"isDoctor != null \"> and u.is_doctor = #{isDoctor}</if>\n" +
            "            <if test=\"loginIp != null  and loginIp != ''\"> and u.login_ip = #{loginIp}</if>\n" +
            "            <if test=\"loginDate != null \"> and u.login_date = #{loginDate}</if>\n" +
            "            <if test=\"hospitalId != null \"> and u.hospital_id = #{hospitalId}</if>\n" +
            "        <if test=\"beginTime != null and beginTime != ''\">\n" +
            "            AND date_format(u.create_time,'%y%m%d') &gt;= date_format(#{beginTime},'%y%m%d')\n" +
            "        </if>\n" +
            "        <if test=\"endTime != null and endTime != ''\">\n" +
            "            AND date_format(u.create_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')\n" +
            "        </if>\n" +
            " </where>"+
            "</script>"})
    List<HospitalUserListHVO> selectHospitalUserListVO(HospitalUser user);
    @Select("    select u.*,d.dept_name from hospital_user u LEFT JOIN hospital_dept d ON d.dept_id=u.dept_id where u.user_id = #{userId}")
    HospitalUser selectHospitalUserByUserIdVO(Long userId);
}
