<?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.yc.ship.module.otc.dal.mysql.store.StoreMapper">

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->
    <select id="getSellerMachine" resultType="com.yc.ship.module.otc.controller.admin.store.machine.SellerMachineRespVO">
        SELECT
            m.id,
            m.store_id storeId,
            s.store_name storeName,
            m.machine_id machineId,
            m.machine_name machineName,
            m.machine_type machineType,
            m.sort_num sortNum
        FROM
            otc_agency_store_machine m
                INNER JOIN otc_agency_store s ON s.id = m.store_id
                INNER JOIN otc_agency_store_user u ON u.store_id = m.store_id
        WHERE
            m.is_use = 1 AND m.deleted = 0
            AND u.user_id = #{sellerId}
    </select>

    <select id="selectListByStatus" resultType="com.yc.ship.module.otc.dal.dataobject.store.StoreDO">
        SELECT t1.id,
               t1.store_name,
               t1.store_code,
               t1.is_use,
               t2.balance
        FROM otc_agency_store t1
                 INNER JOIN otc_agency_store_card t2 ON t1.id = t2.store_id
            AND t1.deleted = 0 AND t2.deleted = 0
            AND t1.tenant_id = #{tenantId} AND t1.is_use = #{status}
        ORDER BY t1.create_time DESC
    </select>

    <select id="getStoreByUserId" resultType="com.yc.ship.module.otc.api.store.dto.StoreCardRespDTO">
        SELECT t1.id,
               t1.store_name,
               t1.store_code,
               t1.is_use,
               t2.*
        FROM otc_agency_store t1
                 INNER JOIN otc_agency_store_user t3 ON t1.id = t3.store_id
                 INNER JOIN otc_agency_store_card t2 ON t1.id = t2.store_id
        WHERE
          t1.deleted = 0 AND t2.deleted = 0 AND t3.user_id =  #{userId}
         LIMIT 1
    </select>

    <update id="updateStoreProductIsUse">
        UPDATE otc_agency_store_product set is_use = #{isUse} WHERE deleted = 0 and store_id = #{storeId}
        <if test="ids != null">
            and product_id in
            <foreach collection="ids" item="id" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
    </update>

    <select id="selectAuditPage" resultType="com.yc.ship.module.otc.controller.admin.store.vo.StoreRespVO">
        select s.*,od.name agencyName
        from otc_agency_store s
        left join ota_distributor od on od.id = s.agency_id
        <where>
            <if test="reqVO.auditStatus != null">
                and s.audit_status = #{reqVO.auditStatus}
            </if>
            <if test="reqVO.storeName != null and reqVO.storeName != ''">
                and s.store_name like  concat('%',#{reqVO.storeName},'%')
            </if>
            <if test="reqVO.agencyIds != null and reqVO.agencyIds.size()!=0">
                and od.id in
                <foreach collection="reqVO.agencyIds" item="id" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
        </where>
        order by s.create_time desc
    </select>

</mapper>
