credit_tracker/src/main/resources/mybatis/mapper/signal/PhonesMapMapper.xml

217 lines
No EOL
7.7 KiB
XML

<?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="ru.nbch.credit_tracker.mapper.signal.PhonesMapMapper">
<resultMap id="PhoneMap" type="ru.nbch.credit_tracker.entities.app.PhonesMap">
<result property="id" column="id"/>
<result property="fid" column="fid"/>
<result property="phone" column="phone"/>
<result property="flagDebt5000" column="flag_debt5000"/>
<result property="flag90days" column="flag_90_12"/>
<result property="searchedAt" column="searched_at"/>
<result property="calculatedAt" column="calculated_at"/>
</resultMap>
<select id="findByPhone" resultMap="PhoneMap">
select id,
fid,
phone,
flag_debt5000,
flag_90_12,
searched_at,
calculated_at
FROM signals_phones_map
WHERE phone = #{phone}
</select>
<select id="findByFid" resultMap="PhoneMap">
select id,
fid,
phone,
flag_debt5000,
flag_90_12,
searched_at,
calculated_at
FROM signals_phones_map
WHERE fid = #{fid}
</select>
<select id="findByFids" resultMap="PhoneMap">
select
fid,
phone,
flag_debt5000,
flag_90_12
FROM signals_phones_map
WHERE fid in
<foreach item='fid' collection='fids' open='(' separator=',' close=')'>
#{fid}
</foreach>
</select>
<select id="searchedAtLessThan" resultMap="PhoneMap">
select id,
fid,
phone,
flag_debt5000,
flag_90_12,
searched_at,
calculated_at
FROM signals_phones_map
WHERE searched_at &lt; now() - make_interval(days => #{daysCount}) and id > #{lastId}
order by id
limit #{batchSize}
</select>
<select id="getCachedData" resultMap="PhoneMap" resultType="java.util.List">
SELECT fid,
phone,
flag_debt5000,
flag_90_12,
searched_at,
calculated_at
FROM signals_phones_map
WHERE phone in
<foreach item="phone" collection="phones" open="(" separator="," close=")">
#{phone}
</foreach>
and fid is not null
</select>
<select id="registerJson" parameterType="string">
WITH src AS (SELECT row_number() OVER () AS rn,
fid,
phone
FROM json_to_recordset(#{json}::json) AS t(
fid int,
phone bigint
))
INSERT
INTO signals_phones_map
(fid, phone, searched_at)
SELECT fid,
phone,
now()
FROM src
order by phone
ON CONFLICT (phone) DO UPDATE
SET fid = EXCLUDED.fid,
phone = EXCLUDED.phone
</select>
<select id="findEligibleForFlagsUpdate" resultMap="PhoneMap">
select id, fid, phone
from signals_phones_map
where fid is not null
and calculated_at is not null
and calculated_at &lt; (current_date - make_interval(days => #{interval}))
and id > #{lastId}
order by id
limit #{batchSize}
</select>
<select id="findEligibleForFlagsInsert" resultMap="PhoneMap">
select id, fid, phone
from signals_phones_map
where fid is not null
and calculated_at is null
and id > #{lastId}
order by id
limit #{batchSize}
</select>
<update id="updateNameCachedData" parameterType="string">
WITH src AS (SELECT DISTINCT ON (phone) fid,
phone
FROM json_to_recordset(#{json}::json) AS src(
fid int,
phone bigint
)
order by phone),
to_lock AS (select dst.id
from signals_phones_map dst
join src on src.phone = dst.phone
order by dst.phone
for update)
update signals_phones_map dst
set fid = src.fid,
searched_at = now()
FROM json_to_recordset(#{json}::json) AS src(
fid int,
phone bigint
)
where dst.phone = src.phone
AND dst.id in (select id from to_lock);
</update>
<update id="updateFlagCachedData" parameterType="string">
WITH src AS (SELECT DISTINCT ON (phone) phone,
flag_debt5000,
flag_90_12
FROM json_to_recordset(#{json}::json) AS src(
phone bigint,
flag_debt5000 smallint,
flag_90_12 smallint
)
order by phone),
to_lock AS (select dst.id
from signals_phones_map dst
join src on src.phone = dst.phone
order by dst.phone
for update)
update signals_phones_map dst
set flag_debt5000 = src.flag_debt5000,
flag_90_12 = src.flag_90_12,
calculated_at = now()
FROM json_to_recordset(#{json}::json) AS src(
phone bigint,
flag_debt5000 smallint,
flag_90_12 smallint
)
where dst.phone = src.phone
AND dst.id in (select id from to_lock);
</update>
<update id="resetFidAndPD" parameterType="string">
update signals_phones_map dst
set fid = null,
flag_debt5000 = null,
flag_90_12 = null,
calculated_at = null,
searched_at = now()
FROM json_to_recordset(#{json}::json) AS src(
phone bigint)
where dst.phone = src.phone
</update>
<update id="updateSearchedAt" parameterType="string">
update signals_phones_map dst
set searched_at = now()
FROM json_to_recordset(#{json}::json) AS src(
phone bigint)
where dst.phone = src.phone
</update>
<delete id="deleteUnusedPhones">
DELETE
FROM signals_phones_map spm
WHERE NOT EXISTS (SELECT 1
FROM signals_phone_fid_map spr
JOIN signals_user_packages sup
ON sup.id = spr.package_id
WHERE spr.phone_l = spm.phone
AND sup.status IN
(
'ON_MONITORING',
'PROCESSING',
'PROCESSING_REPEATED'
))
</delete>
</mapper>