<?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.czg.service.account.mapper.BkContactListMapper">

    <select id="getUserList" resultType="com.czg.account.entity.BkContactList">
        SELECT user.nick_name                                             AS nickName,
               `order`.call_phone                                         AS phone,
               count(1)                                                   AS orderNum,
               sum(CASE `order`.`status` WHEN '已取消' THEN 1 ELSE 0 END) AS cancelNum,
               MAX(`order`.create_time)                                   AS lastBookingTime
        FROM `bk_order` `order`
                 INNER JOIN bk_contact_list contact
                            ON `order`.call_phone = contact.phone AND contact.shop_id = #{shopId}
                 LEFT JOIN tb_shop_user user ON `contact`.phone = user.phone and user.main_shop_id = #{mainShopId}
        WHERE `order`.shop_id = #{shopId}
        GROUP BY `order`.call_phone
    </select>
</mapper>
