<?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.com.countrygarden.bi.main.dao.dmn.DnmDomainInformationMapper">
    <!-- 定义resultMap -->
    <resultMap id="DnmDomainInformationResultMap" type="cn.com.countrygarden.bi.main.model.dmn.DnmDomainInformation">
        <!-- 主键映射 -->
        <id property="id" column="id"/>
        <!-- 其他字段映射 -->
        <result property="domainName" column="domain_name"/>
        <result property="domainCouponNumber" column="domain_coupon_number"/>
        <result property="owner" column="owner"/>
        <result property="auditStatus" column="audit_status"/>
        <result property="applicationTime" column="application_time"/>
        <result property="agencyId" column="agency_id"/>
        <result property="renewalId" column="renewal_id"/>
        <result property="renewalTime" column="renewal_time"/>
        <result property="renewalAmount" column="renewal_amount"/>
        <result property="businessModel" column="business_model"/>
        <result property="departmentUsed" column="department_used"/>
        <result property="registrar" column="registrar"/>
        <result property="registrationTime" column="registration_time"/>
        <result property="accountId" column="account_id"/>
        <result property="auditor" column="auditor"/>
        <result property="defalg" column="defalg"/>
        <result property="draftStatusTypeId" column="draft_status_type_id"/>
        <result property="costInformationId" column="cost_information_id"/>
    </resultMap>

    <insert id="insertDomainInformation" parameterType="cn.com.countrygarden.bi.main.model.dmn.DnmDomainInformation">
        <!-- 注意这里的改动 拿到自增的id -->
        <selectKey keyProperty="id" resultType="java.lang.Integer" order="AFTER">
            SELECT LAST_INSERT_ID()
        </selectKey>
        INSERT INTO dnm_domain_information (domain_name,
        domain_coupon_number,
        owner,
        audit_status,
        application_time,
        agency_id,
        renewal_id,
        renewal_time,
        renewal_amount,
        business_model,
        department_used,
        registrar,
        registration_time,
        account_id,
        auditor,
        defalg,
        draft_status_type_id,
        cost_information_id)
        VALUES (#{domainName},
        #{domainCouponNumber},
        #{owner},
        #{auditStatus},
        #{applicationTime},
        #{agencyId},
        #{renewalId},
        #{renewalTime},
        #{renewalAmount},
        #{businessModel},
        #{departmentUsed},
        #{registrar},
        #{registrationTime},
        #{accountId},
        #{auditor},
        #{defalg},
        #{draftStatusTypeId},
        #{costInformationId})
    </insert>

    <update id="updateDnmDomainInformation">
        UPDATE dnm_domain_information
        SET domain_name          = #{domainName},
            owner                = #{owner},
            audit_status         = #{auditStatus},
            application_time     = #{applicationTime},
            agency_id            = #{agencyId},
            renewal_id           = #{renewalId},
            renewal_time         = #{renewalTime},
            renewal_amount       = #{renewalAmount},
            business_model       = #{businessModel},
            department_used      = #{departmentUsed},
            registrar            = #{registrar},
            registration_time    = #{registrationTime},
            account_id           = #{accountId},
            auditor              = #{auditor},
            draft_status_type_id = #{draftStatusTypeId},
            cost_information_id  = #{costInformationId}
        WHERE id = #{id}
    </update>
    <update id="deleteDnmDomainInformation">
        UPDATE dnm_domain_information
        set defalg = '1'
        where id = #{id}
    </update>
    <update id="updateDnmDomainInformationById">
        UPDATE dnm_domain_information
        set audit_status = #{auditStatus}
        where id = #{id}
    </update>


    <select id="queryDomainInfo" resultMap="DnmDomainInformationResultMap">
        select
        <include refid="dnmDomainInformationColumns"/>
        from dnm_domain_information
        where domain_name= #{domainName}
        and defalg ='0'
        and audit_status != '草稿'
    </select>
    <!-- 在mapper.xml中定义动态SQL -->
    <select id="dynamicQuery" resultMap="DnmDomainInformationResultMap">
        <!-- 明确指定需要查询的字段 -->
        select
        <include refid="dnmDomainInformationColumns"/>
        FROM dnm_domain_information
        <where>
            <!-- domainName -->
            <if test="domainName != null and domainName != ''">
                AND domain_name LIKE CONCAT(#{domainName}, '%')
            </if>
            <!-- domainCouponNumber -->
            <if test="domainCouponNumber != null and domainCouponNumber != ''">
                AND domain_coupon_number LIKE CONCAT(#{domainCouponNumber}, '%')
            </if>
            <!-- owner -->
            <if test="owner != null and owner != ''">
                AND owner LIKE CONCAT(#{owner}, '%')
            </if>
            <!-- auditStatus -->
            <if test="auditStatus != null and auditStatus != ''">
                AND audit_status = #{auditStatus}
            </if>
            <!-- startDate -->
            <if test="startDate != null or endDate != null">
                <!-- application_time between startDate and endDate -->
                AND application_time <![CDATA[ BETWEEN #{startDate} AND #{endDate} ]]>
            </if>
            AND defalg ='0'
        </where>
        ORDER BY application_time DESC
        <!-- 分页 -->
        <if test="limit != null">
            LIMIT #{limit}
        </if>
        <if test="offset != null">
            OFFSET #{offset}
        </if>


    </select>

    <!-- 定义需要查询的字段列表 -->
    <sql id="dnmDomainInformationColumns">
        <!-- 明确指定每个字段 -->
        id, domain_name, domain_coupon_number, owner, audit_status, application_time,
        agency_id, renewal_id, renewal_time, renewal_amount, business_model,
        department_used, registrar, registration_time, account_id, auditor,
        defalg, draft_status_type_id, cost_information_id
    </sql>
    <select id="dynamicQueryCount" resultMap="DnmDomainInformationResultMap">
        <!-- 明确指定需要查询的字段 -->
        select
        <include refid="dnmDomainInformationColumns"/>
        FROM dnm_domain_information
        <where>
            <!-- domainName -->
            <if test="domainName != null and domainName != ''">
                AND domain_name LIKE CONCAT(#{domainName}, '%')
            </if>
            <!-- domainCouponNumber -->
            <if test="domainCouponNumber != null and domainCouponNumber != ''">
                AND domain_coupon_number LIKE CONCAT(#{domainCouponNumber}, '%')
            </if>
            <!-- owner -->
            <if test="owner != null and owner != ''">
                AND owner LIKE CONCAT(#{owner}, '%')
            </if>
            <!-- auditStatus -->
            <if test="auditStatus != null and auditStatus != ''">
                AND audit_status = #{auditStatus}
            </if>
            <!-- startDate -->
            <if test="startDate != null or endDate != null">
                <!-- application_time between startDate and endDate -->
                AND application_time <![CDATA[ BETWEEN #{startDate} AND #{endDate} ]]>
            </if>
            AND defalg ='0'
        </where>

    </select>
    <select id="getDnmDomainInformationById"
            resultMap="DnmDomainInformationResultMap">
        select
        <include refid="dnmDomainInformationColumns"/>
        FROM dnm_domain_information
        WHERE id = #{id}
        and defalg ='0'
    </select>
    <select id="checkDomainName"  resultMap="DnmDomainInformationResultMap">
        select
        <include refid="dnmDomainInformationColumns"/>
        from dnm_domain_information
        where domain_name= #{domainName}
        and defalg ='0'
        and audit_status != '草稿'
    </select>
    <select id="dynamicQueryByLeader"  resultMap="DnmDomainInformationResultMap">
        <!-- 明确指定需要查询的字段 -->
        select
        <include refid="dnmDomainInformationColumns"/>
        FROM dnm_domain_information
        <where>
            <!-- domainName -->
            <if test="domainName != null and domainName != ''">
                AND domain_name LIKE CONCAT(#{domainName}, '%')
            </if>
            <!-- domainCouponNumber -->
            <if test="domainCouponNumber != null and domainCouponNumber != ''">
                AND domain_coupon_number LIKE CONCAT(#{domainCouponNumber}, '%')
            </if>
            <!-- owner -->
            <if test="owner != null and owner != ''">
                AND owner LIKE CONCAT(#{owner}, '%')
            </if>
            <!-- auditStatus -->
            <if test="auditStatus != null and auditStatus != ''">
                AND audit_status = #{auditStatus}
            </if>
            <!-- startDate -->
            <if test="startDate != null or endDate != null">
                <!-- application_time between startDate and endDate -->
                AND application_time <![CDATA[ BETWEEN #{startDate} AND #{endDate} ]]>
            </if>
            AND defalg ='0'
            AND audit_status != '草稿'
        </where>
        ORDER BY application_time DESC
        <!-- 分页 -->
        <if test="limit != null">
            LIMIT #{limit}
        </if>
        <if test="offset != null">
            OFFSET #{offset}
        </if>

    </select>
    <select id="dynamicQueryCountByLeader"
            resultMap="DnmDomainInformationResultMap">
        <!-- 明确指定需要查询的字段 -->
        select
        <include refid="dnmDomainInformationColumns"/>
        FROM dnm_domain_information
        <where>
            <!-- domainName -->
            <if test="domainName != null and domainName != ''">
                AND domain_name LIKE CONCAT(#{domainName}, '%')
            </if>
            <!-- domainCouponNumber -->
            <if test="domainCouponNumber != null and domainCouponNumber != ''">
                AND domain_coupon_number LIKE CONCAT(#{domainCouponNumber}, '%')
            </if>
            <!-- owner -->
            <if test="owner != null and owner != ''">
                AND owner LIKE CONCAT(#{owner}, '%')
            </if>
            <!-- auditStatus -->
            <if test="auditStatus != null and auditStatus != ''">
                AND audit_status = #{auditStatus}
            </if>
            <!-- startDate -->
            <if test="startDate != null or endDate != null">
                <!-- application_time between startDate and endDate -->
                AND application_time <![CDATA[ BETWEEN #{startDate} AND #{endDate} ]]>
            </if>
            AND defalg ='0'
            AND audit_status != '草稿'
        </where>
    </select>
</mapper>