TsConsultantKpiMapper.xml 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.huiju.estateagents.mapper.TsConsultantKpiMapper">
  4. <!-- 按照项目, 置业分组统计, 注意查询与分组条件, 必须与下面的汇总,导出一致 -->
  5. <select id="stsKPIDaily" resultType="com.huiju.estateagents.entity.TsConsultantKpi">
  6. SELECT * FROM (
  7. SELECT
  8. 1 AS serial_no,
  9. '2020' AS statis_date,
  10. t.org_id,
  11. t.building_id,
  12. t.building_name,
  13. t.user_id,
  14. t.user_name,
  15. t.phone,
  16. IFNULL(sum( t.new_persons ),0) AS new_persons,
  17. IFNULL(sum( t.share_num ),0) AS share_num,
  18. IFNULL(sum( t.visit_persons ),0) AS visit_persons,
  19. IFNULL(sum( t.visit_num ),0) AS visit_num,
  20. IFNULL(sum( t.share_persons ),0) AS share_persons,
  21. IFNULL(sum( t.home_page_persons ),0) AS home_page_persons,
  22. IFNULL(sum( t.home_page_nums ),0) AS home_page_nums,
  23. IFNULL(sum( t.chat_persons ),0) AS chat_persons,
  24. IFNULL(sum( t.favor_num ),0) AS favor_num,
  25. (select count(DISTINCT s.customer_id) from ta_recommend_customer s
  26. where s.org_id = t.org_id and (s.realty_consultant = t.user_id or s.realty_consultant = m.person_id) and (s.building_id = t.building_id or IFNULL(t.building_id, '') = '') and s.status > 0) as total_persons
  27. FROM
  28. ts_consultant_kpi t
  29. LEFT JOIN ta_person m on m.org_id = #{orgId} and m.user_id = t.user_id
  30. WHERE
  31. t.org_id = #{orgId}
  32. AND t.statis_date BETWEEN #{startDate} AND #{endDate}
  33. <if test="buildingIds != null">
  34. AND t.building_id in
  35. <foreach item="buildingId" index="index" collection="buildingIds"
  36. open="(" separator="," close=")">
  37. #{buildingId}
  38. </foreach>
  39. </if>
  40. GROUP BY
  41. t.user_id,
  42. t.building_id
  43. ) a
  44. <if test="asc != null and asc != ''">
  45. ORDER BY ${asc} asc
  46. </if>
  47. <if test="desc != null and desc != ''">
  48. ORDER BY ${desc} desc
  49. </if>
  50. </select>
  51. <!-- 按照小程序分组统计, 注意查询与分组条件, 必须与之前的汇总,导出一致 -->
  52. <select id="stsKPITotalByOrg" resultType="com.huiju.estateagents.entity.TsConsultantKpi">
  53. SELECT
  54. 1 AS serial_no,
  55. '2020' AS statis_date,
  56. t.org_id,
  57. '' as building_id,
  58. '-' as building_name,
  59. 0 as user_id,
  60. '-' as user_name,
  61. IFNULL(sum( t.new_persons ),0) AS new_persons,
  62. IFNULL(sum( t.share_num ),0) AS share_num,
  63. IFNULL(sum( t.visit_persons ),0) AS visit_persons,
  64. IFNULL(sum( t.visit_num ),0) AS visit_num,
  65. IFNULL(sum( t.share_persons ),0) AS share_persons,
  66. IFNULL(sum( t.home_page_persons ),0) AS home_page_persons,
  67. IFNULL(sum( t.home_page_nums ),0) AS home_page_nums,
  68. IFNULL(sum( t.chat_persons ),0) AS chat_persons,
  69. IFNULL(sum( t.favor_num ),0) AS favor_num,
  70. <!-- 这一列单独统计 -->
  71. 0 as total_persons
  72. FROM
  73. ts_consultant_kpi t
  74. WHERE
  75. t.org_id = #{orgId}
  76. AND t.statis_date BETWEEN #{startDate} AND #{endDate}
  77. <if test="buildingIds != null">
  78. AND t.building_id in
  79. <foreach item="buildingId" index="index" collection="buildingIds"
  80. open="(" separator="," close=")">
  81. #{buildingId}
  82. </foreach>
  83. </if>
  84. </select>
  85. <!-- 导出 -->
  86. <select id="stsKPIDailyExport" resultType="com.huiju.estateagents.excel.ConsultantKPIExport">
  87. SELECT
  88. 1 AS serial_no,
  89. '2020' AS statis_date,
  90. t.org_id,
  91. t.building_id,
  92. t.building_name,
  93. t.user_id,
  94. t.user_name,
  95. t.phone,
  96. IFNULL(sum( t.new_persons ),0) AS new_persons,
  97. IFNULL(sum( t.share_num ),0) AS share_num,
  98. IFNULL(sum( t.visit_persons ),0) AS visit_persons,
  99. IFNULL(sum( t.visit_num ),0) AS visit_num,
  100. IFNULL(sum( t.share_persons ),0) AS share_persons,
  101. IFNULL(sum( t.home_page_persons ),0) AS home_page_persons,
  102. IFNULL(sum( t.home_page_nums ),0) AS home_page_nums,
  103. IFNULL(sum( t.chat_persons ),0) AS chat_persons,
  104. IFNULL(sum( t.favor_num ),0) AS favor_num,
  105. (select count(DISTINCT s.customer_id) from ta_recommend_customer s
  106. where s.org_id = t.org_id and (s.realty_consultant = t.user_id or s.realty_consultant = m.person_id) and (s.building_id = t.building_id or IFNULL(t.building_id, '') = '') and s.status > 0) as total_persons
  107. FROM
  108. ts_consultant_kpi t
  109. LEFT JOIN ta_person m on m.org_id = #{orgId} and m.user_id = t.user_id
  110. WHERE
  111. t.org_id = #{orgId}
  112. AND t.statis_date BETWEEN #{startDate} AND #{endDate}
  113. <if test="buildingIds != null">
  114. AND t.building_id in
  115. <foreach item="buildingId" index="index" collection="buildingIds"
  116. open="(" separator="," close=")">
  117. #{buildingId}
  118. </foreach>
  119. </if>
  120. GROUP BY
  121. t.user_id,
  122. t.building_id
  123. ORDER BY sum( new_persons ) desc
  124. </select>
  125. <select id="stsAllCustomersByOrg" resultType="java.lang.Integer">
  126. SELECT
  127. count(DISTINCT p.customer_id)
  128. FROM
  129. ta_recommend_customer p
  130. INNER JOIN ta_person s ON s.org_id = p.org_id
  131. AND ( p.realty_consultant = s.user_id OR p.realty_consultant = s.person_id )
  132. INNER JOIN ts_consultant_kpi t ON t.org_id = s.org_id
  133. AND t.user_id = s.user_id
  134. AND ( t.building_id = p.building_id OR IFNULL( t.building_id, '' ) = '' )
  135. WHERE
  136. p.org_id = #{orgId}
  137. AND p.`status` > 0
  138. AND t.statis_date BETWEEN #{startDate} AND #{endDate}
  139. <if test="buildingIds != null">
  140. AND t.building_id in
  141. <foreach item="buildingId" index="index" collection="buildingIds"
  142. open="(" separator="," close=")">
  143. #{buildingId}
  144. </foreach>
  145. </if>
  146. </select>
  147. <select id="getConsultantShareCustomers" resultType="com.huiju.estateagents.entity.TaPerson">
  148. SELECT
  149. t.*,
  150. e.create_date as visit_time
  151. FROM ta_customer_from e
  152. INNER JOIN ta_person t ON t.person_id = e.person_id
  153. WHERE e.org_id = #{orgId}
  154. <if test="buildingId != null and buildingId !=''">
  155. AND e.building_id = #{buildingId}
  156. </if>
  157. AND e.target_type is not null
  158. AND e.create_date BETWEEN #{startDate} and #{endDate}
  159. AND e.is_project_first = 1
  160. AND t.user_id = #{userId}
  161. AND t.person_type != 'Realty Consultant'
  162. <!-- 为了与存储过程一致 -->
  163. <!-- AND t.`status` = 1-->
  164. ORDER BY e.create_date DESC
  165. </select>
  166. <select id="getConsultantHomePagePersons" resultType="com.huiju.estateagents.entity.TaPerson">
  167. SELECT
  168. t.*,
  169. count( * ) as visit_times,
  170. max(f.visit_time) as visit_time
  171. FROM
  172. ta_person t
  173. INNER JOIN ta_person_visit_record f ON f.org_id = #{orgId} AND f.person_id = t.person_id
  174. INNER JOIN ta_person s on s.person_id = f.target_id
  175. WHERE t.org_id = #{orgId}
  176. <!-- 为了与存储过程一致 -->
  177. <!-- AND t.`status` = 1-->
  178. <if test="buildingId != null and buildingId !=''">
  179. AND f.building_id = #{buildingId}
  180. </if>
  181. AND f.event_type = 'card'
  182. AND f.visit_time BETWEEN #{startDate} and #{endDate}
  183. AND s.user_id = #{userId}
  184. <!-- 为了与存储过程一致 -->
  185. <!-- AND s.`status` = 1-->
  186. GROUP BY t.person_id, DATE_FORMAT(f.visit_time, '%Y%m%d')
  187. ORDER BY f.visit_time DESC
  188. </select>
  189. <select id="getConsultantHomePageTimes" resultType="com.huiju.estateagents.entity.TaPerson">
  190. SELECT
  191. t.*,
  192. f.visit_time as visit_time
  193. FROM
  194. ta_person t
  195. INNER JOIN ta_person_visit_record f ON f.org_id = #{orgId} AND f.person_id = t.person_id
  196. INNER JOIN ta_person s on s.person_id = f.target_id
  197. WHERE t.org_id = #{orgId}
  198. <!-- 为了与存储过程一致 -->
  199. <!-- AND t.`status` = 1-->
  200. <if test="buildingId != null and buildingId !=''">
  201. AND f.building_id = #{buildingId}
  202. </if>
  203. AND f.event_type = 'card'
  204. AND f.visit_time BETWEEN #{startDate} and #{endDate}
  205. AND s.user_id = #{userId}
  206. <!-- 为了与存储过程一致 -->
  207. <!-- AND s.`status` = 1-->
  208. <if test="personId != null and personId !=''">
  209. AND t.person_id = #{personId}
  210. </if>
  211. ORDER BY f.visit_time DESC
  212. </select>
  213. <select id="getConsultantChatPersons" resultType="com.huiju.estateagents.entity.TaPerson">
  214. SELECT
  215. t.*,
  216. max(h.create_date) AS visit_time
  217. FROM
  218. ta_person t
  219. INNER JOIN ta_chat h ON h.send_person = t.person_id
  220. INNER JOIN ta_person s ON s.person_id = h.receive_person
  221. WHERE t.org_id = #{orgId}
  222. <!-- 为了与存储过程一致 -->
  223. <!-- AND t.`status` = 1-->
  224. AND h.create_date BETWEEN #{startDate} and #{endDate}
  225. AND s.user_id = #{userId}
  226. <!-- 为了与存储过程一致 -->
  227. <!-- AND s.`status` = 1-->
  228. GROUP BY
  229. t.person_id, DATE_FORMAT(h.create_date, '%Y%m%d')
  230. ORDER BY
  231. h.create_date DESC
  232. </select>
  233. <select id="getConsultantFavor" resultType="com.huiju.estateagents.entity.TaPerson">
  234. SELECT
  235. t.*,
  236. i.create_date AS visit_time
  237. FROM
  238. ta_person t
  239. INNER JOIN ta_favor i ON i.person_id = t.person_id
  240. INNER JOIN ta_person s ON s.person_id = i.be_favor
  241. WHERE t.org_id = #{orgId}
  242. <!-- 为了与存储过程一致 -->
  243. <!-- AND t.`status` = 1-->
  244. AND i.tagert_type = 'consultant'
  245. AND i.create_date BETWEEN #{startDate} and #{endDate}
  246. AND s.user_id = #{userId}
  247. <!-- 为了与存储过程一致 -->
  248. <!-- AND s.`status` = 1-->
  249. ORDER BY
  250. i.create_date DESC
  251. </select>
  252. <select id="getConsultantShareTargets" resultType="com.huiju.estateagents.entity.TaPerson">
  253. SELECT
  254. s.*,
  255. t.tagert_type as target_type,
  256. t.target_name,
  257. t.be_share as target_id,
  258. t.create_date as visit_time
  259. FROM
  260. ta_share_count t
  261. INNER JOIN ta_person s ON s.person_id = t.person_id
  262. WHERE
  263. t.org_id = #{orgId}
  264. <!-- 没有设置 targetType 查询条件, 则只查询固定的几个 -->
  265. <if test="targetType == null or targetType ==''">
  266. AND (
  267. t.tagert_type IN ( 'consultant', 'project', 'activity', 'group', 'h5', 'help', 'news', 'main' )
  268. OR t.tagert_type like 'house%'
  269. OR t.tagert_type like 'live%'
  270. )
  271. </if>
  272. <if test="targetType != null and targetType =='card_share'">
  273. AND t.tagert_type = 'consultant'
  274. </if>
  275. <if test="targetType != null and targetType !='' and targetType != 'card_share'">
  276. AND t.tagert_type like concat(#{targetType}, '%')
  277. </if>
  278. AND t.create_date BETWEEN #{startDate} and #{endDate}
  279. <if test="targetName != null and targetName !=''">
  280. AND t.target_name LIKE concat('%', #{targetName}, '%')
  281. </if>
  282. AND s.user_id = #{userId}
  283. <!-- 为了与存储过程一致 -->
  284. <!-- AND s.`status` = 1-->
  285. ORDER BY t.create_date DESC
  286. </select>
  287. <select id="getConsultantSharePersons" resultType="com.huiju.estateagents.entity.TaPerson">
  288. SELECT
  289. t.*,
  290. count(*) as visit_times,
  291. max(e.create_date) as visit_time
  292. FROM ta_share_person_from e
  293. INNER JOIN ta_person t ON t.person_id = e.person_id
  294. WHERE e.org_id = #{orgId}
  295. AND e.share_person = #{userId}
  296. AND e.create_date BETWEEN #{startDate} and #{endDate}
  297. AND (
  298. e.target_type IN ( 'card_share', 'building_share', 'dynamic_share', 'group_share', 'h5_share', 'help_share', 'house_share', 'live_share', 'news_share', 'poster' )
  299. OR ( e.target_type = 'share' AND e.target_id = 'index' )
  300. )
  301. <if test="buildingId != null and buildingId !=''">
  302. AND e.building_id = #{buildingId}
  303. </if>
  304. <!-- AND t.`status` = 1-->
  305. GROUP BY t.person_id, DATE_FORMAT(e.create_date, '%Y%m%d')
  306. ORDER BY e.create_date DESC
  307. </select>
  308. <select id="getConsultantShareTimes" resultType="com.huiju.estateagents.entity.TaPerson">
  309. SELECT
  310. t.*,
  311. e.create_date as visit_time,
  312. e.target_type,
  313. e.target_id,
  314. e.target_name
  315. FROM ta_share_person_from e
  316. INNER JOIN ta_person t ON t.person_id = e.person_id
  317. WHERE e.org_id = #{orgId}
  318. AND e.share_person = #{userId}
  319. <!-- 没有设置 targetType 查询条件, 则只查询固定的几个 -->
  320. <if test="targetType == null or targetType ==''">
  321. AND (
  322. e.target_type IN ( 'card_share', 'building_share', 'dynamic_share', 'group_share', 'h5_share', 'help_share', 'house_share', 'live_share', 'news_share', 'poster' )
  323. OR ( e.target_type = 'share' AND e.target_id = 'index' )
  324. )
  325. </if>
  326. <if test="targetType != null and targetType !=''">
  327. AND e.target_type = #{targetType}
  328. </if>
  329. <if test="targetType != null and targetType =='share'">
  330. AND e.target_type = 'share'
  331. AND e.target_id = 'index'
  332. </if>
  333. <if test="targetName != null and targetName !=''">
  334. AND e.target_name like concat('%', #{targetName} ,'%')
  335. </if>
  336. AND e.create_date BETWEEN #{startDate} and #{endDate}
  337. <if test="buildingId != null and buildingId !=''">
  338. AND e.building_id = #{buildingId}
  339. </if>
  340. <if test="personId != null and personId !=''">
  341. AND t.person_id = #{personId}
  342. </if>
  343. <if test="userName != null and userName !=''">
  344. AND IFNULL(t.name, t.nickname) like concat('%', #{userName} ,'%')
  345. </if>
  346. <!-- AND t.`status` = 1-->
  347. ORDER BY e.create_date DESC
  348. </select>
  349. </mapper>