<?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="org.springblade.modules.system.mapper.UserMerchantMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="userMerchantResultMap" type="org.springblade.modules.system.entity.UserMerchant">
        <result column="id" property="id"/>
        <result column="merchant_name" property="merchantName"/>
        <result column="user_id" property="userId"/>
        <result column="id_card" property="idCard"/>
        <result column="business_licence" property="businessLicence"/>
        <result column="address" property="address"/>
        <result column="parent_id" property="parentId"/>
        <result column="auth_menu" property="authMenu"/>
        <result column="audit_status" property="auditStatus"/>
    </resultMap>
    <select id="selectUserMerchantPage" resultType="org.springblade.modules.system.vo.UserMerchantVO">
        SELECT
            m.*,
            u.avatar,
            u.phone,
            u.real_name,
            u.`name`,
            u.account,
            m.id childAccountId
        FROM
            blade_user_merchant m
                JOIN blade_user u ON m.user_id = u.id
        WHERE u.is_deleted = 0
        <if test="@cn.hutool.core.util.StrUtil@isNotBlank(p.merchantName)">
            AND INSTR( m.merchant_name, #{p.merchantName} ) > 0
        </if>
        <if test="p.auditStatus != null">
            AND m.audit_status = #{p.auditStatus}
        </if>
        <if test="p.parentId != null">
            AND m.parent_id = #{p.parentId}
        </if>
    </select>

    <select id="pageIncome" resultType="org.springblade.modules.api.vo.MerchantIncomeVO">
        select a.order_id,
               a.goods_id,
               a.goods_num,
               a.goods_name,
               a.goods_price,
               a.goods_image,
               a.goods_no,
               a.shop_id,
               a.store_name,
               a.device_id,
               b.facility_name as deviceName,
               a.cargo_way_id,
               c.cargo_no      as cargoWayNo
        from t_order_goods a
                 left join t_facility b on a.device_id = b.id
                 left join t_facility_cargo_way c on a.cargo_way_id = c.id
        where a.is_delete = 0
          and a.merchant_id = #{merchantId}
    </select>

    <select id="pageMember" resultType="org.springblade.modules.api.vo.PageMemberVO">
        select a.order_id,
               a.goods_id,
               a.goods_num,
               a.goods_name,
               a.goods_price,
               a.goods_image,
               a.goods_no,
               a.shop_id,
               a.store_name,
               a.device_id,
               b.facility_name as deviceName,
               a.cargo_way_id,
               c.cargo_no      as cargoWayNo
        from t_order_goods a
                 left join t_facility b on a.device_id = b.id
                 left join t_facility_cargo_way c on a.cargo_way_id = c.id
        where a.is_delete = 0
          and a.merchant_id = #{merchantId}
    </select>

    <select id="getChildNum" resultType="java.lang.String">
        select count(1)
        from blade_user_merchant
        where parent_id = #{merchantId}
    </select>

</mapper>
