<?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.casic.missiles.mapper.business.BusinessSiteExecutiveLogMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.casic.missiles.model.business.BusinessSiteExecutiveLog">
        <id column="id" property="id" />
        <result column="log_no" property="logNo" />
        <result column="site_executive_id" property="siteExecutiveId" />
        <result column="contacts" property="contacts" />
        <result column="contact_hone" property="contactHone" />
        <result column="executive_address" property="executiveAddress" />
        <result column="output_voltage" property="outputVoltage" />
        <result column="zero_ground_voltage" property="zeroGroundVoltage" />
        <result column="temperature" property="temperature" />
        <result column="humidity" property="humidity" />
        <result column="remark" property="remark" />
        <result column="is_del" property="isDel" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, log_no, site_executive_id, contacts, contact_hone, executive_address, output_voltage, zero_ground_voltage, temperature, humidity, remark, is_del, create_time, update_time
    </sql>

    <select id="siteExecutiveLogListPage" resultType="com.casic.missiles.model.business.BusinessSiteExecutiveLog">
        SELECT
        sel.id as id,sel.log_name AS logName,su.name  AS recorderName,lab.organize_name as laboratoryName,sel.executive_address as executiveAddress,sel.log_no as logNo,record_time AS recordTime,sel.create_time AS createTime,customerName
        FROM
        business_site_executive_log sel
        JOIN(
            SELECT  customerName,id
            FROM business_site_executive_info
            JOIN(
                SELECT customer_name AS customerName,id AS customerId
                FROM customer_info
                where is_del=0
                <if test="request.customerName != null and request.customerName !=''">
                    and customer_name like concat('%', #{request.customerName}, '%')
                </if>
             )cs ON  customer_id=cs.customerId
            WHERE is_del = 0
        ) bsei ON bsei.id = sel.site_executive_id
        LEFT JOIN (
               SELECT id, organize_name
               FROM meter_organize
               WHERE is_del = 0 AND is_lab = 1
               ) lab ON lab.id = sel.laboratory_id
        LEFT JOIN sys_user su ON su.id = sel.recorder where 1=1
        <if test="request.logNo != null and request.logNo !=''">
            and sel.log_no like concat('%', #{request.logNo}, '%')
        </if>
        <if test="request.recorderName != null and request.recorderName !=''">
            and su.name like concat('%', #{request.recorderName}, '%')
        </if>
        <if test="request.laboratoryName != null and request.laboratoryName !=''">
            and lab.organize_name like concat('%', #{request.laboratoryName}, '%')
        </if>
        <if test="request.ids != null and request.ids.size()>0">
            and id in
            <foreach collection="request.ids" item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        order  by sel.update_time desc
    </select>

    <select id="getLaboratoryName" resultType="String">
        SELECT organize_name
        FROM meter_organize
        WHERE is_del = 0 AND is_lab = 1
          AND id =#{laboratoryId}
    </select>

</mapper>
