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

    <select id="queryChargingStandrdList" resultType="com.gree.mall.miniapp.bean.chargingstandard.ChargingStandardBean">
        select
            a.* ,
            b.name as 'typeName',
          c.brand_name,
            (select name from goods_category where type=2 and category_id = a.one_category_id) as 'oneCategoryName',
            (select name from goods_category where type=2 and category_id = a.two_category_id) as 'twoCategoryName'
        from charging_standard a
        left join charging_standard_type b on a.charging_standard_type_id = b.id and b.status=1
        join brand c on c.id = a.brand_id
        where a.status=1  and c.status=1
        <if test="id != null and id !=''">
            and a.id = #{id}
        </if>
        <if test="type != null">
            and a.type = #{type}
        </if>
        <if test="brandId != null and brandId !=''">
            and a.brand_id = #{brandId}
        </if>
        <if test="chargingStandardTypeId != null and chargingStandardTypeId != ''">
            and a.charging_standard_type_id = #{chargingStandardTypeId}
        </if>
        <if test="twoCategoryId != null and twoCategoryId !=''">
            and a.two_category_id = #{twoCategoryId}
        </if>
        <if test="companyWechatId != null and companyWechatId !=''">
            and a.company_wechat_id = #{companyWechatId}
        </if>
        group by concat(a.brand_id,a.one_category_id,a.two_category_id,content1)
        order by a.create_time desc
    </select>


    <select id="queryChargingStandrdDetailList" resultType="com.gree.mall.miniapp.bean.chargingstandard.ChargingStandardBean">
        select
            a.* ,
            b.name as 'typeName',
            c.brand_name,
            (select name from goods_category where type=2 and category_id = a.one_category_id) as 'oneCategoryName',
            (select name from goods_category where type=2 and category_id = a.two_category_id) as 'twoCategoryName'
        from charging_standard a
        left join charging_standard_type b on a.charging_standard_type_id = b.id and b.status=1
        join brand c on c.id = a.brand_id
        where a.status=1  and c.status=1
        <if test="type != null">
            and a.type = #{type}
        </if>
        <if test="brandId != null and brandId !=''">
            and a.brand_id = #{brandId}
        </if>
        <if test="chargingStandardTypeId != null and chargingStandardTypeId != ''">
            and a.charging_standard_type_id = #{chargingStandardTypeId}
        </if>
        <if test="twoCategoryId != null and twoCategoryId !=''">
            and a.two_category_id = #{twoCategoryId}
        </if>
        <if test="content1 != null and content1 !=''">
            and a.content1 = #{content1}
        </if>
        order by a.create_time desc
    </select>


    <select id="websitList" resultType="com.gree.mall.miniapp.bean.websit.WebsitVO">
        SELECT
            a.*,
            ST_DISTANCE_SPHERE(
                POINT(#{lng}, #{lat}),
                POINT(lng, lat)
            ) AS distance
        FROM
            admin_websit  a
            where a.type='C'
            <if test="isIncre != null">
                and a.is_incre = #{isIncre}
            </if>
            <if test="websitIds != null and websitIds.size > 0">
                AND a.websit_id IN
                <foreach item="item" index="index" collection="websitIds" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            and a.company_wechat_id = #{companyWechatId}
        ORDER BY distance
    </select>

    <select id="queryUserSettle" resultType="com.gree.mall.miniapp.bean.user.SettleMonthWagesWorkerBean">
        SELECT
            a.*,
            b.send_date
        FROM
            settle_month_wages_worker a LEFT JOIN settle_month_wages_detail b ON a.settle_month_wages_detail_id = b.id
        WHERE
            a.company_wechat_id = #{companyWechatId}
            AND a.worker_name = #{userId}
        ORDER BY b.send_date DESC
    </select>

    <select id="goodsLeaseCategoryList" resultType="com.gree.mall.miniapp.plus.entity.GoodsCategory">
        SELECT * FROM (
        SELECT
            a.*
        FROM
            goods_category a JOIN goods_lease b ON a.category_id = b.category_id
        WHERE

            a.company_wechat_id = #{companyWechatId}
            AND b.del = 0
            AND b.status = 1
            AND a.type = 2
            <if test="categoryName != null and categoryName != ''">
                AND a.name LIKE  CONCAT('%', #{categoryName},'%')
            </if>
        GROUP BY a.category_id
        ) a
        ORDER BY a.sort_num, a.create_time DESC
    </select>

    <select id="leaseOrderList" resultType="com.gree.mall.miniapp.bean.order.LeaseOrderVO">
        SELECT
            a.*,
            b.category_name,
            b.category_url,
            b.goods_lease_name,
            b.repair_remark,
            b.qty,
            b.end_date,
            b.start_date,
            b.real_end_date
        FROM
            lease_order a LEFT JOIN lease_order_item b ON a.id = b.lease_order_id
        WHERE
            a.user_id = #{userId}
            <if test="keyword != null and keyword != ''">
                AND (a.id LIKE CONCAT('%', #{keyword}, '%')  OR b.goods_lease_name LIKE CONCAT('%', #{keyword}, '%'))
            </if>
            <if test="payStatus != null">
                AND a.pay_status = #{payStatus}
            </if>
            <if test="status != null and status != '' and isEndDate == false">
                AND a.status = #{status}
            </if>
            <if test="status != null and status != '' and isEndDate == true">
                AND a.status IN ('OVER', 'RECOVER')
            </if>
        ORDER BY a.create_time DESC
    </select>

    <select id="partsApplyCount" resultType="com.gree.mall.miniapp.bean.material.MyDataCountBean">
        SELECT
            SUM(IF(a.status = 'ING', 1, 0)) AS partsIngCount,
            SUM(IF(a.status = 'END', 1, 0)) AS partsEndCount,
            SUM(IF(a.status = 'CANCEL', 1, 0)) AS partsCancelCount
        FROM
            websit_parts_apply a
        WHERE
            a.create_by_id = #{userId}
    </select>

    <select id="promotionActivity" resultType="com.gree.mall.miniapp.bean.activity.PromotionActivityBean">
        SELECT
            a.*,
            b.name,
            b.detail_imgs
        FROM
            promotion_activity a LEFT JOIN promotion_questionnaire b ON a.promotion_questionnaire_id = b.id
        <where>
            <if test="companyWechatId != null and companyWechatId != ''">
                AND a.company_wechat_id = #{companyWechatId}
            </if>
            <if test="userId != null and userId != ''">
                AND a.user_id = #{userId}
            </if>
            <if test="promotionQuestionnaireId != null and promotionQuestionnaireId != ''">
                AND a.promotion_questionnaire_id = #{promotionQuestionnaireId}
            </if>
        </where>
        ORDER BY a.create_time DESC
    </select>

    <select id="esGoodsList" resultType="com.gree.mall.miniapp.bean.es.ESGoodsBean">
        SELECT
            a.*
        FROM
            es_goods a
        WHERE
            a.del = 0
            <if test="companyWechatId != null and companyWechatId != ''">
                AND a.company_wechat_id = #{companyWechatId}
            </if>
            <if test="goodsName != null and goodsName != ''">
                AND (
                    a.goods_name LIKE CONCAT('%', #{goodsName}, '%') OR
                    a.brand LIKE CONCAT('%', #{goodsName}, '%') OR
                    a.power LIKE CONCAT('%', #{goodsName}, '%') OR
                    a.mark LIKE CONCAT('%', #{goodsName}, '%')
                    )
            </if>
            <if test="categoryId != null and categoryId != ''">
                AND a.category_id = #{categoryId}
            </if>
            <if test="userId != null and userId != ''">
                AND a.user_id = #{userId}
            </if>
            <if test="brand != null and brand != ''">
                AND a.brand = #{brand}
            </if>
            <if test="power != null and power != ''">
                AND a.power = #{power}
            </if>
            <if test="mark != null and mark != ''">
                AND a.mark = #{mark}
            </if>
            <if test="statusList != null and statusList.size > 0">
                AND a.status IN
                <foreach item="item" index="index" collection="statusList" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>

<!--            <if test="sortType != null and sortType != '' and sortType == 'A'.toString">-->
<!--                AND a.sort <![CDATA[ >= ]]> 0 AND a.status IN ('ON','SALE')-->
<!--            </if>-->
<!--            <if test="sortType != null and sortType != '' and sortType == 'B'.toString">-->
<!--                AND a.status = 'ON'-->
<!--            </if>-->
        ORDER BY
        <if test="sortType != null and sortType != '' and sortType != 'C'.toString">
            a.is_top DESC,
            a.top_time DESC,
            a.status,
        </if>
        <if test="sortType != null and sortType != '' and sortType == 'C'.toString">
            a.like_count DESC,
        </if>
        a.update_time DESC

    </select>
    <select id="esGoodsTopList" resultType="com.gree.mall.miniapp.bean.es.ESGoodsBean">
        SELECT
            a.*
        FROM
            es_goods a
        WHERE
            a.company_wechat_id = #{companyWechatId}
            AND a.is_top = 1
            AND a.del = 0
        ORDER BY a.top_time
    </select>

    <select id="guessList" resultType="java.lang.String">
        SELECT
        <choose>
            <when test="typeName != null and typeName != '' and typeName == 'brand'.toString">
                a.brand
            </when>
            <when test="typeName != null and typeName != '' and typeName == 'power'.toString">
                a.power
            </when>
            <otherwise>
                a.mark
            </otherwise>
        </choose>
        FROM
            es_goods a
        WHERE
            a.company_wechat_id = #{companyWechatId}
            AND a.del = 0
        GROUP BY
        <choose>
            <when test="typeName != null and typeName != '' and typeName == 'brand'.toString">
                a.brand
            </when>
            <when test="typeName != null and typeName != '' and typeName == 'power'.toString">
                a.power
            </when>
            <otherwise>
                a.mark
            </otherwise>
        </choose>
    </select>


</mapper>