<?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="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 (#{longitude}, #{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="distance!=null">
            and ROUND(ST_Distance_Sphere (POINT (#{longitude}, #{latitude}),POINT (smp.longitude, smp.latitude )))  <![CDATA[<=]]>  #{distance}*1000
        </if>
        <if test="categoryId!=null and categoryId!=''">
            and sig.goods_category_id = #{categoryId}
        </if>
        <if test="searchValue!=null and searchValue!=''">
            and (smp.partner_name like concat('%',#{searchValue},'%') or sig.title like concat('%',#{searchValue},'%') )
        </if>
    GROUP BY
        smp.partner_id
    <choose>
    <when test="orderType!=null and orderType!='' and orderType == 'jl'">
        order by distance asc,smp.order_num
    </when>
    <when test="orderType!=null and orderType!='' and orderType == 'hp'">
        order by smp.average_score desc,smp.order_num
    </when>
    <when test="orderType!=null and orderType!='' and 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 oi.partner_id = #{partnerId} and 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 oi.partner_id = #{partnerId} and 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
    FROM
      order_info oi
    WHERE oi.partner_id = #{partnerId}
</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,
	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
    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="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 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.partner_id = #{partnerId}
    <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="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="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="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
    left JOIN shop_inspection_goods goods ON goods.goods_category_id = cate.id and goods.partner_id = #{partnerId}
    where goods.id is null
    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="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="staticsTable2" 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.partner_id =#{partnerId}
    and 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="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>
</mapper>
