<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://www.72crm.com/mybatis-3.5.3-mapper.dtd">
<mapper namespace="com.kakarote.crm.mapper.CrmExamineLogMapper">
    <select id="queryUserByRecordId" resultType="com.alibaba.fastjson.JSONObject">
        select sael.examine_time  as examineTime ,
        sael.examine_status,sau.realname ,sau.user_id,sau.img
        from wk_crm_examine_log as sael
        LEFT JOIN wk_admin_user as sau on sau.user_id = sael.examine_user
        where sael.record_id = #{recordId} and  sael.examine_status = #{status} and sael.is_recheck != 1
    </select>

    <select id="queryRecordByUserIdAndStatus" resultType="com.alibaba.fastjson.JSONObject">
        SELECT DISTINCT user_id, realname  ,img, 6 as examine_status, 5 as stepType, #{examineTime} as examineTime
        from wk_admin_user WHERE user_id = #{userId}
    </select>

    <select id="queryRecordAndId" resultType="com.alibaba.fastjson.JSONObject">
        select create_user, sau.realname as examineUserName ,
        sau.user_id , sau.img, aser.create_time as examineTime, 6 as examine_status
        from wk_crm_examine_record  as aser
        LEFT JOIN wk_admin_user as sau on sau.user_id = aser.create_user where record_id = #{recordId}
    </select>

    <select id="queryExamineLogAndUserByRecordId" resultType="com.alibaba.fastjson.JSONObject">
        select sael.examine_time  as examineTime , sael.examine_status,sau.realname ,sau.img , sael.log_id
        from wk_crm_examine_log as sael
        LEFT JOIN wk_admin_user as sau on sau.user_id = sael.examine_user
        where sael.record_id = #{recordId} and sael.is_recheck != 1 order by sael.create_time
    </select>

    <select id="queryExamineLogAndUserByLogId" resultType="com.alibaba.fastjson.JSONObject">
        select sael.examine_time  as examineTime , sael.examine_status,sau.realname ,sau.img , sael.log_id
        from wk_crm_examine_log as sael
        LEFT JOIN wk_admin_user as sau on sau.user_id = sael.examine_user where
        sael.log_id =#{id} and sael.is_recheck != 1
        order by sael.create_time
    </select>

    <select id="queryExamineLog" resultType="com.alibaba.fastjson.JSONObject">
        SELECT * FROM wk_crm_examine_log
        WHERE examine_status = 0 and is_recheck != 1
        and record_id = #{recordId}
        and  examine_user = #{id}
        <if test="stepId!=null">
            and examine_step_id = #{stepId}
        </if>
        order by create_time desc limit 1
    </select>

    <select id="queryUserByRecordIdAndStepIdAndStatus" resultType="com.alibaba.fastjson.JSONObject">
        select sael.examine_time  as examineTime , sael.examine_status,sau.realname as realname , sau.user_id , sau.img
        from wk_crm_examine_log as sael
        LEFT JOIN wk_admin_user as sau on sau.user_id = sael.examine_user
        where record_id = #{recordId} and examine_step_id = #{stepId}
        and sael.is_recheck != 1  order by sael.create_time
    </select>

    <select id="queryUserByUserId" resultType="com.alibaba.fastjson.JSONObject">
      SELECT DISTINCT saud.user_id, saud.realname , 0 as examine_status from wk_admin_user as sau
      LEFT JOIN wk_admin_user as saud on saud.user_id = sau.parent_id WHERE sau.user_id = #{id}
    </select>

    <select id="queryUserByUserIdAnd" resultType="com.alibaba.fastjson.JSONObject">
      SELECT DISTINCT user_id, realname ,img, 0 as examine_status from wk_admin_user WHERE user_id = #{id}
    </select>

    <select id="queryExamineLogByRecordIdByStep" resultType="com.alibaba.fastjson.JSONObject">
      select sael.order_id as order_id , sau.user_id , sau.realname , sau.img ,sael.examine_status,sael.examine_time,sael.remarks
      from wk_crm_examine_log as sael
      LEFT JOIN wk_admin_user as sau on sau.user_id = sael.examine_user
      where sael.record_id = #{recordId} AND sael.examine_status not in (0,5) order by sael.create_time
    </select>

    <select id="queryNowadayExamineLogByRecordIdAndStepId" resultType="com.kakarote.crm.entity.PO.CrmExamineLog">
      select * from wk_crm_examine_log where
      record_id = #{recordId} and examine_step_id = #{stepId} and examine_user = #{userId} and is_recheck = 0
    </select>

    <select id="queryNowadayExamineLogByRecordIdAndStatus" resultType="com.kakarote.crm.entity.PO.CrmExamineLog">
      select * from wk_crm_examine_log where
      record_id = #{recordId} and examine_status = 0 and examine_user = #{userId} and is_recheck = 0 limit 1
    </select>

    <select id="queryCountByStepId" resultType="java.lang.Integer">
        SELECT COUNT(*) FROM wk_crm_examine_log
        WHERE record_id = #{recordId} and examine_step_id = #{stepId} and examine_status != 2
    </select>

    <select id="queryExamineStepByNextExamineIdOrderByStepId" resultType="com.kakarote.crm.entity.PO.CrmExamineStep">
        SELECT * FROM wk_crm_examine_step WHERE examine_id = #{examineId}
        and step_num =  ((SELECT step_num FROM wk_crm_examine_step where step_id = #{stepId}) + 1) limit 0,1
    </select>
    <select id="queryByRecordIdAndStatus" resultType="com.alibaba.fastjson.JSONObject">
        select * from wk_crm_examine_log
        where record_id = #{recordId} and examine_status = 0 and is_recheck = 0
    </select>
</mapper>
