<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.yifu.cloud.plus.v1.yifu.social.mapper.FailReasonConfigMapper">

	<resultMap id="failReasonConfigMap" type="com.yifu.cloud.plus.v1.yifu.social.entity.FailReasonConfig">
		<id property="id" column="ID"/>
		<result property="reason" column="REASON"/>
		<result property="reasonKey" column="REASON_KEY"/>
		<result property="simpleReason" column="SIMPLE_REASON"/>
		<result property="replay" column="REPLAY"/>
		<result property="status" column="STATUS"/>
		<result property="createTime" column="CREATE_TIME"/>
		<result property="createName" column="CREATE_NAME"/>
		<result property="updateTime" column="UPDATE_TIME"/>
		<result property="updateBy" column="UPDATE_BY"/>
		<result property="updateName" column="UPDATE_NAME"/>
		<result property="isReplace" column="IS_REPLACE"/>
		<result property="replaceLeft" column="REPLACE_LEFT"/>
		<result property="replaceRight" column="REPLACE_RIGHT"/>
		<result property="huId" column="hu_id"/>
	</resultMap>
	<sql id="Base_Column_List">
		a.ID,
                a.REASON,
                a.REASON_KEY,
                a.SIMPLE_REASON,
                a.REPLAY,
                a.`STATUS`,
                a.CREATE_TIME,
                a.CREATE_NAME,
				a.CREATE_BY,
                a.UPDATE_TIME,
                a.UPDATE_BY,
				a.UPDATE_NAME,
				a.IS_REPLACE,
				a.REPLACE_LEFT,
				a.REPLACE_RIGHT
	</sql>
	<sql id="failReasonConfig_where">
		<if test="failReasonConfig != null">
			<if test="failReasonConfig.reason != null and failReasonConfig.reason.trim() != ''">
				AND a.REASON LIKE concat('%',#{failReasonConfig.reason},'%')
			</if>
			<if test="failReasonConfig.reasonKey != null and failReasonConfig.reasonKey.trim() != ''">
				AND a.REASON_KEY LIKE concat('%',#{failReasonConfig.reasonKey},'%')
			</if>
			<if test="failReasonConfig.simpleReason != null and failReasonConfig.simpleReason.trim() != ''">
				AND a.SIMPLE_REASON LIKE concat('%',#{failReasonConfig.simpleReason},'%')
			</if>
			<if test="failReasonConfig.replay != null and failReasonConfig.replay.trim() != ''">
				AND a.REPLAY = #{failReasonConfig.replay}
			</if>
			<if test="failReasonConfig.status != null and failReasonConfig.status.trim() != ''">
				AND a.STATUS = #{failReasonConfig.status}
			</if>
			<if test="failReasonConfig.isReplace != null and failReasonConfig.isReplace.trim() != ''">
				AND a.IS_REPLACE = #{failReasonConfig.isReplace}
			</if>
			<if test="failReasonConfig.createName != null and failReasonConfig.createName.trim() != ''">
				AND a.CREATE_NAME LIKE concat('%',#{failReasonConfig.createName},'%')
			</if>
			<if test="failReasonConfig.updateName != null and failReasonConfig.updateName.trim() != ''">
				AND a.UPDATE_NAME LIKE concat('%',#{failReasonConfig.updateName},'%')
			</if>
		</if>
	</sql>
	<!--failReasonConfig简单分页查询-->
	<select id="getFailReasonConfigPage" resultMap="failReasonConfigMap">
		SELECT
		<include refid="Base_Column_List"/>
		FROM fail_reason_config a
		<where>
			1=1
			<include refid="failReasonConfig_where"/>
		</where>
		order by a.CREATE_TIME desc
	</select>

	<!--查列表-->
	<select id="getFailListByReasonKey" resultMap="failReasonConfigMap">
		SELECT
			a.ID,
			a.REASON_KEY,
			a.REPLAY
		FROM fail_reason_config a
		where a.STATUS = '0'
		<if test="reasonKey != null and reasonKey.trim() != ''">
			AND a.REASON_KEY = #{reasonKey}
		</if>
		order by a.CREATE_TIME desc
	</select>

	<!--获取list组装map来使用 hgw-->
	<select id="getFailReasonConfigList" resultMap="failReasonConfigMap">
		SELECT
			<include refid="Base_Column_List"/>
		FROM fail_reason_config a
		where a.status = '0'
	</select>
	<!--获取户配置的list组装map来使用 hgw-->
	<select id="getFailReasonConfigListByHu" resultMap="failReasonConfigMap">
		SELECT
			a.ID,
			a.REASON,
			a.REASON_KEY,
			a.SIMPLE_REASON,
			r.REPLAY,
			a.STATUS,
			a.CREATE_TIME,
			a.CREATE_NAME,
			a.CREATE_BY,
			a.UPDATE_TIME,
			a.UPDATE_BY,
			a.UPDATE_NAME,
			a.IS_REPLACE,
			a.REPLACE_LEFT,
			a.REPLACE_RIGHT
			,r.id hu_id
		FROM sys_house_hold_info_fail_res r
		LEFT JOIN fail_reason_config a on r.FAIL_ID=a.id
		where a.status = '0'
	</select>

	<!-- 导出查条数 -->
	<select id="getFailReasonConfigExportCount" resultType="java.lang.Integer">
		SELECT
			count(1)
		FROM fail_reason_config a
		<where>
			1=1
			<include refid="failReasonConfig_where"/>
			<if test="failReasonConfig != null">
				<if test="failReasonConfig.idList != null">
					AND a.id in
					<foreach item="idStr" index="index" collection="failReasonConfig.idList" open="(" separator="," close=")">
						#{idStr}
					</foreach>
				</if>
			</if>
		</where>
	</select>

	<!-- 导出数据 -->
	<select id="getFailReasonConfigListExport" resultType="com.yifu.cloud.plus.v1.yifu.social.vo.FailReasonConfigExport">
		SELECT
			a.REASON reason,
			a.REASON_KEY reasonKey,
			a.SIMPLE_REASON simpleReason,
			case a.REPLAY when '1' then '继续办理' when '2' then '中止办理' when '3' then '人工办理' else a.REPLAY end replay,
			if(a.STATUS = '0','启用','禁用') status,
			a.UPDATE_NAME updateName,
			DATE_FORMAT(a.UPDATE_TIME,'%Y-%m-%d %H:%i:%s') updateTime,
			a.CREATE_NAME createName,
			DATE_FORMAT(a.CREATE_TIME,'%Y-%m-%d %H:%i:%s') createTime
		FROM fail_reason_config a
		<where>
			1=1
			<include refid="failReasonConfig_where"/>
			<if test="failReasonConfig != null">
				<if test="failReasonConfig.idList != null">
					AND a.id in
					<foreach item="idStr" index="index" collection="failReasonConfig.idList" open="(" separator="," close=")">
						#{idStr}
					</foreach>
				</if>
			</if>
		</where>
		order by a.CREATE_TIME desc
		<if test="failReasonConfig != null">
			<if test="failReasonConfig.limitStart != null">
				limit #{failReasonConfig.limitStart},#{failReasonConfig.limitEnd}
			</if>
		</if>
	</select>

</mapper>