<?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.ota.dal.mysql.distributor.DistributorMapper">


    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->


    <select id="getOtcSimpleList" resultType="java.util.Map">
        select id,name from ota_distributor
        where deleted = 0 and is_use=1 and type = 2 and is_attachment = 1
        <if test="params.id != null and params.id != ''">
            and id != #{params.id}
        </if>
    </select>

    <select id="getOtcDistributor" resultType="com.yc.ship.module.ota.controller.admin.distributor.vo.DistributorRespVO">
        select * from ota_distributor
        where deleted = 0 and is_use=1
        <if test="type != null">
          and type = #{type}
        </if>
    </select>
    <select id="getOtcDistributorByOtaId" resultType="com.yc.ship.module.ota.controller.admin.distributor.vo.DistributorRespVO">
        select * from ota_distributor where deleted = 0 and id = #{distributorId}
    </select>
    <select id="selectDistributorStore" resultType="com.yc.ship.module.trade.api.dto.StoreRespVO">
        select * from otc_agency_store where is_use = 1 and deleted=0
        and agency_id = #{agencyId}
    </select>
    <select id="getOtcDistributorWithId" resultType="com.yc.ship.module.ota.controller.admin.distributor.vo.DistributorRespVO">
        select d.*, dp.id distributorProductId
        from ota_distributor d
            inner join ota_distributor_product dp on dp.distributor_id = d.id and dp.deleted=0
        where d.deleted = 0 and d.is_use=1 and dp.product_id = #{productId}
        <if test="type != null">
          and d.type = #{type}
        </if>
    </select>

    <select id="getDistributorById" resultType="com.yc.ship.module.ota.controller.admin.distributor.vo.DistributorRespVO">
        select * from ota_distributor where deleted = 0 and id = #{id}
    </select>
    <select id="getDistributorNew" resultType="com.yc.ship.module.ota.dal.dataobject.distributor.DistributorDO">
        select t1.* from ota_distributor t1 inner join system_users t2 on t1.id = t2.distributor_id  where t1.deleted = 0 and t2.id = #{id}
    </select>
    <select id="getById" resultType="com.yc.ship.module.ota.controller.admin.distributor.vo.DistributorRespVO">
        select * from ota_distributor
        where id = #{id}
    </select>

    <select id="getByTenantId" resultType="com.yc.ship.module.ota.controller.admin.distributor.vo.DistributorRespVO">
        select * from ota_distributor
        where account_tenant_id = #{tenantId} and type = 2
    </select>

    <update id="addBalance">
        update ota_distributor
        set balance = balance + #{money}
        where id = #{distributorId}
    </update>

    <update id="subBalance">
        update ota_distributor set balance = balance - #{money} where id =#{distributorId}
        and  balance >= #{money}
    </update>

    <update id="addInventory">
        update ota_distributor
        set inventory = inventory + #{money}
        where id = #{distributorId}
    </update>

    <update id="subInventory">
        update ota_distributor
        set inventory = inventory - #{money}
        where id = #{distributorId}  and  inventory >= #{money}
    </update>

    <update id="addSurplus">
        update ota_distributor
        set surplus = surplus + #{money}
        where id = #{distributorId}
        and  (pay_credit is null or pay_credit - surplus >= #{money})
    </update>

    <update id="addPayCredit">
        update ota_distributor
        set pay_credit = pay_credit + #{money}
        where id = #{distributorId}
    </update>

    <update id="subPayCredit">
        update ota_distributor
        set pay_credit = pay_credit - #{money}
        where id = #{distributorId} and pay_credit >= #{money}
    </update>

    <update id="subSurplus">
        update ota_distributor set surplus = surplus - #{money} where id =#{distributorId}
    </update>

    <select id="getDistributorByOtaAccountAndType" resultType="com.yc.ship.module.ota.dal.dataobject.distributor.DistributorDO">
        select * from ota_distributor od
        <if test="typeCode != null and typeCode != '' ">
            inner join ota_distributor_type odt on od.ota_type_id = odt.id and odt.is_use =1 and odt.type_code = #{typeCode}
        </if>
        where od.deleted = 0 and od.is_use = 1

        <if test="otaAccount != null and otaAccount != '' ">
            and od.ota_account = #{otaAccount}
        </if>

    </select>



    <select id="getTenantIds" resultType="java.lang.Long">
        select account_tenant_id from ota_distributor
        where type = 2 and is_use = 1 and deleted=0
    </select>

    <select id="getIds" resultType="java.lang.Long">
        select id from ota_distributor
        where type = 2 and is_use = 1 and deleted=0
    </select>

    <select id="queryDistributorMoneyTotal" resultType="com.yc.ship.module.ota.controller.admin.distributor.vo.DistributorRespVO">
        SELECT sum(IF(recharge_type = 1, money, 0.00)) balanceTotal, sum(IF(recharge_type = 2, money, 0.00)) payCreditTotal
        FROM ota_distributor_recharge
        WHERE `status` = 1 and deleted = 0 and distributor_id = #{distributorId}
    </select>

    <select id="getPlatTenantId" resultType="com.yc.ship.module.ota.dal.dataobject.distributor.DistributorDO">
        select * from ota_distributor where account_tenant_id = #{currentTenantId} limit 1
    </select>
    <select id="getDistributorByTenantId"
            resultType="com.yc.ship.module.ota.dal.dataobject.distributor.DistributorDO">
        SELECT od.*
        FROM ota_distributor od where od.account_tenant_id=#{tenantId} limit 1
    </select>
    <select id="getProductId" resultType="java.lang.Long">
        select product_id from ota_distributor_product where id = #{productId} limit 1
    </select>
    <select id="getOtaProductPage"
            resultType="com.yc.ship.module.product.api.dto.ProductSpuRespDTO">
        SELECT ps.*,td.id distributorProductId,sk.spec_type,td.start_date,td.end_date
        FROM ota_distributor_product td
        INNER JOIN product_spu ps on td.product_id = ps.id
        LEFT  JOIN product_sku sk on sk.id = td.product_sku_id
        where 1= 1  and td.deleted = 0
        <if test="vo.productName != null and vo.productName != ''">
            AND ps.product_name = #{vo.productName}
        </if>
        <if test="vo.orderStatus != null and vo.orderStatus != ''">
            AND td.is_use = #{vo.orderStatus}
        </if>
        <if test="vo.type == 1">
            AND td.distributor_id = #{distributorId}
        </if>
    </select>
    <select id="getDistributor"
            resultType="com.yc.ship.module.ota.dal.dataobject.distributor.DistributorDO">
        SELECT od.*
        FROM ota_distributor od where od.login_name=#{userName} and od.deleted = 0
    </select>
    <select id="selectOtaSpuList"
            resultType="com.yc.ship.module.product.api.dto.ProductSpuRespDTO">
        SELECT
        p.id,
        p.product_name productName,
        p.product_code productCode,
        p.product_type productType,
        p.category_id categoryId,
        p.sort_num sortNum,
        p.is_validated isValidated,
        odp.is_use isUse,
        odp.is_use_otc isUseOtc,
        odp.sale_price salePrice,
        odp.cost_price costPrice,
        odp.product_sku_id productSkuId,
        pk.spec_type,
        pc.cate_name categoryName
        FROM
        product_spu p
        INNER JOIN ota_distributor_product odp ON odp.product_id = p.id
        INNER JOIN ota_distributor od ON od.id = odp.distributor_id
        INNER JOIN product_sku pk ON pk.id = odp.product_sku_id
        INNER JOIN product_category pc ON pc.id = p.category_id
        WHERE
        od.deleted = 0
        AND od.is_use = 1
        AND odp.is_use_otc = 1
        AND odp.is_use = 1
        <if test="categoryId != null and categoryId != ''">
            AND pc.id = #{categoryId}
        </if>
        <if test="useDate != null and useDate != ''">
            AND p.id in (
            SELECT product_id as id FROM stock_day_product WHERE is_use = 1 and use_date = #{useDate}
            union ALL
            SELECT business_id as id FROM stock_day_config WHERE type = 0 and deleted = 0
            and start_date <![CDATA[ <= ]]> #{useDate} AND end_date <![CDATA[ >= ]]> #{useDate}
            union ALL
            SELECT product_id as id FROM stock_day_product_distributor WHERE deleted = 0 and use_date = #{useDate}
            )
        </if>
        AND odp.deleted = 0
        AND p.deleted = 0
        AND od.id = #{distributorId}
        ORDER BY p.id,p.update_time
    </select>
</mapper>