<?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.miniapp.commonmapper.MaterialMapper">

    <select id="buyList" resultType="com.gree.mall.miniapp.bean.material.SalesOrderBean">
        SELECT
            a.*
        FROM
            websit_sales a
        <where>
            a.company_wechat_id = #{companyWechatId}
            AND worker_id = #{userId}
            <if test="flag!=null and flag!=''">
                AND a.flag = #{flag}
            </if>
            <if test="flag==null or flag==''">
                AND a.flag IN ('SUBMIT', 'PAY_NOT_TAKE', 'PAY_TAKE')
            </if>
        </where>
        ORDER BY a.create_time DESC
    </select>

    <select id="existStockCategory" resultType="com.gree.mall.miniapp.bean.material.WebsitSalesCategoryVO">
        SELECT
            c.category_id,
            c.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.miniapp.bean.material.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,
        b.convert_bit_scale,
        b.goods_sales_convert_qty,
        b.manage_worker_stock,
        a.qty
        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.websit_id = c.websit_id AND a.goods_id = c.goods_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>
        AND a.websit_id = #{websitId}
        AND a.goods_type = #{goodsType}
        AND a.qty > 0
        GROUP BY a.goods_id
        LIMIT 100
    </select>

    <select id="notStockGoods" resultType="com.gree.mall.miniapp.bean.material.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.status = 'ON'
        AND b.norm_type = 'M'
    </select>

    <select id="normRecordList" resultType="com.gree.mall.miniapp.bean.material.NormRecordVO">
        SELECT
            a.*
        FROM
            websit_norm_record a
        WHERE
            a.company_wechat_id = #{companyWechatId}
            AND a.worker_id = #{userId}
        <if test="orderId!=null and orderId!=''">
            AND a.order_id LIKE CONCAT ('%', #{orderId}, '%')
        </if>
        <if test="goodsType!=null and goodsType!=''">
            AND a.goods_type = #{goodsType}
        </if>
        <if test="startTime!=null and startTime!=''">
            AND a.use_time BETWEEN #{startTime} AND #{endTime}
        </if>
        ORDER BY a.use_time DESC
    </select>

    <select id="partsRefundList" resultType="com.gree.mall.miniapp.bean.material.PartsRefundVO">
        SELECT
        a.*
        FROM
        websit_parts_ret a
        WHERE
        a.company_wechat_id = #{companyWechatId}
        AND a.worker_id = #{userId}
        <if test="type!=null and type!=''">
            AND a.type = #{type}
        </if>
        <if test="status!=null and status!=''">
            AND a.flag = #{status}
        </if>
    </select>

    <select id="queryShoppingCartList" resultType="com.gree.mall.miniapp.bean.material.WebsitShoppingCartList">
        SELECT
            a.*
        FROM
             websit_shopping_cart a
        WHERE
              a.worker_id = #{workerId}
          AND a.websit_id = #{websitId}
          AND a.goods_type = #{goodsType}
    </select>
    <select id="queryWorkerStockQty"
            resultType="com.gree.mall.miniapp.bean.material.stock.WorkerNormStockBean">
        SELECT
            a.norm_id,
            a.worker_goods_id,
            a.qty,
            b.goods_name,
            b.is_small,
            b.sales_unit,
            c.qty 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>