package thyyxxk.webserver.dao.his;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import thyyxxk.webserver.entity.dictionary.CodeName;
import thyyxxk.webserver.entity.login.UserInfo;
import thyyxxk.webserver.entity.login.VueMenu;

import java.util.List;

@Mapper
public interface LoginDao {
    @Select("select a.code, rtrim(b.name) name, a.password, a.code_rs, " +
            "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 a.del_flag=0")
    UserInfo findUserByCodeRsFromDjUserBase(@Param("codeRs") String codeRs);

    @Select("select code, name, code_rs, password='123456',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}, '123456', 0)")
    void insertNewUserToDjUserBase(UserInfo user);

    @Select("select code, name, password, code_rs,yb_code = (select yb_code from a_employee_mi where code=#{code}), " +
            "deptCode=(select dept_code from a_employee_mi where code=#{code}) " +
            "from dj_user_base where code=#{code} and del_flag=0")
    UserInfo findUserByCode(@Param("code") String code);

    @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 as completeRoute from t_intergration_platform_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<VueMenu> selectVueMenusByRoles(@Param("roles") List<Integer> roles);

    @Select("<script>" +
            "select path as completeRoute, meta_title,hide_tabs,hide_menu from " +
            "t_intergration_platform_menu where type=2 and 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<VueMenu> selectRoutesByRoles(@Param("roles") List<Integer> roles);

    @Select("<script>" +
            "select path from t_intergration_platform_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 password " +
            "from dj_user_base a, " +
            "     a_employee_mi b " +
            "where a.code = b.code " +
            "  and isnull(nullif(b.del_flag, ''), 0) = 0 " +
            "  and b.code = #{code}")
    String getPasswordByCode(String code);
}
