ThirdPartyStationInfoMapper.xml 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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.zsElectric.boot.business.mapper.ThirdPartyStationInfoMapper">
  4. <resultMap id="StationInfoResultMap" type="com.zsElectric.boot.business.model.vo.ThirdPartyStationInfoVO">
  5. <id property="id" column="id"/>
  6. <result property="stationId" column="station_id"/>
  7. <result property="stationName" column="station_name"/>
  8. <result property="areaCode" column="area_code"/>
  9. <result property="equipmentOwnerId" column="equipment_owner_id"/>
  10. <result property="address" column="address"/>
  11. <result property="construction" column="construction"/>
  12. <result property="equipmentCount" column="equipment_count"/>
  13. <result property="stationStatus" column="station_status"/>
  14. <result property="serviceTel" column="service_tel"/>
  15. <result property="stationType" column="station_type"/>
  16. <result property="stationLng" column="station_lng"/>
  17. <result property="stationLat" column="station_lat"/>
  18. <result property="siteGuide" column="site_guide"/>
  19. <result property="stationTips" column="station_tips"/>
  20. <result property="salesType" column="sales_type"/>
  21. <result property="firmId" column="firm_id"/>
  22. <result property="thirdPartyId" column="third_party_id"/>
  23. </resultMap>
  24. <select id="selectStationInfoPage" resultMap="StationInfoResultMap">
  25. SELECT
  26. tpsi.id,
  27. tpsi.station_id,
  28. tpsi.station_name,
  29. tpsi.area_code,
  30. tpsi.equipment_owner_id,
  31. tpsi.address,
  32. tpsi.construction,
  33. COUNT(DISTINCT tpei.id) AS equipment_count,
  34. tpsi.station_status,
  35. tpsi.service_tel,
  36. tpsi.station_type,
  37. tpsi.station_lng,
  38. tpsi.station_lat,
  39. tpsi.site_guide
  40. FROM third_party_station_info tpsi
  41. LEFT JOIN third_party_equipment_info tpei ON tpsi.station_id = tpei.station_id AND tpei.is_deleted = 0
  42. WHERE tpsi.is_deleted = 0
  43. <if test="query.stationId != null and query.stationId != ''">
  44. AND tpsi.station_id = #{query.stationId}
  45. </if>
  46. <if test="query.stationName != null and query.stationName != ''">
  47. AND tpsi.station_name LIKE CONCAT('%', #{query.stationName}, '%')
  48. </if>
  49. <if test="query.areaCode != null and query.areaCode != ''">
  50. AND tpsi.area_code = #{query.areaCode}
  51. </if>
  52. <if test="query.equipmentOwnerId != null and query.equipmentOwnerId != ''">
  53. AND tpsi.equipment_owner_id = #{query.equipmentOwnerId}
  54. </if>
  55. <if test="query.stationStatus != null and query.stationStatus != 0">
  56. AND tpsi.station_status = #{query.stationStatus}
  57. </if>
  58. <if test="query.stationType != null and query.stationType != 0">
  59. AND tpsi.station_type = #{query.stationType}
  60. </if>
  61. <if test="query.serviceTel != null and query.serviceTel != ''">
  62. AND tpsi.service_tel = #{query.serviceTel}
  63. </if>
  64. GROUP BY tpsi.id
  65. ORDER BY tpsi.update_time DESC
  66. </select>
  67. <select id="selectStationInfoPageByEquipment" resultMap="StationInfoResultMap">
  68. SELECT
  69. tpsi.id,
  70. tpsi.station_id,
  71. tpsi.station_name,
  72. tpsi.area_code,
  73. tpsi.equipment_owner_id,
  74. tpsi.address,
  75. tpsi.construction,
  76. COUNT(DISTINCT tpei.id) AS equipment_count,
  77. tpsi.station_status,
  78. tpsi.service_tel,
  79. tpsi.station_type,
  80. tpsi.station_lng,
  81. tpsi.station_lat,
  82. tpsi.site_guide,
  83. tpsi.station_tips,
  84. CASE
  85. WHEN pf.firm_id IS NOT NULL THEN 1
  86. WHEN pf.third_party_id IS NOT NULL THEN 2
  87. ELSE 0
  88. END AS sales_type,
  89. pf.firm_id,
  90. pf.third_party_id
  91. FROM third_party_station_info tpsi
  92. LEFT JOIN third_party_equipment_info tpei ON tpsi.station_id = tpei.station_id AND tpei.is_deleted = 0
  93. LEFT JOIN (
  94. SELECT p1.station_info_id, p1.firm_id, p1.third_party_id
  95. FROM c_policy_fee p1
  96. INNER JOIN (
  97. SELECT station_info_id,
  98. IFNULL(firm_id, 0) AS grp_firm_id,
  99. IFNULL(third_party_id, 0) AS grp_third_party_id,
  100. MIN(id) AS min_id
  101. FROM c_policy_fee
  102. WHERE is_deleted = 0
  103. GROUP BY station_info_id, IFNULL(firm_id, 0), IFNULL(third_party_id, 0)
  104. ) p2 ON p1.id = p2.min_id
  105. WHERE p1.is_deleted = 0
  106. ) pf ON pf.station_info_id = tpsi.id
  107. WHERE tpsi.is_deleted = 0
  108. AND tpsi.equipment_owner_id = 'MA6DP6BE7'
  109. AND tpsi.policy_configured = 1
  110. <if test="query.stationId != null and query.stationId != ''">
  111. AND tpsi.station_id = #{query.stationId}
  112. </if>
  113. <if test="query.stationName != null and query.stationName != ''">
  114. AND tpsi.station_name LIKE CONCAT('%', #{query.stationName}, '%')
  115. </if>
  116. <if test="query.areaCode != null and query.areaCode != ''">
  117. AND tpsi.area_code = #{query.areaCode}
  118. </if>
  119. <if test="query.equipmentOwnerId != null and query.equipmentOwnerId != ''">
  120. AND tpsi.equipment_owner_id = #{query.equipmentOwnerId}
  121. </if>
  122. <if test="query.stationStatus != null and query.stationStatus != 0">
  123. AND tpsi.station_status = #{query.stationStatus}
  124. </if>
  125. <if test="query.stationType != null and query.stationType != 0">
  126. AND tpsi.station_type = #{query.stationType}
  127. </if>
  128. <if test="query.serviceTel != null and query.serviceTel != ''">
  129. AND tpsi.service_tel = #{query.serviceTel}
  130. </if>
  131. GROUP BY tpsi.id, pf.firm_id, pf.third_party_id
  132. ORDER BY tpsi.update_time DESC
  133. </select>
  134. <!-- 小程序首页站点信息查询结果映射 -->
  135. <resultMap id="AppletStationInfoResultMap" type="com.zsElectric.boot.business.model.vo.StationInfoVO">
  136. <result property="stationId" column="station_info_id"/>
  137. <result property="stationName" column="station_name"/>
  138. <result property="tips" column="station_tips"/>
  139. <result property="distance" column="distance"/>
  140. <result property="fastCharging" column="fast_charging"/>
  141. <result property="slowCharging" column="slow_charging"/>
  142. <result property="peakValue" column="peak_value"/>
  143. <result property="peakTime" column="peak_time"/>
  144. <result property="platformPrice" column="platform_price"/>
  145. <result property="enterprisePrice" column="enterprise_price"/>
  146. </resultMap>
  147. <!-- 小程序首页分页查询站点信息 -->
  148. <select id="selectAppletStationInfoPage" resultMap="AppletStationInfoResultMap">
  149. SELECT
  150. tpsi.id AS station_info_id,
  151. tpsi.station_name,
  152. tpsi.station_tips,
  153. <!-- 计算距离(km) -->
  154. <if test="query.longitude != null and query.latitude != null">
  155. ROUND(
  156. 6371 * ACOS(
  157. COS(RADIANS(#{query.latitude})) * COS(RADIANS(tpsi.station_lat))
  158. * COS(RADIANS(tpsi.station_lng) - RADIANS(#{query.longitude}))
  159. + SIN(RADIANS(#{query.latitude})) * SIN(RADIANS(tpsi.station_lat))
  160. ), 2
  161. ) AS distance,
  162. </if>
  163. <if test="query.longitude == null or query.latitude == null">
  164. NULL AS distance,
  165. </if>
  166. <!-- 快充统计(空闲/总数) -->
  167. CONCAT(
  168. COUNT(tpci.id),
  169. '/',
  170. COUNT(tpci.id)
  171. ) AS fast_charging,
  172. <!-- 慢充统计(直接0/0) -->
  173. '0/0' AS slow_charging,
  174. <!-- 峰值(根据当前时段获取) -->
  175. (
  176. SELECT
  177. CASE tppi.period_flag
  178. WHEN 1 THEN '尖'
  179. WHEN 2 THEN '峰'
  180. WHEN 3 THEN '平'
  181. WHEN 4 THEN '谷'
  182. ELSE ''
  183. END
  184. FROM third_party_policy_info tppi
  185. INNER JOIN third_party_equipment_price_policy tpepp ON tppi.price_policy_id = tpepp.id AND tpepp.is_deleted = 0
  186. INNER JOIN third_party_connector_info tpci2 ON tpepp.connector_id = tpci2.connector_id AND tpci2.is_deleted = 0
  187. WHERE tpci2.station_id = tpsi.station_id
  188. AND tppi.is_deleted = 0
  189. AND tppi.start_time &lt;= #{currentTime}
  190. ORDER BY tppi.start_time DESC
  191. LIMIT 1
  192. ) AS peak_value,
  193. <!-- 峰时段时间(period_flag=2为峰时段),格式: HH:mm - HH:mm -->
  194. (
  195. SELECT CONCAT(
  196. CONCAT(SUBSTRING(tppi.start_time, 1, 2), ':', SUBSTRING(tppi.start_time, 3, 2)),
  197. ' - ',
  198. IFNULL(
  199. (
  200. SELECT CONCAT(SUBSTRING(next_tppi.start_time, 1, 2), ':', SUBSTRING(next_tppi.start_time, 3, 2))
  201. FROM third_party_policy_info next_tppi
  202. WHERE next_tppi.price_policy_id = tppi.price_policy_id
  203. AND next_tppi.is_deleted = 0
  204. AND next_tppi.start_time > tppi.start_time
  205. ORDER BY next_tppi.start_time ASC
  206. LIMIT 1
  207. ),
  208. '24:00'
  209. )
  210. )
  211. FROM third_party_policy_info tppi
  212. INNER JOIN third_party_equipment_price_policy tpepp ON tppi.price_policy_id = tpepp.id AND tpepp.is_deleted = 0
  213. INNER JOIN third_party_connector_info tpci5 ON tpepp.connector_id = tpci5.connector_id AND tpci5.is_deleted = 0
  214. WHERE tpci5.station_id = tpsi.station_id
  215. AND tppi.is_deleted = 0
  216. AND tppi.period_flag = 2
  217. LIMIT 1
  218. ) AS peak_time,
  219. <!-- 平台价(电费+服务费+运营费+综合销售费) -->
  220. (
  221. SELECT
  222. ROUND(
  223. IFNULL(tppi.elec_price, 0) + IFNULL(tppi.service_price, 0)
  224. + IFNULL(pf.op_fee, 0) + IFNULL(pf.comp_sales_fee, 0),
  225. 4
  226. )
  227. FROM c_policy_fee pf
  228. INNER JOIN third_party_policy_info tppi ON pf.start_time = tppi.start_time
  229. INNER JOIN third_party_equipment_price_policy tpepp ON tppi.price_policy_id = tpepp.id AND tpepp.is_deleted = 0
  230. INNER JOIN third_party_connector_info tpci3 ON tpepp.connector_id = tpci3.connector_id AND tpci3.is_deleted = 0
  231. WHERE tpci3.station_id = tpsi.station_id
  232. AND pf.station_info_id = tpsi.id
  233. AND pf.sales_type = 0
  234. AND pf.is_deleted = 0
  235. AND tppi.is_deleted = 0
  236. AND pf.start_time &lt;= #{currentTime}
  237. ORDER BY pf.start_time DESC
  238. LIMIT 1
  239. ) AS platform_price,
  240. <!-- 企业价(如果有企业ID) -->
  241. <if test="firmId != null">
  242. (
  243. SELECT
  244. ROUND(
  245. IFNULL(tppi.elec_price, 0) + IFNULL(tppi.service_price, 0)
  246. + IFNULL(pf.op_fee, 0) + IFNULL(pf.comp_sales_fee, 0),
  247. 4
  248. )
  249. FROM c_policy_fee pf
  250. INNER JOIN third_party_policy_info tppi ON pf.start_time = tppi.start_time
  251. INNER JOIN third_party_equipment_price_policy tpepp ON tppi.price_policy_id = tpepp.id AND tpepp.is_deleted = 0
  252. INNER JOIN third_party_connector_info tpci4 ON tpepp.connector_id = tpci4.connector_id AND tpci4.is_deleted = 0
  253. WHERE tpci4.station_id = tpsi.station_id
  254. AND pf.station_info_id = tpsi.id
  255. AND pf.sales_type = 1
  256. AND pf.firm_id = #{firmId}
  257. AND pf.is_deleted = 0
  258. AND tppi.is_deleted = 0
  259. AND pf.start_time &lt;= #{currentTime}
  260. ORDER BY pf.start_time DESC
  261. LIMIT 1
  262. ) AS enterprise_price
  263. </if>
  264. <if test="firmId == null">
  265. NULL AS enterprise_price
  266. </if>
  267. FROM third_party_station_info tpsi
  268. LEFT JOIN third_party_connector_info tpci ON tpsi.station_id = tpci.station_id AND tpci.is_deleted = 0
  269. WHERE tpsi.is_deleted = 0
  270. AND tpsi.equipment_owner_id = 'MA6DP6BE7'
  271. AND tpsi.policy_configured = 1
  272. <!-- 确保存在平台价配置 -->
  273. AND EXISTS (
  274. SELECT 1 FROM c_policy_fee pf_check
  275. WHERE pf_check.station_info_id = tpsi.id
  276. AND pf_check.sales_type = 0
  277. AND pf_check.is_deleted = 0
  278. AND (pf_check.op_fee IS NOT NULL OR pf_check.comp_sales_fee IS NOT NULL)
  279. )
  280. GROUP BY tpsi.id
  281. <if test="query.sortType != null">
  282. <if test="query.sortType == 1 and query.longitude != null and query.latitude != null">
  283. ORDER BY distance ASC
  284. </if>
  285. <if test="query.sortType == 3">
  286. ORDER BY platform_price ASC
  287. </if>
  288. </if>
  289. <if test="query.sortType == null or (query.sortType == 1 and (query.longitude == null or query.latitude == null))">
  290. ORDER BY tpsi.update_time DESC
  291. </if>
  292. </select>
  293. <!-- 小程序首页地图模式站点信息查询结果映射 -->
  294. <resultMap id="AppletStationInfoMapResultMap" type="com.zsElectric.boot.business.model.vo.StationInfoMapVO">
  295. <result property="stationId" column="station_info_id"/>
  296. <result property="stationName" column="station_name"/>
  297. <result property="tips" column="station_tips"/>
  298. <result property="distance" column="distance"/>
  299. <result property="fastCharging" column="fast_charging"/>
  300. <result property="slowCharging" column="slow_charging"/>
  301. <result property="peakValue" column="peak_value"/>
  302. <result property="peakTime" column="peak_time"/>
  303. <result property="platformPrice" column="platform_price"/>
  304. <result property="enterprisePrice" column="enterprise_price"/>
  305. <result property="parkingFee" column="parking_fee"/>
  306. <result property="address" column="address"/>
  307. </resultMap>
  308. <!-- 小程序首页地图模式查询最近站点信息 -->
  309. <select id="selectNearestStationMap" resultMap="AppletStationInfoMapResultMap">
  310. SELECT
  311. tpsi.id AS station_info_id,
  312. tpsi.station_name,
  313. tpsi.station_tips,
  314. tpsi.address,
  315. CAST(tpsi.park_fee AS DECIMAL(10,2)) AS parking_fee,
  316. <!-- 计算距离(km) -->
  317. ROUND(
  318. 6371 * ACOS(
  319. COS(RADIANS(#{latitude})) * COS(RADIANS(tpsi.station_lat))
  320. * COS(RADIANS(tpsi.station_lng) - RADIANS(#{longitude}))
  321. + SIN(RADIANS(#{latitude})) * SIN(RADIANS(tpsi.station_lat))
  322. ), 2
  323. ) AS distance,
  324. <!-- 快充统计(空闲/总数) -->
  325. CONCAT(
  326. COUNT(tpci.id),
  327. '/',
  328. COUNT(tpci.id)
  329. ) AS fast_charging,
  330. <!-- 慢充统计(直接0/0) -->
  331. '0/0' AS slow_charging,
  332. <!-- 峰值(根据当前时段获取) -->
  333. (
  334. SELECT
  335. CASE tppi.period_flag
  336. WHEN 1 THEN '尖'
  337. WHEN 2 THEN '峰'
  338. WHEN 3 THEN '平'
  339. WHEN 4 THEN '谷'
  340. ELSE ''
  341. END
  342. FROM third_party_policy_info tppi
  343. INNER JOIN third_party_equipment_price_policy tpepp ON tppi.price_policy_id = tpepp.id AND tpepp.is_deleted = 0
  344. INNER JOIN third_party_connector_info tpci2 ON tpepp.connector_id = tpci2.connector_id AND tpci2.is_deleted = 0
  345. WHERE tpci2.station_id = tpsi.station_id
  346. AND tppi.is_deleted = 0
  347. AND tppi.start_time &lt;= #{currentTime}
  348. ORDER BY tppi.start_time DESC
  349. LIMIT 1
  350. ) AS peak_value,
  351. <!-- 峰时段时间(period_flag=2为峰时段),格式: HH:mm - HH:mm -->
  352. (
  353. SELECT CONCAT(
  354. CONCAT(SUBSTRING(tppi.start_time, 1, 2), ':', SUBSTRING(tppi.start_time, 3, 2)),
  355. ' - ',
  356. IFNULL(
  357. (
  358. SELECT CONCAT(SUBSTRING(next_tppi.start_time, 1, 2), ':', SUBSTRING(next_tppi.start_time, 3, 2))
  359. FROM third_party_policy_info next_tppi
  360. WHERE next_tppi.price_policy_id = tppi.price_policy_id
  361. AND next_tppi.is_deleted = 0
  362. AND next_tppi.start_time > tppi.start_time
  363. ORDER BY next_tppi.start_time ASC
  364. LIMIT 1
  365. ),
  366. '24:00'
  367. )
  368. )
  369. FROM third_party_policy_info tppi
  370. INNER JOIN third_party_equipment_price_policy tpepp ON tppi.price_policy_id = tpepp.id AND tpepp.is_deleted = 0
  371. INNER JOIN third_party_connector_info tpci5 ON tpepp.connector_id = tpci5.connector_id AND tpci5.is_deleted = 0
  372. WHERE tpci5.station_id = tpsi.station_id
  373. AND tppi.is_deleted = 0
  374. AND tppi.period_flag = 2
  375. LIMIT 1
  376. ) AS peak_time,
  377. <!-- 平台价(电费+服务费+运营费+综合销售费) -->
  378. (
  379. SELECT
  380. ROUND(
  381. IFNULL(tppi.elec_price, 0) + IFNULL(tppi.service_price, 0)
  382. + IFNULL(pf.op_fee, 0) + IFNULL(pf.comp_sales_fee, 0),
  383. 4
  384. )
  385. FROM c_policy_fee pf
  386. INNER JOIN third_party_policy_info tppi ON pf.start_time = tppi.start_time
  387. INNER JOIN third_party_equipment_price_policy tpepp ON tppi.price_policy_id = tpepp.id AND tpepp.is_deleted = 0
  388. INNER JOIN third_party_connector_info tpci3 ON tpepp.connector_id = tpci3.connector_id AND tpci3.is_deleted = 0
  389. WHERE tpci3.station_id = tpsi.station_id
  390. AND pf.station_info_id = tpsi.id
  391. AND pf.sales_type = 0
  392. AND pf.is_deleted = 0
  393. AND tppi.is_deleted = 0
  394. AND pf.start_time &lt;= #{currentTime}
  395. ORDER BY pf.start_time DESC
  396. LIMIT 1
  397. ) AS platform_price,
  398. <!-- 企业价(如果有企业ID) -->
  399. <if test="firmId != null">
  400. (
  401. SELECT
  402. ROUND(
  403. IFNULL(tppi.elec_price, 0) + IFNULL(tppi.service_price, 0)
  404. + IFNULL(pf.op_fee, 0) + IFNULL(pf.comp_sales_fee, 0),
  405. 4
  406. )
  407. FROM c_policy_fee pf
  408. INNER JOIN third_party_policy_info tppi ON pf.start_time = tppi.start_time
  409. INNER JOIN third_party_equipment_price_policy tpepp ON tppi.price_policy_id = tpepp.id AND tpepp.is_deleted = 0
  410. INNER JOIN third_party_connector_info tpci4 ON tpepp.connector_id = tpci4.connector_id AND tpci4.is_deleted = 0
  411. WHERE tpci4.station_id = tpsi.station_id
  412. AND pf.station_info_id = tpsi.id
  413. AND pf.sales_type = 1
  414. AND pf.firm_id = #{firmId}
  415. AND pf.is_deleted = 0
  416. AND tppi.is_deleted = 0
  417. AND pf.start_time &lt;= #{currentTime}
  418. ORDER BY pf.start_time DESC
  419. LIMIT 1
  420. ) AS enterprise_price
  421. </if>
  422. <if test="firmId == null">
  423. NULL AS enterprise_price
  424. </if>
  425. FROM third_party_station_info tpsi
  426. LEFT JOIN third_party_connector_info tpci ON tpsi.station_id = tpci.station_id AND tpci.is_deleted = 0
  427. WHERE tpsi.is_deleted = 0
  428. AND tpsi.equipment_owner_id = 'MA6DP6BE7'
  429. AND tpsi.policy_configured = 1
  430. <!-- 确保存在平台价配置 -->
  431. AND EXISTS (
  432. SELECT 1 FROM c_policy_fee pf_check
  433. WHERE pf_check.station_info_id = tpsi.id
  434. AND pf_check.sales_type = 0
  435. AND pf_check.is_deleted = 0
  436. AND (pf_check.op_fee IS NOT NULL OR pf_check.comp_sales_fee IS NOT NULL)
  437. )
  438. GROUP BY tpsi.id
  439. ORDER BY distance ASC
  440. LIMIT 1
  441. </select>
  442. </mapper>