package cn.hnthyy.thmz.mapper.thmz;


import cn.hnthyy.thmz.entity.thmz.User;
import cn.hnthyy.thmz.vo.UserVo;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 门诊系统用户体系
 */
public interface UserMapper {
    /**
     * 查询所有的用户
     *
     * @return
     */
    @Select("select id,user_id_code,user_code,user_name,password,id_card,create_time,create_user,update_time,update_user,status,profile_image,email,phone_number,gender,lock_screen_password from t_user ")
    List<User> selectAllUser();

    /**
     * 分页查询用户
     *
     * @return
     */
    @Select({"<script>",
            "select id,user_id_code,user_code,user_name,password,id_card,create_time,create_user,update_time,update_user," +
                    "status,profile_image,email,phone_number,gender,lock_screen_password FROM t_user WHERE 1 = 1 " +
                    "<when test='id != null'>" +
                    " and id = #{id}" +
                    "</when>" +
                    "<when test='userIdCode != null'>" +
                    " and user_id_code = #{userIdCode}" +
                    "</when>" +
                    "<when test='userCode != null'>" +
                    " and user_code = #{userCode}" +
                    "</when>"+
                    "<when test='userName != null'>" +
                    " and user_name like '%${userName}%'" +
                    "</when>" +
                    " ORDER BY ID desc limit #{offset},#{pageSize} ",
            "</script>"})
    List<User> selectUserPage(UserVo userVo);

    /**
     * 用户统计
     *
     * @param userVo 用户查询条件
     * @return
     */
    @Select({"<script>",
            "select count(1) FROM t_user WHERE 1 = 1 " +
                    "<when test='id != null'>" +
                    " and id = #{id}" +
                    "</when>" +
                    "<when test='userIdCode != null'>" +
                    " and user_id_code = #{userIdCode}" +
                    "</when>" +
                    "<when test='userCode != null'>" +
                    " and user_code = #{userCode}" +
                    "</when>"+
                    "<when test='userName != null'>" +
                    " and user_name like '%${userName}%'" +
                    "</when>",
            "</script>"})
    int countUser(UserVo userVo);

    /**
     * 按照主键询员工对象
     *
     * @param id 主键
     * @return
     */
    @Select("select id,user_id_code,user_code,user_name,password,id_card,create_time,create_user,update_time,update_user,status,profile_image,email,phone_number,gender,lock_screen_password from t_user where id=#{id}")
    User selectUserById(@Param("id") Long id);

    /**
     * 按照工号查询员工对象
     *
     * @param userCode 工号
     * @return
     */
    @Select("select id,user_id_code,user_code,user_name,password,id_card,create_time,create_user,update_time,update_user,status,profile_image,email,phone_number,gender,lock_screen_password from t_user where user_code=#{userCode}")
    User selectUserByCode(@Param("userCode") String userCode);

    /**
     * 新增用户
     *
     * @param user
     * @return
     */
    @Insert("INSERT INTO t_user(user_id_code,user_code, user_name, password, id_card, create_time, create_user, update_time, update_user, status,profile_image,email,phone_number,gender,lock_screen_password) VALUES " +
            "(#{userIdCode,jdbcType=VARCHAR},#{userCode,jdbcType=VARCHAR}, #{userName,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},  #{idCard,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}," +
            " #{createUser,jdbcType=BIGINT},  #{updateTime,jdbcType=TIMESTAMP}, #{updateUser,jdbcType=BIGINT}, #{status,jdbcType=INTEGER},#{profileImage,jdbcType=BIGINT}" +
            ",#{email,jdbcType=VARCHAR},#{phoneNumber,jdbcType=VARCHAR},#{gender,jdbcType=CHAR},#{lockScreenPassword,jdbcType=VARCHAR})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int insertUser(User user);

    /**
     * 更新用户信息
     *
     * @param user
     * @return
     */
    @Update({"<script>",
            "update t_user",
            "<trim prefix='set' prefixOverrides=',' suffix=' where id = #{id} '>",
            "<when test='userIdCode!=null'>",
            "user_id_code =#{userIdCode,jdbcType=VARCHAR}",
            "</when>",
            "<when test='userCode!=null'>",
            ",user_code =#{userCode,jdbcType=VARCHAR}",
            "</when>",
            "<when test='userName!=null'>",
            ",user_name =#{userName,jdbcType=VARCHAR}",
            "</when>",
            "<when test='password!=null'>",
            ",password =#{password,jdbcType=VARCHAR}",
            "</when>",
            "<when test='idCard!=null'>",
            ",id_card =#{idCard,jdbcType=VARCHAR}",
            "</when>",
            "<when test='createTime!=null'>",
            ",create_time =#{createTime,jdbcType=TIMESTAMP}",
            "</when>",
            "<when test='createUser!=null'>",
            ",create_user =#{createUser,jdbcType=BIGINT}",
            "</when>",
            "<when test='updateTime!=null'>",
            ",update_time =#{updateTime,jdbcType=TIMESTAMP}",
            "</when>",
            "<when test='updateUser!=null'>",
            ",update_user =#{updateUser,jdbcType=BIGINT}",
            "</when>",
            "<when test='status!=null'>",
            ",status =#{status,jdbcType=INTEGER}",
            "</when>",
            "<when test='profileImage!=null'>",
            ",profile_image =#{profileImage,jdbcType=BIGINT}",
            "</when>",
            "<when test='email!=null'>",
            ",email =#{email,jdbcType=VARCHAR}",
            "</when>",
            "<when test='phoneNumber!=null'>",
            ",phone_number =#{phoneNumber,jdbcType=VARCHAR}",
            "</when>",
            "<when test='gender!=null'>",
            ",gender =#{gender,jdbcType=CHAR}",
            "</when>",
            "<when test='lockScreenPassword!=null'>",
            ",lock_screen_password =#{lockScreenPassword,jdbcType=VARCHAR}",
            "</when>",
            "</trim>",
            "</script>"})
    int updateUser(User user);


    /**
     * 按照员工编码查询员工对象
     *
     * @param userIdCode 员工编码
     * @return
     */
    @Select("select id,user_id_code,user_code,user_name,password,id_card,create_time,create_user,update_time,update_user,status,profile_image,email,phone_number,gender,lock_screen_password from t_user where user_id_code=#{userIdCode}")
    User selectUserByUserIdCode(@Param("userIdCode") String userIdCode);
}
