<?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_address" 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_address`,
               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>

    <select id="countByProjectIds" resultType="com.chushang.inspection.project.vo.CountVO">
        SELECT COUNT(*) AS total, wi.project_id AS projectId
        FROM wrk_info wi
        where wi.del_state = 0
        <if test="projectIds != null and projectIds.size() > 0">
            AND wi.project_id IN
            <foreach collection="projectIds" open="(" close=")" item="projectId" separator=",">
                #{projectId}
            </foreach>
        </if>
        <if test="wrkTypes != null and wrkTypes.size() > 0">
            AND wi.work_type IN
            <foreach collection="wrkTypes" open="(" close=")" separator="," item="wrkType">
                #{wrkType}
            </foreach>
        </if>
        GROUP BY wi.project_id
    </select>

    <select id="wrkData" resultType="com.chushang.inspection.work.vo.WrkDataVO">
        SELECT
        wi.dept_id AS deptId,
        p.project_name AS projectName,
        pt.project_id AS projectId,
        pt.NAME AS taskName,
        pt.id AS taskId,
        COALESCE ( t.totalTer, 0 ) AS totalTer,
        COALESCE ( w.totalWrk, 0 ) AS totalWrk,
        COALESCE ( s.totalSuc, 0 ) AS totalSuc,
        SUM( CASE WHEN w2.end_time IS NOT NULL AND w2.end_time &lt;= wi.dispose_time THEN 1 ELSE 0 END ) OVER ( PARTITION BY wi.project_id ) AS totalTimeOut
        FROM
        wrk_info wi
        LEFT JOIN ta_polling_task pt ON pt.id = wi.lower_task_id
        LEFT JOIN wrk_project p ON pt.project_id = p.project_id
        LEFT JOIN ( SELECT project_id, COUNT(*) AS totalTer FROM wrk_info_terminal_record GROUP BY project_id ) t ON wi.project_id = t.project_id
        LEFT JOIN ( SELECT project_id, COUNT(*) AS totalWrk FROM wrk_info GROUP BY project_id ) w ON wi.project_id = w.project_id
        LEFT JOIN ( SELECT project_id, COUNT(*) AS totalSuc FROM wrk_info WHERE wrk_status IN ( 4, 7, 8 ) GROUP BY project_id ) s ON wi.project_id = s.project_id
        LEFT JOIN wrk_info w2 ON wi.project_id = w2.project_id
        WHERE wi.del_state = 0
            <if test="1 == 1">
                ${query.sqlParam.get('sqlWhere')}
            </if>
            <if test="1 == 1">
                ${query.sqlParam.get('dataScope')}
            </if>
        GROUP BY pt.project_id
    </select>

    <select id="empData" resultType="com.chushang.inspection.project.vo.EmployeeVO">
        SELECT user_id as userId,dept_id as deptId,count(user_id) as dispatchNum,count(CASE WHEN wrk_status = 4 OR wrk_status = 7
        THEN 1 ELSE 0 END) as aduitNum
        FROM wrk_info
        where del_state = 0
        <if test="1 == 1">
            ${query.sqlParam.get('sqlWhere')}
        </if>
        GROUP BY user_id
    </select>

    <select id="empDetail" resultType="com.chushang.inspection.project.vo.EmployeeVO">
        SELECT t1.user_id as userId,
               t1.dept_id as deptId,
               t1.lower_task_name as taskName,
               t2.project_name as projectName,
               count(1) as dispatchNum,count(CASE WHEN t1.wrk_status = 4 OR t1.wrk_status = 7
                THEN 1 ELSE 0 END) as aduitNum
        FROM wrk_info t1 inner join wrk_project t2 on t1.project_id = t2.project_id
        where t1.del_state = 0
        and t1.user_id = #{userId}
        GROUP BY t1.project_id,t1.lower_task_id
    </select>
</mapper>
