<?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.cku.oa.clubdog.dao.ClubDogDao">
    
	<sql id="clubDogColumns">
		a.id AS "id",
		a.pedigree_certified_code AS "pedigreeCertifiedCode",
		a.dog_owner AS "dogOwner",
		a.member_code AS "memberCode",
		a.name_en AS "nameEn",
		a.name_cn AS "nameCn",
		a.gender AS "gender",
		a.dog_breed AS "dogBreed",
		a.birthdate AS "birthdate",
		a.identification_flag AS "identificationFlag",
		a.color_flag AS "colorFlag",
		a.identifyer AS "identifyer",
		a.identify_code AS "identifyCode",
		a.identify_time AS "identifyTime",
		a.identify_site AS "identifySite",
		a.payment_state AS "paymentState",
		a.payment_time AS "paymentTime",
		a.review_state AS "reviewState",
		a.review_time AS "reviewTime",
		a.review_remarks AS "reviewRemarks",
		a.process_state AS "processState",
		a.process_time AS "processTime",
		a.print_state AS "printState",
		a.first_print AS "firstPrint",
		a.operator AS "operator",
		a.running_number AS "runningNumber",
		a.create_by AS "createBy.id",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",
		a.remarks AS "remarks",
		a.del_flag AS "delFlag"
	</sql>
	
	<sql id="clubDogJoins">
	</sql>
    
	<select id="get" resultType="ClubDog">
		SELECT 
			<include refid="clubDogColumns"/>
		FROM club_dog a
		<include refid="clubDogJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="ClubDog">
		SELECT 
			<include refid="clubDogColumns"/>
		FROM club_dog a
		<include refid="clubDogJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="pedigreeCertifiedCode != null and pedigreeCertifiedCode != ''">
				AND a.pedigree_certified_code LIKE 
					<if test="dbName == 'oracle'">'%'||#{pedigreeCertifiedCode}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{pedigreeCertifiedCode}+'%'</if>
					<if test="dbName == 'mysql'">concat('%',#{pedigreeCertifiedCode},'%')</if>
			</if>
			<if test="dogOwner != null and dogOwner != ''">
				AND a.dog_owner = #{dogOwner}
			</if>
			<if test="memberCode != null and memberCode != ''">
				AND a.member_code = #{memberCode}
			</if>
			<if test="identifyCode != null and identifyCode != ''">
				AND a.identify_code = #{identifyCode}
			</if>
			<if test="dogBreed != null and dogBreed != ''">
				AND a.dog_breed = #{dogBreed}
			</if>
			<if test="createBy!=null and createBy.id != null and createBy.id != ''">
				AND a.create_by = #{createBy.id}
			</if>
			<if test="identificationFlag != null and identificationFlag != ''">
				AND a.identification_flag = #{identificationFlag}
			</if>
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.create_date DESC
			</otherwise>
		</choose>
	</select>
	
	<select id="findAllList" resultType="ClubDog">
		SELECT 
			<include refid="clubDogColumns"/>
		FROM club_dog a
		<include refid="clubDogJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
		</where>		
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>
	<!-- 根据芯片号查询记录 -->
	<select id="getByIdentificationFlag" resultType="ClubDog">
		SELECT 
			<include refid="clubDogColumns"/>
		FROM club_dog a
		<include refid="clubDogJoins"/>
		<where>
			a.del_flag = '0' and a.identification_flag =  #{identificationFlag}
		</where>		
	</select>
	
	<!-- 根据证书号查询记录 -->
	<select id="getByPedigreeCertifiedCode" resultType="ClubDog">
		SELECT 
			<include refid="clubDogColumns"/>
		FROM club_dog a
		<include refid="clubDogJoins"/>
		<where>
			a.del_flag = '0' and a.pedigree_certified_code =  #{pedigreeCertifiedCode}
		</where>		
	</select>
	
	<insert id="insert">
		INSERT INTO club_dog(
			id,
			pedigree_certified_code,
			dog_owner,
			member_code,
			name_en,
			name_cn,
			gender,
			dog_breed,
			birthdate,
			identification_flag,
			color_flag,
			identifyer,
			identify_code,
			identify_time,
			identify_site,
			payment_state,
			payment_time,
			review_state,
			review_time,
			review_remarks,
			process_state,
			process_time,
			print_state,
			first_print,
			operator,
			running_number,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag
		) VALUES (
			#{id},
			#{pedigreeCertifiedCode},
			#{dogOwner},
			#{memberCode},
			#{nameEn},
			#{nameCn},
			#{gender},
			#{dogBreed},
			#{birthdate},
			#{identificationFlag},
			#{colorFlag},
			#{identifyer},
			#{identifyCode},
			#{identifyTime},
			#{identifySite},
			#{paymentState},
			#{paymentTime},
			#{reviewState},
			#{reviewTime},
			#{reviewRemarks},
			#{processState},
			#{processTime},
			#{printState},
			#{firstPrint},
			#{operator},
			#{runningNumber},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag}
		)
	</insert>
	
	<update id="update">
		UPDATE club_dog SET 	
			pedigree_certified_code = #{pedigreeCertifiedCode},
			dog_owner = #{dogOwner},
			member_code = #{memberCode},
			name_en = #{nameEn},
			name_cn = #{nameCn},
			gender = #{gender},
			dog_breed = #{dogBreed},
			birthdate = #{birthdate},
			identification_flag = #{identificationFlag},
			color_flag = #{colorFlag},
			identifyer = #{identifyer},
			identify_code = #{identifyCode},
			identify_time = #{identifyTime},
			identify_site = #{identifySite},
			payment_state = #{paymentState},
			payment_time = #{paymentTime},
			review_state = #{reviewState},
			review_time = #{reviewTime},
			review_remarks = #{reviewRemarks},
			process_state = #{processState},
			process_time = #{processTime},
			print_state = #{printState},
			first_print = #{firstPrint},
			operator = #{operator},
			running_number = #{runningNumber},
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks}
		WHERE id = #{id}
	</update>
	
	<update id="delete">
		UPDATE club_dog SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
</mapper>