<?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.terminal.mapper.TerminalMapper">
    <resultMap id="BaseResultMap" type="com.chushang.inspection.terminal.po.Terminal">
        <!--@mbg.generated-->
        <!--@Table st_terminal-->
        <id column="terminal_id" jdbcType="BIGINT" property="terminalId"/>
        <result column="dept_id" jdbcType="BIGINT" property="deptId"/>
        <result column="task_id" jdbcType="BIGINT" property="taskId"/>
        <result column="lower_task_id" jdbcType="BIGINT" property="lowerTaskId"/>
        <result column="store_id" jdbcType="BIGINT" property="storeId"/>
        <result column="store_no" jdbcType="VARCHAR" property="storeNo"/>
        <result column="terminal_sn" jdbcType="VARCHAR" property="terminalSn"/>
        <result column="terminal_no" jdbcType="VARCHAR" property="terminalNo"/>
        <result column="terminal_type" jdbcType="TINYINT" property="terminalType"/>
        <result column="terminal_model" jdbcType="VARCHAR" property="terminalModel"/>
        <result column="terminal_version" jdbcType="VARCHAR" property="terminalVersion"/>
        <result column="terminal_address" jdbcType="VARCHAR" property="terminalAddress"/>
        <result column="terminal_source" jdbcType="TINYINT" property="terminalSource"/>
        <result column="terminal_property" jdbcType="TINYINT" property="terminalProperty"/>
        <result column="occupy" jdbcType="BOOLEAN" property="occupy"/>
        <result column="terminal_status" jdbcType="TINYINT" property="terminalStatus"/>
        <result column="remark" jdbcType="VARCHAR" property="remark"/>
        <result column="version" jdbcType="BIGINT" property="version"/>
        <result column="del_state" jdbcType="BOOLEAN" property="delState"/>
        <result column="create_by" jdbcType="VARCHAR" property="createBy"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_by" jdbcType="VARCHAR" property="updateBy"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
    </resultMap>
    <sql id="Base_Column_List">
        <!--@mbg.generated-->
        terminal_id,
        dept_id,
        task_id,
        lower_task_id,
        store_id,
        store_no,
        terminal_sn,
        terminal_no,
        terminal_type,
        terminal_model,
        terminal_version,
        terminal_address,
        terminal_source,
        terminal_property,
        occupy,
        terminal_status,
        remark,
        version,
        del_state,
        create_by,
        create_time,
        update_by,
        update_time
    </sql>

    <select id="pageList" resultType="com.chushang.inspection.terminal.vo.StoreTerminalVO">
        <include refid="selectStoreTerminalSql"/>
        <if test="1 == 1">
            ${query.sqlParam.get('sqlWhere')}
        </if>
        <if test="1 == 1">
            ${query.sqlParam.get('dataScope')}
        </if>
        ORDER BY #{query.orderBy} #{query.isAsc}
    </select>
    <sql id="selectStoreTerminalSql">
        SELECT pt.project_id          AS projectId,
               p.project_name         AS projectName,
               pt.id                  AS task_id,
               pt.`name`              AS taskName,
               s.lower_task_id        AS lowerTaskId,
               lpt.`name`             AS lowerTaskName,
               s.dept_id              AS deptId,
               s.store_id             AS storeId,
               s.store_no             AS storeNo,
               s.store_name           AS storeName,
               s.store_contact        AS storeContact,
               s.store_phone          AS storePhone,
               s.shop_name            AS shopName,
               s.special_num          AS specialNum,
               s.store_address        AS storeAddress,
               s.store_type           AS storeType,
               s.products             AS products,
               s.tip_tool             AS tipTool,
               s.register_time        AS registerTime,
               s.register_address     AS registerAddress,
               s.legal_name           AS legalName,
               s.create_by            AS createBy,
               s.create_time          AS createTime,
               s.state                AS storeState,
               s.ins_fre              AS insFre,
               s.account_phone        AS accountPhone,
               s.account_manager      AS accountManager,
               t.terminal_id          AS terminalId,
               t.terminal_no          AS terminalNo,
               t.terminal_sn          AS terminalSn,
               t.terminal_type        AS terminalType,
               t.terminal_model       AS terminalModel,
               t.terminal_version     AS terminalVersion,
               t.terminal_address     AS terminalAddress,
               t.terminal_source      AS terminalSource,
               t.occupy               AS occupy,
               t.terminal_status      AS terminalStatus,
               ti.deviation           AS deviation,
               ti.operator_id         AS operatorId,
               ti.inspection_status   AS inspectionStatus,
               ti.inspection_time     AS inspectionTime,
               ti.times_pass          AS timesPass,
               ti.service_result      AS serviceResult,
               ti.geographic_address  AS geographicAddress,
               ti.geographic_location AS geographicLocation,
               ti.work_location       AS workLocation,
               ti.work_adderss        AS workAddress
        FROM st_terminal t
                 LEFT JOIN st_terminal_ins ti ON t.terminal_id = ti.terminal_id
                 INNER JOIN st_store s ON t.store_id = s.store_id
                 INNER JOIN ta_polling_task pt ON t.task_id = pt.id
                 INNER JOIN wrk_project p ON pt.project_id = p.project_id
                 LEFT JOIN ta_polling_task lpt ON t.lower_task_id = lpt.id
        where t.del_state = 0
    </sql>
    <select id="selectPageApp" resultType="com.chushang.inspection.project.vo.TerminalAppVO">
        SELECT t.terminal_id        AS terminalId,
               t.terminal_no        AS terminalNo,
               t.terminal_address   AS terminalAddress,
               ti.inspection_status AS inspectionStatus,
               ti.inspection_time   AS inspectionTime,
               ti.times_pass        AS timesPass,
               ti.service_result    AS serviceResults,
               s.store_contact      AS storeContact,
               s.store_address      AS storeAddress,
               s.ins_fre            AS insFre,
               s.store_name         AS storeName,
               pt.`name`            AS taskName
        FROM st_terminal t
                 LEFT JOIN st_terminal_ins ti ON t.terminal_id = ti.terminal_id
                 LEFT JOIN ta_polling_task pt on pt.id = t.lower_task_id
                 INNER JOIN st_store s ON s.store_id = t.store_id
        WHERE t.del_state = 0
        <if test="query.taskId != null">
            AND t.task_id = #{query.taskId}
        </if>
        <if test="query.storeNo != null">
            AND t.store_no = #{query.storeNo}
        </if>
        <if test="query.terminalNo != null">
            AND t.terminal_no = #{query.terminalNo}
        </if>
        <if test="query.terminalSn != null">
            AND t.terminal_no = #{query.terminalSn}
        </if>
        <if test="query.terminalAddress != null and query.terminalAddress != ''">
            AND t.terminal_address like concat('%', #{query.terminalAddress}, '%')
        </if>
        <if test="query.occupy != null">
            AND t.occupy = #{query.occupy}
        </if>
        <if test="query.storeState != null">
            AND s.state = #{query.storeState}
        </if>
        <if test="query.shopName != null and query.shopName != ''">
            AND s.shop_name like concat('%', #{query.shopName}, '%')
        </if>
        <if test="query.lowerTaskName != null and query.lowerTaskName != ''">
            AND pt.name like concat('%', #{query.lowerTaskName}, '%')
        </if>
        <if test="query.storeName != null and query.storeName != ''">
            AND s.store_name like concat('%', #{query.storeName}, '%')
        </if>
        <if test="query.storeContact != null and query.storeContact != ''">
            AND s.store_contact like concat('%', #{query.storeContact}, '%')
        </if>
        <if test="query.storeAddress != null and query.storeAddress != ''">
            AND s.store_address like concat('%', #{query.storeAddress}, '%')
        </if>
        <if test="1 == 1">
            ${query.sqlParam.get('dataScope')}
        </if>
        ORDER BY t.create_time DESC
    </select>

    <select id="getStoreByTasKIdOrIds" resultType="com.chushang.inspection.work.dto.DispatchDTO">
        select t1.terminal_id         AS terminalId,
               t1.terminal_sn         AS terminalSn,
               t1.terminal_no         AS terminalNo,
               t1.terminal_type       AS terminalType,
               t1.terminal_model      AS terminalModel,
               t1.terminal_version    AS terminalVersion,
               t1.terminal_address    AS terminalAddress,
               t1.terminal_source     AS terminalSource,
               t1.terminal_property   AS terminalProperty,
               t2.store_no            AS storeNo,
               t2.store_id            AS storeId,
               t2.store_name          AS storeName,
               t2.store_contact       AS storeContact,
               t2.store_phone         AS storePhone,
               t2.store_address       AS storeAddress,
               t2.special_num         AS specialNum,
               t2.shop_name           AS shopName,
               t2.store_type          AS storeType,
               t2.products            AS products,
               t2.tip_tool            AS tipTool,
               t2.ins_fre             AS insFre,
               t2.task_id             AS taskId,
               t2.lower_task_id       AS lowerTaskId,
               t2.register_address    AS registerAddress,
               t2.legal_name          AS legalName,
               t2.account_manager     AS accountManager,
               t3.management_code     AS managementCode,
               t3.geographic_location AS geographicLocation,
               t3.geographic_address  AS geographicAddress
        from st_terminal t1
                 left join st_store t2 on t1.store_id = t2.store_id
                 left join st_terminal_ins t3 on t3.terminal_id = t1.terminal_id
        WHERE t1.del_state = 0
        <if test="query.taskId != null">
            AND t1.task_id = #{query.taskId}
        </if>
        <if test="query.nos != null and query.nos.size > 0">
            AND t2.store_no in
            <foreach collection="query.nos" item="no" open="(" separator="," close=")">
                #{no}
            </foreach>
        </if>
        <if test="query.terminalNos != null and query.terminalNos.size > 0">
            AND t1.terminal_no in
            <foreach collection="query.terminalNos" item="no" open="(" separator="," close=")">
                #{no}
            </foreach>
        </if>
        <if test="query.terminalIds != null and query.terminalIds.size > 0">
            AND t1.terminal_id in
            <foreach collection="query.terminalIds" item="id" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
    </select>
</mapper>
