<?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.base.manager.mapper.TbJcglXfllMapper">
    <resultMap type="com.mosty.base.model.vo.jcgl.TbJcglXfllVo" id="TbJcglXfllMap">
        <result property="id" column="id" jdbcType="VARCHAR"/>
        <result property="ryid" column="ryid" jdbcType="VARCHAR"/>
        <result property="fl" column="fl" jdbcType="VARCHAR"/>
        <result property="lx" column="lx" jdbcType="VARCHAR"/>
        <result property="xm" column="xm" jdbcType="VARCHAR"/>
        <result property="xbdm" column="xbdm" jdbcType="VARCHAR"/>
        <result property="sfzh" column="sfzh" jdbcType="VARCHAR"/>
        <result property="jh" column="jh" jdbcType="VARCHAR"/>
        <result property="jx" column="jx" jdbcType="VARCHAR"/>
        <result property="zzmm" column="zzmm" jdbcType="VARCHAR"/>
        <result property="hyzk" column="hyzk" jdbcType="VARCHAR"/>
        <result property="rzsj" column="rzsj" jdbcType="TIMESTAMP"/>
        <result property="csrq" column="csrq" jdbcType="TIMESTAMP"/>
        <result property="mzdm" column="mzdm" jdbcType="VARCHAR"/>
        <result property="whcddm" column="whcddm" jdbcType="VARCHAR"/>
        <result property="jgdm" column="jgdm" jdbcType="VARCHAR"/>
        <result property="jtzz" column="jtzz" jdbcType="VARCHAR"/>
        <result property="lxdh" column="lxdh" jdbcType="VARCHAR"/>
        <result property="dzyx" column="dzyx" jdbcType="VARCHAR"/>
        <result property="tp" column="tp" jdbcType="VARCHAR"/>
        <result property="ssbm" column="ssbm" jdbcType="VARCHAR"/>
        <result property="ssbmdm" column="ssbmdm" jdbcType="VARCHAR"/>
        <result property="ssxgaj" column="ssxgaj" jdbcType="VARCHAR"/>
        <result property="ssxgajdm" column="ssxgajdm" 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="ssxgajid" column="ssxgajid" jdbcType="VARCHAR"/>
        <result property="ssbmid" column="ssbmid" jdbcType="VARCHAR"/>
        <result property="bz" column="bz" jdbcType="VARCHAR"/>
        <collection property="expList" javaType="ArrayList"
                    ofType="com.mosty.base.model.vo.jcgl.TbJcglBqVo" select="selectExpTag"
                    column="{xfll_id = id}"/>
        <collection property="sklList" javaType="ArrayList"
                    ofType="com.mosty.base.model.vo.jcgl.TbJcglBqVo" select="selectSklTag" column="{xfll_id = id}"/>
        <collection property="xljlList" javaType="ArrayList"
                    ofType="com.mosty.base.model.vo.jcgl.TbJcglBqVo" select="selectXljl"
                    column="{xfll_id = id}"/>
    </resultMap>

    <resultMap type="com.mosty.base.model.vo.jcgl.TbJcglZyjz" id="tbJcglZyjz">
        <result property="id" column="id" jdbcType="VARCHAR"/>
        <result property="ryid" column="ryid" jdbcType="VARCHAR"/>
        <result property="fl" column="fl" jdbcType="VARCHAR"/>
        <result property="lx" column="lx" jdbcType="VARCHAR"/>
        <result property="xm" column="xm" jdbcType="VARCHAR"/>
        <result property="xbdm" column="xbdm" jdbcType="VARCHAR"/>
        <result property="sfzh" column="sfzh" jdbcType="VARCHAR"/>
        <result property="jh" column="jh" jdbcType="VARCHAR"/>
        <result property="jx" column="jx" jdbcType="VARCHAR"/>
        <result property="zzmm" column="zzmm" jdbcType="VARCHAR"/>
        <result property="hyzk" column="hyzk" jdbcType="VARCHAR"/>
        <result property="rzsj" column="rzsj" jdbcType="TIMESTAMP"/>
        <result property="csrq" column="csrq" jdbcType="TIMESTAMP"/>
        <result property="mzdm" column="mzdm" jdbcType="VARCHAR"/>
        <result property="whcddm" column="whcddm" jdbcType="VARCHAR"/>
        <result property="jgdm" column="jgdm" jdbcType="VARCHAR"/>
        <result property="jtzz" column="jtzz" jdbcType="VARCHAR"/>
        <result property="lxdh" column="lxdh" jdbcType="VARCHAR"/>
        <result property="dzyx" column="dzyx" jdbcType="VARCHAR"/>
        <result property="tp" column="tp" jdbcType="VARCHAR"/>
        <result property="ssbm" column="ssbm" jdbcType="VARCHAR"/>
        <result property="ssbmdm" column="ssbmdm" jdbcType="VARCHAR"/>
        <result property="ssxgaj" column="ssxgaj" jdbcType="VARCHAR"/>
        <result property="ssxgajdm" column="ssxgajdm" 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="ssxgajid" column="ssxgajid" jdbcType="VARCHAR"/>
        <result property="ssbmid" column="ssbmid" jdbcType="VARCHAR"/>
        <result property="bz" column="bz" jdbcType="VARCHAR"/>
        <result property="wzXz" column="wz_xz" jdbcType="VARCHAR"/>
        <result property="wzBc" column="wz_bc" jdbcType="VARCHAR"/>
        <result property="jd" column="jd" jdbcType="NUMERIC"/>
        <result property="wd" column="wd" jdbcType="NUMERIC"/>
        <result property="jlzt" column="jlzt" jdbcType="VARCHAR"/>
        <result property="distance" column="distance" jdbcType="NUMERIC"/>
    </resultMap>

    <resultMap type="com.mosty.base.model.vo.jcgl.TbJcglBqVo" id="TbJcglBqMap">
        <result property="id" column="id" jdbcType="VARCHAR"/>
        <result property="bqlx" column="bqlx" jdbcType="VARCHAR"/>
        <result property="bqmc" column="bqmc" jdbcType="VARCHAR"/>
        <result property="bqpym" column="bqpym" jdbcType="VARCHAR"/>
        <result property="bqtb" column="bqtb" jdbcType="VARCHAR"/>
        <result property="mxId" column="mx_id" jdbcType="VARCHAR"/>
        <result property="bz" column="bz" jdbcType="VARCHAR"/>
    </resultMap>

    <resultMap type="com.mosty.base.model.vo.jcgl.TbJcglXljlVo" id="TbJcglXljlMap">
        <result property="id" column="id" jdbcType="VARCHAR"/>
        <result property="xfllId" column="xfll_id" jdbcType="VARCHAR"/>
        <result property="xlrq" column="xlrq" jdbcType="VARCHAR"/>
        <result property="xlcj" column="xlcj" jdbcType="VARCHAR"/>
        <result property="xlsc" column="xlsc" jdbcType="VARCHAR"/>
        <result property="xllx" column="xllx" jdbcType="VARCHAR"/>
    </resultMap>

    <select id="selectXljl" resultMap="TbJcglXljlMap">

                       SELECT
                            id,xfll_id,xlrq,xlcj,xlsc,xllx
                        FROM
                            tb_jcgl_xljl
                        WHERE
                            xfll_id = #{xfll_id}
                            AND xt_sjzt = '1'
                            AND xt_scbz = '0'

    </select>


    <select id="selectExpTag" resultMap="TbJcglBqMap">


                                select b.id, b.bqmc, b.bqpym, b.bqtb, b.mx_id, b.bz
                                from tb_jcgl_xfll_bq q
                                             inner join tb_jcgl_bq b on q.bq_id = b.id and q.xt_sjzt = '1' and q.xt_scbz = '0'
                                where b.xt_sjzt = '1'
                                  and b.xt_scbz = '0'
                                  and q.xfll_id = #{xfll_id}
                                  and b.bqlx = 'EXP'
                                <![CDATA[ and DATE_FORMAT(q.yxsj, '%Y-%m-%d') >= DATE_FORMAT(now(), '%Y-%m-%d')



        ]]>
    </select>

    <select id="selectSklTag" resultMap="TbJcglBqMap">



                                select b.id, b.bqmc, b.bqpym, b.bqtb, b.mx_id, b.bz
                                from tb_jcgl_xfll_bq q
                                             inner join tb_jcgl_bq b on q.bq_id = b.id and q.xt_sjzt = '1' and q.xt_scbz = '0'
                                where b.xt_sjzt = '1'
                                  and b.xt_scbz = '0'
                                  and q.xfll_id = #{xfll_id}
                                  and b.bqlx = 'SKL'



    </select>

    <sql id="selectVo">



                                SELECT id,
                                       ryid,
                                       fl,
                                       lx,
                                       xm,
                                       xbdm,
                                       sfzh,
                                       jh,
                                       jx,
                                       zzmm,
                                       hyzk,
                                       rzsj,
                                       csrq,
                                       mzdm,
                                       whcddm,
                                       jgdm,
                                       jtzz,
                                       lxdh,
                                       dzyx,
                                       tp,
                                       password,
                                       salt,
                                       ssbm,
                                       ssbmid,
                                       ssbmdm,
                                       ssxgaj,
                                       ssxgajid,
                                       ssxgajdm,
                                       sssgaj,
                                       sssgajid,
                                       sssgajdm,
                                       bz,
                                       xt_sjzt,
                                       tpid
                                FROM tb_jcgl_xfll



    </sql>

    <update id="deleteList">
        update tb_jcgl_xfll set xt_scbz = '1' where id in
        <foreach collection="id" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </update>

    <select id="getList" resultMap="TbJcglXfllMap">
        <include refid="selectVo"/>
        where xt_scbz = '0'
        <if test="params.xtSjzt != null and params.xtSjzt != ''">
            and xt_sjzt = #{params.xtSjzt}
        </if>
        <if test="params.fl != null and params.fl != ''">
            and fl = #{params.fl}
        </if>
        <if test="params.lx != null and params.lx != ''">
            and lx = #{params.lx}
        </if>
        <if test="params.xm != null and params.xm != ''">
            and xm like concat('%',#{params.xm},'%')
        </if>
        <if test="params.xbdm != null and params.xbdm != ''">
            and xbdm = #{params.xbdm}
        </if>
        <if test="params.sfzh != null and params.sfzh != ''">
            and sfzh like concat('%',#{params.sfzh},'%')
        </if>
        <if test="params.jh != null and params.jh != ''">
            and jh = #{params.jh}
        </if>
        <if test="params.lxdh != null and params.lxdh != ''">
            and rzsj = #{params.lxdh}
        </if>
        <if test="params.ssbmdm != null and params.ssbmdm != ''">
            and ssbmdm  like concat(#{params.ssbmdm},'%')
        </if>
<!--        <if test="params.ssbmid != null and params.ssbmid != ''">-->
<!--            and ssbmid = #{params.ssbmid}-->
<!--        </if>-->
        <if test="params.bqId != null and params.bqId != ''">
            and id in (select xfll_id from tb_jcgl_xfll_bq where xt_sjzt = '1' and bq_id = #{params.bqId})
        </if>
        ORDER BY xt_cjsj DESC
        <!-- 数据范围过滤 -->
<!--        ${createSql}-->
    </select>

    <select id="queryById" resultMap="TbJcglXfllMap">
        <include refid="selectVo">
        </include>
        where id = #{id}
    </select>

    <select id="selectAllPolice" resultType="java.util.Map">



                                select count(*)                             allPoilceRoom,
                                       (
                                               SELECT count(*)
                                               FROM `tb_jcgl_xfll`
                                               where sfzh not in (select sfzh from mosty_qwgl.tb_qw_jl where xt_sjzt = 1
                                                 and sfzh !='')

        <if test="ssbmdm != null and ssbmdm != ''">
            and  ssbmdm like concat(#{ssbmdm,jdbcType=VARCHAR},'%')
        </if>
        ) onAllPoilceRoom
                                FROM `tb_jcgl_xfll`
                                where sfzh
                            <if test="ssbmdm != null and ssbmdm != ''">
                                and  ssbmdm like concat(#{ssbmdm,jdbcType=VARCHAR},'%')
                            </if>

    </select>

    <select id="selectPolice" resultType="java.util.Map">



                                select count(*)                          allPoilce,
                                       (
                                               SELECT count(*)
                                               FROM `tb_jcgl_xfll`
                                               where sfzh not in
                                                   (select sfzh from mosty_qwgl.tb_qw_jl where xt_sjzt = 1
                                                 and sfzh !='')
                                                 and fl ='1'
                                <if test="ssbmdm != null and ssbmdm != ''">
                                    and  ssbmdm like concat(#{ssbmdm,jdbcType=VARCHAR},'%')
                                </if>
                                                 ) onAllPoilce
                                FROM `tb_jcgl_xfll`
                                where sfzh
                                  and fl ='1'
                                <if test="ssbmdm != null and ssbmdm != ''">
                                    and  ssbmdm like concat(#{ssbmdm,jdbcType=VARCHAR},'%')
                                </if>



    </select>
    <select id="selectAuxPolice" resultType="java.util.Map">



                                select count(*)                          allAuxPoilce,
                                       (
                                               SELECT count(*)
                                               FROM `tb_jcgl_xfll`
                                               where sfzh not in
                                                   (select sfzh from mosty_qwgl.tb_qw_jl where xt_sjzt = 1
                                                 and sfzh !='')
                                                 and fl ='2'
        <if test="ssbmdm != null and ssbmdm != ''">
            and  ssbmdm like concat(#{ssbmdm,jdbcType=VARCHAR},'%')
        </if>
        ) onAllAuxPoilce
                                FROM `tb_jcgl_xfll`
                                where sfzh
                                  and fl ='2'
        <if test="ssbmdm != null and ssbmdm != ''">
            and  ssbmdm like concat(#{ssbmdm,jdbcType=VARCHAR},'%')
        </if>



    </select>

    <!--条件查询巡防力量-->
    <select id="getxfllList" parameterType="Map" resultMap="tbJcglZyjz">
        select a.*, f.jd, f.wd,f.wz_xz,f.wz_bc,
        (
        select jlzt from mosty_qwgl.tb_qw_bmjlbb_jlmx g where DATE_FORMAT(now(),'%Y-%m-%d') =
        DATE_FORMAT(g.bbrq,'%Y-%m-%d')
        and g.xt_scbz = '0' and g.xt_sjzt = '1' and (g.jl_id = a.id or g.jl_id = a.ryid)
        ) as jlzt
        <if test="dto.jd != null and dto.wd != null">
            ,st_distance_sphere(POINT(f.jd,f.wd),POINT(#{dto.jd},#{dto.wd})) as distance
        </if>
        from mosty_jcgl.tb_jcgl_xfll a,
        (
        select d.ssbmdm, c.jd, c.wd,c.wz_xz,c.wz_bc
        from mosty_yszx.tb_ys_gajg c,
        mosty_yszx.tb_ys_gajg_glbm d
        where c.ysdl = '001'
        and c.yszl = '001001'
        and c.ysxl in ('001001002', '001001003', '001001004')
        and d.xt_scbz = '0'
        and d.xt_sjzt = '1'
        and c.id = d.gajg_id
        and c.xt_scbz = '0'
        and c.xt_sjzt = '1'
        <if test="dto.radius != null">
            <![CDATA[and st_distance_sphere(POINT(c.jd,c.wd),POINT(#{dto.jd},#{dto.wd})) <= #{dto.radius}]]>
        </if>
        ) f
        where a.ssbmdm = f.ssbmdm and a.fl = '01'
        and a.id in (
        select b.xfll_id from mosty_jcgl.tb_jcgl_xfll_bq b where a.xt_scbz = '0' and a.xt_sjzt = '1'
        <if test="dto.bqList != null and dto.bqList.size > 0">
            and b.bq_id in
            <foreach collection="dto.bqList" open="(" separator="," close=")" item="item">
                #{item}
            </foreach>
        </if>
        )
        and a.xt_scbz = '0'
        and a.xt_sjzt = '1'
        and (
        a.id in (
        select jl_id from mosty_qwgl.tb_qw_bmjlbb_jlmx g where g.xt_scbz = '0' and g.xt_sjzt = '1'
        and DATE_FORMAT(now(),'%Y-%m-%d') = DATE_FORMAT(g.bbrq,'%Y-%m-%d')
        <if test="dto.jlzt != null and dto.jlzt != ''">
            and g.jlzt = #{dto.jlzt}
        </if>
        ) or
        a.ryid in (
        select jl_id from mosty_qwgl.tb_qw_bmjlbb_jlmx g where g.xt_scbz = '0' and g.xt_sjzt = '1'
        and DATE_FORMAT(now(),'%Y-%m-%d') = DATE_FORMAT(g.bbrq,'%Y-%m-%d')
        <if test="dto.jlzt != null and dto.jlzt != ''">
            and g.jlzt = #{dto.jlzt}
        </if>
        )
        )
        <if test="dto.ssbmdm != null and dto.ssbmdm != ''">
            and a.ssbmdm = #{dto.ssbmdm}
        </if>
        <if test="dto.ssbmid != null and dto.ssbmid != ''">
            and a.ssbmid = #{dto.ssbmid}
        </if>
        <if test="dto.jd != null and dto.wd != null">
            order by distance asc
        </if>
        limit #{pageIndex},#{pageSize}
    </select>

    <!--条件查询巡防力量-->
    <select id="getxfllCount" parameterType="Map" resultType="Integer">
        select count(1)
        from mosty_jcgl.tb_jcgl_xfll a,
        (
        select d.ssbmdm, c.jd, c.wd,c.wz_xz,c.wz_bc
        from mosty_yszx.tb_ys_gajg c,
        mosty_yszx.tb_ys_gajg_glbm d
        where c.ysdl = '001'
        and c.yszl = '001001'
        and c.ysxl in ('001001002', '001001003', '001001004')
        and d.xt_scbz = '0'
        and d.xt_sjzt = '1'
        and c.id = d.gajg_id
        and c.xt_scbz = '0'
        and c.xt_sjzt = '1'
        <if test="dto.radius != null">
            <![CDATA[and st_distance_sphere(POINT(c.jd,c.wd),POINT(#{dto.jd},#{dto.wd})) <= #{dto.radius}]]>
        </if>
        ) f
        where a.ssbmdm = f.ssbmdm and a.fl = '01'
        and a.id in (
        select b.xfll_id from mosty_jcgl.tb_jcgl_xfll_bq b where a.xt_scbz = '0' and a.xt_sjzt = '1'
        <if test="dto.bqList != null and dto.bqList.size > 0">
            and b.bq_id in
            <foreach collection="dto.bqList" open="(" separator="," close=")" item="item">
                #{item}
            </foreach>
        </if>
        )
        and a.xt_scbz = '0'
        and a.xt_sjzt = '1'
        and (
        a.id in (
        select jl_id from mosty_qwgl.tb_qw_bmjlbb_jlmx g where g.xt_scbz = '0' and g.xt_sjzt = '1'
        and DATE_FORMAT(now(),'%Y-%m-%d') = DATE_FORMAT(g.bbrq,'%Y-%m-%d')
        <if test="dto.jlzt != null and dto.jlzt != ''">
            and g.jlzt = #{dto.jlzt}
        </if>
        ) or
        a.ryid in (
        select jl_id from mosty_qwgl.tb_qw_bmjlbb_jlmx g where g.xt_scbz = '0' and g.xt_sjzt = '1'
        and DATE_FORMAT(now(),'%Y-%m-%d') = DATE_FORMAT(g.bbrq,'%Y-%m-%d')
        <if test="dto.jlzt != null and dto.jlzt != ''">
            and g.jlzt = #{dto.jlzt}
        </if>
        )
        )
        <if test="ssbmdm != null and ssbmdm != ''">
            and a.ssbmdm = #{dto.ssbmdm}
        </if>
    </select>
</mapper>
