<?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="cn.iocoder.yudao.module.inspection.mapper.AppInspectionPartnerMapper">
    <select id="selectById" resultType="cn.iocoder.yudao.module.shop.entity.ShopMallPartners" >
        select  * from shop_mall_partners where  partner_id = #{id}
    </select>
    <select id="selectByIdNew" resultType="cn.iocoder.yudao.module.shop.entity.ShopMallPartners" >
        select  * from shop_mall_partners
    </select>
<select id="partnerList" parameterType="cn.iocoder.yudao.module.inspection.query.PartnerListQuery" resultType="cn.iocoder.yudao.module.inspection.vo.PartnerListVo">
    SELECT
        smp.partner_logo,
        smp.address,
        smp.average_score,
        smp.contact_number,
        smp.partner_id,
        smp.work_time,
        smp.partner_name,
        smp.sales_num,
        ROUND(ST_Distance_Sphere (POINT (#{vo.longitude}, #{vo.latitude}),POINT (smp.longitude, smp.latitude ))) AS distance,
        COUNT(sig.id) goodsNum
    FROM
        shop_mall_partners smp
        Inner JOIN shop_inspection_goods sig ON smp.partner_id = sig.partner_id  AND sig.is_listing='1'
    WHERE
        smp.type = 'jc' and smp.is_banned = '0'  and smp.partner_logo is not null
        <if test="vo.distance!=null">
            and ROUND(ST_Distance_Sphere (POINT (#{vo.longitude}, #{vo.latitude}),POINT (smp.longitude, smp.latitude )))  <![CDATA[<=]]>  #{vo.distance}*1000
        </if>
        <if test="vo.categoryId!=null and vo.categoryId!=''">
            and sig.goods_category_id = #{vo.categoryId}
        </if>
        <if test="vo.searchValue!=null and vo.searchValue!=''">
            and (smp.partner_name like concat('%',#{vo.searchValue},'%') or sig.title like concat('%',#{vo.searchValue},'%') )
        </if>
    GROUP BY
        smp.partner_id
    <choose>
    <when test="vo.orderType!=null and vo.orderType!='' and vo.orderType == 'jl'">
        order by distance asc,smp.order_num
    </when>
    <when test="vo.orderType!=null and vo.orderType!='' and vo.orderType == 'hp'">
        order by smp.average_score desc,smp.order_num
    </when>
    <when test="vo.orderType!=null and vo.orderType!='' and vo.orderType == 'xl'">
        order by smp.sales_num desc,smp.order_num
    </when>
    <otherwise>
        order by smp.order_num,distance asc
    </otherwise>
    </choose>
</select>
<select id="shopDetail" parameterType="cn.iocoder.yudao.module.inspection.query.PartnerListQuery" resultType="cn.iocoder.yudao.module.inspection.vo.PartnerListVo">
    SELECT
    smp.partner_logo,
    smp.address,
    smp.average_score,
    smp.contact_number,
    smp.partner_id,
    smp.work_time,
    smp.partner_name,
    smp.sales_num,
    ROUND(ST_Distance_Sphere (POINT (#{longitude}, #{latitude}),POINT (smp.longitude, smp.latitude ))) AS distance,
    smp.longitude,
    smp.latitude,
    smp.province,
    smp.city,
    smp.area,
    smp.shop_images,
    ifnull(smp.is_working,'0') isWorking
    FROM
    shop_mall_partners smp
    where partner_id = #{partnerId}
</select>
<update id="addSalesNum">
    update
    shop_mall_partners
    set sales_num = sales_num+1
    where partner_id = #{partnerId}
</update>
<select id="workNum" resultType="cn.iocoder.yudao.module.inspection.vo.StatisticsInfo">
    SELECT
          IfNULL(sum(info.`status`='0') ,0) as workingNum
    FROM
        inspection_info info
        INNER JOIN order_info oi ON info.inspection_order_id = oi.id
        WHERE oi.partner_id = #{partnerId} and info.create_time like CONCAT(#{timeStr},'%')
</select>
<select id="srlNum" resultType="java.lang.Integer">
    SELECT
        count(1)
    FROM
        inspection_info info
        INNER JOIN order_info oi ON info.inspection_order_id = oi.id
    WHERE info.end_time like CONCAT(#{timeStr},'%')
      and info.is_pass ='1'  and oi.goods_title like concat('%','双燃料','%')
</select>
<select id="hgNum" resultType="java.lang.Integer">
    SELECT
        count(1)
    FROM
        inspection_info info
            INNER JOIN order_info oi ON info.inspection_order_id = oi.id
    WHERE   info.end_time like CONCAT(#{timeStr},'%')
      and info.is_pass ='1'
</select>
<select id="orderNum" resultType="cn.iocoder.yudao.module.inspection.vo.StatisticsInfo">
    SELECT IfNULL(
                   sum((oi.is_online = '1' and oi.validation_time is not null and
                        oi.validation_time like CONCAT(#{timeStr}, '%'))
                       or
                       (oi.is_online = '0' and oi.create_time is not null and
                        oi.create_time like CONCAT(#{timeStr}, '%'))
                   ), 0) as todayOrderNum,
           IfNULL(
                   sum((oi.pay_time like CONCAT(#{timeStr}, '%') and oi.create_time like CONCAT(#{timeStr}, '%'))),
                   0)    as workedNum,
           -- 统计当天未完成的订单
           IFNULL(
                   SUM(
                           (oi.pay_time IS NULL OR oi.validation_time IS NULL) -- 订单未支付或未验证
                               AND oi.create_time LIKE CONCAT(#{timeStr}, '%') -- 创建时间属于当天
                   ), 0
           )             AS uncompletedNum
    FROM order_info oi
    WHERE oi.deleted = 0
</select>
<select id="allAmount" resultType="java.lang.Integer">
SELECT
	  IfNULL(sum(oi.pay_money+oi.balance),0) as allAmount
FROM
  order_info oi
	WHERE oi.partner_id = #{partnerId} and validation_time  is not null
</select>
<select id="todayAmount" resultType="java.lang.Integer">
    SELECT
	  IfNULL(sum(oi.pay_money+oi.balance),0) as allAmount
    FROM
    order_info oi
	WHERE oi.partner_id = #{partnerId} and validation_time like CONCAT(#{timeStr},'%')
</select>
<select id="hotGoodsList" resultType="cn.iocoder.yudao.module.inspection.vo.HotGoodsVo">
SELECT
	goods.title AS goodsName,
    ROUND(
            SUM(
                    CASE
                        WHEN (ii.`status` = '1' AND ii.is_pass = '1') OR ii.`status` = '0'
                            THEN oi.goods_price
                        ELSE 0
                        END
            ) / 100.0,
            2
    ) AS salesAmount,
    SUM( case when (ii.`status`='1' and ii.is_pass = '1') or  (ii.`status`='0') then   1 else 0 end   ) AS salesNum
FROM
    shop_inspection_goods goods
	left join order_info oi on oi.goods_id = goods.id and validation_time is not null and  validation_time like concat(#{dateStr},'%')
	left JOIN inspection_info ii on oi.id = ii.inspection_order_id
WHERE
	goods.partner_id = #{partnerId}
GROUP BY
	goods.id
	order by salesNum desc
</select>
<select id="newHotGoodsList" resultType="cn.iocoder.yudao.module.inspection.vo.HotGoodsVo">
SELECT
	goods.title AS goodsName,
	SUM( case when (ii.`status`='1' and ii.is_pass = '1') or  (ii.`status`='0') then   oi.goods_price else 0 end   ) AS salesAmount,
	SUM( case when (ii.`status`='1' and ii.is_pass = '1') or  (ii.`status`='0') then   1 else 0 end   ) AS salesNum
FROM
    order_info oi
	left join shop_inspection_goods goods on goods.id = oi.goods_id
	left JOIN inspection_info ii on oi.id = ii.inspection_order_id
WHERE  validation_time is not null and  validation_time like concat(#{dateStr},'%')
GROUP BY
	goods.id
	order by salesNum desc
</select>
<select id="manageGoodsList" resultType="cn.iocoder.yudao.module.inspection.vo.GoodsVo">
SELECT
    sig.id as goodsId,sig.title as goodsTitle,sig.image as goodsImage,sig.price,count(oi.id) as salesNum,sig.listing_status,sig.reject_reason
FROM
    shop_inspection_goods sig
LEFT JOIN order_info oi ON sig.id = oi.goods_id and oi.pay_time is not null
where sig.partner_id =#{partnerId}
<if test="isListing !=null and isListing!=''">
    and sig.is_listing = #{isListing}
</if>
<if test="goodsTitle !=null and goodsTitle!=''">
    and sig.title like concat('%',#{goodsTitle},'%')
</if>
 group by sig.id
 order by sig.order_num,sig.create_time desc
</select>
<select id="orderList" resultType="cn.iocoder.yudao.module.inspection.vo.OrderAppDetail">
    SELECT
    *
    FROM
    `order_info`
    where partner_id =#{partnerId}
    and (order_status BETWEEN '1' and '5')
    <if test="title!=null and title!='' ">
        and goods_title like concat('%',#{title},'%')
    </if>
    <if test="phoneNum!=null and phoneNum!='' ">
        and phonenumber like concat('%',#{phoneNum},'%')
    </if>
    order by validation_time desc
</select>
<select id="getWorkList" resultType="cn.iocoder.yudao.module.partner.entity.PartnerWorker">
    SELECT
        pw.id,pw.user_id,su.nickname as workName,su.mobile as workPhone, su.avatar
    FROM
    partner_worker pw
    INNER JOIN system_users su ON pw.user_id = su.id
    left JOIN system_user_post sup on  sup.user_id = su.id
    where pw.partner_id = #{partnerId}
    <if test="workName!=null and workName!=''">
        and su.nickname like concat('%',#{workName},'%')
    </if>
    <if test="phoneNum!=null and phoneNum!=''">
        and su.mobile like concat('%',#{phoneNum},'%')
    </if>
    <if test="postId!=null and postId!=''">
        and sup.post_id = #{postId}
    </if>
    order by pw.create_time desc
</select>
    <select id="inspectionList" resultType="cn.iocoder.yudao.module.inspection.entity.InspectionInfo">
        SELECT
        info.id,
        info.worker_name,
        info.worker_phone,
        info.worker_avatar,
        info.`status`,
        info.is_pass,
        info.start_time,
        info.end_time,
        step.title AS lastTitle,
        su.nickname AS buyName,
        su.mobile   AS buyPhone,
        oi.order_no      AS orderNo,
        oi.pay_money + oi.balance AS realPayMoney,
        oi.goods_title   AS goodsName,
        info.car_num,
        info.is_retrial,
        info.customer_source AS customerSource,
        info.other_phone    AS otherPhone,
        oi.sku_id    AS skuId,
        oi.sku_name  AS skuName,
        oi.goods_price AS goodsPrice
        FROM inspection_info info
        LEFT JOIN inspection_step_info step
        ON info.id = step.inspection_info_id
        LEFT JOIN inspection_work_node iwn
        ON info.id = iwn.inspection_info_id
        INNER JOIN system_users su
        ON su.id = info.user_id
        INNER JOIN order_info oi
        ON oi.id = info.inspection_order_id
        WHERE info.partner_id = #{partnerId}

        <!-- 待复检 -->
        <if test="status == 4">
            AND iwn.status = '3'
            AND info.status != '1'
        </if>

        <!-- 异常工单 -->
        <if test="status == 5">
            <!-- 有“制证”节点已完成 -->
            AND EXISTS (
            SELECT 1
            FROM inspection_work_node w
            JOIN inspection_project p
            ON p.id = w.project_id
            WHERE w.inspection_info_id = info.id
            AND p.project_name LIKE '%制证%'
            AND w.status = '2'
            AND w.type = '1'
            )
            <!-- 同一工单还有其它节点未完成 -->
            AND EXISTS (
            SELECT 1
            FROM inspection_work_node w2
            WHERE w2.inspection_info_id = info.id
            AND w2.status &lt;&gt; '2'
            )
        </if>

        <!-- 其它普通状态 -->
        <if test="status != 4 and status != 5">
            AND info.status = #{status}
        </if>

        <!-- 车牌号模糊 -->
        <if test="carNum != null and carNum != ''">
            AND info.car_num LIKE CONCAT('%', #{carNum}, '%')
        </if>

        GROUP BY info.id
        ORDER BY info.create_time DESC
    </select>

<select id="workerInspectionList" resultType="cn.iocoder.yudao.module.inspection.entity.InspectionInfo">
    SELECT
    info.id,info.worker_name,info.worker_phone,info.worker_avatar,info.`status`,info.is_pass,info.start_time,info.end_time,step.title as lastTitle,
    su.nickname buyName,su.mobile as buyPhone,oi.order_no as orderNo,oi.pay_money+oi.balance as realPayMoney,oi.goods_title as goodsName,info.car_num,info.is_retrial
    from
    inspection_info info
    LEFT JOIN inspection_step_info step ON info.id = step.inspection_info_id
    INNER JOIN system_users su on su.id = info.user_id
    INNER JOIN order_info oi on oi.id = info.inspection_order_id
    WHERE  info.status = #{status} and info.work_id = #{workerId}
    <if test="searchValue!=null and searchValue!=''">
        and (info.car_num like concat('%',#{searchValue},'%') or su.phonenumber like concat('%',#{searchValue},'%'))
    </if>
    GROUP BY info.id
    order by info.create_time desc
</select>
<select id="validationList" resultType="cn.iocoder.yudao.module.payment.entity.OrderInfo">
    SELECT
	order_no,goods_title,goods_price,pay_money,balance,reduce_money,validation_time,validation_real_name,real_name,phonenumber
FROM
	order_info
	WHERE  validation_time  is not null and partner_id = #{partnerId}
	<if test="searchValue !=null and searchValue!=''">
        and (phonenumber like concat('%',{searchValue},'%') or validation_real_name like concat('%',{searchValue},'%'))
    </if>
    order by validation_time desc
</select>
<select id="getPickCarList" resultType="cn.iocoder.yudao.module.inspection.entity.InspectionPickCar">
    SELECT
        pickCar.*
    FROM
        inspection_pick_car pickCar
      inner join inspection_appointment ia on ia.pick_car_id = pickCar.id
      where pickCar.partner_id = #{partnerId}
      <if test="phoneNum !=null and phoneNum!=''">
        and pickCar.user_phone like concat('%',#{phoneNum},'%')
      </if>
    <if test="pickStatus == '0'.toString()">
        and pickCar.worker_id  is null
    </if>
    <if test="pickStatus == '1'.toString()">
        and pickCar.worker_id  is not null
    </if>
     order by pickCar.create_time desc
</select>
    <select id="getPickCarListOfWorker" resultType="cn.iocoder.yudao.module.inspection.entity.InspectionPickCar">
        SELECT
        pickCar.*
        FROM
        inspection_pick_car pickCar
        inner join order_info oi on oi.id = pickCar.order_id and oi.order_status = '1'
        where pickCar.worker_id = #{workerId}
        <if test="phoneNum !=null and phoneNum!=''">
            and pickCar.user_phone like concat('%',#{phoneNum},'%')
        </if>
        <if test="phoneNum !=null and phoneNum!=''">
            and pickCar.user_phone like concat('%',#{phoneNum},'%')
        </if>
        order by appointment_day desc,appointment_time desc
    </select>
    <select id="chartInfoAmount" resultType="cn.iocoder.yudao.module.payment.entity.OrderInfo">
        select  sum(pay_money) as realPayMoney,goods_title as goodsTitle from
        order_info
        where validation_time  is not null and validation_time between #{startTime} and #{endTime}
        and partner_id = #{partnerId}
        group by goods_id
        order by realPayMoney desc
    </select>
    <select id="newChartInfoAmount" resultType="cn.iocoder.yudao.module.payment.entity.OrderInfo">
        select  sum(pay_money) as realPayMoney,goods_title as goodsTitle from
        order_info
        where validation_time  is not null and validation_time between #{startTime} and #{endTime}
        group by goods_id
        order by realPayMoney desc
    </select>
    <select id="chartInfoNum" resultType="cn.iocoder.yudao.module.payment.entity.OrderInfo">
        select  count(1) as realPayNum,goods_title as goodsTitle from
        order_info
        where validation_time  is not null and validation_time between #{startTime} and #{endTime}
        and partner_id = #{partnerId}
        group by goods_id
        order by realPayNum desc
    </select>
    <select id="newChartInfoNum" resultType="cn.iocoder.yudao.module.payment.entity.OrderInfo">
        select  count(1) as realPayNum,goods_title as goodsTitle from
        order_info
        where validation_time  is not null and validation_time between #{startTime} and #{endTime}
        group by goods_id
        order by realPayNum desc
    </select>

    <select id="chartInfoRatio" resultType="java.util.Map">
   SELECT
	ROUND((SUM(ins.is_pass='1')/SUM(1))*100,2) as passRatio,oi.goods_title as goodsTitle
   FROM
	inspection_info ins
	inner join order_info oi on oi.id = ins.inspection_order_id
	WHERE  ins.create_time between #{startTime} and #{endTime}
    and ins.partner_id = #{partnerId}
    GROUP BY oi.goods_title
    order by passRatio desc
    </select>

    <select id="newChartInfoRatio" resultType="java.util.Map">
   SELECT
	ROUND((SUM(ins.is_pass='1')/SUM(1))*100,2) as passRatio,oi.goods_title as goodsTitle
   FROM
	inspection_info ins
	inner join order_info oi on oi.id = ins.inspection_order_id
	WHERE  ins.create_time between #{startTime} and #{endTime}
    GROUP BY oi.goods_title
    order by passRatio desc
    </select>
    <select id="partnerCategoryList" resultType="cn.iocoder.yudao.module.inspection.entity.ShopInspectionCategory">
    SELECT
        cate.*
    FROM
    shop_inspection_category cate
    INNER JOIN inspection_category_template template on template.category_id = cate.id
    <if test="partnerId == null">
        left JOIN shop_inspection_goods goods ON goods.goods_category_id = cate.id and goods.partner_id = #{partnerId}
        where goods.id is null
    </if>
    group by cate.id
    ORDER BY cate.order_num

    </select>
    <select id="chartLineInspectionAmount" resultType="cn.iocoder.yudao.module.payment.entity.OrderInfo">
    SELECT
	IFNULL(SUM(oi.pay_money),0)as realPayMoney
    FROM
	order_info oi
	INNER JOIN inspection_info ii on oi.id = ii.inspection_order_id
	WHERE oi.partner_id = #{partnerId} and ii.create_time like concat(#{dateStr},'%') and ii.`status`='1' and ii.is_pass = '1' and oi.validation_time  is not null
    </select>
    <select id="newChartLineInspectionAmount" resultType="cn.iocoder.yudao.module.payment.entity.OrderInfo">
    SELECT
	IFNULL(SUM(oi.pay_money),0)as realPayMoney
    FROM
	order_info oi
	INNER JOIN inspection_info ii on oi.id = ii.inspection_order_id
	WHERE ii.create_time like concat(#{dateStr},'%') and ii.`status`='1' and ii.is_pass = '1' and oi.validation_time  is not null
    </select>
    <select id="staticsTable1" resultType="java.util.Map">
    SELECT
        ROUND(IFNULL(SUM(oi.goods_price),0)/100)  gsAmount,
        ROUND(IFNULL(SUM(case when ii.`status`='1' and ii.is_pass = '1' then   oi.pay_money else 0 end)/100,0))   ysAmount,
        ROUND(IFNULL(SUM(case when ii.`status`='1' and ii.is_pass = '1' and oi.validation_time  is not null then   oi.pay_money else 0 end)/100,0))  yjsAmount
    FROM
        order_info oi
    INNER JOIN inspection_info ii on oi.id = ii.inspection_order_id
    WHERE oi.partner_id =#{partnerId}
    and (ii.`status`='1' and ii.is_pass = '1' or ii.`status`='0')
    and ii.create_time BETWEEN #{startTime} and #{endTime}
    </select>
    <select id="newStaticsTable1" resultType="java.util.Map">
    SELECT
        ROUND(IFNULL(SUM(oi.goods_price),0)/100)  gsAmount,
        ROUND(IFNULL(SUM(case when ii.`status`='1' and ii.is_pass = '1' then   oi.pay_money else 0 end)/100,0))   ysAmount,
        ROUND(IFNULL(SUM(case when ii.`status`='1' and ii.is_pass = '1' and oi.validation_time  is not null then   oi.pay_money else 0 end)/100,0))  yjsAmount
    FROM
        order_info oi
    INNER JOIN inspection_info ii on oi.id = ii.inspection_order_id
    WHERE (ii.`status`='1' and ii.is_pass = '1' or ii.`status`='0')
    and ii.create_time BETWEEN #{startTime} and #{endTime}
    </select>
    <select id="staticsTable2" resultType="java.util.Map">
        SELECT
        count(1) allNum,
        IFNULL(SUM(ii.status='0'),0) jxzNum,
        IFNULL(SUM(ii.status='1'),0) ywcNum,
        IFNULL(SUM(oi.sku_name LIKE '%年审%'),0) nsNum,
        IFNULL(SUM(oi.sku_name LIKE '%上户%'),0) shNum,
        IFNULL(SUM(oi.sku_name LIKE '%非定检%'),0) fdjNum,
        IFNULL(SUM(oi.sku_name LIKE '%双燃料%'),0) srlNum,
        IFNULL(SUM(oi.sku_name LIKE '%其他检测%'),0) qtjcNum,
        IFNULL(SUM(ii.recheck_count),0) recheckNum,
        IFNULL(SUM(ii.reinspect_count),0) reinspectNum
        FROM
        inspection_info ii
        LEFT JOIN order_info oi on oi.id = ii.inspection_order_id
        <where>
            ii.partner_id =#{partnerId} and ii.deleted = 0 and oi.deleted = 0
            <if test="startTime != null and endTime != null">
                and ii.create_time BETWEEN #{startTime} and #{endTime}
            </if>
        </where>

    </select>
    <select id="newStaticsTable2" resultType="java.util.Map">
    SELECT
         count(1)  allNum,
        IFNULL(SUM(ii.status='0'),0)  jxzNum,
        IFNULL(SUM(ii.status='1'),0)  ywcNum
    FROM
    inspection_info ii
    WHERE  ii.create_time BETWEEN #{startTime} and #{endTime}
    </select>
    <select id="staticsTable3" resultType="java.util.Map">
    SELECT
       ii.customer_source customerSource,IFNULL(sdd.remark,'未知类别') remark,count(1) theNum, ROUND(IFNULL(SUM(oi.goods_price),0)/100) theAmount
    FROM
        order_info oi
    INNER JOIN inspection_info ii on oi.id = ii.inspection_order_id
    left join system_dict_data sdd on sdd.value = ii.customer_source and sdd.dict_type = concat('customer_source-',#{partnerId})
    WHERE oi.partner_id =#{partnerId}
    and ii.create_time BETWEEN #{startTime} and #{endTime}
	GROUP BY sdd.remark
	having count(1)>0
	order by theNum desc
    </select>
    <select id="newStaticsTable3" resultType="java.util.Map">
    SELECT
       ii.customer_source customerSource,IFNULL(sdd.remark,'未知类别') remark,count(1) theNum, ROUND(IFNULL(SUM(oi.goods_price),0)/100) theAmount
    FROM
        order_info oi
    INNER JOIN inspection_info ii on oi.id = ii.inspection_order_id
    left join system_dict_data sdd on sdd.value = ii.customer_source
    WHERE  ii.create_time BETWEEN #{startTime} and #{endTime}
	GROUP BY sdd.remark
	having count(1)>0
	order by theNum desc
    </select>
    <select id="staticsTable3Detail" resultType="java.util.Map">
    SELECT
       ii.customer_source customerSource,oi.goods_title as goodsTitle,IFNULL(sdd.remark,'未知类别') remark,count(1) theNum, ROUND(IFNULL(SUM(oi.goods_price),0)/100) theAmount
    FROM
        order_info oi
    INNER JOIN inspection_info ii on oi.id = ii.inspection_order_id
    left join system_dict_data sdd on sdd.value = ii.customer_source and sdd.dict_type = concat('customer_source-',#{partnerId})
    WHERE oi.partner_id =#{partnerId}
    <if test="remark !=null and remark !=''">
       <choose>
           <when test="remark == '未知类别'.toString()">
               and sdd.remark is null
           </when>
           <otherwise>
               and sdd.remark = #{remark}
           </otherwise>
       </choose>
    </if>
    and ii.create_time BETWEEN #{startTime} and #{endTime}
	GROUP BY ii.customer_source,oi.goods_title
	having count(1)>0
	order by theNum desc
    </select>
    <select id="staticsTable4" resultType="java.util.Map">
    SELECT
      count(1) theNum,oi.goods_title goodsTitle
    FROM
        order_info oi
    INNER JOIN inspection_info ii on oi.id = ii.inspection_order_id
    WHERE oi.partner_id =#{partnerId}
    and ii.create_time BETWEEN #{startTime} and #{endTime}
	GROUP BY oi.goods_title
    </select>
    <select id="staticsTable5" resultType="java.util.Map">
	SELECT
       ROUND(IFNULL(SUM(oi.goods_price),0)/100) theAmount,su.nickname theName
    FROM
    order_info oi
    INNER JOIN inspection_info ii on oi.id = ii.inspection_order_id
    inner join system_users su on su.id = oi.user_id
    WHERE oi.partner_id ='19'
        and ii.create_time BETWEEN #{startTime} and #{endTime}
    and  oi.validation_time is null and ii.`status`='1' and ii.is_pass = '1'
	GROUP BY su.id
	order by theAmount desc
    </select>
    <select id="dhjNum" resultType="java.lang.Long">
    SELECT
        IFNULL(SUM(case when oi.sku_name like CONCAT('%','环检','%')  then   1 else 0 end ),0)  + IFNULL(SUM(case when oi.goods_title like CONCAT('%','双燃料','%') and   oi.sku_name like CONCAT('%','环检','%')  then   1 else 0 end ),0)
    FROM
        `inspection_info` ini
        INNER JOIN order_info oi on oi.id = ini.inspection_order_id
        where ini.is_pass = '1' and ini.partner_id = #{partnerId} and ini.create_time like CONCAT(#{dateStr},'%')
    </select>
    <select id="getAppointNum" resultType="java.lang.Long">
        SELECT
            COUNT(1)
        FROM
            inspection_appointment ia
        where ia.deleted = 0 and ia.partner_id = #{partnerId} and appointment_day = #{formDate}
        order by create_time desc
    </select>
    <select id="getPickNum" resultType="java.lang.Long">
        SELECT
            COUNT(1)
        FROM
            inspection_pick_car ipc
        inner join inspection_appointment ia on ia.pick_car_id = ipc.id
        where ipc.deleted = 0 and ia.deleted= 0 and ipc.partner_id = #{partnerId} and ia.appointment_day = #{formDate}
        order by ipc.create_time desc
    </select>
    <select id="pageWorkList" resultType="cn.iocoder.yudao.module.partner.entity.PartnerWorker">
        SELECT
        pw.id,pw.user_id,su.nickname as workName,su.mobile as workPhone, su.avatar
        FROM
        partner_worker pw
        INNER JOIN system_users su ON pw.user_id = su.id
        left JOIN system_user_post sup on  sup.user_id = su.id
        where  pw.deleted = 0
        <if test="workName!=null and workName!=''">
            and su.nickname like concat('%',#{workName},'%')
        </if>
        <if test="phoneNum!=null and phoneNum!=''">
            and su.mobile like concat('%',#{phoneNum},'%')
        </if>
        <if test="postId!=null and postId!=''">
            and sup.post_id = #{postId}
        </if>
        order by pw.create_time desc
    </select>

    <select id="getOrderByDate" resultType="cn.iocoder.yudao.module.inspection.vo.OrderTable">
        SELECT t.id,
        t.carNum,
        t.type,
        t.pay,
        t.payType,
        t.payTime,  -- 新增字段
        t.createTime,
        t.customerSource,
        t.carModel,
        CASE
        WHEN t.status = '已完成' AND t.is_pass = 0 THEN '不合格'
        WHEN t.status = '已完成' AND t.is_pass = 1 THEN '合格'
        WHEN t.status = '已完成' THEN '未知'
        ELSE ''
        END AS result,
        t.status
        FROM (
        SELECT ii.id,
        ii.car_num                  AS carNum,
        oi.sku_name                 AS type,
        oi.pay_type                 AS payType,
        oi.pay_time                 AS payTime,  -- 新增字段
        oi.create_time             AS createTime,
        ii.customer_source          AS customerSource,
        ii.car_model                AS carModel,
        CASE
        WHEN oi.pay_type IS NULL THEN '未支付'
        ELSE '已支付'
        END                     AS pay,
        CASE
/*WHEN  THEN '检测中'*/
            /*WHEN (COALESCE(max_iwn_status, 0) = 2 OR ii.status = 1) THEN '已完成'*/
        WHEN ( ii.status = 1) THEN '已完成'
            /*WHEN ((ii.status = 0 OR ii.status = 2) AND has_status_1 OR has_status_0_or_null)THEN '检测中'*/
        WHEN ((ii.status = 0 OR ii.status = 2))THEN '检测中'
        WHEN (ii.status = 0 OR ii.status = 2) AND COALESCE(max_iwn_status, 0) = 0 THEN '待检测'
        WHEN (ii.status = 1 ) AND ii.is_pass = 0 THEN '退办理'
        ELSE '未知状态'
        END                     AS status,
        ii.is_pass,
        COALESCE(max_iwn_status, 0) AS max_iwn_status,
        oi.create_time
        FROM order_info oi
        INNER JOIN inspection_info ii
        ON oi.id = ii.inspection_order_id
        <if test="query.type == 1">
            INNER JOIN inspection_appointment ia ON oi.id = ia.order_id
        </if>
        LEFT JOIN (
        SELECT inspection_info_id,
        MAX(COALESCE(status, 0))                                 AS max_iwn_status,
        MAX(CASE WHEN COALESCE(status, 0) = 1 THEN 1 ELSE 0 END) AS has_status_1,
        MAX(CASE WHEN COALESCE(status, 0) = 0 THEN 1 ELSE 0 END) AS has_status_0_or_null
        FROM inspection_work_node
        GROUP BY inspection_info_id
        ) iwn_agg
        ON ii.id = iwn_agg.inspection_info_id
        <where>
            <if test="query.payType!=null and query.payType!=''">
                AND oi.pay_type = #{query.payType}
            </if>
            <if test="query.skuName!=null and query.skuName!=''">
                AND oi.sku_name LIKE concat('%',#{query.skuName},'%')
            </if>
            <if test="query.customerSource!=null and query.customerSource!=''">
                AND ii.customer_source = #{query.customerSource}
            </if>
            <if test="query.carModelOrCarYear!=null and query.carModelOrCarYear!=''">
                AND ( ii.car_model LIKE concat('%',#{query.carModelOrCarYear},'%')
                    OR ii.car_num LIKE concat('%',#{query.carModelOrCarYear},'%') )
            </if>
            <if test="query.startTime!=null and query.startTime!=''">
                AND oi.create_time BETWEEN #{query.startTime} AND #{query.endTime}
            </if>
            <if test="query.goods!=null and query.goods!=''">
                AND oi.goods_id = #{query.goods}
            </if>
            <if test="query.carYear!=null and query.carYear!=''">
                AND (TIMESTAMPDIFF(YEAR, ii.car_register_date, CURDATE()) >= #{query.carYear}
                AND TIMESTAMPDIFF(YEAR, ii.car_register_date, CURDATE()) &lt; #{query.carYear} + 1)
            </if>
        </where>
        AND oi.deleted = '0'
        ) t
        <where>
            <choose>
                <when test="query.chooseStatus == '2'.toString()">
                    t.status = '检测中'
                </when>
                <when test="query.chooseStatus == '3'.toString()">
                    t.status = '已完成'
                </when>
                <when test="query.chooseStatus == '4'.toString()">
                    t.status = '退办理'
                </when>
            </choose>
        </where>
        ORDER BY t.create_time DESC;
    </select>

    <select id="getTypeCount" resultType="java.util.Map">
        SELECT t2.type as type, COUNT(*) as count FROM (
        SELECT t.id,
        t.carNum,
        t.type,
        t.pay,
        t.payType,
        CASE
        WHEN t.status = '已完成' AND t.is_pass = 0 THEN '不合格'
        WHEN t.status = '已完成' AND t.is_pass = 1 THEN '合格'
        WHEN t.status = '已完成' THEN '未知'
        ELSE ''
        END AS result,
        t.status
        FROM (SELECT ii.id,
        ii.car_num                  AS carNum,
        oi.sku_name                 AS type,
        oi.pay_type                 as payType,
        CASE
        WHEN oi.pay_type IS NULL THEN '未支付'
        ELSE '已支付'
        END                     AS pay,
        CASE
        WHEN oi.pay_type IS NOT NULL THEN '已完成'
        WHEN has_status_0_or_null THEN '检测中'
        WHEN (ii.status = 0 OR ii.status = 2) AND has_status_1 THEN '检测中'
        WHEN (COALESCE(max_iwn_status, 0) = 2 OR ii.status = 1) THEN '已完成'
        WHEN (ii.status = 0 OR ii.status = 2) AND COALESCE(max_iwn_status, 0) = 0 THEN '待检测'
        ELSE '未知状态' -- 这是为了处理任何未预期的情况
        END                     AS status,
        ii.is_pass,
        COALESCE(max_iwn_status, 0) AS max_iwn_status,
        oi.create_time
        FROM order_info oi
        INNER JOIN
        inspection_info ii
        ON
        oi.id = ii.inspection_order_id
        LEFT JOIN (SELECT inspection_info_id,
        MAX(COALESCE(status, 0))                                 AS max_iwn_status,
        MAX(CASE WHEN COALESCE(status, 0) = 1 THEN 1 ELSE 0 END) AS has_status_1,
        MAX(CASE WHEN COALESCE(status, 0) = 0 THEN 1 ELSE 0 END) AS has_status_0_or_null
        FROM inspection_work_node
        GROUP BY inspection_info_id) iwn_agg
        ON
        ii.id = iwn_agg.inspection_info_id
        WHERE oi.create_time BETWEEN #{startTime} AND #{endTime}
        AND oi.deleted = '0') t
        <where>
            <choose>
                <when test="chooseStatus == '2'.toString()">
                    t.status = '检测中'
                </when>
                <when test="chooseStatus == '3'.toString()">
                    t.status = '已完成'
                </when>
            </choose>
        </where>
        ) t2
        GROUP BY t2.type
    </select>
    <select id="queryInspectionSkuList" resultType="java.util.Map">
        SELECT
        s.sku_name AS skuName,
        IFNULL(COUNT(o.id),0) AS orderCount
        FROM
        (
        SELECT '年审' AS sku_name
        UNION ALL SELECT '上户'
        UNION ALL SELECT '双燃料'
        UNION ALL SELECT '非定检'
        UNION ALL SELECT '其他检测'
        ) s
        LEFT JOIN order_info o
        ON o.sku_name = s.sku_name
        <if test="startTime != null">
            AND o.create_time BETWEEN #{startTime} AND #{endTime}
        </if>
        GROUP BY
        s.sku_name
    </select>
</mapper>
