<?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.wzzx.mapper.TbWzSswzMapper">

    <resultMap type="com.mosty.base.model.entity.wzzx.TbWzSswz" id="BaseResultMap">
        <result property="id" column="id"/>
        <result property="sjly" column="sjly"/>
        <result property="sjlydm" column="sjlydm"/>
        <result property="ywid" column="ywid"/>
        <result property="yhId" column="yh_id"/>
        <result property="yhXm" column="yh_xm"/>
        <result property="yhSfzh" column="yh_sfzh"/>
        <result property="jzId" column="jz_id"/>
        <result property="jzMc" column="jz_mc"/>
        <result property="dwrq" column="dwrq"/>
        <result property="dwsj" column="dwsj"/>
        <result property="jd" column="jd"/>
        <result property="wd" column="wd"/>
        <result property="zb" column="zb" typeHandler="com.mosty.base.feign.handle.PointTypeHandler"/>
        <result property="zbhash" column="zbhash"/>
        <result property="wztgz" column="wztgz"/>
        <result property="guo" column="guo"/>
        <result property="sheng" column="sheng"/>
        <result property="shi" column="shi"/>
        <result property="csbm" column="csbm"/>
        <result property="ssqx" column="ssqx"/>
        <result property="qxbm" column="qxbm"/>
        <result property="dzxz" column="dzxz"/>
        <result property="dzjc" column="dzjc"/>
        <result property="dwbz" column="dwbz"/>
        <result property="xfsc" column="xfsc"/>
        <result property="xflc" column="xflc"/>
        <result property="jrbh" column="jrbh"/>
        <result property="ssbm" column="ssbm"/>
        <result property="ssbmdm" column="ssbmdm" />
        <result property="ssbmid" column="ssbmid" />
        <result property="ssxgaj" column="ssxgaj" />
        <result property="ssxgajid" column="ssxgajid" />
        <result property="ssxgajdm" column="ssxgajdm" />
        <result property="sssgaj" column="sssgaj" />
        <result property="sssgajid" column="sssgajid" />
        <result property="sssgajdm" column="sssgajdm" />
        <result property="xtSjly" column="xt_sjly" />
        <result property="xtSjzt" column="xt_sjzt" />
        <result property="xtScbz" column="xt_scbz" />
        <result property="xtCjip" column="xt_cjip" />
        <result property="xtCjsj" column="xt_cjsj" />
        <result property="xtCjrId" column="xt_cjr_id" />
        <result property="xtCjr" column="xt_cjr" />
        <result property="xtCjbmdm" column="xt_cjbmdm" />
        <result property="xtCjbmmc" column="xt_cjbmmc" />
        <result property="xtZhgxip" column="xt_zhgxip" />
        <result property="xtZhgxsj" column="xt_zhgxsj" />
        <result property="xtZhgxrid" column="xt_zhgxrid" />
        <result property="xtZhgxr" column="xt_zhgxr" />
        <result property="xtZhgxbmdm" column="xt_zhgxbmdm" />
        <result property="xtZhgxbm" column="xt_zhgxbm" />
        <result property="bz" column="bz" />
    </resultMap>

    <sql id="base_column_list">
        id, sjly, sjlydm, ywid, yh_id, yh_xm, yh_sfzh, jz_id, jz_mc,
        dwrq, dwsj, jd, wd, zbhash, wztgz, guo, sheng, shi,
        csbm, ssqx, qxbm, dzxz, dzjc, dwbz, xfsc, xflc, jrbh,
        ssbm, ssbmdm, ssxgaj, ssxgajdm, sssgaj, sssgajdm,ssbmid,ssxgajid,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
    </sql>

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

    <insert id="insertEntity" parameterType="com.mosty.base.model.entity.wzzx.TbWzSswz">
        insert into tb_wz_sswz (id,sjly,sjlydm,ywid,yh_id,yh_xm,
                                yh_sfzh,jz_id,jz_mc,dwrq,dwsj,jd,wd,zb,
                                zbhash, wztgz, guo, sheng, shi, csbm, ssqx,
                                qxbm, dzxz, dzjc, dwbz, xfsc, xflc, jrbh,
                                ssbm, ssbmdm, ssxgaj, ssxgajdm,ssbmid,ssxgajid,sssgajid,
                                sssgaj, sssgajdm, 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},#{sjly},#{sjlydm},#{ywid},#{yhId}, #{yhXm},
                #{yhSfzh},#{jzId},#{jzMc},#{dwrq},#{dwsj},#{jd},#{wd},
                ST_GEOMFROMTEXT(#{zb, typeHandler = com.mosty.base.feign.handle.PointTypeHandler}),
                #{zbhash}, #{wztgz}, #{guo}, #{sheng}, #{shi}, #{csbm}, #{ssqx},
                #{qxbm}, #{dzxz}, #{dzjc}, #{dwbz}, #{xfsc}, #{xflc}, #{jrbh},
                #{ssbm}, #{ssbmdm}, #{ssxgaj}, #{ssxgajdm},#{ssbmid},#{ssxgajid},#{sssgajid},
                #{sssgaj}, #{sssgajdm}, #{xtSjly}, #{xtSjzt}, #{xtScbz}, #{xtCjip}, #{xtCjsj}, #{xtCjrId}, #{xtCjr},
                #{xtCjbmdm}, #{xtCjbmmc}, #{xtZhgxip},
                #{xtZhgxsj}, #{xtZhgxrid}, #{xtZhgxr}, #{xtZhgxbmdm}, #{xtZhgxbm}, #{bz})
    </insert>

    <!--id查询-->
    <select id="queryById" resultMap="BaseResultMap">
        select
        <include refid="base_column_list"/>,
        <include refid="zb_sql"/>
        from tb_wz_sswz
        where xt_scbz = '0' and id = #{id,jdbcType=VARCHAR}
    </select>

    <!--查询用户最新的位置-->
    <select id="querySswzNow" resultMap="BaseResultMap">
        SELECT
        <include refid="base_column_list"/>,
        <include refid="zb_sql"/>
        FROM `tb_wz_sswz`
        where xt_scbz = '0'
        <if test="yhId != null and yhId != ''">
            and yh_id = #{yhId}
        </if>
        <if test="jrbh != null and jrbh != ''">
            and jrbh = #{jrbh}
        </if>
        order by dwsj desc limit 0,1
    </select>

    <!--查询用户最新的位置-->
    <select id="querySswzToday" resultMap="BaseResultMap">
        SELECT
        <include refid="base_column_list"/>,
        <include refid="zb_sql"/>
        FROM `tb_wz_sswz`
        where xt_scbz = '0'
        and to_days(dwsj) = to_days(now());
        <if test="yhId != null and yhId != ''">
            and yh_id = #{yhId}
        </if>
        <if test="jrbh != null and jrbh != ''">
            and jrbh = #{jrbh}
        </if>
        order by dwsj desc
    </select>

    <!--根据用户ID查询-->
    <select id="querySswzYhid" resultType="java.lang.String">
        SELECT
            yh_id
        FROM
            tb_wz_sswz
        WHERE
            xt_scbz = '0'
          AND yh_id IS NOT NULL
          AND to_days( dwsj ) = to_days(
                now())
        GROUP BY
            yh_id
    </select>

    <!--删除7日前数据-->
    <delete id="delHistory" parameterType="java.lang.String">
        delete from tb_wz_sswz where DATE(dwsj) &lt;= DATE(DATE_SUB(NOW(),INTERVAL 7 DAY));
    </delete>

</mapper>
