<?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="cn.iocoder.yudao.module.inspection.mapper.SiteInfoMapper">

    <resultMap type="cn.iocoder.yudao.module.inspection.entity.SiteInfo" id="SiteInfoResult">
        <result property="id"    column="id"    />
        <result property="siteName"    column="site_name"    />
        <result property="siteLongitude"    column="site_longitude"    />
        <result property="siteLatitude"    column="site_latitude"    />
        <result property="siteAddress"    column="site_address"    />
        <result property="siteContractName"    column="site_contract_name"    />
        <result property="siteContractPhone"    column="site_contract_phone"    />
        <result property="orderNum"    column="order_num"    />
        <result property="createTime"    column="create_time"    />
        <result property="creator"    column="creator"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updater"    column="updater"    />
    </resultMap>

    <sql id="selectSiteInfoVo">
        select id, site_name, site_longitude, site_latitude, site_address, site_contract_name, site_contract_phone, order_num, create_time, creator, update_time, updater from site_info
    </sql>

    <select id="selectSiteInfoList"  resultType="cn.iocoder.yudao.module.inspection.entity.SiteInfo">
        select id, site_name, site_longitude, site_latitude, site_address, site_contract_name,
        site_contract_phone, order_num, create_time, creator, update_time, updater,
        ROUND(ST_Distance_Sphere (POINT (#{vo.longitude}, #{vo.latitude}),POINT (site_longitude, site_latitude ))) AS distance
         from site_info
        <where>
            <if test="vo.siteName != null  and vo.siteName != ''"> and site_name like concat('%', #{vo.siteName}, '%')</if>
            <if test="vo.siteLongitude != null "> and site_longitude = #{vo.siteLongitude}</if>
            <if test="vo.siteLatitude != null "> and site_latitude = #{vo.siteLatitude}</if>
            <if test="vo.siteAddress != null  and vo.siteAddress != ''"> and site_address = #{vo.siteAddress}</if>
            <if test="vo.siteContractName != null  and vo.siteContractName != ''"> and site_contract_name like concat('%', #{vo.siteContractName}, '%')</if>
            <if test="vo.siteContractPhone != null  and vo.siteContractPhone != ''"> and site_contract_phone = #{vo.siteContractPhone}</if>
            <if test="vo.orderNum != null "> and order_num = #{orderNum}</if>
        </where>
        order by distance asc
    </select>

    <select id="selectSiteInfoById" parameterType="Long" resultMap="SiteInfoResult">
        <include refid="selectSiteInfoVo"/>
        where id = #{id}
    </select>

    <insert id="insertSiteInfo" parameterType="cn.iocoder.yudao.module.inspection.entity.SiteInfo">
        insert into site_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="siteName != null">site_name,</if>
            <if test="siteLongitude != null">site_longitude,</if>
            <if test="siteLatitude != null">site_latitude,</if>
            <if test="siteAddress != null">site_address,</if>
            <if test="siteContractName != null">site_contract_name,</if>
            <if test="siteContractPhone != null">site_contract_phone,</if>
            <if test="orderNum != null">order_num,</if>
            <if test="createTime != null">create_time,</if>
            <if test="creator != null">creator,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updater != null">updater,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="siteName != null">#{siteName},</if>
            <if test="siteLongitude != null">#{siteLongitude},</if>
            <if test="siteLatitude != null">#{siteLatitude},</if>
            <if test="siteAddress != null">#{siteAddress},</if>
            <if test="siteContractName != null">#{siteContractName},</if>
            <if test="siteContractPhone != null">#{siteContractPhone},</if>
            <if test="orderNum != null">#{orderNum},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="creator != null">#{creator},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updater != null">#{updater},</if>
         </trim>
    </insert>

    <update id="updateSiteInfo" parameterType="cn.iocoder.yudao.module.inspection.entity.SiteInfo">
        update site_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="siteName != null">site_name = #{siteName},</if>
            <if test="siteLongitude != null">site_longitude = #{siteLongitude},</if>
            <if test="siteLatitude != null">site_latitude = #{siteLatitude},</if>
            <if test="siteAddress != null">site_address = #{siteAddress},</if>
            <if test="siteContractName != null">site_contract_name = #{siteContractName},</if>
            <if test="siteContractPhone != null">site_contract_phone = #{siteContractPhone},</if>
            <if test="orderNum != null">order_num = #{orderNum},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="creator != null">creator = #{creator},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updater != null">updater = #{updater},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSiteInfoById" parameterType="Long">
        delete from site_info where id = #{id}
    </delete>

    <delete id="deleteSiteInfoByIds" parameterType="String">
        delete from site_info where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>


    <select id="selectAll" resultType="com.alibaba.fastjson.JSONObject" parameterType="int">
        select
               user_id as id,
               user_name as username,
               password,
               dept_id as deptId,
               nick_name as nickName,
               user_type as userType,
               email,
               phonenumber as mobile,
               sex,
               avatar,
               status,
               login_ip as loginIp,
               login_date as loginDate,
               create_by as creator,
               create_time as createTime,
               update_by as updater,
               update_time as updateTime,
               open_id as openId,
               jc_open_id as jcOpenId,
               rescue_open_id as rescueOpenId,
               union_id as unionId,
               id_card as idCard,
               id_card_image as idCardImage,
               user_age as userAge,
               invite_id as inviteId
        FROM sys_user
    </select>


    <insert id="addAll" parameterType="cn.iocoder.yudao.module.inspection.entity.NweUsers">
        INSERT INTO system_users
        (id,username,password,dept_id,nickname,
         user_type,email,mobile,sex,
         avatar,status,login_ip,login_date,
         creator,create_time,updater,
         open_id,jc_open_id,rescue_open_id,union_id,
         id_card,id_card_image,user_age,invite_id)
        VALUES(#{id},#{username},#{password},#{deptId},#{nickName},#{userType},
               #{email},#{mobile},#{sex},#{avatar},#{status},#{loginIp},#{loginDate},
               #{creator},#{createTime},#{updater},#{openId},#{jcOpenId},
               #{rescueOpenId},#{unionId},#{idCard},#{idCardImage},#{userAge},#{inviteId})
    </insert>


    <update id="updateUserId1"  >
        update rescue_info set user_id = #{newUserId} where user_id = #{oldUserId}
    </update>

    <update id="updateUserId2"  >
        update rescue_order_info set validation_user_id = #{newUserId} where user_id = #{oldUserId}
    </update>

    <update id="updateUserId3"  >
        update rescue_order_info set user_id = #{newUserId} where user_id = #{oldUserId}
    </update>
    <update id="updateUserId4"  >
        update rescue_order_return set user_id = #{newUserId} where user_id = #{oldUserId}
    </update>
    <update id="updateUserId5"  >
        update driver_info set user_id = #{newUserId} where user_id = #{oldUserId}
    </update>


</mapper>
