<?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.domainname.DomainnameMapper">
    <resultMap id="BaseResultMap" type="cn.com.countrygarden.bi.main.model.domainname.DomainName">
    </resultMap>
    <!--
      WARNING - @mbg.generated
    -->

    <select id="checkIsDomainAdmin" parameterType="java.lang.String" resultType="java.lang.Integer">
        SELECT count(id) AS c
        FROM user_roles
        WHERE user_code = #{userCode}
          AND role_code = 'domain_admin'
    </select>


    <!--序号	域名	域名券号	所有人	审核状态	域名申请日期	账户ID	操作-->
    <select id="getDomainnameList"
            resultMap="BaseResultMap">
        SELECT
        DISTINCT
        dm.`domain_id`,
        dm.`domain_name`,
        dm.`domain_coupon`,
        dm. `owner`,
        dm.`audit_status`,
        dm.`apply_date`,
        dm.`register_user`
        FROM
        domain_manager AS dm
        <where>
            1=1
                <!--0-待审核 1-审核通过 2-审核未通过 3-草稿'-->
                <if test="userCode != null and userCode != ''">
                    <!--是管理员而非审核员
                管理员可查看：本人草稿状态的域名信息， 和有效状态的全部状态的域名信息。
            （usercode=登记人and audit_status=3  or (所有人的and audit_status！=3 )）-->
                    and ((dm.register_user = #{userCode}
                    and dm.audit_status=3) )or dm.audit_status!=3
                    <!--and dm.audit_status!=3-->
                 </if>
<!--                 <if test="userCode is null or userCode = ''">-->
<!--                     &lt;!&ndash;是审核员而非管理员,可查看所有已发布、待审核数据-->
<!--                         &ndash;&gt;-->
<!--                    and dm.audit_status in (0,1)-->
<!--                </if>-->
            <if test="userCode ==null or userCode=''">
                and dm.audit_status in (0,1)
            </if>
            <!--域名（模糊匹配）	域名券号（模糊匹配） 所有人（模糊匹配）	审核状态（下拉)  域名申请日期（区间）-->

            <if test="domainName.domain_name != null and domainName.domain_name != ''">
                and dm.domain_name like CONCAT('%',#{domainName.domain_name},'%')
            </if>
            <if test="domainName.domain_coupon != null and domainName.domain_coupon != ''">
                and dm.domain_coupon like CONCAT('%',#{domainName.domain_coupon},'%')
            </if>
            <if test="domainName.owner  != null and domainName.owner != ''">
                and dm.owner like CONCAT('%',#{domainName.owner},'%')
            </if>
            <if test="domainName.audit_status != null and domainName.audit_status != ''">
                and dm.audit_status = #{domainName.audit_status}
            <!--/if>
            <if test="timeMap.beginDate!=null and timeMap.beginDate!= ''">
                <![CDATA[
                   and dm.apply_date >= #{timeMap.beginDate}
                ]]>
            </if>
            <if test="timeMap.endDate!=null and timeMap.endDate!= ''">
                <![CDATA[
                  and dm.apply_date <= #{timeMap.endDate}
                ]]>
            </if-->
            </if>
            <if test="domainName.beginDate!=null and domainName.beginDate!= ''">
                <![CDATA[
                   and dm.apply_date >= #{domainName.beginDate}
                ]]>
            </if>
            <if test="domainName.endDate!=null and domainName.endDate!= ''">
                <![CDATA[
                  and dm.apply_date <= #{domainName.endDate}
                ]]>
            </if>
        </where>
        ORDER BY
        dm.audit_status,
        dm.apply_date DESC
    </select>


    <update id="update">
        UPDATE  domain_manager
        <set>
            <if test="domain_name!=null and domain_name!=''">domain_name=#{domain_name},</if>
            <if test="owner!=null and owner!=''">owner=#{owner},</if>
            <if test="apply_date!=null and apply_date!=''">apply_date=#{apply_date},</if>
            <if test="agency!=null and agency!=''">agency=#{agency},</if>
            <if test="next_renewal_date!=null and next_renewal_date!=''">next_renewal_date=#{next_renewal_date},</if>
            <if test="next_renew_time!=null and next_renew_time!=''">next_renew_time=#{next_renew_time},</if>
            <if test="renew_money!=null and renew_money!=''">renew_money=#{renew_money},</if>
            <if test="business_model!=null">business_model=#{business_model},</if>
            <if test="use_department!=null and use_department!=''">use_department=#{use_department},</if>
        </set>
        WHERE domain_id = #{domain_id};
    </update>

    <update id="publish">
        UPDATE  domain_manager
        <set>
            audit_status =0
        </set>
        WHERE domain_id = #{domain_id};
    </update>

    <delete id="deleteMyself">
        DELETE FROM domain_manager WHERE domain_id = #{domain_id}
    </delete>





</mapper>