<?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.slodon.b2b2c.dao.read.member.MemberEnquiryReadMapper">
    <resultMap id="resultMap" type="com.slodon.b2b2c.member.pojo.MemberEnquiry">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="email" property="email"/>
        <result column="phone_code" property="phoneCode"/>
        <result column="phone" property="phone"/>
        <result column="quantity" property="quantity" />
        <result column="company" property="company"/>
        <result column="message" property="message"/>
        <result column="country" property="country"/>
        <result column="region" property="region"/>
        <result column="ip" property="ip"/>
        <result column="page_url" property="pageUrl"/>
        <result column="item_type" property="itemType"/>
        <result column="item_id" property="itemId"/>
        <result column="create_uid" property="createUid"/>
        <result column="create_time" property="createTime"/>
        <result column="status" property="status"/>
        <result column="last_uid" property="lastUid"/>
        <result column="last_username" property="lastUsername"/>
        <result column="last_time" property="lastTime"/>
        <result column="vendor_id" property="vendorId"/>
        <result column="vendor_name" property="vendorName"/>
        <result column="store_id" property="storeId"/>
        <result column="valid_status" property="validStatus"/>
        <result column="enquiry_day" property="enquiryDay"/>
        <result column="enquiry_num" property="enquiryNum"/>
        <result column="goods_name" property="goodsName"/>
    </resultMap>
    <!--按照主键值进行操作-->
    <sql id="pkWhere">
    WHERE `id` = #{primaryKey}
  </sql>
    <!--操作条件-->
    <sql id="whereCondition">
        <if test="example != null">
            <trim prefix="WHERE" prefixOverrides="AND|OR">
                <if test="example.createUid != null">
                    AND `create_uid` = #{example.createUid}
                </if>
                <if test="example.statusNotEqual != null">
                    AND `status` != #{example.statusNotEqual}
                </if>
                <if test="example.storeId != null">
                    AND `store_id` = #{example.storeId}
                </if>
                <if test="example.statusEqual != null">
                    AND `status` = #{example.statusEqual}
                </if>
                <if test="example.validStatus != null">
                    AND `valid_status` = #{example.validStatus}
                </if>
                <if test="example.createTimeLte != null">
                    AND `create_time` <![CDATA[ <= ]]> #{example.createTimeLte}
                </if>
                <if test="example.createTimeGte != null">
                    AND `create_time` >= #{example.createTimeGte}
                </if>
                <if test="example.keyword != null">
                    AND (
                    `name` like concat('%',#{example.keyword},'%')
                    or `email` like concat('%',#{example.keyword},'%')
                    or `company` like concat('%',#{example.keyword},'%')
                    or `message` like concat('%',#{example.keyword},'%')
                    )
                </if>
                <if test="example.nameLike != null">
                    AND `name` like concat('%',#{example.nameLike},'%')
                </if>
                <if test="example.emailLike != null">
                    AND `email` like concat('%',#{example.emailLike},'%')
                </if>
                <if test="example.itemType != null">
                    AND `item_type` = #{example.itemType}
                </if>
                <if test="example.itemTypeIn != null">
                    AND `item_type` in ${example.itemTypeIn}
                </if>
            </trim>
        </if>
    </sql>
    <!--排序条件-->
    <sql id="orderBy">
    ORDER BY `id` DESC
  </sql>
    <sql id="orderByOther">
    order by ${example.orderBy}
  </sql>
    <!--分组条件-->
    <sql id="groupBy">
    group by ${example.groupBy}
  </sql>
    <!--分页条件-->
    <sql id="limit">
        <if test="size != null and size &gt; 0">
            limit #{startRow},#{size}
        </if>
    </sql>
    <!--查询符合条件的记录数-->
    <select id="countByExample" parameterType="com.slodon.b2b2c.member.example.MemberExample"
            resultType="java.lang.Integer">
        SELECT
        COUNT(*)
        FROM `enquiry`
        <include refid="whereCondition"/>
    </select>
    <!--根据主键查询记录-->
    <select id="getByPrimaryKey" resultMap="resultMap">
        SELECT
        *
        FROM `enquiry`
        <include refid="pkWhere"/>
    </select>
    <!--查询符合条件的记录(所有字段)-->
    <select id="listByExample" resultMap="resultMap">
        SELECT
        *
        FROM `enquiry`
        <include refid="whereCondition"/>
        <if test="example.groupBy != null">
            <include refid="groupBy"/>
        </if>
        <choose>
            <when test="example.orderBy != null">
                <include refid="orderByOther"/>
            </when>
            <otherwise>
                <include refid="orderBy"/>
            </otherwise>
        </choose>
    </select>
    <!--分页查询符合条件的记录(所有字段)-->
    <select id="listPageByExample" resultMap="resultMap">
        SELECT
        *
        FROM `enquiry`
        <include refid="whereCondition"/>
        <if test="example.groupBy != null">
            <include refid="groupBy"/>
        </if>
        <choose>
            <when test="example.orderBy != null">
                <include refid="orderByOther"/>
            </when>
            <otherwise>
                <include refid="orderBy"/>
            </otherwise>
        </choose>
        <include refid="limit"/>
    </select>
    <!--查询符合条件的记录(指定字段)-->
    <select id="listFieldsByExample" resultMap="resultMap">
        SELECT
        ${fields}
        FROM `enquiry`
        <include refid="whereCondition"/>
        <if test="example.groupBy != null">
            <include refid="groupBy"/>
        </if>
        <choose>
            <when test="example.orderBy != null">
                <include refid="orderByOther"/>
            </when>
            <otherwise>
                <include refid="orderBy"/>
            </otherwise>
        </choose>
    </select>
    <!--分页查询符合条件的记录(指定字段)-->
    <select id="listFieldsPageByExample" resultMap="resultMap">
        SELECT
        ${fields}
        FROM `enquiry`
        <include refid="whereCondition"/>
        <if test="example.groupBy != null">
            <include refid="groupBy"/>
        </if>
        <choose>
            <when test="example.orderBy != null">
                <include refid="orderByOther"/>
            </when>
            <otherwise>
                <include refid="orderBy"/>
            </otherwise>
        </choose>
        <include refid="limit"/>
    </select>

    <!--分页查询符合条件的记录(指定字段)-->
    <select id="enquiryListPageCount" resultType="java.lang.Integer">
        select COUNT(*) from enquiry t1 LEFT JOIN enquiry_assign t2 on t1.id = t2.enquiry_id LEFT JOIN vendor t3 ON t2.vendor_id = t3.vendor_id LEFT JOIN goods t4 on t1.item_id = t4.default_product_id and t1.`item_type` = 'GOODS' where t1.`status` != 'DELETE' AND (t1.`item_type` = 'SHOP' or t1.`item_type` = 'GOODS')
        <if test="storeId != null">
            AND t1.`store_id` = #{storeId}
        </if>
        <if test="nameLike != null">
            AND t1.`name` like concat('%',#{nameLike},'%')
        </if>
        <if test="emailLike != null">
            AND t1.`email` like concat('%',#{emailLike},'%')
        </if>
        <if test="isStoreAdmin == 0">
            AND t2.`vendor_id` = #{vendorId}
        </if>
        <if test="validStatus != null">
            AND t1.`valid_status` = #{validStatus}
        </if>
        order by t1.id desc
    </select>

    <!--分页查询符合条件的记录(指定字段)-->
    <select id="enquiryListPage" resultMap="resultMap">
        select t1.*,t2.vendor_id,t3.vendor_name,t4.goods_name from enquiry t1 LEFT JOIN enquiry_assign t2 on t1.id = t2.enquiry_id LEFT JOIN vendor t3 ON t2.vendor_id = t3.vendor_id LEFT JOIN goods t4 on t1.item_id = t4.default_product_id and t1.`item_type` = 'GOODS' where t1.`status` != 'DELETE' AND (t1.`item_type` = 'SHOP' or t1.`item_type` = 'GOODS')
        <if test="storeId != null">
            AND t1.`store_id` = #{storeId}
        </if>
        <if test="nameLike != null">
            AND t1.`name` like concat('%',#{nameLike},'%')
        </if>
        <if test="emailLike != null">
            AND t1.`email` like concat('%',#{emailLike},'%')
        </if>
        <if test="isStoreAdmin == 0">
            AND t2.`vendor_id` = #{vendorId}
        </if>
        <if test="validStatus != null">
            AND t1.`valid_status` = #{validStatus}
        </if>
        order by t1.id desc
        <include refid="limit"/>
    </select>

    <!--分页查询符合条件的记录(指定字段)-->
    <select id="enquiryAdminListPageCount" resultType="java.lang.Integer">
        select COUNT(*) from enquiry t1 LEFT JOIN enquiry_assign t2 on t1.id = t2.enquiry_id LEFT JOIN vendor t3 ON t2.vendor_id = t3.vendor_id where t1.`status` != 'DELETE'
        <if test="nameLike != null">
            AND t1.`name` like concat('%',#{nameLike},'%')
        </if>
        <if test="emailLike != null">
            AND t1.`email` like concat('%',#{emailLike},'%')
        </if>
<!--        <if test="isSupAdmin == 0">-->
<!--            AND t2.`vendor_id` = #{sysAdminId}-->
<!--        </if>-->
        <if test="validStatus != null">
            AND t1.`valid_status` = #{validStatus}
        </if>
        <if test="storeId != null">
            AND t1.`store_id` = #{storeId}
        </if>
        order by t1.id desc
    </select>

    <!--分页查询符合条件的记录(指定字段)-->
    <select id="enquiryAdminListPage" resultMap="resultMap">
        select t1.*,t2.vendor_id,t3.admin_name as vendor_name,(select store_name from store s where s.store_id = t1.store_id) storeName
               from enquiry t1 LEFT JOIN enquiry_assign t2 on t1.id = t2.enquiry_id LEFT JOIN sys_admin t3 ON t2.vendor_id = t3.admin_id where t1.`status` != 'DELETE'
        <if test="nameLike != null">
            AND t1.`name` like concat('%',#{nameLike},'%')
        </if>
        <if test="emailLike != null">
            AND t1.`email` like concat('%',#{emailLike},'%')
        </if>
<!--        <if test="isSupAdmin == 0">-->
<!--            AND t2.`vendor_id` = #{sysAdminId}-->
<!--        </if>-->
        <if test="validStatus != null">
            AND t1.`valid_status` = #{validStatus}
        </if>
        <if test="storeId != null">
            AND t1.`store_id` = #{storeId}
        </if>
        order by t1.id desc
        <include refid="limit"/>
    </select>

    <!--分页查询符合条件的记录(指定字段)-->
    <select id="getSellerEnquiryTrendByTime" resultMap="resultMap">
        SELECT
            DATE_FORMAT( t1.create_time, '%Y-%m-%d' ) enquiry_day,
            count( 1 ) enquiry_num
        FROM
            enquiry t1
        WHERE
            t1.`status` != 'DELETE' AND ( t1.`item_type` = 'SHOP' OR t1.`item_type` = 'GOODS' )
            and t1.create_time >= #{startTime} and #{endTime} > t1.create_time
            AND t1.`store_id` = #{storeId}
        GROUP BY
            date_format( t1.create_time, '%Y-%m-%d' )
        ORDER BY enquiry_day
    </select>

    <!--分页查询符合条件的记录(指定字段)-->
    <select id="getSellerEnquiryTrendByHour" resultMap="resultMap">
        SELECT
            DATE_FORMAT( t1.create_time, '%H:00' ) enquiry_day,
            count( 1 ) enquiry_num
        FROM
            enquiry t1
        WHERE
            t1.`status` != 'DELETE' AND ( t1.`item_type` = 'SHOP' OR t1.`item_type` = 'GOODS' )
            and t1.create_time >= #{startTime} and #{endTime} > t1.create_time
            AND t1.`store_id` = #{storeId}
        GROUP BY
            date_format( t1.create_time, '%H:00' )
        ORDER BY
            enquiry_day
    </select>

    <!--分页查询符合条件的记录(指定字段)-->
    <select id="getAdminEnquiryTrendByTime" resultMap="resultMap">
        SELECT
            DATE_FORMAT( t1.create_time, '%Y-%m-%d' ) enquiry_day,
            count( 1 ) enquiry_num
        FROM
            enquiry t1
        WHERE
            t1.`status` != 'DELETE' AND ( t1.`item_type` = 'MALL')
            and t1.create_time >= #{startTime} and #{endTime} > t1.create_time
        GROUP BY
            date_format( t1.create_time, '%Y-%m-%d' )
        ORDER BY enquiry_day
    </select>

    <!--分页查询符合条件的记录(指定字段)-->
    <select id="getAdminEnquiryTrendByHour" resultMap="resultMap">
        SELECT
            DATE_FORMAT( t1.create_time, '%H:00' ) enquiry_day,
            count( 1 ) enquiry_num
        FROM
            enquiry t1
        WHERE
            t1.`status` != 'DELETE' AND ( t1.`item_type` = 'MALL')
            and t1.create_time >= #{startTime} and #{endTime} > t1.create_time
        GROUP BY
            date_format( t1.create_time, '%H:00' )
        ORDER BY
            enquiry_day
    </select>
</mapper>