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 isnull(b.del_flag,0)!=1")
    UserInfo findUserByCodeRsFromDjUserBase(@Param("codeRs") String codeRs);

    @Select("select code, name, code_rs, password='706cd5d552991d02d80356a30c9cf23d',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}, '706cd5d552991d02d80356a30c9cf23d', 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 distinct code,name from " +
            "(select name=c.name,code=a.dept " +
            "from zd_dept_all a, a_employee_mi b, zd_unit_code c " +
            "where b.code=#{code} and c.code=a.dept " +
            "and (b.dept_code=a.small_dept or b.dept_code=a.dept) " +
            "union " +
            "select name=c.name,code=a.dept " +
            "from zd_dept_all a, zy_part_time_dept b,zd_unit_code c " +
            "where b.code=#{code} and c.code=a.dept " +
            "and (b.dept_code=a.small_dept or b.dept_code=a.dept)) t " +
            "order by code")
    List<CodeName> getUserWards(@Param("code") String code);

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

    @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 = '706cd5d552991d02d80356a30c9cf23d' ,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);
}
