Purchase.xml 7.02 KB
<?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.diligrp.titan.dao.PurchaseDao">

	<!-- purchase 所有查询列 -->
	<sql id="QUERY_COLUMN_LIST">
		<![CDATA[id,title,user_name AS userName,category_id AS categoryId,producing_area_id AS producingAreaId,locality_area_id AS localityAreaId,`count`,unit_id AS unitId,phone,expire_time AS expireTime,status,user_id AS userId,source,reason,ctime,utime]]>
	</sql>

	<!-- purchase 查询列来源表-->
	<sql id="QUERY_FROM_TABLE"><![CDATA[FROM purchase]]></sql>
	
	<!-- 全部条件(更多功能可以通过queryData扩展实现)  -->
	<sql id="QUERY_WHERE_CLAUSE">
		<where>
			<if test="id != null and id != ''"><![CDATA[AND id = #{id}]]></if>
            <if test="title != null and title != ''"><![CDATA[AND title like CONCAT('%',#{title},'%') ]]></if>
            <if test="userName != null and userName != ''"><![CDATA[AND user_name = #{userName}]]></if>
			<if test="categoryId != null and categoryId != ''"><![CDATA[AND category_id = #{categoryId}]]></if>
			<if test="localityAreaId != null and localityAreaId != ''"><![CDATA[AND locality_area_id = #{localityAreaId}]]></if>
            <if test="producingAreaId != null and producingAreaId != ''"><![CDATA[AND producing_area_id = #{producingAreaId}]]></if>
			<if test="count != null and count != ''"><![CDATA[AND count = #{`count`}]]></if>
			<if test="unitId != null and unitId != ''"><![CDATA[AND unit_id = #{unitId}]]></if>
			<if test="phone != null and phone != ''"><![CDATA[AND phone = #{phone}]]></if>
			<if test="expireTime != null and expireTime != ''"><![CDATA[AND expire_time = #{expireTime}]]></if>
			<if test="status != null and status != ''"><![CDATA[AND status = #{status}]]></if>
            <if test="status == null or status == ''"><![CDATA[AND status >0]]></if>
            <if test="statuses != null and statuses != ''">
                <![CDATA[AND status IN ]]>
                <foreach collection="statuses" item="status" open="(" separator="," close=")">
                    <![CDATA[#{status}]]>
                </foreach>
            </if>
            <if test="userId != null and userId != ''"><![CDATA[AND user_id = #{userId}]]></if>
			<if test="source != null and source != ''"><![CDATA[AND source = #{source}]]></if>
			<if test="reason != null and reason != ''"><![CDATA[AND reason = #{reason}]]></if>
            <if test="startCtime != null and endCtime == null "><![CDATA[AND ctime > #{startCtime}]]></if>
            <if test="startCtime  == null and endCtime != null "><![CDATA[AND ctime < DATE_ADD(#{endCtime},INTERVAL 1 DAY)]]></if>
            <if test="startCtime != null and endCtime != null "><![CDATA[AND ctime between #{startCtime} and DATE_ADD(#{endCtime},INTERVAL 1 DAY)]]></if>
            <if test="startExpiretime != null and endExpiretime == null "><![CDATA[AND expire_time > #{startExpiretime}]]></if>
            <if test="startExpiretime  == null and endExpiretime != null "><![CDATA[AND expire_time < DATE_ADD(#{endExpiretime},INTERVAL 1 DAY)]]></if>
            <if test="startExpiretime != null and endExpiretime != null  "><![CDATA[AND expire_time between #{startExpiretime} and DATE_ADD(#{endExpiretime},INTERVAL 1 DAY)]]></if>
		</where>
	</sql>
	
	<!-- 智能排序与分页 -->
	<sql id="QUERY_ORDER_LIMIT_CONDTION">
		<if test="orderField != null and orderField != '' and orderFieldType != null and orderFieldType != ''"><![CDATA[ORDER BY ${orderField} ${orderFieldType}]]></if>
		<if test="startIndex != null and startIndex &gt;= 0 and pageSize != null and pageSize &gt; 0"><![CDATA[LIMIT #{startIndex},#{pageSize}]]></if>
	</sql>

	<!-- 更新列字段,只要不为NULL则更新,除开主键列 -->
	<sql id="UPDATE_COLUMN_SET">
		<set>
			<if test="categoryId != null"><![CDATA[category_id = #{categoryId},]]></if>
            <if test="title != null"><![CDATA[title = #{title},]]></if>
            <if test="userName != null"><![CDATA[user_name = #{userName},]]></if>
            <if test="producingAreaId != null"><![CDATA[producing_area_id = #{producingAreaId},]]></if>
			<if test="localityAreaId != null"><![CDATA[locality_area_id = #{localityAreaId},]]></if>
			<if test="count != null"><![CDATA[`count` = #{count},]]></if>
			<if test="unitId != null"><![CDATA[unit_id = #{unitId},]]></if>
			<if test="phone != null"><![CDATA[phone = #{phone},]]></if>
			<if test="expireTime != null"><![CDATA[expire_time = #{expireTime},]]></if>
			<if test="status != null"><![CDATA[status = #{status},]]></if>
			<if test="userId != null"><![CDATA[user_id = #{userId},]]></if>
			<if test="source != null"><![CDATA[source = #{source},]]></if>
			<if test="reason != null"><![CDATA[reason = #{reason},]]></if>
			<if test="ctime != null"><![CDATA[ctime = #{ctime},]]></if>
            <![CDATA[ utime = now()]]>
		</set>
	</sql>

	<!-- 插入purchase记录 -->
	<insert id="insertEntry" parameterType="purchase" >
		<![CDATA[
			INSERT INTO purchase (id,title,user_name,category_id,producing_area_id,locality_area_id,`count`,unit_id,phone,expire_time,status,user_id,source,ctime)
			VALUES (#{id},#{title},#{userName},#{categoryId},#{producingAreaId},#{localityAreaId},#{count},#{unitId},#{phone},#{expireTime},#{status},#{userId},#{source},now())
		]]>
	</insert>
	
	<!-- 返回插入的编号,在事务开启状态下有效 -->
	<select id="lastSequence" resultType="int"><![CDATA[SELECT LAST_INSERT_ID() AS id]]></select>

    <!-- 删除记录,主键IN(array) -->
    <update id="deleteByArrayKey" parameterType="java.lang.reflect.Array" >
        <![CDATA[UPDATE purchase SET status=-1 WHERE id IN]]>
        <foreach collection="array" item="id" open="(" separator="," close=")">
            <![CDATA[#{id}]]>
        </foreach>
    </update>

	<!-- 删除,通过条件 -->
	<update id="deleteByCondtion" parameterType="purchase" >
		<![CDATA[DELETE FROM purchase]]>
		<include refid="QUERY_WHERE_CLAUSE"/>
	</update>

	<!-- 修改记录通过主键 -->
	<update id="updateByKey" parameterType="purchase" >
		<![CDATA[UPDATE purchase]]>
		<include refid="UPDATE_COLUMN_SET"/>
		<![CDATA[WHERE id = #{id}]]>
	</update>

	<!-- 查询,通过主键IN(array) -->
	<select id="selectEntryArray" parameterType="java.lang.reflect.Array" resultType="purchase">
		<![CDATA[SELECT]]>
		<include refid="QUERY_COLUMN_LIST"/>
		<include refid="QUERY_FROM_TABLE"/>
		<![CDATA[WHERE id IN]]>
		<foreach collection="array" item="id" open="(" separator="," close=")">
			<![CDATA[#{id}]]>
		</foreach>
	</select>

	<!-- 查询,通过条件 -->
	<select id="selectEntryList" parameterType="purchase" resultType="purchase">
		<![CDATA[SELECT]]>
		<include refid="QUERY_COLUMN_LIST"/>
		<include refid="QUERY_FROM_TABLE"/>
		<include refid="QUERY_WHERE_CLAUSE"/>
		<include refid="QUERY_ORDER_LIMIT_CONDTION"/>
	</select>

	<!-- 总数查询,通过条件 -->
	<select id="selectEntryListCount" parameterType="purchase" resultType="int">
		<![CDATA[SELECT COUNT(id) AS dataCount]]>
		<include refid="QUERY_FROM_TABLE"/>
		<include refid="QUERY_WHERE_CLAUSE"/>
	</select>
	
	<!-- 其它SQL语句 -->
	
	
</mapper>