<?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.mosty.yjzl.mapper.TbZlxxZxjlMapper">

    <resultMap type="com.mosty.base.model.entity.yjzl.TbZlxxZxjl" id="tbZlZxjl">
        <result property="id" column="id" jdbcType="VARCHAR"/>
        <result property="zlId" column="zl_id" jdbcType="VARCHAR"/>
        <result property="zxsj" column="zxsj" jdbcType="TIMESTAMP"/>
        <result property="zlzxzt" column="zlzxzt" jdbcType="VARCHAR"/>
        <result property="zxnr" column="zxnr" jdbcType="VARCHAR"/>
        <result property="yysc" column="yysc" jdbcType="INTEGER"/>
        <result property="sptpid" column="sptpid" jdbcType="VARCHAR"/>
        <result property="zlzxddXzqhdm" column="zlzxdd_xzqhdm" jdbcType="VARCHAR"/>
        <result property="zlzxdd" column="zlzxdd" jdbcType="TIMESTAMP"/>
        <result property="jd" column="jd" jdbcType="NUMERIC"/>
        <result property="wd" column="wd" jdbcType="NUMERIC"/>
        <result property="zb" column="zb" typeHandler="com.mosty.base.feign.handle.PointTypeHandler"/>
        <result property="zbhash" column="zbhash" jdbcType="VARCHAR"/>
        <result property="zlzxrXzid" column="zlzxr_xzid" jdbcType="VARCHAR"/>
        <result property="zlzxrId" column="zlzxr_id" jdbcType="VARCHAR"/>
        <result property="zlzxrXm" column="zlzxr_xm" jdbcType="INTEGER"/>
        <result property="zlzxrSfz" column="zlzxr_sfz" jdbcType="VARCHAR"/>
        <result property="zlzxrDh" column="zlzxr_dh" jdbcType="VARCHAR"/>
        <result property="fjid" column="fjid" jdbcType="TIMESTAMP"/>
        <result property="ssbm" column="ssbm" jdbcType="VARCHAR"/>
        <result property="ssbmdm" column="ssbmdm" jdbcType="VARCHAR"/>
        <result property="ssbmid" column="ssbmid" jdbcType="VARCHAR"/>
        <result property="ssxgaj" column="ssxgaj" jdbcType="VARCHAR"/>
        <result property="ssxgajdm" column="ssxgajdm" jdbcType="VARCHAR"/>
        <result property="ssxgajid" column="ssxgajid" jdbcType="VARCHAR"/>
        <result property="sssgaj" column="sssgaj" jdbcType="VARCHAR"/>
        <result property="sssgajdm" column="sssgajdm" jdbcType="VARCHAR"/>
        <result property="sssgajid" column="sssgajid" jdbcType="VARCHAR"/>
        <result property="xtSjly" column="xt_sjly" jdbcType="VARCHAR"/>
        <result property="xtSjzt" column="xt_sjzt" jdbcType="VARCHAR"/>
        <result property="xtScbz" column="xt_scbz" jdbcType="VARCHAR"/>
        <result property="xtCjip" column="xt_cjip" jdbcType="VARCHAR"/>
        <result property="xtCjsj" column="xt_cjsj" jdbcType="VARCHAR"/>
        <result property="xtCjrId" column="xt_cjr_id" jdbcType="VARCHAR"/>
        <result property="xtCjr" column="xt_cjr" jdbcType="VARCHAR"/>
        <result property="xtCjbmdm" column="xt_cjbmdm" jdbcType="VARCHAR"/>
        <result property="xtCjbmmc" column="xt_cjbmmc" jdbcType="VARCHAR"/>
        <result property="xtZhgxip" column="xt_zhgxip" jdbcType="VARCHAR"/>
        <result property="xtZhgxsj" column="xt_zhgxsj" jdbcType="VARCHAR"/>
        <result property="xtZhgxrid" column="xt_zhgxrid" jdbcType="VARCHAR"/>
        <result property="xtZhgxr" column="xt_zhgxr" jdbcType="VARCHAR"/>
        <result property="xtZhgxbmdm" column="xt_zhgxbmdm" jdbcType="VARCHAR"/>
        <result property="xtZhgxbm" column="xt_zhgxbm" jdbcType="VARCHAR"/>
        <result property="bz" column="bz" jdbcType="VARCHAR"/>
        <result property="distance" column="distance" jdbcType="NUMERIC"/>
    </resultMap>

    <sql id="base_column_list">
        id,zl_id,zxsj,zlzxzt,zxnr,zlzxdd_xzqhdm,zlzxdd,jd,wd,zb,zbhash,zlzxr_xzid,
        zlzxr_id,zlzxr_xm,zlzxr_sfz,zlzxr_dh,fjid,zxfj,yysc,sptpid
    </sql>

    <sql id="Blob_Column_List">
        ST_ASWKT
        (zb) as zb
    </sql>

    <insert id="insertEntity" parameterType="com.mosty.base.model.entity.yjzl.TbZlxxZxjl">
        insert into tb_zlxx_zxjl(id,zl_id,zxsj,zlzxzt,zxnr,zlzxdd_xzqhdm,zlzxdd,jd,wd,zb,zbhash,zlzxr_xzid,
                          zlzxr_id,zlzxr_xm,zlzxr_sfz,zlzxr_dh,fjid,zxfj,yysc,sptpid,
                          ssbm, ssbmdm,ssbmid, ssxgaj, ssxgajdm,ssxgajid, sssgaj, sssgajdm,sssgajid,
                           xt_sjly, xt_sjzt, xt_scbz,
                          xt_cjip, xt_cjsj, xt_cjr_id, xt_cjr, xt_cjbmdm, xt_cjbmmc, xt_zhgxip, xt_zhgxsj,
                          xt_zhgxrid, xt_zhgxr, xt_zhgxbmdm, xt_zhgxbm, bz)
        values (#{id},#{zlId},#{zxsj},#{zlzxzt},#{zxnr},#{zlzxddXzqhdm},#{zlzxdd},#{jd},#{wd},
                ST_GEOMFROMTEXT(#{zb,typeHandler = com.mosty.base.feign.handle.PointTypeHandler}),
                #{zbhash},#{zlzxrXzid},#{zlzxrId},#{zlzxrXm},#{zlzxrSfz},#{zlzxrDh},#{fjid},#{zxfj},#{yysc},#{sptpid},
                #{ssbm}, #{ssbmdm},#{ssbmid}, #{ssxgaj}, #{ssxgajdm},#{ssxgajid}, #{sssgaj}, #{sssgajdm},#{sssgajid},
                #{xtSjly}, #{xtSjzt}, #{xtScbz},
                #{xtCjip}, #{xtCjsj}, #{xtCjrId}, #{xtCjr}, #{xtCjbmdm}, #{xtCjbmmc}, #{xtZhgxip}, #{xtZhgxsj},
                #{xtZhgxrid}, #{xtZhgxr}, #{xtZhgxbmdm}, #{xtZhgxbm}, #{bz})
    </insert>

    <!--查询指令数量-->
    <select id="getCount" resultType="Integer" parameterType="Map">
        select count(1) from tb_zlxx where xt_sjzt = '1' and xt_scbz = '0'
        <if test="dto.zllx != null and dto.zllx != ''">
            and zllx = #{dto.zllx}
        </if>
        <if test="dto.zlly != null and dto.zlly != ''">
            and zlly = #{dto.zlly}
        </if>

        <if test="dto.zlbt != null and dto.zlbt != ''">
            and zlbt like concat('%',#{dto.zlbt},'%')
        </if>
        <if test="dto.zlnr != null and dto.zlnr != ''">
            and zlnr like concat('%',#{dto.zlnr},'%')
        </if>
        <if test="dto.zldj != null and dto.zldj != ''">
            and zldj = #{dto.zldj}
        </if>
        <if test="dto.sfcl != null and dto.sfcl != ''">
            and sfcl = #{dto.sfcl}
        </if>
        <if test="dto.startTime != null and dto.startTime != '' and dto.endTime != null and dto.endTime != ''">
            and zlrq between #{dto.startTime} and #{dto.endTime}
        </if>
        <if test="dto.jd != null and dto.wd != null and dto.radius != null">
            <![CDATA[and st_distance_sphere(POINT(jd,wd),POINT(#{dto.jd},#{dto.wd})) <= #{dto.radius}]]>
        </if>
    </select>

    <!--查询指令列表-->
    <select id="getListPage" resultMap="tbZlZxjl" parameterType="Map">
        select
        <include refid="base_column_list"/>
        <if test="dto.jd != null and dto.wd != null and dto.radius != null">
            ,st_distance_sphere(POINT(jd,wd),POINT(#{dto.jd},#{dto.wd})) as distance
        </if>
        from tb_zlxx where xt_sjzt = '1' and xt_scbz = '0'
        <if test="dto.zllx != null and dto.zllx != ''">
            and zllx = #{dto.zllx}
        </if>
        <if test="dto.zlly != null and dto.zlly != ''">
            and zlly = #{dto.zlly}
        </if>

        <if test="dto.zlbt != null and dto.zlbt != ''">
            and zlbt like concat('%',#{dto.zlbt},'%')
        </if>
        <if test="dto.zlnr != null and dto.zlnr != ''">
            and zlnr like concat('%',#{dto.zlnr},'%')
        </if>
        <if test="dto.zldj != null and dto.zldj != ''">
            and zldj = #{dto.zldj}
        </if>
        <if test="dto.sfcl != null and dto.sfcl != ''">
            and sfcl = #{dto.sfcl}
        </if>
        <if test="dto.startTime != null and dto.startTime != '' and dto.endTime != null and dto.endTime != ''">
            and zlrq between #{dto.startTime} and #{dto.endTime}
        </if>
        <if test="dto.jd != null and dto.wd != null and dto.radius != null">
            <![CDATA[having distance <= #{dto.radius}]]>
        </if>
        order by zlfssj desc
        limit #{pageIndex},#{pageSize}
    </select>

    <select id="selectInstructCount" resultType="java.util.Map">
        SELECT count(*) zlzs,
        (SELECT count(*) zlzs
        FROM `tb_zlxx`
        where and xt_sjzt = '1' and xt_scbz = '0'
        <if test="startTime != null and endTime != null">
            and zlrq between #{startTime} and #{endTime}
        </if>
        ) zlzxs
        FROM `tb_zlxx`
        where xt_sjzt = '1' and xt_scbz = '0'
        <if test="startTime != null and endTime != null">
            and zlrq between #{startTime} and #{endTime}
        </if>
    </select>


    <!--根据ID删除-->
    <update id="deleteZxjl">
        update tb_zlxx_zxjl set xt_scbz = '1' where id in
        <foreach collection="list" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </update>

    <!--根据指令ID删除明细-->
    <delete id="deleteByZlid">
        delete from tb_zlxx_zxjl where zl_id = #{zlid}
    </delete>

</mapper>
