package thyyxxk.webserver.dao.his;

import org.apache.ibatis.annotations.*;
import thyyxxk.webserver.entity.dictionary.CodeName;
import thyyxxk.webserver.entity.login.UserInfo;

import java.util.Date;
import java.util.List;

@Mapper
public interface LoginDao {

    @Select("select a.code, rtrim(b.name) name, a.password, a.code_rs,user_config, " +
            "b.dept_code as dept_code," +
            "rtrim(b.yb_code) as yb_code," +
            "emp_tit_code " +
            "from dj_user_base a " +
            "         left join a_employee_mi b on (a.code = b.code)  " +
            "where a.code_rs=#{codeRs} and b.del_flag=0")
    UserInfo findUserByCodeRsFromDjUserBase(@Param("codeRs") String codeRs);

    @Select("select code, name, code_rs, password='e10adc3949ba59abbe56e057f20f883e',emp_tit_code " +
            "from a_employee_mi where code_rs=#{codeRs} and isnull(del_flag,0)!=1")
    UserInfo findUserByCodeRsFromEmployeeMi(@Param("codeRs") String codeRs);

    @Insert("insert into dj_user_base (code, code_rs, name, password, del_flag) " +
            "values (#{code}, #{codeRs}, #{name}, 'e10adc3949ba59abbe56e057f20f883e', 0)")
    void insertNewUserToDjUserBase(UserInfo user);

    @Select("select role_id from dj_user_role where user_code=#{code}")
    List<Integer> getUserRoles(@Param("code") String code);

    @Select("select rtrim(name) " +
            "from zd_unit_code " +
            "where code = #{deptCode} ")
    String getDeptName(@Param("deptCode") String deptCode);

    @Select("select hz_day  from zd_unit_code where code = #{deptCode}")
    Integer getDeptHzDayByCode(@Param("deptCode") String deptCode);

    @Select("SELECT DISTINCT " +
            "name=(SELECT name FROM zd_unit_code WHERE code=a.dept), " +
            "code=a.dept FROM zd_dept_all a, a_employee_mi b " +
            "WHERE b.code= #{code} " +
            "AND (b.dept_code=a.small_dept or b.dept_code=a.dept)")
    List<CodeName> getUserWards(@Param("code") String code);

    @Select("select rtrim(code) code, rtrim(name) name, rtrim(py_code) pyCode " +
            "from zd_unit_code where code like '8%' order by code")
    List<CodeName> getAllWards();

    @Select("<script>" +
            "select path from t_intergration_menu where id in " +
            "(select menu_id from t_intergration_platform_role_menu where role_id in " +
            "<foreach collection='roles' item='role' separator=',' open='(' close='))'>" +
            "#{role}" +
            "</foreach> order by sort" +
            "</script>")
    List<String> selectVueMenusPathByRoles(@Param("roles") List<Integer> roles);

    @Select("select top 1 a.code, a.code_rs," +
            "             b.password " +
            "from a_employee_mi a, " +
            "     dj_user_base b " +
            "where a.code = #{code} " +
            "  and a.code = b.code " +
            "  and isnull(a.del_flag, '0') = '0'")
    UserInfo getUserInfoByCode(String code);

    @Update("update dj_user_base set " +
            "   verification_code = #{verificationCode}, " +
            "   verification_time = #{verificationTime} " +
            "       where code_rs = #{codeRs}")
    void updateVerificationCodeByCodeRs(String codeRs, String verificationCode, Date verificationTime);


    @Select("select count(1) from dj_user_base " +
            "where code_rs = #{codeRs} " +
            "   and verification_time >= getdate() " +
            "   and verification_code = #{verificationCode} ")
    int checkVerificationCode(String codeRs, String verificationCode);

    @Update("update dj_user_base set password = 'e10adc3949ba59abbe56e057f20f883e' ,verification_code = null , verification_time = null" +
            " where code_rs = #{codeRs}")
    int updatePasswordByCodeRs(String codeRs);

    @Update("update dj_user_base set password = #{pwd} ,verification_code = null , verification_time = null" +
            " where code_rs = #{codeRs}")
    int updatePasswordByCodeRsV2(String codeRs, String pwd);
}
