<?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">
    <update id="updateItemPushFlag">
        UPDATE websit_parts_sales_item SET push_flag = #{pushFlag} WHERE sales_id = #{id}
    </update>

    <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}
            <if test="storageId != null and storageId != ''">
                AND a.storage_id = #{storageId}
            </if>
            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.id,
            a.goods_id,
            b.goods_name,
        <if test="goodsType=='M'.toString()">
            c.price,
        </if>
        <if test="goodsType=='P'.toString()">
            b.market_price AS price,
            a.goods_id AS partsNumber,
            b.goods_name AS partsName,
            b.market_price,
            b.part_type,
        </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,
            b.material_group_name,
            a.old_qty
        FROM
            websit_stock a
                JOIN websit_goods b ON
                <if test="goodsType!=null and goodsType!='' and goodsType=='M'.toString()">
                    a.goods_id = b.goods_id
                </if>
                <if test="goodsType!=null and goodsType!='' and goodsType=='P'.toString()">
                    a.goods_id = b.goods_code
                </if>
                <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>
        <if test="partType!=null and partType!=''">
            AND b.part_type = #{partType}
        </if>
        <if test="storageId!=null and storageId!=''">
            AND a.storage_id = #{storageId}
        </if>
        <if test="goodsType=='P'.toString()">
            AND a.qty > 0
        </if>
        GROUP BY a.goods_id
        LIMIT 200
    </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}
        AND goods_type = 'M'
        AND pay_type IN ('WECHAT', 'ALLINPAY')
        AND order_type = 'S'
        <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>
    <select id="websitVenderGoodsPage"
            resultType="com.gree.mall.manager.bean.material.vender.WebsitVenderGoodsVO">
        SELECT
            ${ex.selected}
        FROM websit_vender_goods a
            ${ex.query}
        <if test="ex.orderBy == null or ex.orderBy ==''">
            ORDER BY a.create_time DESC
        </if>
        ${ex.orderBy}
    </select>

    <select id="websitVenderGoodsRalaPage"
            resultType="com.gree.mall.manager.bean.material.vender.WebsitVenderGoodsRelaVO">
        SELECT
            ${ex.selected}
        FROM
            websit_vender_goods_rela a
            JOIN websit_goods b ON a.company_wechat_id = b.company_wechat_id AND a.goods_id = b.goods_id
            LEFT JOIN websit_goods_category c ON b.goods_category_id = c.category_id
        ${ex.query}
        <if test="ex.orderBy == null or ex.orderBy ==''">
            ORDER BY a.create_time DESC
        </if>
        ${ex.orderBy}
    </select>

    <select id="websitPurchaseApplyPage"
            resultType="com.gree.mall.manager.bean.material.manage.WebsitPurchaseApplyVO">
        SELECT
            ${ex.selected}
        FROM
            websit_purchase 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>
        AND a.flag IN ('SAVE', 'SUBMIT')
        AND a.is_recheck = 'NO'
        <if test="ex.orderBy == null or ex.orderBy ==''">
            ORDER BY a.create_time DESC
        </if>
        ${ex.orderBy}
    </select>

    <select id="websitPurchaseCheckPage"
            resultType="com.gree.mall.manager.bean.material.vender.WebsitPurchaseCheckVO">
        SELECT
            ${ex.selected}
        FROM
            websit_purchase 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>
        AND a.flag IN ('SUBMIT', 'OK')
        <if test="ex.orderBy == null or ex.orderBy ==''">
            ORDER BY a.create_time DESC
        </if>
        ${ex.orderBy}
    </select>

    <select id="websitPurchaseOrderPage"
            resultType="com.gree.mall.manager.bean.material.manage.WebsitPurchaseOrderVO">
        SELECT
            ${ex.selected}
        FROM
            websit_purchase 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>
        AND a.flag IN ('SUBMIT', 'OK', 'CANCEL')
        AND a.is_recheck = 'YES'
        <if test="ex.orderBy == null or ex.orderBy ==''">
            ORDER BY a.create_time DESC
        </if>
        ${ex.orderBy}
    </select>

    <select id="websitPurchaseAdjustmentPage"
            resultType="com.gree.mall.manager.bean.material.manage.WebsitPurchaseAdjustmentVO">
        SELECT
            ${ex.selected}
        FROM
            websit_purchase_adjustment 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="websitPartsNewInPage"
            resultType="com.gree.mall.manager.bean.material.manage.WebsitPartsNewInVO">
        SELECT
            ${ex.selected}
        FROM
            websit_parts_new_in_record a
        ${ex.query}
        <if test="ex.adminWebsitIds != null and ex.adminWebsitIds.size > 0">
            AND a.receive_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="websitPartsOldOutPage"
            resultType="com.gree.mall.manager.bean.material.manage.WebsitPartsOldOutVO">
        SELECT
            ${ex.selected}
        FROM
            websit_parts_old_out_record a
        ${ex.query}
        <if test="ex.receiveDate != null">
            AND a.receive_date IS NULL
        </if>
        <if test="ex.adminWebsitIds != null and ex.adminWebsitIds.size > 0">
            AND a.send_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="websitWorkSettlementPage"
            resultType="com.gree.mall.manager.bean.material.manage.WebsitWorkSettlementVO">
        SELECT
            ${ex.selected}
        FROM
            websit_parts_settlement_record a
        ${ex.query}
        <if test="ex.orderBy == null or ex.orderBy ==''">
            ORDER BY a.import_time DESC
        </if>
        ${ex.orderBy}
    </select>

    <select id="websitPartsConfigPage"
            resultType="com.gree.mall.manager.bean.material.manage.WebsitPartsConfigVO">
        SELECT
            ${ex.selected}
        FROM
            websit_parts_refund_config a LEFT JOIN material_config b ON a.company_wechat_id = b.company_wechat_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="websitPartsSalesPage"
            resultType="com.gree.mall.manager.bean.material.manage.WebsitPartsSalesVO">
        SELECT
            ${ex.selected},
            CASE WHEN a.delivery_type = 0 AND a.buy_people = 'WORKER' AND a.websit_confirm_date IS NOT NULL THEN 1 ELSE 0 END AS deliveryConfirmFlag
        FROM
        websit_parts_sales a LEFT JOIN websit_parts_sales_item b ON a.id = b.sales_id
        ${ex.query}
        AND del = 0
        <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="websitPartsSalesWPage"
            resultType="com.gree.mall.manager.bean.material.manage.WebsitPartsSalesWVO">
        SELECT
            ${ex.selected},
            CASE WHEN a.delivery_type = 0 AND a.buy_people = 'WORKER' AND a.websit_confirm_date IS NOT NULL THEN 1 ELSE 0 END AS deliveryConfirmFlag
        FROM
            websit_parts_sales a
        ${ex.query}
        AND del = 0
        <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="selectItemBySalesOrderId"
            resultType="com.gree.mall.manager.bean.material.manage.WebsitPartsSalesItemBean">
        select
            a.id ,
            a.identity ,
            a.websit_id,
            a.parts_websit_id,
            a.parts_id,
            a.parts_number,
            a.parts_name,
            a.material_group_name,
            a.goods_stock_unit,
            a.sales_id,
            a.sales_price,
            a.market_price,
            a.second_price,
            IF(d.status = 'END', a.examine_qty, a.qty) AS qty,
            a.examine_qty,
            a.new_refund_qty,
            a.old_refund_qty ,
            a.total_amount,
            a.push_flag ,
            a.create_by ,
            a.create_time,
            a.update_by,
            a.update_time,
            IFNULL(c.qty, 0) stockQty
        FROM websit_parts_sales_item a JOIN websit_goods b ON a.parts_id = b.goods_id
                                      LEFT JOIN websit_stock c ON a.websit_id = c.websit_id AND a.parts_number = c.goods_id AND a.parts_websit_id = c.parts_websit_id
                                      JOIN websit_parts_sales d ON a.sales_id = d.id
        WHERE a.sales_id = #{id}
    </select>

    <select id="queryOldRefundRecordById"
            resultType="com.gree.mall.manager.bean.material.worker.OldRefundManageRecordBean">
        SELECT
            a.*
        FROM
            websit_parts_old_refund_manage_record a
        WHERE
            sales_id = #{id}
    </select>

    <select id="newRefundManagePage"
            resultType="com.gree.mall.manager.bean.material.worker.NewRefundManageVO">
        SELECT
            ${ex.selected}
        FROM
            websit_parts_new_refund_manage a
        ${ex.query}
        <if test="ex.adminWebsitIds != null and ex.adminWebsitIds.size > 0">
            AND a.receive_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="oldRefundManagePage" resultType="com.gree.mall.manager.bean.material.worker.OldRefundManageVO">
        SELECT
            ${ex.selected}
        FROM
            websit_parts_old_refund_manage a
        ${ex.query}
        <if test="ex.adminWebsitIds != null and ex.adminWebsitIds.size > 0">
            AND a.receive_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="websitPurchaseChangeSalesPage"
            resultType="com.gree.mall.manager.bean.material.manage.WebsitPartsChangeSalesVO">
        SELECT
            ${ex.selected}
        FROM
            websit_parts_change_sales 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="websitPartsRefundRecordPage"
            resultType="com.gree.mall.manager.bean.material.manage.WebsitPartsRefundRecordVO">
        SELECT
            ${ex.selected}
        FROM
            websit_parts_refund_record 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="workerBuySum" resultType="com.gree.mall.manager.bean.material.report.MaterialSalesSumBean">
        SELECT
            t2.websit_id,
            t2.name AS websit_name,
            t1.worker_mobile,
            t1.worker_id,
            t1.worker_name,
            IFNULL(SUM( t1.totalSaleValue ) - SUM( t1.retValue ), 0) AS totalSaleValue,
            IFNULL(SUM( t1.cash ), 0) AS cash,
            IFNULL(SUM( t1.allinpay ), 0) AS allinpay,
            IFNULL(SUM( t1.cashSaleValue ), 0) AS cashSaleValue,
            IFNULL(SUM( t1.allinpaySaleValue ), 0) AS allinpaySaleValue,
            IFNULL(SUM( t1.retValue ), 0) AS retValue
        FROM
        (
            SELECT
                a.websit_id,
                a.worker_mobile,
                a.worker_id,
                a.worker_name,
                a.websit_name,
                COUNT(DISTINCT a.sales_id, IF ( a.pay_type = 'CASH', 1, NULL )) AS cash,
                COUNT(DISTINCT a.sales_id, IF( a.pay_type = 'ALLINPAY', 1, NULL )) AS allinpay,
                SUM( a.sales_qty * a.price ) AS totalSaleValue,
                SUM(IF( a.pay_type = 'CASH', a.sales_qty * a.price, 0 )) AS cashSaleValue,
                SUM(IF( a.pay_type = 'ALLINPAY', a.sales_qty * a.price, 0 )) AS allinpaySaleValue,
                0 AS retValue
            FROM
            (
                SELECT
                    t1.sales_id,
                    t1.websit_id,
                    t1.websit_name,
                    t1.worker_mobile,
                    t1.worker_id,
                    t1.worker_name,
                    t1.pay_type,
                    t2.goods_id,
                    t2.sales_qty,
                    t2.price
                FROM
                    websit_sales t1,
                    websit_sales_item t2
                WHERE
                    t1.sales_id = t2.sales_id
                    AND t1.company_wechat_id = #{companyWechatId}
                    AND t1.buy_people = 'WORKER'
                    AND t1.flag = 'PAY_TAKE'
                    <if test="websitId != null and websitId != ''">
                        AND t1.websit_id = #{websitId}
                    </if>
                    <if test="workerMobile != null and workerMobile != ''">
                        AND t1.worker_mobile = #{workerMobile}
                    </if>
                    <if test="workerId != null and workerId != ''">
                        AND t1.worker_id LIKE CONCAT('%', #{workerId},'%')
                    </if>
                    <if test="workerName != null and workerName != ''">
                        AND t1.worker_name LIKE CONCAT('%', #{workerName},'%')
                    </if>
                    <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                        AND t1.confirm_time BETWEEN #{startTime} AND #{endTime}
                    </if>
                    <if test="websitId == null and websits != null and websits.size > 0">
                        AND t1.websit_id IN
                        <foreach item="item" index="index" collection="websits" open="(" separator=","
                                 close=")">
                            #{item}
                        </foreach>
                    </if>
            ) a
            GROUP BY
            1,
            2,
            3
        UNION ALL
            SELECT
                a.websit_id,
                a.worker_mobile,
                a.worker_id,
                a.worker_name,
                a.websit_name,
                0 AS cash,
                0 AS allinpay,
                0 AS totalSaleValue,
                0 AS cashSaleValue,
                0 AS allinpaySaleValue,
                SUM( a.ret_qty * a.price ) AS retValue
            FROM
               (
                SELECT
                    t1.sales_ret_id,
                    t1.websit_id,
                    t1.worker_mobile,
                    t1.worker_id,
                    t1.worker_name,
                    t1.websit_name,
                    t2.goods_id,
                    t2.ret_qty,
                    t2.price
                FROM
                    websit_sales_ret t1,
                    websit_sales_ret_item t2
                WHERE
                    t1.sales_ret_id = t2.sales_ret_id
                    AND t1.company_wechat_id = #{companyWechatId}
                    AND t1.confirm_by IS NOT NULL
                    <if test="websitId != null and websitId != ''">
                        AND t1.websit_id = #{websitId}
                    </if>
                    <if test="workerMobile != null and workerMobile != ''">
                        AND t1.worker_mobile = #{workerMobile}
                    </if>
                    <if test="workerId != null and workerId != ''">
                        AND t1.worker_id LIKE CONCAT('%', #{workerId},'%')
                    </if>
                    <if test="workerName != null and workerName != ''">
                        AND t1.worker_name LIKE CONCAT('%', #{workerName},'%')
                    </if>
                    <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                        AND t1.confirm_time BETWEEN #{startTime} AND #{endTime}
                    </if>
                    <if test="websitId == null and websits != null and websits.size > 0">
                        AND t1.websit_id IN
                        <foreach item="item" index="index" collection="shops" open="(" separator=","
                                 close=")">
                            #{item}
                        </foreach>
                    </if>
            ) a
            GROUP BY
            1,
            2,
            3
        ) t1
        LEFT JOIN admin_websit t2 ON t1.websit_id = t2.websit_id
        GROUP BY
        t1.websit_id,
        t1.worker_mobile,
        t1.worker_id
    </select>

    <select id="workerBuyDetail"
            resultType="com.gree.mall.manager.bean.material.report.MaterialSalesSumDetailBean">
        SELECT
            t1.websit_id,
            t1.websit_name,
            t1.pay_type,
            t1.worker_mobile,
            t1.worker_id,
            t1.worker_name,
            t1.pay_time,
            t1.sales_id,
            t1.goods_id,
            t1.goods_name,
            t1.sales_qty,
            t1.price,
            t1.sale_amount,
            t1.cost,
            t1.cost_value
        FROM
        (
            SELECT
                t1.websit_id,
                t1.websit_name,
                t1.pay_type,
                t1.worker_mobile,
                t1.worker_id,
                t1.worker_name,
                t1.pay_time,
                t2.sales_id,
                t2.goods_id,
                t2.goods_name,
                t2.sales_qty,
                t2.price,
                t2.sale_amount,
                t2.cost,
                t2.cost_value
            FROM
                websit_sales t1 INNER JOIN websit_sales_item t2 ON t1.sales_id = t2.sales_id
            WHERE
                t1.company_wechat_id = #{companyWechatId}
                AND t1.buy_people = 'WORKER'
                AND t1.flag = 'PAY_TAKE'
                <if test="websitId != null and websitId != ''">
                    AND t1.websit_id = #{websitId}
                </if>
                <if test="workerMobile != null and workerMobile != ''">
                    AND t1.worker_mobile = #{workerMobile}
                </if>
                <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                    AND t1.confirm_time BETWEEN #{startTime} AND #{endTime}
                </if>
                <if test="websitId == null and websits != null and websits.size > 0">
                    AND t1.websit_id IN
                    <foreach item="item" index="index" collection="websits" open="(" separator=","
                             close=")">
                        #{item}
                    </foreach>
                </if>
        ) t1 LEFT JOIN admin_websit t2 ON t1.websit_id = t2.websit_id
        ORDER BY
        t1.sales_id
    </select>
    <select id="workerBuySumDetail"
            resultType="com.gree.mall.manager.bean.material.report.MaterialSalesSumDetailBean">
        SELECT
        t1.websit_id,
        t1.websit_name,
        t1.pay_flag,
        t1.worker_mobile,
        t1.worker_id,
        t1.worker_name,
        t1.goods_id,
        t1.goods_name,
        t1.goods_sales_convert_qty,
        t1.goods_category_name,
        SUM(t1.sales_qty) as sales_qty,
        t1.price,
        SUM(t1.sale_amount) as sale_amount,
        IFNULL(t1.cost, 0) as cost,
        IFNULL(SUM(t1.cost_value), 0) as cost_value,
        SUM(t1.convert_qty) as convert_qty
        FROM
        (
            SELECT
                t1.websit_id,
                t1.websit_name,
                t1.pay_flag,
                t1.worker_mobile,
                t1.worker_id,
                t1.worker_name,
                t2.goods_id,
                t2.goods_name,
                t2.sales_qty,
                t2.price,
                t2.sale_amount,
                t2.cost,
                t2.cost_value,
                t2.goods_sales_convert_qty,
                t2.goods_category_name,
                t2.convert_qty
            FROM
                websit_sales t1 INNER JOIN websit_sales_item t2 ON t1.sales_id = t2.sales_id
            WHERE
                t1.company_wechat_id = #{companyWechatId}
                AND t1.buy_people = 'WORKER'
                AND t1.flag = 'PAY_TAKE'
                <if test="websitId != null and websitId != ''">
                    AND t1.websit_id = #{websitId}
                </if>
                <if test="workerMobile != null and workerMobile != ''">
                    AND t1.worker_mobile = #{workerMobile}
                </if>
                <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                    AND t1.confirm_time BETWEEN #{startTime} AND #{endTime}
                </if>
                <if test="websitId == null and websits != null and websits.size > 0">
                    AND t1.websit_id IN
                    <foreach item="item" index="index" collection="websits" open="(" separator=","
                             close=")">
                        #{item}
                    </foreach>
                </if>
        ) t1
        GROUP BY
        t1.worker_mobile, t1.pay_flag, t1.goods_id
        ORDER BY
        t1.worker_mobile, t1.pay_flag DESC
    </select>

    <select id="sumBuyDetail"
            resultType="com.gree.mall.manager.bean.material.report.MaterialSalesSumDetailBean">
        SELECT
            t1.websit_id,
            t1.websit_name,
            t1.pay_flag,
            t1.goods_id,
            t1.goods_name,
            t1.goods_specification,
            t1.goods_category_name,
            t1.goods_sales_convert_qty,
            SUM(t1.sales_qty) as sales_qty,
            t1.price as price,
            SUM(t1.sale_amount) as sale_amount,
            IFNULL(t1.cost, 0) as cost,
            IFNULL(SUM(t1.cost_value), 0) as cost_value,
            SUM(t1.convert_qty) as convert_qty
        FROM
        (
            SELECT
                t1.websit_id,
                t1.websit_name,
                t1.pay_flag,
                t1.worker_mobile,
                t1.worker_id,
                t1.worker_name,
                t2.goods_id,
                t2.goods_name,
                t2.sales_qty,
                t2.price,
                t2.sale_amount,
                t2.cost,
                t2.cost_value,
                t2.goods_specification,
                t2.goods_category_name,
                t2.goods_sales_convert_qty,
                t2.convert_qty
            FROM
                websit_sales t1 INNER JOIN websit_sales_item t2 ON t1.sales_id = t2.sales_id
            WHERE
                t1.company_wechat_id = #{companyWechatId}
                AND t1.buy_people = 'WORKER'
                AND t1.flag = 'PAY_TAKE'
                <if test="websitId != null and websitId != ''">
                    AND t1.websit_id = #{websitId}
                </if>
                <if test="workerMobile != null and workerMobile != ''">
                    AND t1.worker_mobile = #{workerMobile}
                </if>
                <if test="workerId != null and workerId != ''">
                    AND t1.worker_id LIKE CONCAT('%', #{workerId},'%')
                </if>
                <if test="workerName != null and workerName != ''">
                    AND t1.worker_name LIKE CONCAT('%', #{workerName},'%')
                </if>
                <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                    AND t1.confirm_time BETWEEN #{startTime} AND #{endTime}
                </if>
                <if test="websitId == null and websits != null and websits.size > 0">
                    AND t1.websit_id IN
                    <foreach item="item" index="index" collection="websits" open="(" separator=","
                             close=")">
                        #{item}
                    </foreach>
                </if>

        ) t1
        GROUP BY
        t1.pay_flag, t1.goods_id
        ORDER BY
        t1.pay_flag DESC, t1.goods_id
    </select>

    <sql id="purchaseGoodsColSql">
        t1.websit_id,
        t1.websit_name,
        t1.vender_id,
        t1.vender_name,
        t2.goods_id,
        t2.goods_name,
        t2.goods_category_name,
        <!-- SUM(t2.purchaseQty) AS purchaseQty, -->
        SUM(t2.rec_qty) AS rec_qty,
        SUM(t2.rec_gift_qty) AS rec_gift_qty,
        SUM(t2.cost_value) AS cost_value
    </sql>

    <sql id="purchaseGoodsSumColSql">
        t1.websit_id,
        t1.websit_name,
        '' AS vender_id,
        '' AS vender_name,
        '' AS goods_id,
        '' AS goods_name,
        '合计：' AS goods_category_name,
        <!-- SUM(t2.purchaseQty) AS purchaseQty, -->
        SUM(t2.rec_qty) AS rec_qty,
        SUM(t2.rec_gift_qty) AS rec_gift_qty,
        SUM(t2.cost_value) AS cost_value
    </sql>

    <select id="purchaseGoodsSum" resultType="com.gree.mall.manager.bean.material.report.PurchaseDetailBean">
        SELECT
        <if test="!isSum">
            <include refid="purchaseGoodsColSql" />
        </if>
        <if test="isSum">
            <include refid="purchaseGoodsSumColSql" />
        </if>
        FROM
            websit_purchase t1
        INNER JOIN websit_purchase_item t2 ON t1.purchase_id = t2.purchase_id AND t1.flag = 'OK'
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            AND t1.purchase_time BETWEEN #{startTime} AND #{endTime}
        </if>
        <where>
            <if test="websitId != null and websitId != ''">
                AND t1.websit_id = #{websitId}
            </if>
            <if test="websitId == null and websits != null and websits.size > 0">
                AND t1.websit_id IN
                <foreach item="item" index="index" collection="websits" open="(" separator=","
                         close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        <if test="!isSum">
            GROUP BY
            t1.websit_id, t1.vender_id, t2.goods_id
            ORDER BY
            t1.websit_id, t1.vender_id, t2.goods_id
        </if>
        <if test="isSum">
            GROUP BY
            t1.websit_id
            ORDER BY
            t1.websit_id
        </if>
    </select>
    <select id="getMonthSummaryData"
            resultType="com.gree.mall.manager.bean.material.report.PurchaseCategoryBean">
        SELECT
            LEFT(#{startDate}, 7) AS `yearMonth`,
            t1.websit_id,
            t1.websit_name,
            ROUND(IFNULL( SUM( IF ( t1.goods_category_id = 6, t1.totalQty, 0 )), 0 ), 4) AS 'tgQty',
            ROUND(IFNULL( SUM( IF ( t1.goods_category_id = 6, t1.totalCostValue, 0 )), 0 ), 4) 'tgVal',
            ROUND(IFNULL( SUM( IF ( t1.goods_category_id = 4, t1.totalQty, 0 )), 0 ), 4) AS 'bwmQty',
            ROUND(IFNULL( SUM( IF ( t1.goods_category_id = 4, t1.totalCostValue, 0 )), 0 ), 4) 'bwmVal',
            ROUND(IFNULL( SUM( IF ( t1.goods_category_id = 18, t1.totalQty, 0 )), 0 ), 4) AS 'dlQty',
            ROUND(IFNULL( SUM( IF ( t1.goods_category_id = 18, t1.totalCostValue, 0 )), 0 ), 4) 'dlVal',
            ROUND(IFNULL( SUM( IF ( t1.goods_category_id = 13, t1.totalQty, 0 )), 0 ), 4) AS 'zjQty',
            ROUND(IFNULL( SUM( IF ( t1.goods_category_id = 13, t1.totalCostValue, 0 )), 0 ), 4) 'zjVal',
            ROUND(IFNULL( SUM( IF ( t1.goods_category_id = 11, t1.totalQty, 0 )), 0 ), 4) AS 'lmQty',
            ROUND(IFNULL( SUM( IF ( t1.goods_category_id = 11, t1.totalCostValue, 0 )), 0 ), 4) 'lmVal',
            ROUND(IFNULL( SUM( IF ( t1.goods_category_id = 22, t1.totalQty, 0 )), 0 ), 4) AS 'wjQty',
            ROUND(IFNULL( SUM( IF ( t1.goods_category_id = 22, t1.totalCostValue, 0 )), 0 ), 4) 'wjVal',
            ROUND(IFNULL( SUM( IF ( t1.goods_category_id = 2, t1.totalQty, 0 )), 0 ), 4) AS 'jtQty',
            ROUND(IFNULL( SUM( IF ( t1.goods_category_id = 2, t1.totalCostValue, 0 )), 0 ), 4) 'jtVal',
            ROUND(IFNULL( SUM( IF ( t1.goods_category_id = 29, t1.totalQty, 0 )), 0 ), 4) AS 'kgQty',
            ROUND(IFNULL( SUM( IF ( t1.goods_category_id = 29, t1.totalCostValue, 0 )), 0 ), 4) 'kgVal',
            ROUND(IFNULL( SUM( IF ( t1.goods_category_id = 30, t1.totalQty, 0 )), 0 ), 4) AS 'bzdQty',
            ROUND(IFNULL( SUM( IF ( t1.goods_category_id = 30, t1.totalCostValue, 0 )), 0 ), 4) 'bzdVal',
            ROUND(IFNULL( SUM( IF ( t1.goods_category_id = 31, t1.totalQty, 0 )), 0 ), 4) AS 'ljqQty',
            ROUND(IFNULL( SUM( IF ( t1.goods_category_id = 31, t1.totalCostValue, 0 )), 0 ), 4) 'ljqVal',
            ROUND(IFNULL( SUM( IF ( t1.goods_category_id = 32, t1.totalQty, 0 )), 0 ), 4) AS 'rsgQty',
            ROUND(IFNULL( SUM( IF ( t1.goods_category_id = 32, t1.totalCostValue, 0 )), 0 ), 4) 'rsgVal',
            ROUND(IFNULL( SUM( IF ( t1.goods_category_id = 33, t1.totalQty, 0 )), 0 ), 4) AS 'qxggQty',
            ROUND(IFNULL( SUM( IF ( t1.goods_category_id = 33, t1.totalCostValue, 0 )), 0 ), 4) 'qxggVal',
            ROUND(IFNULL( SUM( IF ( t1.goods_category_id = 35, t1.totalQty, 0 )), 0 ), 4) AS 'gcQty',
            ROUND(IFNULL( SUM( IF ( t1.goods_category_id = 35, t1.totalCostValue, 0 )), 0 ), 4) 'gcVal',
            ROUND(IFNULL( SUM( IF ( t1.goods_category_id = 37, t1.totalQty, 0 )), 0 ), 4) AS 'ysqQty',
            ROUND(IFNULL( SUM( IF ( t1.goods_category_id = 37, t1.totalCostValue, 0 )), 0 ), 4) 'ysqVal'
        FROM
        (
            SELECT
                t1.websit_id,
                t1.websit_name,
                SUM( t1.totalQty ) AS totalQty,
                SUM( t1.totalCostValue ) AS totalCostValue,
                t1.goods_category_id,
                t1.goods_category_name
            FROM
                (
                SELECT
                    t1.websit_id,
                    t1.websit_name,
                    t2.goods_category_id,
                    t2.goods_category_name,
                    t2.goods_id,
                    t2.cost,
                    IFNULL( t2.rec_qty, 0 ) + IFNULL( t2.rec_gift_qty, 0 ) AS totalQty,
                    IFNULL( t2.rec_qty, 0 ) * t2.cost AS totalCostValue
                FROM
                    websit_purchase t1
                    JOIN websit_purchase_item t2 ON t1.purchase_id = t2.purchase_id
                WHERE
                    t1.company_wechat_id = #{companyWechatId}
                    AND t1.flag = 'OK'
                    <if test="websitId != null and websitId != ''">
                        AND t1.websit_id = #{websitId}
                    </if>
                    <if test="websitId == null and websits != null and websits.size > 0">
                        AND t1.websit_id IN
                        <foreach item="item" index="index" collection="websits" open="(" separator=","
                                 close=")">
                            #{item}
                        </foreach>
                    </if>
                    AND t1.purchase_time BETWEEN #{startDate} AND #{endDate}
                UNION ALL
                SELECT
                    t1.websit_id,
                    t1.websit_name,
                    t2.goods_category_id,
                    t2.goods_category_name,
                    t2.goods_id,
                    t2.cost,
                    (IFNULL( t2.ret_qty, 0 ) * -1 ) AS totalRecQty,
                    (IFNULL( t2.ret_qty, 0 ) * t2.cost * -1 ) AS totalCostValue
                FROM
                    websit_purchase_ret t1
                    JOIN websit_purchase_ret_item t2 ON t1.purchase_ret_id = t2.purchase_ret_id
                WHERE
                    t1.company_wechat_id = #{companyWechatId}
                    AND t1.flag = 'OK'
                    <if test="websitId != null and websitId != ''">
                        AND t1.websit_id = #{websitId}
                    </if>
                    <if test="websitId == null and websits != null and websits.size > 0">
                        AND t1.websit_id IN
                        <foreach item="item" index="index" collection="websits" open="(" separator=","
                                 close=")">
                            #{item}
                        </foreach>
                    </if>
                    AND t1.ret_time BETWEEN #{startDate} AND #{endDate}
                ) t1
            GROUP BY
            t1.websit_id,
            t1.goods_category_id
        ) t1
        GROUP BY
        t1.websit_id
    </select>

    <select id="getLastMonthStockSumVal"
            resultType="com.gree.mall.manager.bean.material.report.WebsiteStockValSumBean">
        SELECT
            a.websit_id,
            ROUND(SUM(a.closeQty * IFNULL( b.cost, 0 )), 4) AS lastMonthVal
        FROM
        (
            SELECT
                a.websit_id,
                a.goods_id,
                SUM(IFNULL(a.change_qty, 0) * IF(a.direct_flag = 'ADD', 1, -1)) AS closeQty
            FROM
                websit_stock_acc a,
                websit_goods b
            <where>
                a.goods_id = b.goods_id
                AND a.company_wechat_id = #{companyWechatId}
                <if test="websitId != null and websitId != ''">
                    AND a.websit_id = #{websitId}
                </if>
                <if test="websitId == null and websits != null and websits.size > 0">
                    AND a.websit_id IN
                    <foreach item="item" index="index" collection="websits" open="(" separator=","
                             close=")">
                        #{item}
                    </foreach>
                </if>
                AND a.sdate <![CDATA[ < ]]> #{startDate}
            </where>
            GROUP BY a.websit_id, a.goods_id
        ) a
        LEFT JOIN (
        SELECT
            t1.websit_id,
            t2.goods_id,
            max( t2.id ) id,
            t2.cost
        FROM
            websit_purchase t1,
            websit_purchase_item t2
        WHERE
            t1.purchase_id = t2.purchase_id
            AND t1.company_wechat_id = #{companyWechatId}
            <if test="websitId != null and websitId != ''">
                AND t1.websit_id = #{websitId}
            </if>
            <if test="websitId == null and websits != null and websits.size > 0">
                AND t1.websit_id IN
                <foreach item="item" index="index" collection="websits" open="(" separator=","
                         close=")">
                    #{item}
                </foreach>
            </if>
            AND t1.flag = 'OK'
            AND t1.update_time <![CDATA[ < ]]>  #{startDate}
        GROUP BY
            t1.websit_id,
            t2.goods_id
        ) b ON a.websit_id = b.websit_id AND a.goods_id = b.goods_id
        GROUP BY
        a.websit_id
    </select>

    <select id="getWorkerMonthSummaryData"
            resultType="com.gree.mall.manager.bean.material.report.WorkerPurchaseCategoryBean">
        SELECT
            a.identity,
            LEFT(#{startDate}, 7) AS `yearMonth`,
            a.worker_id,
            a.worker_name,
            a.websit_id,
            a.websit_name,
            ROUND(IFNULL( SUM( IF ( a.category_id = 6, a.totalQty, 0 )), 0 ), 4)  AS 'tgQty',
            ROUND(IFNULL( SUM( IF ( a.category_id = 6, a.totalSaleVal, 0 )), 0 ), 4) AS 'tgVal',
            ROUND(IFNULL( SUM( IF ( a.category_id = 6, a.totalConvertQty, 0 )), 0 ), 4) AS 'tgConvertQty',
            ROUND(IFNULL( SUM( IF ( a.category_id = 4, a.totalQty, 0 )), 0 ), 4)  AS 'bwmQty',
            ROUND(IFNULL( SUM( IF ( a.category_id = 4, a.totalSaleVal, 0 )), 0 ), 4) AS 'bwmVal',
            ROUND(IFNULL( SUM( IF ( a.category_id = 4, a.totalConvertQty, 0 )), 0 ), 4) AS 'bwmConvertQty',
            ROUND(IFNULL( SUM( IF ( a.category_id = 18, a.totalQty, 0 )), 0 ), 4)  AS 'dlQty',
            ROUND(IFNULL( SUM( IF ( a.category_id = 18, a.totalSaleVal, 0 )), 0 ), 4) AS 'dlVal',
            ROUND(IFNULL( SUM( IF ( a.category_id = 18, a.totalConvertQty, 0 )), 0 ), 4) AS 'dlConvertQty',
            ROUND(IFNULL( SUM( IF ( a.category_id = 13, a.totalQty, 0 )), 0 ), 4)  AS 'zjQty',
            ROUND(IFNULL( SUM( IF ( a.category_id = 13, a.totalSaleVal, 0 )), 0 ), 4) AS 'zjVal',
            ROUND(IFNULL( SUM( IF ( a.category_id = 13, a.totalConvertQty, 0 )), 0 ), 4) AS 'zjConvertQty',
            ROUND(IFNULL( SUM( IF ( a.category_id = 11, a.totalQty, 0 )), 0 ), 4)  AS 'lmQty',
            ROUND(IFNULL( SUM( IF ( a.category_id = 11, a.totalSaleVal, 0 )), 0 ), 4) AS 'lmVal',
            ROUND(IFNULL( SUM( IF ( a.category_id = 11, a.totalConvertQty, 0 )), 0 ), 4) AS 'lmConvertQty',
            ROUND(IFNULL( SUM( IF ( a.category_id = 22, a.totalQty, 0 )), 0 ), 4)  AS 'wjQty',
            ROUND(IFNULL( SUM( IF ( a.category_id = 22, a.totalSaleVal, 0 )), 0 ), 4) AS 'wjVal',
            ROUND(IFNULL( SUM( IF ( a.category_id = 22, a.totalConvertQty, 0 )), 0 ), 4) AS 'wjConvertQty',
            ROUND(IFNULL( SUM( IF ( a.category_id = 2, a.totalQty, 0 )), 0 ), 4)  AS 'jtQty',
            ROUND(IFNULL( SUM( IF ( a.category_id = 2, a.totalSaleVal, 0 )), 0 ), 4) AS 'jtVal',
            ROUND(IFNULL( SUM( IF ( a.category_id = 2, a.totalConvertQty, 0 )), 0 ), 4) AS 'jtConvertQty',
            ROUND(IFNULL( SUM( IF ( a.category_id = 29, a.totalQty, 0 )), 0 ), 4) AS 'kgQty',
            ROUND(IFNULL( SUM( IF ( a.category_id = 29, a.totalSaleVal, 0 )), 0 ), 4) 'kgVal',
            ROUND(IFNULL( SUM( IF ( a.category_id = 29, a.totalConvertQty, 0 )), 0 ), 4) AS 'kgConvertQty',
            ROUND(IFNULL( SUM( IF ( a.category_id = 30, a.totalQty, 0 )), 0 ), 4) AS 'bzdQty',
            ROUND(IFNULL( SUM( IF ( a.category_id = 30, a.totalSaleVal, 0 )), 0 ), 4) 'bzdVal',
            ROUND(IFNULL( SUM( IF ( a.category_id = 30, a.totalConvertQty, 0 )), 0 ), 4) AS 'bzdConvertQty',
            ROUND(IFNULL( SUM( IF ( a.category_id = 31, a.totalQty, 0 )), 0 ), 4) AS 'ljqQty',
            ROUND(IFNULL( SUM( IF ( a.category_id = 31, a.totalSaleVal, 0 )), 0 ), 4) 'ljqVal',
            ROUND(IFNULL( SUM( IF ( a.category_id = 31, a.totalConvertQty, 0 )), 0 ), 4) AS 'ljqConvertQty',
            ROUND(IFNULL( SUM( IF ( a.category_id = 32, a.totalQty, 0 )), 0 ), 4) AS 'rsgQty',
            ROUND(IFNULL( SUM( IF ( a.category_id = 32, a.totalSaleVal, 0 )), 0 ), 4) 'rsgVal',
            ROUND(IFNULL( SUM( IF ( a.category_id = 32, a.totalConvertQty, 0 )), 0 ), 4) AS 'rsgConvertQty',
            ROUND(IFNULL( SUM( IF ( a.category_id = 33, a.totalQty, 0 )), 0 ), 4) AS 'qxggQty',
            ROUND(IFNULL( SUM( IF ( a.category_id = 33, a.totalSaleVal, 0 )), 0 ), 4) 'qxggVal',
            ROUND(IFNULL( SUM( IF ( a.category_id = 33, a.totalConvertQty, 0 )), 0 ), 4) AS 'qxggConvertQty',
            ROUND(IFNULL( SUM( IF ( a.category_id = 35, a.totalQty, 0 )), 0 ), 4) AS 'gcQty',
            ROUND(IFNULL( SUM( IF ( a.category_id = 35, a.totalSaleVal, 0 )), 0 ), 4) 'gcVal',
            ROUND(IFNULL( SUM( IF ( a.category_id = 35, a.totalConvertQty, 0 )), 0 ), 4) AS 'gcConvertQty',
            ROUND(IFNULL( SUM( IF ( a.category_id = 37, a.totalQty, 0 )), 0 ), 4) AS 'ysqQty',
            ROUND(IFNULL( SUM( IF ( a.category_id = 37, a.totalSaleVal, 0 )), 0 ), 4) 'ysqVal',
            ROUND(IFNULL( SUM( IF ( a.category_id = 37, a.totalConvertQty, 0 )), 0 ), 4) AS 'ysqConvertQty'
        FROM
            (
                SELECT
                    a.identity,
                    a.worker_id,
                    a.worker_name,
                    a.websit_id,
                    a.websit_name,
                    b.goods_category_id AS category_id,
                    SUM( a.sales_qty ) AS totalQty,
                    SUM( a.totalSaleVal ) AS totalSaleVal,
                    SUM( a.convert_qty ) as totalConvertQty
                FROM
                    (
                        SELECT
                            a.identity,
                            a.worker_id,
                            a.worker_name,
                            a.websit_id,
                            a.websit_name,
                            b.goods_id,
                            b.sales_qty,
                            b.sales_qty * b.price AS totalSaleVal,
                            b.convert_qty
                        FROM
                            websit_sales a,
                            websit_sales_item b
                        WHERE
                            a.sales_id = b.sales_id
                            AND a.company_wechat_id = #{companyWechatId}
                            AND a.flag = 'PAY_TAKE'
                            AND a.confirm_time BETWEEN #{startDate} AND #{endDate}
                        UNION ALL
                        SELECT
                            a.identity,
                            a.worker_id,
                            a.worker_name,
                            a.websit_id,
                            a.websit_name,
                            b.goods_id,
                            b.ret_qty * - 1 AS saleQty,
                            (b.ret_qty * b.price) * - 1 AS totalSaleVal,
                            (b.ret_convert_qty * - 1) AS convert_qty
                        FROM
                            websit_sales_ret a,
                            websit_sales_ret_item b
                        WHERE
                            a.sales_ret_id = b.sales_ret_id
                            AND a.company_wechat_id = #{companyWechatId}
                            AND a.confirm_by IS NOT NULL
                            AND a.confirm_time BETWEEN #{startDate} AND #{endDate}
                    ) a
                    LEFT JOIN websit_goods b ON a.goods_id = b.goods_id
                GROUP BY
                    a.worker_id,
                    a.worker_name,
                    a.websit_id,
                    a.websit_name,
                    b.goods_category_id
            ) a
        GROUP BY
            a.worker_id,
            a.websit_id
    </select>

    <select id="websitPartsCreditPage"
            resultType="com.gree.mall.manager.bean.material.manage.WebsitPartsCreditVO">
        SELECT a.* FROM (
            SELECT
                a.company_wechat_id,
                a.websit_id,
                a.websit_name,
                a.worker_id,
                a.worker_name,
                a.identity,
                a.mobile,
                SUM(a.total_amount) AS total,
                SUM(IF(a.credit_status = 1, a.total_amount, 0)) AS rec_total,
                SUM(IF(a.credit_status = 0, a.total_amount, 0)) AS credit_total,
                MAX(a.update_time) AS update_time
            FROM
                websit_parts_worker_credit_record a
            GROUP BY
                a.company_wechat_id, a.websit_id, a.worker_id
        ) 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>
        ORDER BY
        a.update_time DESC
    </select>

    <select id="websitPartsCreditItemPage"
            resultType="com.gree.mall.manager.bean.material.manage.WebsitPartsCreditItemVO">
        SELECT
            ${ex.selected}
        FROM
            websit_parts_worker_credit_record a LEFT JOIN websit_parts_sales b ON a.sales_id = b.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="queryWorkerStockQty"
            resultType="com.gree.mall.manager.bean.WorkerNormStockBean">
        SELECT
            a.norm_id,
            a.worker_goods_id,
            a.qty,
            b.goods_name,
            b.is_small,
            b.sales_unit,
            ifnull(c.qty, 0) AS stock_qty
        FROM
            websit_norm_rela a
                JOIN
            worker_goods b
            ON a.company_wechat_id = b.company_wechat_id AND a.worker_goods_id = b.goods_id
                LEFT JOIN
            worker_stock c
            ON c.worker_id = #{workerId} AND a.company_wechat_id = c.company_wechat_id AND a.worker_goods_id = c.goods_id
        WHERE
            a.norm_id = #{goodsId}
    </select>

</mapper>