-- 登录命名空间
#namespace("loginPerson")

  -- 获取指定身份的最大主键号
  #sql("getMaxPkByIdentityId")
    select ifnull(max(identity_pk_num),0) as identity_pk_num from t_sys_loginperson where identity_id=?
  #end
  -- 删除一个人员:通过单位ID
  #sql("deletePersonByBureauId")
    update t_sys_loginperson set b_use=0,operator=?,ip_address=? where bureau_id=? and identity_id=4
  #end

  -- 删除一个人员：通过人员编号
  #sql("deletePersonById")
    UPDATE t_sys_loginperson
    SET b_use = 0,status_code=?,operator=?,ip_address=?
    WHERE
      person_id = ?
  #end

  -- 根据学生ID获取家长ID
  #sql("getParentInfoByStudentId")
    select person_id from t_sys_loginperson where p_child_id=?
  #end

  -- 修改密码
  #sql("changePwd")
      update t_sys_loginperson set operator=?,ip_address=?,original_pwd=?,pwd=?,pwdmd5=? where person_id=?
  #end

  -- 获取指定人员所在的区域信息
  #sql("getLoginPersonAreaInfo")
      select city_id,area_id from t_sys_loginperson where person_id=?
  #end

  -- 根据person_id获取是否已绑定了手机号
  #sql("getIsBindPhoneNum")
     select ifnull(telephone, 0) as telephone from t_sys_loginperson where b_use = 1 and person_id = ?
  #end

  -- 根据person_id获取是否已绑定了邮箱
  #sql("getIsBindEmail")
    select ifnull(email, 0) as email from t_sys_loginperson where b_use = 1 and person_id = ?
  #end

  -- 根据person_id获取是否已绑定了QQ
  #sql("getIsBindQq")
    select ifnull(qq_openid, 0) as qq_openid from t_sys_loginperson where b_use = 1 and person_id = ?
  #end

  -- 根据person_id获取是否已绑定了微信
  #sql("getIsBindWx")
    select ifnull(wx_openid, 0) as wx_openid from t_sys_loginperson where b_use = 1 and person_id = ?
  #end

  -- 人员与手机号绑定
  #sql("bindPersonPhoneNum")
    update t_sys_loginperson set telephone= ?,operator=?,ip_address=? where person_id = ?
  #end

  -- 人员与邮箱绑定
  #sql("bindPersonEmail")
    update t_sys_loginperson set email= ?,operator=?,ip_address=? where person_id = ?
  #end

  -- 获取一个QQ的openId获了是否已存在
  #sql("getQqOpenIdIsExist")
    select person_id from t_sys_loginperson where b_use = 1 and qq_openid =?
  #end

  -- 绑定QQ
  #sql("bindPersonQq")
    update t_sys_loginperson set qq_openid= ?,operator=?,ip_address=? where person_id = ?
  #end

  -- 获取一个微信的openId获了是否已存在
  #sql("getWxOpenIdIsExist")
    select person_id from t_sys_loginperson where b_use = 1 and wx_openid =?
  #end

  -- 绑定微信
  #sql("bindPersonWx")
    update t_sys_loginperson set wx_openid= ?,operator=?,ip_address=? where person_id = ?
  #end

  -- 根据人员的ID获取此人员的登录信息
  #sql("getLoginInfoByPersonId")
    select person_id,person_name,identity_id,login_name,pwd,original_pwd,xb,zzmm,mz,idcard_code,birthday,create_time,sort_id,telephone,wx_openid,qq_openid,email,status_code,b_use,identity_pk_num,city_id,area_id,main_school_id,bureau_id,org_id,s_class_id,s_xjh,s_xjfh,s_source,
    t_duty_charge,t_xl_id,t_zc_id,t_stage_id,t_subject_id,date_format(t_teaching_date,'%Y-%m-%d') as t_teaching_date,p_child_id,
    update_ts from t_sys_loginperson where person_id =?
  #end

  -- 根据单位号获取下面正常教师的个数
  #sql("getTeacherCountByBureauId")
      select count(1) as c from t_sys_loginperson where bureau_id=? and b_use=1 and identity_id=5
  #end

  -- 根据部门获取下面正常教师的个数
  #sql("getTeacherCountByOrgId")
    select count(1) as c from t_sys_loginperson where org_id=? and b_use=1 and identity_id=5
  #end

  -- 根据单位号获取下面正常学生的个数
  #sql("getStudentCountByBureauId")
    select count(1) as c from t_sys_loginperson where bureau_id=? and b_use=1 and identity_id=6
  #end

  -- 根据单位号获取下面正常家长的个数
  #sql("getParentCountByBureauId")
    select count(1) as c from t_sys_loginperson where bureau_id=? and b_use=1 and identity_id=7
  #end

  -- 根据身份证号获取是否存在
  #sql("getIdCardIsExist")
    select person_id from t_sys_loginperson where b_use = 1 and idcard_code=?
  #end

  -- 根据手机号获取是否存在
  #sql("getTelephoneIsExist")
    select person_id from t_sys_loginperson where b_use = 1 and telephone=?
  #end

  -- 根据邮箱获取是否存在
  #sql("getEmailIsExist")
    select person_id from t_sys_loginperson where b_use = 1 and email=?
  #end

  -- 根据身份证号和人员ID获取是否存在
  #sql("getIdCardIsExistByPersonId")
    select person_id from t_sys_loginperson where b_use = 1 and idcard_code=? and person_id<>?
  #end

  -- 根据手机号和人员ID获取是否存在
  #sql("getTelephoneIsExistByPersonId")
    select person_id from t_sys_loginperson where b_use = 1 and telephone=? and person_id<>?
  #end

  -- 根据邮箱获和人员ID获取是否存在
  #sql("getEmailIsExistByPersonId")
    select person_id from t_sys_loginperson where b_use = 1 and email=? and person_id<>?
  #end


  -- 市管理员
  #sql("getPrintCityManager")
    select person_id,person_name,login_name,original_pwd,pwd from t_sys_loginperson where identity_id =2 order by sort_id
  #end

  -- 市直属单位
  #sql("getPrintCityBureau")
    select person_id,person_name,login_name,original_pwd,pwd from t_sys_loginperson where identity_id =4
        order by sort_id
  #end

  -- 县区管理员
  #sql("getPrintAreaManager")
    select person_id,person_name,login_name,original_pwd,pwd from t_sys_loginperson where identity_id =3 order by sort_id
  #end

  -- 指定县区的单位
  #sql("getPrintAreaBureau")
    select person_id,person_name,login_name,original_pwd,pwd from t_sys_loginperson where identity_id =4 and area_id=#para(0) order by sort_id
  #end
  -- 更改人员的所在部门
  #sql("changePersonBureau")
    update t_sys_loginperson set city_id=?,area_id=?,main_school_id=?,bureau_id=?,org_id=?,operator=?,ip_address=? where person_id=?
  #end

   -- 更改学生、家长的所在班级
  #sql("changePersonClass")
    update t_sys_loginperson set city_id=?,area_id=?,main_school_id=?,bureau_id=?,s_class_id=?,operator=?,ip_address=? where person_id=?
  #end

  -- 检查身份证号与数据库中的身份证号重复,教师
  #sql("checkRepeatidcardByTeacherImport")
      select t1.idcard_code from t_import_teacher_by_excel as t1 where t1.batch_id=?
      and t1.idcard_code_aes in (select idcard_code from t_sys_loginperson as t2 where t2.b_use=1)
  #end
  -- 检查身份证号与数据库中的身份证号重复,学生
  #sql("checkRepeatidcardByStudentImport")
      select t1.idcard_code from t_import_student_by_excel as t1 where t1.batch_id=?
      and t1.idcard_code_aes in (select idcard_code from t_sys_loginperson as t2 where t2.b_use=1)
  #end

  -- 使用单位ID填充部门ID
  #sql("fillOrgIdByBureauId")
    update t_import_teacher_by_excel set org_id=bureau_id where  batch_id=? and org_name=''
  #end

  -- 有哪些部门需要检查
  #sql("needCheckOrgName")
    select org_name from t_import_teacher_by_excel where batch_id=? and org_name!='' group by org_name
  #end

    -- 有哪些班级需要检查
  #sql("needCheckClassName")
    select class_name,stage_id from t_import_student_by_excel where batch_id=? group by class_name,stage_id
  #end

  -- 找到本次教师导入批次的组织机构名称有哪些
  #sql("selectBatchInfoTeacher")
    select * from t_import_teacher_by_excel where batch_id=?
  #end
  #sql("selectBatchInfoStudent")
    select * from t_import_student_by_excel where batch_id=?
  #end

  -- 根据导入的ID，修改部门编号
  #sql("UpdateOrgIdByImportTeacherId")
    update t_import_teacher_by_excel set org_id=? where id=?
  #end

  -- 根据导入的ID，修改班级编号
  #sql("UpdateOrgIdByImportStudentId")
    update t_import_student_by_excel set class_id=? where id=?
  #end

  --删除导入的临时数据，通过batchId
  #sql("deleteByBatchId")
    delete from t_import_teacher_by_excel where batch_id=?
  #end

 -- 根据人员ID，获取教师的学段id
  #sql("getStageInfoByTeacherId")
    select t_stage_id as s_stage_id from t_sys_loginperson where person_id=?
  #end
  -- 根据人员ID，获取学生的学段id
  #sql("getStageInfoByStudentId")
    select s_stage_id from t_sys_loginperson where person_id=?
  #end
-- 根据人员ID，获取家长的学段id
  #sql("getStageInfoByParentId")
    select s_stage_id from t_sys_loginperson where person_id=?
  #end

  -- 根据主、子账号获取信息
  #sql("getMountAccountInfo")
    select id,person_id,identity_id,main_person_id,main_identity_id,account_type from t_sys_account_mount where main_person_id = ? and person_id = ?
  #end

  -- 挂载子账号
  #sql("mountSubAccount")
    insert into t_sys_account_mount (person_id,identity_id,main_person_id,main_identity_id,account_type) values (?,?,?,?,?)
  #end

  -- 根据人员ID和密码，验证是不是合法，用于修改新密码
  #sql("checkPwdByPersonId")
      select count(*) as c from t_sys_loginperson where person_id=? and pwd=?
  #end

#end