<?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="com.ym.mec.biz.dal.dao.SporadicChargeInfoDao">

	<resultMap type="com.ym.mec.biz.dal.entity.SporadicChargeInfo" id="SporadicChargeInfo">
		<result column="id_" property="id" />
		<result column="title_" property="title" />
		<result column="charge_type_" property="chargeType" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
		<result column="organ_id_" property="organId"/>
		<result column="amount_" property="amount" />
		<result column="discount_amount_" property="discountAmount" />
		<result column="detail_" property="detail" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
		<result column="operator_id_" property="operatorId" />
		<result column="del_flag_" property="delFlag" />
		<result column="organ_name_" property="organName" />
		<result column="user_id_" property="userId" />
	</resultMap>

	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="SporadicChargeInfo">
		SELECT * FROM sporadic_charge_info WHERE id_ = #{id}
	</select>

	<!-- 全查询 -->
	<select id="findAll" resultMap="SporadicChargeInfo">
		SELECT * FROM sporadic_charge_info ORDER BY id_
	</select>

	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.SporadicChargeInfo"
		useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		INSERT INTO sporadic_charge_info
		(title_,charge_type_,organ_id_,amount_,discount_amount_,detail_,create_time_,update_time_,operator_id_,user_id_)
		VALUES(#{title},#{chargeType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
		#{organId},#{amount},#{discountAmount},#{detail},now(),now(),#{operatorId},#{userId})
	</insert>

	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.SporadicChargeInfo">
		UPDATE sporadic_charge_info
		<set>
			<if test="title != null">
				title_ = #{title},
			</if>
			<if test="chargeType != null">
				charge_type_ = #{chargeType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
			</if>
			<if test="organId != null">
				organ_id_ = #{organId},
			</if>
			<if test="amount != null">
				amount_ = #{amount},
			</if>
			<if test="discountAmount != null">
				discount_amount_ = #{discountAmount},
			</if>
			<if test="detail != null">
				detail_ = #{detail},
			</if>
			<if test="updateTime != null">
				update_time_ = now(),
			</if>
			<if test="operatorId != null">
				operator_id_ = #{operatorId},
			</if>
			<if test="userId != null">
				user_id_ = #{userId},
			</if>
		</set>
		WHERE id_ = #{id}
	</update>

	<!-- 根据主键删除一条记录 -->
	<update id="delete">
		UPDATE sporadic_charge_info SET del_flag_ = 1 WHERE id_ = #{id}
	</update>

	<!-- 分页查询 -->
	<select id="queryPage" resultMap="SporadicChargeInfo" parameterType="map">
		SELECT sci.* FROM sporadic_charge_info sci
		LEFT JOIN sys_user su ON su.id_ = sci.user_id_
		<include refid="queryPageSql"/>
		ORDER BY sci.id_ DESC
		<include refid="global.limit" />
	</select>

	<sql id="queryPageSql">
		<where>
				sci.del_flag_ != 1
			<if test="organId != null">
				AND FIND_IN_SET(sci.organ_id_,#{organId})
			</if>
			<if test="chargeType != null">
				AND sci.charge_type_ = #{chargeType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
			</if>
			<if test="type == 'personal'">
				AND sci.user_id_ IS NOT NULL
			</if>
			<if test="type == 'common'">
				AND sci.user_id_ IS NULL
			</if>
			<if test="search != null">
				AND (sci.title_ LIKE CONCAT('%',#{search},'%') OR su.username_ LIKE CONCAT('%',#{search},'%'))
			</if>
		</where>
	</sql>

	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(sci.id_) FROM sporadic_charge_info sci
		LEFT JOIN sys_user su ON su.id_ = sci.user_id_
		<include refid="queryPageSql"/>
	</select>
	<select id="findInfoById" resultMap="SporadicChargeInfo">
		SELECT * FROM sporadic_charge_info where id_ = #{id}
	</select>
	<!-- 根据类型和分部id查询活动 -->
	<select id="findByOrganIdAndType" resultMap="SporadicChargeInfo">
		SELECT * FROM sporadic_charge_info where organ_id_=#{organId} AND charge_type_=#{chargeType} AND del_flag_=0
	</select>
</mapper>
