<?xml version="1.0" encoding="UTF-8"?>

<!--
  ~
  ~      Copyright (c) 2018-2025, lengleng 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 yifu4cloud.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: lengleng (wangiegie@gmail.com)
  ~
  -->

<!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.TSocialfundHouseResMapper">

  <resultMap id="tSocialfundHouseResMap" type="com.yifu.cloud.plus.v1.yifu.social.entity.TSocialfundHouseRes">
    <id property="id" column="ID"/>
    <result property="userId" column="USER_ID"/>
    <result property="socialHousehold" column="SOCIAL_HOUSEHOLD"/>
    <result property="fundHousehold" column="FUND_HOUSEHOLD"/>
    <result property="createTime" column="CREATE_TIME"/>
    <result property="createBy" column="CREATE_BY"/>
    <result property="status" column="STATUS"/>
    <result property="userName" column="USER_NAME"/>
    <result property="userDeptName" column="USER_DEPT_NAME"/>
    <result property="houseNameType" column="HOUSE_NAME_TYPE"/>
    <result property="authType" column="AUTH_TYPE"/>
  </resultMap>
    <sql id="Base_Column_List">
                a.ID,
                a.USER_ID,
                a.SOCIAL_HOUSEHOLD,
                a.FUND_HOUSEHOLD,
                a.CREATE_TIME,
                a.CREATE_BY,
                a.STATUS,
                a.USER_NAME,
                a.USER_DEPT_NAME,
                a.HOUSE_NAME_TYPE,
				a.AUTH_TYPE
    </sql>
    <sql id="tSocialfundHouseRes_where">
		<if test="tSocialfundHouseRes != null">
			<if test="tSocialfundHouseRes.idList != null">
				AND a.id in
				<foreach item="idStr" index="index" collection="tSocialfundHouseRes.idList" open="(" separator=","
						 close=")">
					#{idStr}
				</foreach>
			</if>
			<if test="tSocialfundHouseRes.idList == null">
				<if test="tSocialfundHouseRes.socialHousehold != null and tSocialfundHouseRes.socialHousehold.trim() != ''">
					AND a.SOCIAL_HOUSEHOLD like concat('%',#{tSocialfundHouseRes.socialHousehold},'%')
				</if>
				<if test="tSocialfundHouseRes.fundHousehold != null and tSocialfundHouseRes.fundHousehold.trim() != ''">
					AND a.FUND_HOUSEHOLD like concat('%',#{tSocialfundHouseRes.fundHousehold},'%')
				</if>
				<if test="tSocialfundHouseRes.status != null and tSocialfundHouseRes.status.trim() != ''">
					AND a.STATUS = #{tSocialfundHouseRes.status}
				</if>
				<if test="tSocialfundHouseRes.userName != null and tSocialfundHouseRes.userName.trim() != ''">
					AND a.USER_NAME like concat('%',#{tSocialfundHouseRes.userName},'%')
				</if>
				<if test="tSocialfundHouseRes.userDeptName != null and tSocialfundHouseRes.userDeptName.trim() != ''">
					AND a.USER_DEPT_NAME like concat('%',#{tSocialfundHouseRes.userDeptName},'%')
				</if>
				<if test="tSocialfundHouseRes.houseNameType != null and tSocialfundHouseRes.houseNameType.trim() != ''">
					AND a.HOUSE_NAME_TYPE = #{tSocialfundHouseRes.houseNameType}
				</if>
				<if test="tSocialfundHouseRes.authType != null and tSocialfundHouseRes.authType.trim() != ''">
					AND a.AUTH_TYPE = #{tSocialfundHouseRes.authType}
				</if>
			</if>
		</if>
    </sql>

    <!--tSocialfundHouseRes简单分页查询-->
    <select id="getTSocialfundHouseResPage" resultMap="tSocialfundHouseResMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM t_socialfund_house_res a
        <where>
            1=1
            <include refid="tSocialfundHouseRes_where"/>
        </where>
		order by a.CREATE_TIME desc
    </select>

	<!--tSocialfundHouseResa导出查询-->
	<select id="getexportList" resultMap="tSocialfundHouseResMap">
		SELECT
		a.SOCIAL_HOUSEHOLD,
		a.FUND_HOUSEHOLD,
		a.STATUS,
		a.USER_NAME,
		a.USER_DEPT_NAME,
		a.HOUSE_NAME_TYPE
		FROM t_socialfund_house_res a
		<where>
			1=1
			<include refid="tSocialfundHouseRes_where"/>
		</where>
	</select>

	<!-- 删权限 -->
	<delete id="deleteByUserIdAndHuName">
		delete from t_socialfund_house_res where USER_ID = #{tSocialfundHouseRes.userId} and AUTH_TYPE = #{tSocialfundHouseRes.authType}
		<if test="tSocialfundHouseRes.socialHousehold != null and tSocialfundHouseRes.socialHousehold.trim() != ''">
			AND SOCIAL_HOUSEHOLD = #{tSocialfundHouseRes.socialHousehold}
		</if>
		<if test="tSocialfundHouseRes.fundHousehold != null and tSocialfundHouseRes.fundHousehold.trim() != ''">
			AND FUND_HOUSEHOLD = #{tSocialfundHouseRes.fundHousehold}
		</if>
		<if test="tSocialfundHouseRes.socialHousehold == null and tSocialfundHouseRes.fundHousehold == null">
			AND 1=2
		</if>
	</delete>

	<select id="getSocialListByUserId" resultType="java.lang.String">
		SELECT a.SOCIAL_HOUSEHOLD FROM t_socialfund_house_res a WHERE user_id = #{userId} AND STATUS = '0' AND AUTH_TYPE = '1' AND HOUSE_NAME_TYPE = '0'
	</select>
	<select id="getHouseHoldUserMap" resultType="java.util.Map">
		<!-- 
			查询各个户籍/公积金账户对应的用户ID列表
			条件：
			- STATUS = '0': 有效状态
			- AUTH_TYPE = '1': 授权类型为1
			- HOUSE_NAME_TYPE = '0': 社保户籍类型 或 HOUSE_NAME_TYPE = '1': 公积金户籍类型
		-->
		SELECT x.HOUSEHOLD as 'key',GROUP_CONCAT(DISTINCT x.USER_ID) as 'value'
		FROM
			(
				SELECT a.SOCIAL_HOUSEHOLD as HOUSEHOLD,a.USER_ID  
				FROM t_socialfund_house_res a 
				WHERE STATUS = '0' AND AUTH_TYPE = '1' AND HOUSE_NAME_TYPE = '0'
				
				UNION ALL
				
				SELECT a.FUND_HOUSEHOLD as HOUSEHOLD,a.USER_ID  
				FROM t_socialfund_house_res a 
				WHERE STATUS = '0' AND AUTH_TYPE = '1' AND HOUSE_NAME_TYPE = '1'
			) AS x 
		WHERE x.HOUSEHOLD IS NOT NULL 
		GROUP BY x.HOUSEHOLD
	</select>
</mapper>
