<?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.gree.mall.manager.commonmapper.MaterialMapper">
    <select id="partsBasePage" resultType="com.gree.mall.manager.bean.material.base.PartsBaseVO">
        SELECT
            ${ex.selected}
        FROM websit_parts_base a
            ${ex.query}
        <if test="ex.orderBy == null or ex.orderBy ==''">
            ORDER BY a.create_time DESC
        </if>
        ${ex.orderBy}
    </select>

    <select id="materialCategoryPage" resultType="com.gree.mall.manager.bean.material.base.MaterialCategoryVO">
        SELECT
        ${ex.selected}
        FROM websit_goods_category a
        ${ex.query}
        <if test="ex.orderBy == null or ex.orderBy ==''">
            ORDER BY a.parent_category_id, a.sort, a.create_time DESC
        </if>
        ${ex.orderBy}
    </select>

    <select id="mPage" resultType="com.gree.mall.manager.bean.material.base.WebsitGoodsMVO">
        SELECT
        ${ex.selected}
        FROM websit_goods a LEFT JOIN websit_goods_category b ON a.goods_category_id = b.category_id
        ${ex.query}
        AND a.goods_type = 'M'
        <if test="ex.orderBy == null or ex.orderBy ==''">
            ORDER BY a.create_time DESC
        </if>
        ${ex.orderBy}
    </select>

    <select id="pPage" resultType="com.gree.mall.manager.bean.material.base.WebsitGoodsPVO">
        SELECT
        ${ex.selected}
        FROM websit_goods a
        ${ex.query}
        AND a.goods_type = 'P'
        <if test="ex.orderBy == null or ex.orderBy ==''">
            ORDER BY a.create_time DESC
        </if>
        ${ex.orderBy}
    </select>

    <select id="workGoodsPage" resultType="com.gree.mall.manager.bean.material.base.WorkerGoodsVO">
        SELECT
        ${ex.selected}
        FROM worker_goods a LEFT JOIN websit_goods_category b ON a.goods_category_id = b.category_id
        ${ex.query}
        <if test="ex.orderBy == null or ex.orderBy ==''">
            ORDER BY a.create_time DESC
        </if>
        ${ex.orderBy}
    </select>

    <select id="websitGoodsPricePage"
            resultType="com.gree.mall.manager.bean.material.manage.WebsitGoodsPriceVO">
        SELECT
        ${ex.selected}
        FROM websit_goods_price a LEFT JOIN websit_goods b ON a.goods_id = b.goods_id
        ${ex.query}
        <if test="ex.adminWebsitIds != null and ex.adminWebsitIds.size > 0">
            AND a.websit_id IN
            <foreach item="item" index="index" collection="ex.adminWebsitIds" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="ex.orderBy == null or ex.orderBy ==''">
            ORDER BY a.create_time DESC
        </if>
        ${ex.orderBy}
    </select>

    <select id="websitPriceUpdPage" resultType="com.gree.mall.manager.bean.material.manage.WebsitPriceUpdVO">
        SELECT
        ${ex.selected}
        FROM websit_price_upd a
            <!-- LEFT JOIN websit_price_upd_item b ON a.sheet_id = b.sheet_id -->
        ${ex.query}
        <if test="ex.adminWebsitIds != null and ex.adminWebsitIds.size > 0">
            AND a.websit_id IN
            <foreach item="item" index="index" collection="ex.adminWebsitIds" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="ex.orderBy == null or ex.orderBy ==''">
            ORDER BY a.create_time DESC
        </if>
        ${ex.orderBy}
    </select>

    <select id="websitPurchasePage" resultType="com.gree.mall.manager.bean.material.manage.WebsitPurchaseVO">
        SELECT
        ${ex.selected}
        FROM websit_purchase a LEFT JOIN storage b ON a.storage_id = b.storage_id
        ${ex.query}
        <if test="ex.adminWebsitIds != null and ex.adminWebsitIds.size > 0">
            AND a.websit_id IN
            <foreach item="item" index="index" collection="ex.adminWebsitIds" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="ex.orderBy == null or ex.orderBy ==''">
            ORDER BY a.create_time DESC
        </if>
        ${ex.orderBy}
    </select>

    <select id="websitPurchaseRetPage"
            resultType="com.gree.mall.manager.bean.material.manage.WebsitPurchaseRetVO">
        SELECT
        ${ex.selected}
        FROM websit_purchase_ret a LEFT JOIN storage b ON a.storage_id = b.storage_id
        ${ex.query}
        <if test="ex.adminWebsitIds != null and ex.adminWebsitIds.size > 0">
            AND a.websit_id IN
            <foreach item="item" index="index" collection="ex.adminWebsitIds" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="ex.orderBy == null or ex.orderBy ==''">
            ORDER BY a.create_time DESC
        </if>
        ${ex.orderBy}
    </select>

    <select id="websitVenderPage" resultType="com.gree.mall.manager.bean.material.vender.WebsitVenderVO">
        SELECT
        ${ex.selected}
        FROM websit_vender a
        ${ex.query}
        <if test="ex.orderBy == null or ex.orderBy ==''">
            ORDER BY a.create_time DESC
        </if>
        ${ex.orderBy}
    </select>

    <select id="websitNormChargePage" resultType="com.gree.mall.manager.bean.material.base.WebsitNormChargeVO">
        SELECT
        ${ex.selected}
        FROM websit_norm_charge a LEFT JOIN websit_goods_category b ON a.category_id = b.category_id
        ${ex.query}
        <if test="ex.orderBy == null or ex.orderBy ==''">
            ORDER BY a.create_time DESC
        </if>
        ${ex.orderBy}
    </select>

    <select id="websitSalesPage" resultType="com.gree.mall.manager.bean.material.manage.WebsitSalesVO">
        SELECT
        ${ex.selected}
        FROM websit_sales a
        join websit_sales_item b on a.sales_id = b.sales_id
        LEFT JOIN storage c on a.storage_id = c.storage_id
        ${ex.query}
        <if test="ex.adminWebsitIds != null and ex.adminWebsitIds.size > 0">
            AND a.websit_id IN
            <foreach item="item" index="index" collection="ex.adminWebsitIds" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="goodsName != null and goodsName != ''">
            AND  b.goods_name = #{goodsName}
        </if>
        <if test="goodsSpecification != null and goodsSpecification != ''">
            AND  b.goods_specification = #{goodsSpecification}
        </if>
        <if test="isFilter != null and isFilter != ''">
            AND ( (a.goods_Type = 'M' and b.sales_qty != b.ret_qty) or
            (a.goods_Type = 'P' and (b.sales_qty != (b.ret_qty+ b.old_refund_qty)))
            )
        </if>
        GROUP BY a.sales_id
        <if test="ex.orderBy == null or ex.orderBy ==''">
            ORDER BY a.create_time DESC
        </if>
        ${ex.orderBy}
    </select>

    <select id="existStockCategory" resultType="com.gree.mall.manager.bean.material.stock.WebsitSalesCategoryVO">
        SELECT
            c.category_id,
            c.category_name,
            c.parent_category_id,
            c.parent_category_name
        FROM
            websit_stock a
                LEFT JOIN websit_goods b ON a.goods_id = b.goods_id
                LEFT JOIN websit_goods_category c ON b.goods_category_id = c.category_id
        WHERE
            a.company_wechat_id = #{companyWechatId}
            AND a.websit_id = #{websitId}
            AND a.goods_type = 'M'
            <!-- AND a.qty > 0 -->
        GROUP BY b.goods_category_id
    </select>

    <select id="existStockGoods" resultType="com.gree.mall.manager.bean.material.stock.WebsitSalesGoodsVO">
        SELECT
            a.goods_id,
            b.goods_name,
        <if test="goodsType=='M'.toString()">
            c.price,
        </if>
        <if test="goodsType=='P'.toString()">
            b.market_price AS price,
        </if>
            b.goods_code,
            b.goods_spell,
            b.goods_specification,
            b.goods_stock_unit,
            b.goods_sales_unit,
            b.brand_rela_name,
            b.product_rela_name,
            SUM(a.qty) AS qty,
            'BASE' AS itemSourceType,
            a.goods_id AS itemSourceId,
            b.norm_type
        FROM
            websit_stock a
                JOIN websit_goods b ON a.goods_id = b.goods_id
                <if test="goodsType=='M'.toString()">
                    LEFT JOIN websit_goods_price c ON a.goods_id = c.goods_id AND a.websit_id = c.websit_id
                </if>
        WHERE
            a.company_wechat_id = #{companyWechatId}
        <if test="goodsType=='M'.toString() and categoryId!=null and categoryId!=''">
            AND b.goods_category_id = #{categoryId}
        </if>
        <if test="goodsName!=null and goodsName!=''">
            AND b.goods_name LIKE CONCAT('%',#{goodsName},'%')
        </if>
        <if test="goodsSpecification!=null and goodsSpecification!=''">
            AND b.goods_specification LIKE CONCAT('%',#{goodsSpecification},'%')
        </if>
          AND a.websit_id = #{websitId}
        <if test="goodsType!=null and goodsType!=''">

            AND a.goods_type = #{goodsType}
        </if>
          <!-- AND a.qty > 0 -->
        GROUP BY a.goods_id
        LIMIT 100
    </select>

    <select id="notStockGoods" resultType="com.gree.mall.manager.bean.material.stock.WebsitSalesGoodsVO">
        SELECT
        b.goods_id,
        b.goods_name,
        b.market_price AS price,
        b.goods_code,
        b.goods_spell,
        b.goods_specification,
        b.goods_stock_unit,
        b.goods_sales_unit,
        b.brand_rela_name,
        b.product_rela_name,
        b.convert_bit_scale,
        b.goods_sales_convert_qty,
        b.manage_worker_stock,
        b.part_type,
        0 AS qty
        FROM
        websit_goods b
        WHERE
        b.company_wechat_id = #{companyWechatId}
        <if test="goodsName!=null and goodsName!=''">
            AND b.goods_name LIKE CONCAT('%',#{goodsName},'%')
        </if>
        AND b.goods_type = #{goodsType}
        AND b.norm_type = 'M'
    </select>

    <select id="websitTradePage" resultType="com.gree.mall.manager.bean.material.manage.WebsitTradeVO">
        SELECT
        ${ex.selected}
        FROM websit_sales_pay_order a
        ${ex.query}
        <if test="ex.adminWebsitIds != null and ex.adminWebsitIds.size > 0">
            AND a.websit_id IN
            <foreach item="item" index="index" collection="ex.adminWebsitIds" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="ex.orderBy == null or ex.orderBy ==''">
            ORDER BY a.create_time DESC
        </if>
        ${ex.orderBy}
    </select>

    <select id="mAlonePage" resultType="com.gree.mall.manager.bean.material.base.WebsitGoodsAloneMVO">
        SELECT
        ${ex.selected}
        FROM websit_goods_alone a
        ${ex.query}
        AND a.goods_type = 'M'
        <if test="ex.orderBy == null or ex.orderBy ==''">
            ORDER BY a.create_time DESC
        </if>
        ${ex.orderBy}
    </select>

    <select id="pAlonePage" resultType="com.gree.mall.manager.bean.material.base.WebsitGoodsAlonePVO">
        SELECT
        ${ex.selected}
        FROM websit_goods_alone a
        ${ex.query}
        AND a.goods_type = 'P'
        <if test="ex.orderBy == null or ex.orderBy ==''">
            ORDER BY a.create_time DESC
        </if>
        ${ex.orderBy}
    </select>

    <select id="enginPayManageList" resultType="com.gree.mall.manager.bean.engin.EnginPayManageVO">
        SELECT
        ${ex.selected},
        IF((a.total_sales_amount - a.total_pay_amount) <![CDATA[ < ]]> 0, 0, (a.total_sales_amount - a.total_pay_amount)) AS totalNotPayAmount
        FROM engin_pay_manage a
        ${ex.query}
        <if test="ex.adminWebsitIds != null and ex.adminWebsitIds.size > 0">
            AND a.websit_id IN
            <foreach item="item" index="index" collection="ex.adminWebsitIds" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="ex.orderBy == null or ex.orderBy ==''">
            ORDER BY a.create_time DESC
        </if>
        ${ex.orderBy}
    </select>

    <select id="enginPayManageGatherList" resultType="com.gree.mall.manager.bean.engin.EnginPayManageGatherVO">
        SELECT
        ${ex.selected},
        SUM(a.total_sales_amount) AS totalSalesAmount,
        SUM(a.total_pay_amount) AS totalPayAmount,
        IF((SUM(a.total_sales_amount) - SUM(a.total_pay_amount)) <![CDATA[ < ]]> 0, 0, (SUM(a.total_sales_amount) - SUM(a.total_pay_amount))) AS totalNotPayAmount
        FROM engin_pay_manage a
        ${ex.query}
        <if test="ex.adminWebsitIds != null and ex.adminWebsitIds.size > 0">
            AND a.websit_id IN
            <foreach item="item" index="index" collection="ex.adminWebsitIds" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        GROUP BY a.company_wechat_id, a.project_no
    </select>

    <select id="enginPayManageGatherLastUpdate" resultType="com.gree.mall.manager.bean.engin.EnginPayManageGatherVO">
    SELECT a.project_no, a.last_update_time, a.last_update_by
    FROM engin_pay_manage a
    INNER JOIN (SELECT project_no, MAX(last_update_time) AS max_time
                FROM engin_pay_manage
                WHERE
                    company_wechat_id = #{companyWechatId}
                  AND project_no IN
        <foreach item="item" index="index" collection="projectList" open="(" separator=","
                 close=")">
            #{item}
        </foreach>
        GROUP BY project_no) b
    ON a.project_no = b.project_no AND a.last_update_time = b.max_time AND a.company_wechat_id = #{companyWechatId}
    </select>
    <select id="exportWorkerMaterialRela"
            resultType="com.gree.mall.manager.bean.material.base.WorkerGoodsRelaExportBean">
        SELECT
            a.company_wechat_id,
            a.company_wechat_name,
            a.goods_category_name,
            a.goods_id,
            a.goods_name,
            a.is_small,
            a.status,
            b.goods_category_name AS websitCategoryName,
            b.websit_goods_id,
            b.websit_goods_name,
            c.goods_specification AS specification,
            c.goods_sales_convert_qty,
            c.manage_worker_stock,
            c.status AS websitGoodsStatus
        FROM
            worker_goods a
                LEFT JOIN worker_goods_rela b ON a.company_wechat_id = b.company_wechat_id AND a.goods_id = b.worker_goods_id
                LEFT JOIN websit_goods c ON a.company_wechat_id = c.company_wechat_id AND b.websit_goods_id = c.goods_id
        <where>
            <if test="companyWechatId != null and companyWechatId != ''">
                AND a.company_wechat_id = #{companyWechatId}
            </if>
        </where>

    </select>
</mapper>