<?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.ruoyi.web.modules.store.mapper.BizShopMapper">

    <resultMap id="BizShopResult" type="com.ruoyi.web.modules.store.domain.BizShop">
        <id property="shopId" column="shop_id"/>
        <result property="merchantId" column="merchant_id"/>
        <result property="shopName" column="shop_name"/>
        <result property="shopAvatar" column="shop_avatar"/>
        <result property="shopStatus" column="shop_status"/>
        <result property="shopDesc" column="shop_desc"/>
        <result property="shopPhone" column="shop_phone"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <sql id="selectShopVo">
        select shop_id, merchant_id, shop_name, shop_avatar, shop_status, shop_desc, shop_phone,
               del_flag, create_by, create_time, update_by, update_time, remark
        from biz_shop
    </sql>

    <select id="selectById" resultMap="BizShopResult">
        <include refid="selectShopVo"/>
        where shop_id = #{shopId} and del_flag = '0'
    </select>

    <select id="selectList" resultType="com.ruoyi.web.modules.store.vo.ShopListVO">
        select s.shop_id as shopId,
               s.shop_name as shopName,
               s.shop_avatar as shopAvatar,
               s.shop_status as shopStatus,
               ifnull(nullif(m.merchant_name, ''), ifnull(m.company_name, m.person_name)) as merchantName,
               a.admin_name as accountAdminName,
               a.login_name as accountLoginName
        from biz_shop s
        inner join biz_merchant m on s.merchant_id = m.merchant_id and m.del_flag = '0'
        left join biz_merchant_account a on s.merchant_id = a.merchant_id and a.del_flag = '0'
        where s.del_flag = '0'
        <if test="shopStatus != null and shopStatus != ''">
            and s.shop_status = #{shopStatus}
        </if>
        <if test="merchantName != null and merchantName != ''">
            and (
                m.merchant_name like concat('%', #{merchantName}, '%')
                or m.company_name like concat('%', #{merchantName}, '%')
                or m.person_name like concat('%', #{merchantName}, '%')
            )
        </if>
        <if test="keyword != null and keyword != ''">
            and (
                s.shop_name like concat('%', #{keyword}, '%')
                or m.merchant_name like concat('%', #{keyword}, '%')
                or m.company_name like concat('%', #{keyword}, '%')
                or m.person_name like concat('%', #{keyword}, '%')
                or a.login_name like concat('%', #{keyword}, '%')
                or a.admin_name like concat('%', #{keyword}, '%')
            )
        </if>
        order by s.create_time desc
    </select>

    <select id="selectBriefByMerchantId" resultType="com.ruoyi.web.modules.merchant.vo.MerchantShopBriefVO">
        select shop_id as shopId, shop_name as shopName, shop_status as shopStatus
        from biz_shop
        where merchant_id = #{merchantId} and del_flag = '0'
        order by create_time desc
    </select>

    <select id="countByShopName" resultType="int">
        select count(1) from biz_shop
        where shop_name = #{shopName} and del_flag = '0'
        <if test="excludeShopId != null">
            and shop_id != #{excludeShopId}
        </if>
    </select>

    <insert id="insert" useGeneratedKeys="true" keyProperty="shopId">
        insert into biz_shop (
            merchant_id, shop_name, shop_avatar, shop_status, shop_desc, shop_phone,
            del_flag, create_by, create_time
        ) values (
            #{merchantId}, #{shopName}, #{shopAvatar}, #{shopStatus}, #{shopDesc}, #{shopPhone},
            #{delFlag}, #{createBy}, sysdate()
        )
    </insert>

    <update id="update">
        update biz_shop
        <set>
            <if test="shopName != null">shop_name = #{shopName},</if>
            <if test="shopAvatar != null">shop_avatar = #{shopAvatar},</if>
            <if test="shopStatus != null">shop_status = #{shopStatus},</if>
            <if test="shopDesc != null">shop_desc = #{shopDesc},</if>
            <if test="shopPhone != null">shop_phone = #{shopPhone},</if>
            update_by = #{updateBy},
            update_time = sysdate()
        </set>
        where shop_id = #{shopId} and del_flag = '0'
    </update>

    <update id="logicDeleteByIds">
        update biz_shop set del_flag = '2', update_by = #{updateBy}, update_time = sysdate()
        where shop_id in
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
        and del_flag = '0'
    </update>
</mapper>
