<?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.chushang.inspection.work.mapper.WrkInfoMapper">
    <select id="queryDispatchPage" resultMap="wrkInfoDetailsVOResult">
        <include refid="wrkInfoDetailSql" />
            <if test="1 == 1">
                ${query.sqlParam.get('sqlWhere')}
            </if>
        <if test="1 == 1">
            ${query.sqlParam.get('dataScope')}
        </if>
        ORDER BY i.create_time desc
    </select>

    <select id="exportDispatchPage" resultType="com.chushang.inspection.work.vo.WrkInfoExportVO">
        <include refid="wrkInfoDetailSql" />
        <if test="1 == 1">
            ${query.sqlParam.get('sqlWhere')}
        </if>
        <if test="1 == 1">
            ${query.sqlParam.get('dataScope')}
        </if>
        ORDER BY i.create_time desc
    </select>
    <resultMap id="resultWrkInfoExportVO" type="com.chushang.inspection.work.vo.WrkInfoExportVO">
        <id column="terminal_id" property="terminalId" />
        <result column="user_name" property="userName" />
        <result column="create_time" property="createTime" />
        <result column="legal_name" property="legalName" />
        <result column="terminal_address" property="terminalAddress" />
        <result column="terminal_no" property="terminalNo" />
        <result column="terminal_sn" property="terminalSn" />
        <result column="terminal_source" property="terminalSource" />
        <result column="work_no" property="workNo" />
    </resultMap>

    <select id="queryArchivePage" resultMap="wrkInfoDetailsVOResult">
        <include refid="wrkInfoDetailSql" />
            <if test="1 == 1">
                ${query.sqlParam.get('sqlWhere')}
            </if>
        <if test="1 == 1">
            ${query.sqlParam.get('dataScope')}
        </if>
        ORDER BY i.create_time desc
    </select>

    <select id="queryAppPage" resultType="com.chushang.inspection.work.vo.WrkListAppVO">
        SELECT i.wrk_id                                   AS wrkId,
               i.work_type                                AS workType,
               i.task_name                                AS taskName,
               isr.store_name                             AS storeName,
               isr.store_contact                          AS storeContact,
               isr.store_phone                            AS storePhone,
               isr.ins_fre                                AS insFre,
               itr.terminal_id                            AS terminalId,
               itr.terminal_address                       AS terminalAddress,
               (SELECT count(w1.wrk_id)
                FROM wrk_info w1
                         INNER JOIN
                     wrk_info_terminal_record w2 ON w1.wrk_id = w2.wrk_id
                WHERE w2.terminal_id = itr.terminal_id
                  AND w1.wrk_status = #{query.wrkStatus}) AS numberOfPickups,
               (SELECT w1.dispose_time
                FROM wrk_info w1
                         INNER JOIN
                     wrk_info_terminal_record w2 ON w1.wrk_id = w2.wrk_id
                WHERE w2.terminal_id = itr.terminal_id
                  AND w1.wrk_status = #{query.wrkStatus}
                order by w1.dispose_time desc
                limit 1)                                  AS lastDisposeTime,
               (SELECT w1.service_result
                FROM wrk_info w1
                         INNER JOIN
                     wrk_info_terminal_record w2 ON w1.wrk_id = w2.wrk_id
                WHERE w2.terminal_id = itr.terminal_id
                  AND w1.wrk_status = #{query.wrkStatus}
                order by w1.dispose_time desc
                limit 1)                                  AS lastServiceResult
        FROM wrk_info i
                 INNER JOIN wrk_info_store_record isr ON i.wrk_id = isr.wrk_id
                 INNER JOIN wrk_info_terminal_record itr ON i.wrk_id = itr.wrk_id
        where i.del_state = 0
            <if test="1 == 1">
                ${query.sqlParam.get('sqlWhere')}
            </if>
        <if test="1 == 1">
            ${query.sqlParam.get('dataScope')}
        </if>
        ORDER BY i.create_time desc
    </select>
    <resultMap id="wrkInfoDetailsVOResult" type="com.chushang.inspection.work.vo.WrkInfoDetailsVO">
        <association property="wrkInfo" javaType="com.chushang.inspection.work.vo.WrkInfoVO">
            <id column="wrk_id" property="wrkId" />
            <result column="remark" property="remark" />
            <result column="account_manager" property="accountManager" />
            <result column="account_phone" property="accountPhone" />
            <result column="create_by" property="createBy" />
            <result column="create_time" property="createTime" />
            <result column="dept_id" property="deptId" />
            <result column="dept_name" property="deptName" />
            <result column="dispose_time" property="disposeTime" />
            <result column="end_time" property="endTime" />
            <result column="lower_task_id" property="lowerTaskId" />
            <result column="lower_task_name" property="lowerTaskName" />
            <result column="service_result" property="serviceResult" />
            <result column="task_id" property="taskId" />
            <result column="task_name" property="taskName" />
            <result column="user_id" property="userId" />
            <result column="user_name" property="userName" />
            <result column="work_method" property="workMethod" />
            <result column="work_no" property="workNo" />
            <result column="work_sort" property="workSort" />
            <result column="work_source" property="workSource" />
            <result column="work_type" property="workType" />
            <result column="wrk_status" property="wrkStatus"/>
        </association>
        <association property="infoStore" javaType="com.chushang.inspection.work.vo.WrkInfoStoreVO">
            <id column="store_id" property="storeId" />

            <result column="ins_fre" property="insFre" />
            <result column="legal_name" property="legalName" />
            <result column="products" property="products" />
            <result column="register_address" property="registerAddress" />
            <result column="shop_name" property="shopName" />
            <result column="special_num" property="specialNum" />
            <result column="store_address" property="storeAddress" />
            <result column="store_contact" property="storeContact" />
            <result column="store_name" property="storeName" />
            <result column="store_no" property="storeNo" />
            <result column="store_phone" property="storePhone" />
            <result column="state" property="storeStatus" />
            <result column="store_type" property="storeType" />
            <result column="tip_tool" property="tipTool" />
        </association>
        <association property="infoTerminal" javaType="com.chushang.inspection.work.vo.WrkInfoTerminalVO">
            <id column="terminal_id" property="terminalId" />
            <result column="terminal_address" property="terminalAddress" />
            <result column="terminal_model" property="terminalModel" />
            <result column="terminal_no" property="terminalNo" />
            <result column="terminal_property" property="terminalProperty" />
            <result column="terminal_sn" property="terminalSn" />
            <result column="terminal_source" property="terminalSource" />
            <result column="terminal_status" property="terminalStatus" />
            <result column="terminal_type" property="terminalType" />
            <result column="terminal_version" property="terminalVersion" />
        </association>
        <association property="infoTerminalIns" javaType="com.chushang.inspection.work.vo.WrkInfoTerminalInsVO">
            <id column="terminal_id" property="terminalId" />
            <result column="deviation" property="deviation" />
            <result column="geographic_location" property="geographicLocation" />
            <result column="work_adderss" property="workAddress" />
            <result column="work_location" property="workLocation" />
            <result column="geographic_address" property="locationAddress" />
            <result column="pre_code_encoding" property="preCodeEncoding" />
            <result column="service_result" property="serviceResult" />
            <result column="storage_time" property="storageTime" />
            <result column="times_pass" property="timesPass" />
            <result column="delivery_time" property="deliveryTime" />
            <result column="inspection_status" property="inspectionStatus" />
            <result column="inspection_time" property="inspectionTime" />
        </association>
    </resultMap>
    <select id="listInsTemplate" resultMap="wrkInfoDetailsVOResult">
        <include refid="wrkInfoDetailSql" />
        <if test="1 == 1">
            ${query.sqlParam.get('sqlWhere')}
        </if>
        <if test="1 == 1">
            ${query.sqlParam.get('dataScope')}
        </if>
        ORDER BY i.create_time desc
    </select>
    <select id="getWrkInfoDetails" resultMap="wrkInfoDetailsVOResult">
        <include refid="wrkInfoDetailSql" />
        AND i.wrk_id = #{wrkId}
        ORDER BY i.create_time desc
    </select>
    <sql id="wrkInfoDetailSql">
        SELECT
            i.wrk_id,
            i.`dept_id`,
            i.`task_id`,
            i.`lower_task_id`,
            i.`user_id`,
            i.`user_name`,
            i.`work_no`,
            i.`work_type`,
            i.`work_method`,
            i.`work_sort`,
            i.`work_source`,
            i.`dispose_time`,
            i.`account_manager`,
            i.`account_phone`,
            i.`service_result`,
            i.`remark`,
            i.`end_time`,
            i.`create_by`,
            i.`create_time`,
            i.`task_name`,
            i.`lower_task_name`,
            i.`dept_name`,
            i.`wrk_status`,

            isr.`store_id`,
            isr.`state`,
            isr.`store_no`,
            isr.`store_name`,
            isr.`store_contact`,
            isr.`store_phone`,
            isr.`store_address`,
            isr.`special_num`,
            isr.`shop_name`,
            isr.`store_type`,
            isr.`products`,
            isr.`tip_tool`,
            isr.`ins_fre`,
            isr.`register_address`,
            isr.`legal_name`,

            itir.`geographic_location`,
            itir.`geographic_address`,
            itir.`work_location`,
            itir.`work_adderss`,
            itir.`deviation`,
            itir.`pre_code_encoding`,
            itir.`service_result`,
            itir.`deviation`,
            itir.`storage_time`,
            itir.`delivery_time`,
            itir.`times_pass`,
            itir.`inspection_status`,
            itir.`inspection_time`,

            itr.`terminal_id`,
            itr.`terminal_sn`,
            itr.`terminal_type`,
            itr.`terminal_model`,
            itr.`terminal_no`,
            itr.`terminal_source`,
            itr.`terminal_address`,
            itr.`terminal_version`,
            itr.`terminal_property`,
            itr.`terminal_status`
        FROM
            `wrk_info` i
                LEFT JOIN wrk_info_store_record isr ON i.wrk_id = isr.wrk_id
                LEFT JOIN wrk_info_terminal_record itr ON i.wrk_id = itr.wrk_id
                LEFT JOIN wrk_info_terminal_ins_record itir ON i.wrk_id = itir.wrk_id
        WHERE i.del_state = 0
    </sql>
</mapper>
