<?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.dog.dao.DogChipInsuranceDao">
    
	<sql id="dogChipInsuranceColumns">
		a.id AS "id",
		a.dna_parents_compare_id AS "dnaParentsCompareId",
		a.breed_advise AS "breedAdvise",
		a.merit_assessment AS "meritAssessment",
		a.payment_member_code AS "paymentMemberCode",
		a.operator AS "operator",
		a.price AS "price",
		a.del_time AS "delTime",
		a.running_number AS "runningNumber",
		a.payment_state AS "paymentState",
		a.payment_time AS "paymentTime",
		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="dogChipInsuranceJoins">
	</sql>
    
	<select id="get" resultType="DogChipInsurance">
			SELECT
				    a.id,
					a.breed_advise,
					a.merit_assessment,
					a.payment_member_code,
					a.operator,
					a.price,
					a.del_time,
					a.running_number,
					a.payment_state,
					a.payment_time,
					a.create_by,
					a.create_date,
					a.update_by,
					a.update_date,
					a.remarks,
					a.del_flag,
					a.dna_parents_compare_id,
					IFNULL(d.name_en,b.dog_en_name)                             AS name_en,
				    IFNULL(d.dog_breed,b.dog_breed)                             AS dog_breed,
				    IFNULL(d.color_flag,b.color_flag)                           AS color_flag,
				    IFNULL(d.gender,b.dog_gender)                               AS gender,
				    IFNULL(d.birthdate,b.dog_birthday)                          AS birthdate,
				    IFNULL(d.fdog_pedigree_certified,b.fdog_pedigree_certified) AS male_pedigree_certified,
				    IFNULL(d.mdog_pedigree_certified,b.mdog_pedigree_certified) AS female_pedigree_certified,
				    IF
				    (
				        dpc.apply_source=1,dpc.pedigree_certified,d.pedigree_certified_code)
				    AS pedigree_certified,
				    dpc.birth_code ,
				    IF
				    (
				        dpc.apply_source=1,d.identification_flag,b.identification_flag)
				    AS identification_flag,
				    IF
				    (
				        dpc.apply_source=1,d.dog_owner,b.dog_owenr)
				    AS dog_owner,
				    IF
				    (
				        dpc.apply_source=1,d.member_code,b.member_code)
				    AS member_code,
				    dpc.apply_source,
				    dpc.review_state,
				    dpc.review_time,
				    dpc.review_remarks,
				    dpc.gold_certified_state,
				    dpc.is_canceled
				FROM
				    dog_chip_insurance a
				JOIN
				    dog_dna_parents_compare dpc
				ON
				    a.dna_parents_compare_id = dpc.id
				LEFT JOIN
				    dog d
				ON
				    dpc.pedigree_certified = d.pedigree_certified_code
				AND dpc.apply_source = 1
				AND d.del_flag = '0'
				LEFT JOIN
				    dog_birth_certificate b
				ON
				    dpc.birth_code = b.birth_cer_reg_code
				AND dpc.apply_source = 2
				AND b.del_flag = '0'
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="DogChipInsurance">
			SELECT
			    a.id,
				a.breed_advise,
				a.merit_assessment,
				a.payment_member_code,
				a.operator,
				a.price,
				a.del_time,
				a.running_number,
				a.payment_state,
				a.payment_time,
				a.create_by,
				a.create_date,
				a.update_by,
				a.update_date,
				a.remarks,
				a.del_flag,
				a.dna_parents_compare_id,
			    IF
			    (
			        dpc.apply_source=1,dpc.pedigree_certified,d.pedigree_certified_code)
			    AS pedigree_certified,
			    dpc.birth_code ,
			    IF
			    (
			        dpc.apply_source=1,d.identification_flag,b.identification_flag)
			    AS identification_flag,
			    IF
			    (
			        dpc.apply_source=1,d.dog_owner,b.dog_owenr)
			    AS dog_owner,
			    IF
			    (
			        dpc.apply_source=1,d.member_code,b.member_code)
			    AS member_code,
			    dpc.apply_source,
			    dpc.review_state,
			    dpc.review_time,
			    dpc.review_remarks,
			    dpc.gold_certified_state,
			    dpc.is_canceled
			FROM
			    dog_chip_insurance a
			JOIN
			    dog_dna_parents_compare dpc
			ON
			    a.dna_parents_compare_id = dpc.id
			LEFT JOIN
			    dog d
			ON
			    dpc.pedigree_certified = d.pedigree_certified_code
			AND dpc.apply_source = 1
			AND d.del_flag = '0'
			LEFT JOIN
			    dog_birth_certificate b
			ON
			    dpc.birth_code = b.birth_cer_reg_code
			AND dpc.apply_source = 2
			AND b.del_flag = '0'
		<where>
			 a.del_flag = 0
			AND dpc.del_flag = 0
			<if test="birthCode != null and birthCode != ''">
				AND (dpc.birth_code LIKE concat('%',#{birthCode},'%')
                    OR b.pedigree_certified LIKE concat('%',#{birthCode},'%')
		            OR d.pedigree_certified_code LIKE concat('%',#{birthCode},'%'))
			</if>
			<if test="identificationFlag != null and identificationFlag != ''">
				AND (d.identification_flag LIKE concat('%',#{identificationFlag},'%') OR b.identification_flag LIKE concat('%',#{identificationFlag},'%'))
			</if>
			<if test="paymentMemberCode != null and paymentMemberCode != ''">
				AND (
					a.payment_member_code like concat('%',#{paymentMemberCode},'%') 
					OR d.member_code LIKE concat('%',#{paymentMemberCode},'%') 
					OR d.dog_owner LIKE concat('%',#{paymentMemberCode},'%')
					OR b.member_code LIKE concat('%',#{paymentMemberCode},'%') 
					OR b.dog_owenr LIKE concat('%',#{paymentMemberCode},'%')
				    OR b.temp_member_code LIKE concat('%',#{paymentMemberCode},'%') 
				    OR b.temp_user_name LIKE concat('%',#{paymentMemberCode},'%')
				 )
			</if>
			<if test="reviewState != null and reviewState != ''">
				AND dpc.review_state = #{reviewState}
			</if>
			<if test="paymentState != null and paymentState != ''">
				AND a.payment_state = #{paymentState}
			</if>
			<if test="applySource != null">
				AND dpc.apply_source = #{applySource}
			</if>
			<if test="beginCreateDate != null and beginCreateDate != ''">
				AND date_format(a.create_date, '%Y-%m-%d') <![CDATA[ >= ]]> date_format(#{beginCreateDate}, '%Y-%m-%d')
			</if>
			<if test="endCreateDate != null and endCreateDate != ''">
				AND date_format(a.create_date, '%Y-%m-%d') <![CDATA[ <= ]]> date_format(#{endCreateDate}, '%Y-%m-%d')
			</if>
			<if test="isCanceled != null and isCanceled != ''">
				AND dpc.is_canceled = #{isCanceled}
			</if>
			<if test="goldCertifiedState != null">
				AND dpc.gold_certified_state = #{goldCertifiedState}
			</if>
			<if test="runningNumber != null and runningNumber != ''">
				AND a.running_number = #{runningNumber}
			</if>
		</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="findAllList" resultType="DogChipInsurance">
		SELECT 
			<include refid="dogChipInsuranceColumns"/>
		FROM dog_chip_insurance a
		<include refid="dogChipInsuranceJoins"/>
		<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>
	
	<insert id="insert">
		INSERT INTO dog_chip_insurance(
			id,
			dna_parents_compare_id,
			birth_code, 
	        pedigree_certified, 
			breed_advise,
			merit_assessment,
			payment_member_code,
			operator,
			price,
			del_time,
			running_number,
			payment_state,
			payment_time,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag
		) VALUES (
			#{id},
			#{dnaParentsCompareId},
			#{birthCode},
			#{pedigreeCertified},
			#{breedAdvise},
			#{meritAssessment},
			#{paymentMemberCode},
			#{operator},
			#{price},
			#{delTime},
			#{runningNumber},
			#{paymentState},
			#{paymentTime},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag}
		)
	</insert>
	
	<update id="update">
		UPDATE dog_chip_insurance SET
			dna_parents_compare_id = #{dnaParentsCompareId}
			birth_code = #{birthCode},
			pedigree_certified = #{pedigreeCertified},
			breed_advise = #{breedAdvise},
			merit_assessment = #{meritAssessment},
			payment_member_code = #{paymentMemberCode},
			operator = #{operator},
			price = #{price},
			del_time = #{delTime},
			running_number = #{runningNumber},
			payment_state = #{paymentState},
			payment_time = #{paymentTime},
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks}
		WHERE id = #{id}
	</update>
	
	<update id="delete">
		UPDATE dog_chip_insurance SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	<update id="saveUploadUrl">
		UPDATE dog_chip_insurance 
		<if test="fieldName != null and fieldName == 'breedAdvise'">
			SET  breed_advise = #{fileUrl}
		</if>
		<if test="fieldName != null and fieldName == 'meritAssessment'">
			SET  merit_assessment = #{fileUrl}
		</if>
		WHERE id = #{id}
	</update>
	
	<select id="getByBirthCodeOrPedigreeCertified" resultType="DogChipInsurance">
		SELECT
		    a.*
		FROM
		    dog_chip_insurance a
		JOIN
		    dog_dna_parents_compare dpc
		ON
		    a.dna_parents_compare_id = dpc.id
		<where>
			    a.del_flag =0
			AND ifnull(a.review_state,'0') !=3
			AND dpc.del_flag =0
			AND ifnull(dpc.is_canceled,'0') != 1
			<choose>
			    <when test="birthCode != null and birthCode != '' and pedigreeCertified != null and pedigreeCertified != ''">
			    	AND (dpc.birth_code = #{birthCode} or dpc.pedigree_certified = #{pedigreeCertified})
			    </when>
			    <when test="birthCode != null and birthCode != ''">
			    	AND dpc.birth_code = #{birthCode}
			    </when>
			    <when test="pedigreeCertified != null and pedigreeCertified != ''">
			    	AND dpc.pedigree_certified = #{pedigreeCertified}
			    </when>
			    <otherwise>
			       AND 1=2
			    </otherwise>
			</choose>
		  </where>
	</select>
	
</mapper>