<?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">

    <resultMap id="newRefundManageAppList" type="com.gree.mall.miniapp.bean.material.parts.NewRefundManageBean">
        <id column="apply_no" jdbcType="VARCHAR" property="applyNo"/>
        <result column="sales_Id" jdbcType="VARCHAR" property="salesId"/>
        <result column="apply_category" jdbcType="VARCHAR" property="applyCategory"/>
        <result column="apply_category_name" jdbcType="VARCHAR" property="applyCategoryName"/>
        <result column="apply_type" jdbcType="VARCHAR" property="applyType"/>
        <result column="apply_type_name" jdbcType="VARCHAR" property="applyTypeName"/>
        <result column="refund_mode" jdbcType="VARCHAR" property="refundMode"/>
        <result column="refund_mode_name" jdbcType="VARCHAR" property="refundModeName"/>
        <result column="express_no" jdbcType="VARCHAR" property="expressNo"/>
        <result column="receive_address" jdbcType="VARCHAR" property="receiveAddress"/>
        <result column="receive_websit_id" jdbcType="VARCHAR" property="receiveWebsitId"/>
        <result column="receive_websit_Name" jdbcType="VARCHAR" property="receiveWebsitName"/>
        <result column="receive_parts_websit_id" jdbcType="VARCHAR" property="receivePartsWebsitId"/>
        <result column="websit_Address" jdbcType="VARCHAR" property="websitAddress"/>
        <result column="identity" jdbcType="VARCHAR" property="identity"/>
        <result column="worker_id" jdbcType="VARCHAR" property="workerId"/>
        <result column="worker_name" jdbcType="VARCHAR" property="workerName"/>
        <result column="refund_amount" jdbcType="DECIMAL" property="refundAmount"/>
        <result column="refund_amount_mode" jdbcType="VARCHAR" property="refundAmountMode"/>
        <result column="remark" jdbcType="VARCHAR" property="remark"/>
        <result column="examine_remark" jdbcType="VARCHAR" property="examineRemark"/>
        <result column="flag" jdbcType="VARCHAR" property="flag"/>
        <result column="flag_name" jdbcType="VARCHAR" property="flagName"/>
        <result column="create_by" jdbcType="VARCHAR" property="createBy"/>
        <result column="create_time" jdbcType="DATE" property="createTime"/>
        <result column="update_by" jdbcType="VARCHAR" property="updateBy"/>
        <result column="update_time" jdbcType="DATE" property="updateTime"/>
        <result column="submit_by" jdbcType="VARCHAR" property="submitBy"/>
        <result column="submit_time" jdbcType="DATE" property="submitTime"/>
        <result column="examine_by" jdbcType="VARCHAR" property="examineBy"/>
        <result column="examine_time" jdbcType="DATE" property="examineTime"/>
        <result column="refunded_by" jdbcType="VARCHAR" property="refundedBy"/>
        <result column="refunded_time" jdbcType="DATE" property="refundedTime"/>
        <collection property="newRefundManageItemBeanList" ofType="com.gree.mall.miniapp.bean.material.parts.NewRefundManageItemBean"
                    column="apply_no" javaType="java.util.List"
                    select="selectNewRefundManageItemByApplyNo">
        </collection>
        <collection property="partsNewRefundManageRecordList" ofType="com.gree.mall.miniapp.bean.material.parts.NewRefundManageRecordBean"
                    column="apply_no" javaType="java.util.List"
                    select="selectNewRefundManageRecordByApplyNo">
        </collection>
    </resultMap>

    <resultMap id="newRefundManageItemResultMap" type="com.gree.mall.miniapp.bean.material.parts.NewRefundManageItemBean">
        <id column="id" jdbcType="VARCHAR" property="id"/>
        <result column="apply_no" jdbcType="VARCHAR" property="applyNo"/>
        <result column="sales_Id" jdbcType="VARCHAR" property="salesId"/>
        <result column="parts_number" jdbcType="VARCHAR" property="partsNumber"/>
        <result column="parts_name" jdbcType="VARCHAR" property="partsName"/>
        <result column="material_group_name" jdbcType="VARCHAR" property="materialGroupName"/>
        <result column="goods_stock_unit" jdbcType="VARCHAR" property="goodsStockUnit"/>
        <result column="qty" jdbcType="DECIMAL" property="qty"/>
        <result column="remark" jdbcType="VARCHAR" property="remark"/>
        <result column="image" jdbcType="VARCHAR" property="image"/>
        <result column="custom_no" jdbcType="VARCHAR" property="customNo"/>
        <result column="create_time" jdbcType="DATE" property="createTime"/>
    </resultMap>
    <resultMap id="newRefundManageRecordResultMap" type="com.gree.mall.miniapp.bean.material.parts.NewRefundManageRecordBean">
        <id column="id" jdbcType="VARCHAR" property="id"/>
        <result column="item_id" jdbcType="VARCHAR" property="itemId"/>
        <result column="apply_no" jdbcType="VARCHAR" property="applyNo"/>
        <result column="sales_Id" jdbcType="VARCHAR" property="salesId"/>
        <result column="parts_number" jdbcType="VARCHAR" property="partsNumber"/>
        <result column="parts_name" jdbcType="VARCHAR" property="partsName"/>
        <result column="sales_price" jdbcType="DECIMAL" property="salesPrice"/>
        <result column="market_price" jdbcType="DECIMAL" property="marketPrice"/>
        <result column="second_price" jdbcType="DECIMAL" property="secondPrice"/>
        <result column="qty" jdbcType="DECIMAL" property="qty"/>
        <result column="total_amount" jdbcType="DECIMAL" property="totalAmount"/>
        <result column="refund_amount" jdbcType="DECIMAL" property="refundAmount"/>
        <result column="refund_time" jdbcType="DATE" property="refundTime"/>
        <result column="refund_state" jdbcType="VARCHAR" property="refundState"/>
        <result column="refund_state_name" jdbcType="VARCHAR" property="refundStateName"/>
        <result column="refund_mch_no" jdbcType="VARCHAR" property="refundMchNo"/>
        <result column="refund_account_no" jdbcType="VARCHAR" property="refundAccountNo"/>
        <result column="refund_serialnumber" jdbcType="VARCHAR" property="refundSerialnumber"/>
        <result column="create_time" jdbcType="DATE" property="createTime"/>
    </resultMap>

    <resultMap id="oldRefundManageAppList" type="com.gree.mall.miniapp.bean.material.parts.OldRefundManageBean">
        <id column="apply_no" jdbcType="VARCHAR" property="applyNo"/>
        <result column="apply_category" jdbcType="VARCHAR" property="applyCategory"/>
        <result column="apply_category_name" jdbcType="VARCHAR" property="applyCategoryName"/>
        <result column="apply_type" jdbcType="VARCHAR" property="applyType"/>
        <result column="apply_type_name" jdbcType="VARCHAR" property="applyTypeName"/>
        <result column="refund_mode" jdbcType="VARCHAR" property="refundMode"/>
        <result column="refund_mode_name" jdbcType="VARCHAR" property="refundModeName"/>
        <result column="repair_flag" jdbcType="VARCHAR" property="repairFlag"/>
        <result column="repair_flag_name" jdbcType="VARCHAR" property="repairFlagName"/>
        <result column="express_no" jdbcType="VARCHAR" property="expressNo"/>
        <result column="receive_address" jdbcType="VARCHAR" property="receiveAddress"/>
        <result column="receive_websit_id" jdbcType="VARCHAR" property="receiveWebsitId"/>
        <result column="receive_websit_name" jdbcType="VARCHAR" property="receiveWebsitName"/>
        <result column="receiveParts_websit_id" jdbcType="VARCHAR" property="receivePartsWebsitId"/>
        <result column="websit_address" jdbcType="VARCHAR" property="websitAddress"/>
        <result column="identity" jdbcType="VARCHAR" property="identity"/>
        <result column="worker_id" jdbcType="VARCHAR" property="workerId"/>
        <result column="worker_name" jdbcType="VARCHAR" property="workerName"/>
        <result column="work_order_no" jdbcType="VARCHAR" property="workOrderNo"/>
        <result column="pg_id" jdbcType="VARCHAR" property="pgId"/>
        <result column="refund_amount" jdbcType="DECIMAL" property="refundAmount"/>
        <result column="refund_amount_mode" jdbcType="VARCHAR" property="refundAmountMode"/>
        <result column="settlement_state" jdbcType="BOOLEAN" property="settlementState"/>
        <result column="settlement_state_name" jdbcType="VARCHAR" property="settlementStateName"/>
        <result column="remark" jdbcType="VARCHAR" property="remark"/>
        <result column="examine_remark" jdbcType="VARCHAR" property="examineRemark"/>
        <result column="flag" jdbcType="VARCHAR" property="flag"/>
        <result column="flag_name" jdbcType="VARCHAR" property="flagName"/>
        <result column="create_by" jdbcType="VARCHAR" property="createBy"/>
        <result column="create_time" jdbcType="DATE" property="createTime"/>
        <result column="update_by" jdbcType="VARCHAR" property="updateBy"/>
        <result column="update_time" jdbcType="DATE" property="updateTime"/>
        <result column="submit_by" jdbcType="VARCHAR" property="submitBy"/>
        <result column="submit_time" jdbcType="DATE" property="submitTime"/>
        <result column="examine_by" jdbcType="VARCHAR" property="examineBy"/>
        <result column="examine_time" jdbcType="DATE" property="examineTime"/>
        <result column="refunded_by" jdbcType="VARCHAR" property="refundedBy"/>
        <result column="refunded_time" jdbcType="DATE" property="refundedTime"/>
        <collection property="oldRefundManageItemBeanList" ofType="com.gree.mall.miniapp.bean.material.parts.OldRefundManageItemBean"
                    column="apply_no"
                    select="selectOldRefundManageItemByApplyNo">
        </collection>
        <collection property="partsOldRefundManageRecordList" ofType="com.gree.mall.miniapp.bean.material.parts.OldRefundManageRecordBean"
                    column="apply_no"
                    select="selectOldRefundManageRecordByApplyNo">
        </collection>
    </resultMap>
    <resultMap id="oldRefundManageItemResultMap" type="com.gree.mall.miniapp.bean.material.parts.OldRefundManageItemBean">
        <id column="id" jdbcType="VARCHAR" property="id"/>
        <result column="apply_no" jdbcType="VARCHAR" property="applyNo"/>
        <result column="old_parts_number" jdbcType="VARCHAR" property="oldPartsNumber"/>
        <result column="old_parts_name" jdbcType="VARCHAR" property="oldPartsName"/>
        <result column="qty" jdbcType="DECIMAL" property="qty"/>
        <result column="old_image" jdbcType="VARCHAR" property="oldImage"/>
        <result column="tab" jdbcType="VARCHAR" property="tab"/>
        <result column="new_parts_number" jdbcType="VARCHAR" property="newPartsNumber"/>
        <result column="new_parts_name" jdbcType="VARCHAR" property="newPartsName"/>
        <result column="newMaterialGroupName" jdbcType="VARCHAR" property="newMaterialGroupName"/>
        <result column="new_goods_stock_unit" jdbcType="VARCHAR" property="newGoodsStockUnit"/>
        <result column="repair_record_number" jdbcType="VARCHAR" property="repairRecordNumber"/>
        <result column="machine_barcode" jdbcType="VARCHAR" property="machineBarcode"/>
        <result column="repair_time" jdbcType="DATE" property="repairTime"/>
        <result column="old_compress_number" jdbcType="VARCHAR" property="oldCompressNumber"/>
        <result column="new_compress_number" jdbcType="VARCHAR" property="newCompressNumber"/>
        <result column="parts_barcode" jdbcType="VARCHAR" property="partsBarcode"/>
        <result column="install_time" jdbcType="DATE" property="installTime"/>
        <result column="custom_no" jdbcType="VARCHAR" property="customNo"/>
        <result column="is_backup" jdbcType="BOOLEAN" property="isBackup"/>
        <result column="is_backup_name" jdbcType="VARCHAR" property="isBackupName"/>
        <result column="remark" jdbcType="VARCHAR" property="remark"/>
        <result column="create_time" jdbcType="DATE" property="createTime"/>
    </resultMap>
    <resultMap id="oldRefundManageRecordResultMap" type="com.gree.mall.miniapp.bean.material.parts.OldRefundManageRecordBean">
        <id column="id" jdbcType="VARCHAR" property="id"/>
        <result column="item_id" jdbcType="VARCHAR" property="itemId"/>
        <result column="apply_no" jdbcType="VARCHAR" property="applyNo"/>
        <result column="sales_id" jdbcType="VARCHAR" property="salesId"/>
        <result column="sales_item_id" jdbcType="VARCHAR" property="salesItemId"/>
        <result column="parts_number" jdbcType="VARCHAR" property="partsNumber"/>
        <result column="parts_name" jdbcType="VARCHAR" property="partsName"/>
        <result column="sales_price" jdbcType="DECIMAL" property="salesPrice"/>
        <result column="market_price" jdbcType="DECIMAL" property="marketPrice"/>
        <result column="second_price" jdbcType="DECIMAL" property="secondPrice"/>
        <result column="qty" jdbcType="DECIMAL" property="qty"/>
        <result column="total_amount" jdbcType="DECIMAL" property="totalAmount"/>
        <result column="refund_amount" jdbcType="DECIMAL" property="refundAmount"/>
        <result column="refund_time" jdbcType="DATE" property="refundTime"/>
        <result column="refund_state" jdbcType="VARCHAR" property="refundState"/>
        <result column="refund_state_name" jdbcType="VARCHAR" property="refundStateName"/>
        <result column="refund_mch_no" jdbcType="VARCHAR" property="refundMchNo"/>
        <result column="refund_account_no" jdbcType="VARCHAR" property="refundAccountNo"/>
        <result column="refund_serialnumber" jdbcType="VARCHAR" property="refundSerialnumber"/>
        <result column="repair_flag" jdbcType="VARCHAR" property="repairFlag"/>
        <result column="repair_flag_name" jdbcType="VARCHAR" property="repairFlagName"/>
        <result column="work_order_no" jdbcType="VARCHAR" property="workOrderNo"/>
        <result column="pg_id" jdbcType="VARCHAR" property="pgId"/>
        <result column="customer_name" jdbcType="VARCHAR" property="customerName"/>
        <result column="customer_tel" jdbcType="VARCHAR" property="customerTel"/>
        <result column="is_backup" jdbcType="BOOLEAN" property="isBackup"/>
        <result column="is_backup_name" jdbcType="VARCHAR" property="isBackupName"/>
        <result column="create_time" jdbcType="DATE" property="createTime"/>
    </resultMap>

    <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 ('SAVE', '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 = #{workerId}
        <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 = #{workerId}
        <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,
            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>

    <select id="appList" resultMap="newRefundManageAppList">
        SELECT
            a.company_wechat_id,
            a.company_wechat_name,
            a.apply_no,
            a.sales_id,
            a.apply_category,
            CASE a.apply_category WHEN 'HOME' THEN '家用空调'
            WHEN 'TRADE' THEN '商用空调'
            WHEN 'ELEC' THEN '生活电器（小家电）'
            ELSE '' END AS apply_category_name,
            a.apply_type,
            CASE a.apply_type WHEN 'NEW' THEN '新件返还'
            WHEN 'LOST' THEN '破损返还'
            WHEN 'BUG' THEN '故障返还'
            ELSE '' END AS apply_type_name,
            a.refund_mode,
            CASE a.refund_mode WHEN 'EXPRESS' THEN '快递'
            WHEN 'SELF' THEN '网点自还'
            ELSE '' END AS refund_mode_name,
            a.express_no,
            a.receive_address,
            a.receive_websit_id,
            a.receive_websit_name,
            a.receive_parts_websit_id,
            a.websit_Address,
            a.identity,
            a.worker_id,
            a.worker_name,
            a.refund_amount,
            a.refund_amount_mode,
            IF(refund_amount_mode = 'CASH', '现金', '微信') AS refundAmount_mode_name,
            a.remark,
            a.examine_remark,
            a.flag,
            CASE a.flag WHEN 'SAVE' THEN '保存'
            WHEN 'SUBMIT' THEN '待审批'
            WHEN 'AGREE' THEN '待返还'
            WHEN 'REJECT' THEN '驳回'
            WHEN 'REFUNDED' THEN '已返还'
            ELSE '' END AS flag_name,
            a.create_by,
            a.create_time,
            a.update_by,
            a.update_time,
            a.submit_by,
            a.submit_time,
            a.examine_by,
            a.examine_time,
            a.refunded_by,
            a.refunded_time
        FROM
            websit_parts_new_refund_manage a
            LEFT JOIN websit_parts_new_refund_manage_item b ON a.apply_no = b.apply_no
        <where>
            a.company_wechat_id = #{companyWechatId}
            <if test="applyNo != null and applyNo != ''">
                AND a.apply_no = #{applyNo}
            </if>
            <if test="flag != null and flag != ''">
                AND a.flag = #{flag}
            </if>
            <if test="identity != null and identity != ''">
                AND a.identity = #{identity}
            </if>
            <if test="partsNumber != null and partsNumber != ''">
                AND b.parts_number = #{partsNumber}
            </if>
        </where>
        GROUP BY a.apply_no, a.create_time
        ORDER BY a.create_time DESC
    </select>

    <select id="querySalesPushFlagItem" resultType="com.gree.mall.miniapp.plus.entity.WebsitPartsSalesItem">
        SELECT
            id,
            identity,
            websit_id,
            parts_websit_id,
            parts_id,
            parts_number,
            parts_name,
            material_group_name,
            goods_stock_unit,
            sales_id,
            sales_price,
            market_price,
            second_price,
            qty,
            examine_qty,
            new_refund_qty,
            old_refund_qty,
            total_amount,
            refund_amount_mode,
            push_flag,
            create_by,
            create_time,
            update_by,
            update_time,
            examine_time
        FROM
            websit_parts_sales_item
        WHERE
        identity = #{identity}
        AND websit_id = #{websitId}
        AND parts_websit_id = #{partsWebsitId}
        <if test="pushFlag != null and pushFlag != ''">
            AND push_flag = #{pushFlag}
        </if>
        <if test="salesId != null and salesId != ''">
            AND sales_id = #{salesId}
        </if>
        AND parts_number IN
        <foreach item="item" index="index" collection="records" open="(" separator=","
                 close=")">
            #{item.partsNumber}
        </foreach>
        ORDER BY
            examine_time
        FOR UPDATE
    </select>

    <select id="appSalesList" resultType="com.gree.mall.miniapp.bean.material.parts.PartsSalesOrderBean">
        SELECT
            a.*,
            CASE a.order_source WHEN 'SELF' THEN '自建订单'
            WHEN 'ONLINE' THEN '在线订单'
            ELSE '' END AS buyTypeName,
            CASE a.status WHEN 'SAVE' THEN '保存未提交'
            WHEN 'SUBMIT' THEN '已提交'
            <!--                       WHEN '2' THEN '已支付' -->
            WHEN 'PAYED' THEN
            <!--                       WHEN '3' THEN '已提货' -->
            (case when a.worker_confirm_date is not null and a.websit_confirm_date is not null then '已提货'
            when a.worker_confirm_date is not null then '师傅已提货'
            when a.websit_confirm_date is not null then '网点已提货' else '已支付' end)
            WHEN 'END' THEN '已完成'
            WHEN 'CANCEL' THEN '已取消'
            ELSE '' END AS stateName,
            CASE a.pushFlag WHEN 'SALES' THEN '销售'
            WHEN 'TRANS' THEN '转销售'
            WHEN 'END' THEN '完结'
            ELSE '' END AS pushFlagName
        FROM
            websit_parts_sales a, websit_parts_sales_item b
        WHERE
            a.company_wechat_id = #{companyWechatId}
            AND a.id = b.sales_id
            AND a.del = 0
            AND a.identity = #{identity}
            <if test="salesId != null and salesId != ''">
                AND a.id = #{salesId}
            </if>
            <if test="status != null and status != ''">
                <!--             	and 1 = 0 -->
                <if test="status == 'SAVE'.toString() or status == 'SUBMIT'.toString() or status == 'END'.toString() or status == 'CANCEL'.toString()">
                    AND a.status = #{status}
                </if>
                <if test="status == 'PAYED'.toString()">
                    AND a.status = 'PAYED' AND (a.worker_confirm_date is null and a.websit_confirm_date is null)
                </if>
            </if>

            <if test="pushFlag != null and pushFlag != ''">
                AND a.pushFlag = #{pushFlag}
                AND b.qty > 0
            </if>
            <if test="partsNumber != null and partsNumber != ''">
                AND b.parts_number = #{partsNumber}
            </if>
        GROUP BY
        a.identity, a.id, a.create_time
        ORDER BY
        a.create_time DESC
    </select>
    <select id="selectItemBySalesOrderId"
            resultType="com.gree.mall.miniapp.bean.material.parts.PartsSalesOrderItemBean">
        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.miniapp.bean.material.parts.OldRefundManageRecordBean">
        SELECT
            a.*
        FROM
            websit_parts_old_refund_manage_record a
        WHERE
            sales_id = #{id}
    </select>

    <select id="websitGroupList" resultType="java.lang.String">
        SELECT
            material_group_name
        FROM
            websit_stock
        WHERE
            company_wechat_id = #{companyWechatId}
            AND websit_id = #{websitId}
            AND parts_websit_id = #{partsWebsitId}
            AND qty > 0
        GROUP BY
            material_group_name
    </select>

    <select id="existWebsitStockList"
            resultType="com.gree.mall.miniapp.bean.material.parts.WebsitPartsStockBean">
        SELECT
            a.id,
            a.company_wechat_id,
            a.company_wechat_name,
            a.websit_id,
            a.websit_name,
            a.parts_websit_id,
            a.goods_id,
            a.goods_name,
            a.goods_id AS parts_number,
            a.goods_name AS parts_name,
            b.material_group_name,
            b.part_type,
            b.market_price,
            b.goods_stock_unit,
            a.qty,
            a.old_qty
        FROM
            websit_stock a LEFT JOIN websit_goods b ON a.company_wechat_id = b.company_wechat_id AND a.goods_id = b.goods_code
        WHERE
            a.company_wechat_id = #{companyWechatId}
            AND a.websit_id = #{websitId}
            AND a.parts_websit_id = #{partsWebsitId}
            <if test="partsNumber != null and partsNumber != ''">
                AND a.goods_id = #{partsNumber}
            </if>
            <if test="partsName != null and partsName != ''">
                AND a.goods_name LIKE CONCAT('%', #{partsName}, '%')
            </if>
            <if test="materialGroupName != null and materialGroupName != ''">
                AND a.material_group_name = #{materialGroupName}
            </if>
            AND a.goods_type = 'P'
            AND a.qty > 0

    </select>

    <select id="existWebsitList" resultType="com.gree.mall.miniapp.plus.entity.AdminWebsit">
        SELECT
            websit_id, websit_name AS name, parts_websit_id
        FROM
            worker_stock
        WHERE
            company_wechat_id = #{companyWechatId}
            AND identity = #{identity}
            AND goods_type = 'P'
        GROUP BY
            websit_id, parts_websit_id
    </select>

    <select id="existWebsitGroupList" resultType="java.lang.String">
        SELECT
            material_group_name
        FROM
            worker_stock
        WHERE
            company_wechat_id = #{companyWechatId}
            AND identity = #{identity}
            AND websit_id = #{websitId}
            AND parts_websit_id = #{partsWebsitId}
            AND goods_type = 'P'
        <choose>
            <when test="partsAttr != null and partsAttr == 'NEW'">
                AND qty > 0
            </when>
            <when test="partsAttr != null and partsAttr == 'OLD'">
                AND old_qty > 0
            </when>
            <when test="partsAttr != null and partsAttr == 'WAY'">
                AND way_qty > 0
            </when>
            <otherwise>
                AND id  = 0
            </otherwise>
        </choose>
        GROUP BY
            material_group_name
    </select>

    <select id="existWorkerStockList"
            resultType="com.gree.mall.miniapp.bean.material.parts.PartsWorkerStockBean">
        SELECT
            a.*,
            b.market_price
        FROM
            worker_stock a LEFT JOIN websit_goods b ON a.company_wechat_id = b.company_wechat_id AND a.goods_id = b.goods_code
        WHERE
            a.company_wechat_id = #{companyWechatId}
            AND a.goods_type = 'P'
            AND a.identity = #{identity}
            AND a.websit_id = #{websitId}
            AND a.parts_websit_id = #{partsWebsitId}
            <if test="partsNumber != null and partsNumber != ''">
                AND a.goods_id = #{partsNumber}
            </if>
            <if test="partsName != null and partsName != ''">
                AND a.goods_name LIKE CONCAT('%',#{partsName},'%')
            </if>
            <if test="materialGroupName != null and materialGroupName != ''">
                AND a.material_group_name = #{materialGroupName}
            </if>
            <if test="productRelaId != null and productRelaId != ''">
                AND b.product_rela_id LIKE CONCAT('%',#{productRelaId},'%')
            </if>
            <choose>
                <when test="partsAttr != null and partsAttr == 'NEW'">
                    AND a.qty > 0
                </when>
                <when test="partsAttr != null and partsAttr == 'OLD'">
                    AND a.old_qty > 0
                </when>
                <when test="partsAttr != null and partsAttr == 'WAY'">
                    AND a.way_qty > 0
                </when>
                <otherwise>
                    AND a.id  = 0
                </otherwise>
            </choose>
    </select>

    <select id="querySalesEndTimeByIdentity"
            resultType="com.gree.mall.miniapp.plus.entity.WebsitPartsSalesItem">
        SELECT
            b.parts_number,
            b.qty,
            a.end_time AS examineTime
        FROM
            websit_parts_sales a, websit_parts_sales_item b
        WHERE
            a.id = b.sales_id
            AND a.company_wechat_id = #{companyWechatId}
            AND a.pushFlag = 'SALES'
            AND a.identity = #{identity}
            AND a.websit_id = #{websitId}
            AND a.parts_websit_id = #{partsWebsitId}
            AND b.qty > 0
        GROUP BY b.parts_number
    </select>

    <select id="workerStockStatistics"
            resultType="com.gree.mall.miniapp.bean.material.parts.WorkerStockCensusBean">
        SELECT t.worker_id, t.identity,
        SUM(IFNULL(qty,0)) as newQty,
        SUM(IFNULL(old_qty,0)) as oldQty
        FROM  worker_stock t
        WHERE identity = #{identity}
          AND goods_type = 'P'
        <if test="websitId != null and websitId != ''">
            AND t.websit_id = #{websitId}
        </if>
        GROUP BY t.identity
    </select>

    <select id="workerNewStockStatistics"
            resultType="com.gree.mall.miniapp.bean.material.parts.WorkerStockCensusBean">
        SELECT
            SUM(
            IFNULL( b.qty, 0 )) AS newRefundQty,
            SUM(
            IFNULL( b.total_amount, 0 )) AS newRefundAmount
        FROM
            websit_parts_new_refund_manage a
        INNER JOIN websit_parts_new_refund_manage_record b ON a.apply_no = b.apply_no
        WHERE
            a.flag IN ( 'SAVE', 'SUBMIT', 'AGREE' )
            AND a.identity = #{identity}
            <if test="websitId != null and websitId != ''">
                AND a.receive_websit_id = #{websitId}
            </if>
        GROUP BY
        a.identity
    </select>

    <select id="workerOldStockStatistics"
            resultType="com.gree.mall.miniapp.bean.material.parts.WorkerStockCensusBean">
        SELECT
            SUM(
            IFNULL( b.qty, 0 )) AS oldRefundQty,
            SUM(
            IFNULL( b.total_amount, 0 )) AS oldRefundAmount
        FROM
        websit_parts_old_refund_manage a
        INNER JOIN websit_parts_old_refund_manage_record b ON a.apply_no = b.apply_no
        WHERE
            a.flag IN ( 'SAVE', 'SUBMIT' )
            AND a.identity = #{identity}
            <if test="websitId != null and websitId != ''">
                AND a.receive_websit_id = #{websitId}
            </if>
        GROUP BY
        a.identity
    </select>

    <select id="appOldRefundList" resultMap="oldRefundManageAppList">
        SELECT
            a.company_wechat_id,
            a.company_wechat_name,
            a.apply_no,
            a.apply_category,
            CASE a.apply_category WHEN 'HOME' THEN '家用空调'
            WHEN 'TRADE' THEN '商用空调'
            WHEN 'ELEC' THEN '生活电器（小家电）'
            ELSE '' END AS apply_category_name,
            a.apply_type,
            CASE a.apply_type WHEN 'NEW' THEN '新件返还'
            WHEN 'LOST' THEN '破损返还'
            WHEN 'BUG' THEN '故障返还'
            ELSE '' END AS apply_type_name,
            a.refund_mode,
            CASE a.refund_mode WHEN 'EXPRESS' THEN '快递'
            WHEN 'SELF' THEN '网点自还'
            ELSE '' END AS refund_mode_name,
            a.repair_flag,
            CASE a.repair_flag WHEN 'INNER' THEN '保内'
            WHEN 'OUTSIDE' THEN '保外'
            ELSE '' END AS repair_flag_name,
            a.express_no,
            a.receive_address,
            a.receive_websit_id,
            a.receive_websit_name,
            a.receive_parts_websit_id,
            a.websit_Address,
            a.identity,
            a.worker_id,
            a.worker_name,
            a.work_order_no,
            a.pg_id,
            a.customer_name,
            a.customer_tel,
            a.refund_amount,
            a.refund_amount_mode,
            a.settlement_state,
            CASE a.settlement_state WHEN 0 THEN '未结算'
            WHEN 1 THEN '已结算'
            ELSE '' END AS settlement_state_name,
            a.remark,
            a.examine_remark,
            a.flag,
            CASE  WHEN a.flag = 'SAVE' THEN '保存'
            WHEN a.flag = 'SUBMIT' THEN '待审批'
            WHEN a.flag = 'AGREE' and a.repair_flag = 'INNER' THEN '待返还'
            WHEN a.flag = 'AGREE' and a.repair_flag = 'OUTSIDE' THEN '通过'
            WHEN a.flag = 'REJECT' THEN '驳回'
            WHEN a.flag = 'REFUNDED' THEN '已返还'
            WHEN a.flag = 'REVIEWED' THEN '已复核'
            ELSE a.flag = '' END AS flag_name,
            a.create_by,
            a.create_time,
            a.update_by,
            a.update_time,
            a.submit_by,
            a.submit_time,
            a.examine_by,
            a.examine_time,
            a.refunded_by,
            a.refunded_time
        FROM
            websit_parts_old_refund_manage a
            LEFT JOIN websit_parts_old_refund_manage_item b ON a.apply_no = b.apply_no
        <where>
            a.company_wechat_id = #{companyWechatId}
            <if test="applyNo != null and applyNo != ''">
                AND a.apply_no = #{applyNo}
            </if>
            <if test="flag != null and flag != ''">
                AND a.flag = #{flag}
            </if>
            <if test="identity != null and identity != ''">
                AND a.identity = #{identity}
            </if>
            <if test="partsNumber != null and partsNumber != ''">
                AND b.new_parts_number = #{partsNumber}
            </if>
            <if test="repairFlag != null and repairFlag != ''">
                AND a.repair_flag = #{repairFlag}
            </if>
        </where>
        GROUP BY a.apply_no, a.create_time
        ORDER BY a.create_time DESC
    </select>

    <select id="selectOldRefundManageItemByApplyNo" resultMap="oldRefundManageItemResultMap">
        SELECT
            id,
            apply_no,
            old_parts_number,
            old_parts_name,
            old_material_group_name,
            old_unit_name,
            qty,
            old_image,
            tab,
            new_parts_number,
            new_parts_name,
            new_material_group_name,
            new_unit_name,
            work_old_parts_number,
            work_old_parts_name,
            repair_record_number,
            machine_barcode,
            repair_time,
            old_compress_number,
            new_compress_number,
            parts_barcode,
            install_time,
            custom_no,
            is_backup,
            remark,
            create_time,
            CASE is_backup WHEN 0 THEN '否'
                          WHEN 1 THEN '是'
                          ELSE '' END AS isBackupName
        FROM
            websit_parts_old_refund_manage_item
        WHERE
            apply_no = #{applyNo}
    </select>

    <select id="selectOldRefundManageRecordByApplyNo" resultMap="oldRefundManageRecordResultMap">
        SELECT
            id,
            item_id,
            apply_no,
            sales_id,
            sales_item_id,
            parts_number,
            parts_name,
            sales_price,
            market_price,
            second_price,
            qty,
            total_amount,
            refund_amount_mode,
            refund_amount,
            refund_time,
            refund_state,
            refund_mch_no,
            refund_account_no,
            refund_serialnumber,
            repair_flag,
            work_order_no,
            pg_id,
            customer_name,
            customer_tel,
            is_backup,
            create_time
        FROM
        websit_parts_old_refund_manage_record
        WHERE
        apply_no = #{applyNo}
    </select>

    <select id="queryWebsitList" resultType="com.gree.mall.miniapp.bean.material.parts.WebsitExtends">
        SELECT
        a.*,
        <!-- 按距离排序计算 -->
        TRUNCATE(ACOS(
        SIN((PI() / 180) * lat) * SIN((PI() / 180) * #{latitude})
        + COS((PI() / 180) * lat) * COS((PI() / 180) * #{latitude}) * COS((PI() / 180) * #{longitude} - (PI() / 180) * lng)
        ) * 6371,0) as distance

        from admin_websit a
        <where>
            a.company_wechat_id = #{companyWechatId}
            AND a.type = 'C'
            AND a.parts_websit_id IS NOT NULL
            AND a.status = 1
            <if test="websitId != null and websitId != ''">
                AND a.websit_id = #{websitId}
            </if>
            HAVING distance <![CDATA[ < ]]> 50
        </where>
        order by distance asc, websit_id

    </select>

    <select id="selectNewRefundManageItemByApplyNo" resultMap="newRefundManageItemResultMap">
        SELECT
            id,
            apply_no,
            sales_id,
            parts_number,
            parts_name,
            material_group_name,
            goods_stock_unit,
            qty,
            remark,
            image,
            custom_no,
            create_time
        FROM
            websit_parts_new_refund_manage_item
        WHERE
            apply_no = #{applyNo}
    </select>

    <select id="selectNewRefundManageRecordByApplyNo" resultMap="newRefundManageRecordResultMap">
        SELECT
            id,
            item_id,
            apply_no,
            sales_id,
            sales_item_id,
            parts_number,
            parts_name,
            sales_price,
            market_price,
            second_price,
            qty,
            total_amount,
            refund_amount,
            refund_time,
            refund_state,
            CASE refund_state WHEN 'OK' THEN '成功'
                             WHEN 'FAIL' THEN '失败'
                             WHEN 'WAIT' THEN '等待'
                             ELSE '' END AS refundStateName,
            refund_mch_no,
            refund_account_no,
            refund_serialnumber,
            create_time
        FROM
            websit_parts_new_refund_manage_record
        WHERE
            apply_no = #{applyNo}
    </select>
</mapper>