<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.superdesk.uniorder.dao.mapper.fund.SysFundUserMapper">

	<resultMap id="BaseResultMap" type="cn.superdesk.uniorder.dao.entity.fund.SysFundUser">
		<id column="id" jdbcType="BIGINT" property="id"/>
		<result column="organization_id" jdbcType="BIGINT" property="organizationId"/>
		<result column="enterprise_id" jdbcType="BIGINT" property="enterpriseId"/>
		<result column="user_id" jdbcType="BIGINT" property="userId"/>
		<result column="invoice_count" jdbcType="DECIMAL" property="invoiceCount"/>
		<result column="no_activated_fund" jdbcType="DECIMAL" property="noActivatedFund"/>
		<result column="activated_fund_count" jdbcType="DECIMAL" property="activatedFundCount"/>
		<result column="create_user_id" jdbcType="BIGINT" property="createUserId"/>
		<result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
		<result column="update_user_id" jdbcType="BIGINT" property="updateUserId"/>
		<result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
	</resultMap>

	<sql id="Base_Column_List">
    id, organization_id, enterprise_id, user_id, invoice_count, no_activated_fund, activated_fund_count, 
    create_user_id, create_time, update_user_id, update_time
  </sql>

	<sql id="Example_Where_Clause">
		<where>
			<foreach collection="oredCriteria" item="criteria" separator="or">
				<if test="criteria.valid">
					<trim prefix="(" prefixOverrides="and" suffix=")">
						<foreach collection="criteria.criteria" item="criterion">
							<choose>
								<when test="criterion.noValue">
									and ${criterion.condition}
								</when>
								<when test="criterion.singleValue">
									and ${criterion.condition} #{criterion.value}
								</when>
								<when test="criterion.betweenValue">
									and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
								</when>
								<when test="criterion.listValue">
									and ${criterion.condition}
									<foreach close=")" collection="criterion.value" item="listItem" open="("
									         separator=",">
										#{listItem}
									</foreach>
								</when>
							</choose>
						</foreach>
					</trim>
				</if>
			</foreach>
		</where>
	</sql>

	<sql id="Update_By_Example_Where_Clause">
		<where>
			<foreach collection="example.oredCriteria" item="criteria" separator="or">
				<if test="criteria.valid">
					<trim prefix="(" prefixOverrides="and" suffix=")">
						<foreach collection="criteria.criteria" item="criterion">
							<choose>
								<when test="criterion.noValue">
									and ${criterion.condition}
								</when>
								<when test="criterion.singleValue">
									and ${criterion.condition} #{criterion.value}
								</when>
								<when test="criterion.betweenValue">
									and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
								</when>
								<when test="criterion.listValue">
									and ${criterion.condition}
									<foreach close=")" collection="criterion.value" item="listItem" open="("
									         separator=",">
										#{listItem}
									</foreach>
								</when>
							</choose>
						</foreach>
					</trim>
				</if>
			</foreach>
		</where>
	</sql>

	<select id="selectByExample" parameterType="cn.superdesk.uniorder.dao.entity.fund.SysFundUserExample"
	        resultMap="BaseResultMap">
		select
		<if test="distinct">
			distinct
		</if>
		<include refid="Base_Column_List"/>
		from sys_fund_user
		<if test="_parameter != null">
			<include refid="Example_Where_Clause"/>
		</if>
		<if test="orderByClause != null">
			order by ${orderByClause}
		</if>
		<if test="limit != null">
			<if test="offset != null">
				limit ${offset}, ${limit}
			</if>
			<if test="offset == null">
				limit ${limit}
			</if>
		</if>
	</select>

	<select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
		select
		<include refid="Base_Column_List"/>
		from sys_fund_user
		where id = #{id,jdbcType=BIGINT}
	</select>

	<delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    delete from sys_fund_user
    where id = #{id,jdbcType=BIGINT}
  </delete>

	<delete id="deleteByExample" parameterType="cn.superdesk.uniorder.dao.entity.fund.SysFundUserExample">
		delete from sys_fund_user
		<if test="_parameter != null">
			<include refid="Example_Where_Clause"/>
		</if>
	</delete>

	<insert id="insert" parameterType="cn.superdesk.uniorder.dao.entity.fund.SysFundUser">
    insert into sys_fund_user (id, organization_id, enterprise_id, 
      user_id, invoice_count, no_activated_fund, 
      activated_fund_count, create_user_id, create_time, 
      update_user_id, update_time)
    values (#{id,jdbcType=BIGINT}, #{organizationId,jdbcType=BIGINT}, #{enterpriseId,jdbcType=BIGINT}, 
      #{userId,jdbcType=BIGINT}, #{invoiceCount,jdbcType=DECIMAL}, #{noActivatedFund,jdbcType=DECIMAL}, 
      #{activatedFundCount,jdbcType=DECIMAL}, #{createUserId,jdbcType=BIGINT}, #{createTime,jdbcType=TIMESTAMP}, 
      #{updateUserId,jdbcType=BIGINT}, #{updateTime,jdbcType=TIMESTAMP})
  </insert>

	<insert id="insertSelective" parameterType="cn.superdesk.uniorder.dao.entity.fund.SysFundUser">
		insert into sys_fund_user
		<trim prefix="(" suffix=")" suffixOverrides=",">
			<if test="id != null">
				id,
			</if>
			<if test="organizationId != null">
				organization_id,
			</if>
			<if test="enterpriseId != null">
				enterprise_id,
			</if>
			<if test="userId != null">
				user_id,
			</if>
			<if test="invoiceCount != null">
				invoice_count,
			</if>
			<if test="noActivatedFund != null">
				no_activated_fund,
			</if>
			<if test="activatedFundCount != null">
				activated_fund_count,
			</if>
			<if test="createUserId != null">
				create_user_id,
			</if>
			<if test="createTime != null">
				create_time,
			</if>
			<if test="updateUserId != null">
				update_user_id,
			</if>
			<if test="updateTime != null">
				update_time,
			</if>
		</trim>
		<trim prefix="values (" suffix=")" suffixOverrides=",">
			<if test="id != null">
				#{id,jdbcType=BIGINT},
			</if>
			<if test="organizationId != null">
				#{organizationId,jdbcType=BIGINT},
			</if>
			<if test="enterpriseId != null">
				#{enterpriseId,jdbcType=BIGINT},
			</if>
			<if test="userId != null">
				#{userId,jdbcType=BIGINT},
			</if>
			<if test="invoiceCount != null">
				#{invoiceCount,jdbcType=DECIMAL},
			</if>
			<if test="noActivatedFund != null">
				#{noActivatedFund,jdbcType=DECIMAL},
			</if>
			<if test="activatedFundCount != null">
				#{activatedFundCount,jdbcType=DECIMAL},
			</if>
			<if test="createUserId != null">
				#{createUserId,jdbcType=BIGINT},
			</if>
			<if test="createTime != null">
				#{createTime,jdbcType=TIMESTAMP},
			</if>
			<if test="updateUserId != null">
				#{updateUserId,jdbcType=BIGINT},
			</if>
			<if test="updateTime != null">
				#{updateTime,jdbcType=TIMESTAMP},
			</if>
		</trim>
	</insert>

	<select id="countByExample" parameterType="cn.superdesk.uniorder.dao.entity.fund.SysFundUserExample"
	        resultType="java.lang.Long">
		select count(*) from sys_fund_user
		<if test="_parameter != null">
			<include refid="Example_Where_Clause"/>
		</if>
	</select>

	<update id="updateByExampleSelective" parameterType="map">
		update sys_fund_user
		<set>
			<if test="record.id != null">
				id = #{record.id,jdbcType=BIGINT},
			</if>
			<if test="record.organizationId != null">
				organization_id = #{record.organizationId,jdbcType=BIGINT},
			</if>
			<if test="record.enterpriseId != null">
				enterprise_id = #{record.enterpriseId,jdbcType=BIGINT},
			</if>
			<if test="record.userId != null">
				user_id = #{record.userId,jdbcType=BIGINT},
			</if>
			<if test="record.invoiceCount != null">
				invoice_count = #{record.invoiceCount,jdbcType=DECIMAL},
			</if>
			<if test="record.noActivatedFund != null">
				no_activated_fund = #{record.noActivatedFund,jdbcType=DECIMAL},
			</if>
			<if test="record.activatedFundCount != null">
				activated_fund_count = #{record.activatedFundCount,jdbcType=DECIMAL},
			</if>
			<if test="record.createUserId != null">
				create_user_id = #{record.createUserId,jdbcType=BIGINT},
			</if>
			<if test="record.createTime != null">
				create_time = #{record.createTime,jdbcType=TIMESTAMP},
			</if>
			<if test="record.updateUserId != null">
				update_user_id = #{record.updateUserId,jdbcType=BIGINT},
			</if>
			<if test="record.updateTime != null">
				update_time = #{record.updateTime,jdbcType=TIMESTAMP},
			</if>
		</set>
		<if test="_parameter != null">
			<include refid="Update_By_Example_Where_Clause"/>
		</if>
	</update>

	<update id="updateByExample" parameterType="map">
		update sys_fund_user
		set id = #{record.id,jdbcType=BIGINT},
		organization_id = #{record.organizationId,jdbcType=BIGINT},
		enterprise_id = #{record.enterpriseId,jdbcType=BIGINT},
		user_id = #{record.userId,jdbcType=BIGINT},
		invoice_count = #{record.invoiceCount,jdbcType=DECIMAL},
		no_activated_fund = #{record.noActivatedFund,jdbcType=DECIMAL},
		activated_fund_count = #{record.activatedFundCount,jdbcType=DECIMAL},
		create_user_id = #{record.createUserId,jdbcType=BIGINT},
		create_time = #{record.createTime,jdbcType=TIMESTAMP},
		update_user_id = #{record.updateUserId,jdbcType=BIGINT},
		update_time = #{record.updateTime,jdbcType=TIMESTAMP}
		<if test="_parameter != null">
			<include refid="Update_By_Example_Where_Clause"/>
		</if>
	</update>

	<update id="updateByPrimaryKeySelective" parameterType="cn.superdesk.uniorder.dao.entity.fund.SysFundUser">
		update sys_fund_user
		<set>
			<if test="organizationId != null">
				organization_id = #{organizationId,jdbcType=BIGINT},
			</if>
			<if test="enterpriseId != null">
				enterprise_id = #{enterpriseId,jdbcType=BIGINT},
			</if>
			<if test="userId != null">
				user_id = #{userId,jdbcType=BIGINT},
			</if>
			<if test="invoiceCount != null">
				invoice_count = #{invoiceCount,jdbcType=DECIMAL},
			</if>
			<if test="noActivatedFund != null">
				no_activated_fund = #{noActivatedFund,jdbcType=DECIMAL},
			</if>
			<if test="activatedFundCount != null">
				activated_fund_count = #{activatedFundCount,jdbcType=DECIMAL},
			</if>
			<if test="createUserId != null">
				create_user_id = #{createUserId,jdbcType=BIGINT},
			</if>
			<if test="createTime != null">
				create_time = #{createTime,jdbcType=TIMESTAMP},
			</if>
			<if test="updateUserId != null">
				update_user_id = #{updateUserId,jdbcType=BIGINT},
			</if>
			<if test="updateTime != null">
				update_time = #{updateTime,jdbcType=TIMESTAMP},
			</if>
		</set>
		where id = #{id,jdbcType=BIGINT}
	</update>

	<update id="updateByPrimaryKey" parameterType="cn.superdesk.uniorder.dao.entity.fund.SysFundUser">
    update sys_fund_user
    set organization_id = #{organizationId,jdbcType=BIGINT},
      enterprise_id = #{enterpriseId,jdbcType=BIGINT},
      user_id = #{userId,jdbcType=BIGINT},
      invoice_count = #{invoiceCount,jdbcType=DECIMAL},
      no_activated_fund = #{noActivatedFund,jdbcType=DECIMAL},
      activated_fund_count = #{activatedFundCount,jdbcType=DECIMAL},
      create_user_id = #{createUserId,jdbcType=BIGINT},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      update_user_id = #{updateUserId,jdbcType=BIGINT},
      update_time = #{updateTime,jdbcType=TIMESTAMP}
    where id = #{id,jdbcType=BIGINT}
  </update>

	<!-- 自定义部分 -->
	<resultMap id="SysFundUserMap" type="cn.superdesk.uniorder.dto.vo.fund.SysFundUserVoEx">
		<id column="id" jdbcType="BIGINT" property="id"/>
		<result column="organization_id" jdbcType="BIGINT" property="organizationId"/>
		<result column="enterprise_id" jdbcType="BIGINT" property="enterpriseId"/>
		<result column="user_id" jdbcType="BIGINT" property="userId"/>
		<result column="organization_name" jdbcType="VARCHAR" property="organizationName"/>
		<result column="enterprise_name" jdbcType="VARCHAR" property="enterpriseName"/>
		<result column="realname" jdbcType="VARCHAR" property="realname"/>
		<result column="mobile" jdbcType="VARCHAR" property="mobile"/>
		<result column="invoice_count" jdbcType="DECIMAL" property="invoiceCount"/>
		<result column="fund_balance" jdbcType="DECIMAL" property="fundBalance"/>
		<result column="purchase_count" jdbcType="DECIMAL" property="purchaseCount"/>
		<result column="no_get_count" jdbcType="DECIMAL" property="noGetCount"/>
		<result column="consumer_fund" jdbcType="DECIMAL" property="consumerFund"/>
		<result column="no_activated_fund" jdbcType="DECIMAL" property="noActivatedFund"/>
		<result column="activated_fund_count" jdbcType="DECIMAL" property="activatedFundCount"/>
		<result column="grant_fund_balance" jdbcType="DECIMAL" property="grantFundBalance"/>
		<result column="create_user_id" jdbcType="BIGINT" property="createUserId"/>
		<result column="create_user_name" jdbcType="VARCHAR" property="createUserName"/>
		<result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
		<result column="update_user_id" jdbcType="BIGINT" property="updateUserId"/>
		<result column="update_user_name" jdbcType="VARCHAR" property="updateUserName"/>
		<result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
	</resultMap>
	<select id="selectByCondition" resultMap="SysFundUserMap">
		<!-- 条件搜索 -->
		SELECT
		fu.*, org.organization_name,
		en.enterprise_name,
		oru.realname,
		oru.mobile,
		oru.fund_balance,
		IFNULL((
		fu.activated_fund_count - IFNULL((
		SELECT
		IFNULL(sum(IF(paytype = 1, price, 0)), 0)
		FROM
		sys_order
		WHERE
		organization_id = fu.organization_id
		GROUP BY
		organization_id
		), 0)
		), 0) AS grant_fund_balance,
		IFNULL((
		SELECT
		IFNULL(sum(price), 0)
		FROM
		sys_order
		WHERE
		organization_id = fu.organization_id
		GROUP BY
		organization_id
		), 0) AS purchase_count,
		IFNULL((SELECT
		IFNULL(sum(no_check_count),0)
		FROM
		sys_fund_grant
		WHERE
		fund_user_id = fu.id
		), 0) AS no_get_count,
		IFNULL((
		SELECT
		IFNULL(sum(IF(paytype = 1, price, 0)), 0)
		FROM
		sys_order
		WHERE
		organization_id = fu.organization_id
		GROUP BY
		organization_id
		), 0) AS consumer_fund,
		user1.user_name AS create_user_name,
		user2.user_name AS update_user_name
		FROM
		sys_fund_user fu
		LEFT JOIN sys_enterprise en ON fu.enterprise_id = en.enterprise_id
		LEFT JOIN sys_organization org ON fu.organization_id = org.organization_id
		LEFT JOIN sys_oder_user oru ON fu.user_id = oru.user_id
		LEFT JOIN sys_user user1 ON fu.create_user_id = user1.id
		LEFT JOIN sys_user user2 ON fu.update_user_id = user2.id
		where 1=1
		<if test="userId != null">
			and fu.user_id = #{userId,jdbcType=BIGINT}
		</if>
		<if test="organizationName != null and organizationName != ''">
			and org.organization_name like CONCAT(CONCAT('%',#{organizationName,jdbcType=VARCHAR}),'%')
		</if>
		<if test="enterpriseName != null and enterpriseName != ''">
			and en.enterprise_name like CONCAT(CONCAT('%',#{enterpriseName,jdbcType=VARCHAR}),'%')
		</if>
		<if test="mobile != null and mobile != ''">
			and oru.mobile like CONCAT(CONCAT('%',#{mobile,jdbcType=VARCHAR}),'%')
		</if>
		<if test="realname != null and realname != ''">
			and oru.realname like CONCAT(CONCAT('%',#{realname,jdbcType=VARCHAR}),'%')
		</if>
		order by fu.create_time DESC
	</select>

</mapper>