<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~
  ~      Copyright (c) 2018-2025, hnqz All rights reserved.
  ~
  ~  Redistribution and use in source and binary forms, with or without
  ~  modification, are permitted provided that the following conditions are met:
  ~
  ~ Redistributions of source code must retain the above copyright notice,
  ~  this list of conditions and the following disclaimer.
  ~  Redistributions in binary form must reproduce the above copyright
  ~  notice, this list of conditions and the following disclaimer in the
  ~  documentation and/or other materials provided with the distribution.
  ~  Neither the name of the pig4cloud.com developer nor the names of its
  ~  contributors may be used to endorse or promote products derived from
  ~  this software without specific prior written permission.
  ~  Author: hnqz
  ~
  -->

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.qunzhixinxi.hnqz.daemon.quartz.mapper.SysUserMapper">

	<select id="listUserCertFailure" resultType="java.util.TreeMap">
		SELECT sys_user.user_id                                          AS `ID`,
			   sys_user.realname                                         AS `姓名`,
			   sys_user.username                                         AS `账号`,
			   sys_dept.name                                             AS `所属企业`,
			   (CASE sys_user_sub.gig_type
					WHEN 0 THEN '人力家'
					WHEN 1 THEN '税邦云'
					WHEN 2 THEN '阿拉丁'
					WHEN 3 THEN '3 '
				   END)                                                  AS `结算平台`,
			   (CASE sys_user_sub.subject_location
					WHEN 'REN_LI_JIA' THEN '人力家'
					WHEN 'LUO_SHU_YUN' THEN '税邦云'
					WHEN 'FEI_YI' THEN '阿拉丁'
					WHEN 'JIN_YUAN' THEN '金园数科'
					WHEN 'XIN_TAI_ZI' THEN '薪泰梓'
				   END)                                                  AS '税源地',
				IF(
								sys_user_sub.gig_type = 1 AND sys_user_sub.cert_status = 15, ' 认证成功 ',
								IF(sys_user_sub.gig_type = 0 AND sys_user_sub.cert_status = 1, ' 认证成功 ',
								   IF(sys_user_sub.gig_type = 2 AND sys_user_sub.cert_status = 1, ' 认证成功 ',
									  ' 未完成 '
									   )))                                AS `认证表述`,
			   sys_user_sub.cert_status                                  AS `认证状态`,
			   (CASE sys_user_sub.callback_status
					WHEN 0 THEN '有结果'
					WHEN 1 THEN '进行中'
					WHEN 2 THEN '未发起' END)                               AS `发起状态`,
			   sys_user_sub.cert_remark                                  AS `上次回信信息`,
			   IF(CHAR_LENGTH(sys_user_sub.agreement_url) > 0, '有', '无') AS `手签`,
			   IF(etoken.num = 1, ' 有 ', ' 无 ')                          AS `E证通认证`,
			   sys_user_sub.update_time                                  AS `用户认证时间`
		FROM sys_user_sub
				 LEFT JOIN sys_user ON sys_user_sub.user_id = sys_user.user_id
				 LEFT JOIN sys_dept ON sys_user.dept_id = sys_dept.dept_id
				 LEFT JOIN (SELECT user_id,
								   sub_type,
								   settle_amount,
								   MAX(notify_time) AS lastest_time
							FROM wm_score_package_settle_note
							WHERE settle_note_status = 1
							GROUP BY user_id, sub_type
		) AS tmp1 ON tmp1.user_id = sys_user_sub.user_id AND tmp1.sub_type = sys_user_sub.gig_type
				 LEFT JOIN (SELECT user_id, COUNT(1) AS num
							FROM sys_user_ext_record
							WHERE del_flag = 0
							GROUP BY user_id) AS etoken
						   ON etoken.user_id = sys_user.user_id
		WHERE sys_user.del_flag = 0
		  AND sys_user.lock_flag = 0
		  AND (
				(sys_user_sub.gig_type = 1 AND sys_user_sub.cert_status NOT IN (10, 15))
				OR (sys_user_sub.gig_type = 0 AND sys_user_sub.cert_status NOT IN (0, 1))
				OR (sys_user_sub.gig_type = 2 AND sys_user_sub.cert_status NOT IN (1))
			)
		  AND sys_dept.dept_id NOT IN (1611890563)
		  AND sys_user_sub.update_time > '2022-08-01 00:00:00'
		ORDER BY sys_user_sub.create_time
				DESC
	</select>
</mapper>
