SearchMapper.xml 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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.caimei365.commodity.mapper.SearchMapper">
  4. <sql id="Search_Product_List">
  5. p.productID as p_id,
  6. p.`name` as p_name,
  7. p.searchKey as p_keyword,
  8. p.tags as p_tags,
  9. p.mainImage as p_image,
  10. p.priceFlag as p_price_flag,
  11. p.productCode as p_code,
  12. br.id as p_brand_id,
  13. br.name as p_brand_name,
  14. p.shopID as p_supplier_id,
  15. sh.name as p_supplier_name,
  16. p.bigTypeID as p_category1_id,
  17. b.name as p_category1_name,
  18. p.smallTypeID as p_category2_id,
  19. s.name as p_category2_name,
  20. p.tinyTypeID as p_category3_id,
  21. t.name as p_category3_name,
  22. p.preferredFlag as p_preferred,
  23. p.productCategory as p_type,
  24. copi.validFlag as p_valid,
  25. br.weights as p_sort,
  26. DATE_FORMAT(p.ADDTIME,'%Y%m%d') as p_time,
  27. IFNULL(p.visibility,3) as p_visibility,
  28. ifnull(p.newvalidflag,0) as p_newvalidflag,
  29. p.productType as p_product_type,
  30. p.relatedLabels as p_labels,
  31. (select unit from cm_sku where productID=
  32. #{productId}
  33. order
  34. by
  35. price
  36. asc
  37. LIMIT
  38. 1
  39. )
  40. as
  41. p_unit,
  42. (
  43. select
  44. price
  45. from
  46. cm_sku
  47. where
  48. productID
  49. =
  50. #{productId}
  51. order
  52. by
  53. price
  54. asc
  55. LIMIT
  56. 1
  57. )
  58. as
  59. p_price
  60. </sql>
  61. <sql id="Product_Joins">
  62. left join tinytype as t on p.tinyTypeID = t.tinyTypeID
  63. left join smalltype as s on p.smallTypeID = s.smallTypeID
  64. left join bigtype as b on p.bigTypeID = b.bigTypeID
  65. left join cm_brand as br on p.brandID = br.id
  66. left join shop as sh on p.shopID = sh.shopID
  67. </sql>
  68. <select id="searchProductById" resultType="com.caimei365.commodity.model.search.ProductDO">
  69. select
  70. <include refid="Search_Product_List"/>
  71. from product p
  72. <include refid="Product_Joins"/>
  73. left join cm_organize_product_info copi on copi.productId = p.productID and copi.organizeId = 0
  74. where copi.validFlag in (2,3,9) and p.productCategory = 1
  75. and p.productID = #{productId}
  76. </select>
  77. <select id="findProductCount" resultType="java.lang.Integer">
  78. select count(*)
  79. from product p
  80. left join cm_organize_product_info copi on p.productID = copi.productId
  81. where validFlag in (2, 3, 9)
  82. and productCategory = 1
  83. </select>
  84. <select id="findProductInvalidCount" resultType="java.lang.Integer">
  85. select count(*)
  86. from product p
  87. left join cm_organize_product_info copi on p.productID = copi.productId
  88. where validFlag in (2, 3, 9)
  89. or productCategory != 1
  90. </select>
  91. <select id="findProductInvalidIds" resultType="java.lang.Integer">
  92. select p.productID
  93. from product p
  94. left join cm_organize_product_info copi on p.productID = copi.productId
  95. where validFlag in (2, 3, 9)
  96. or productCategory != 1
  97. </select>
  98. <select id="searchProductList" resultType="com.caimei365.commodity.model.search.ProductDO">
  99. select
  100. <include refid="Search_Product_List"/>
  101. from product p
  102. <include refid="Product_Joins"/>
  103. left join cm_organize_product_info copi on copi.productId = p.productID and copi.organizeId = 0
  104. where copi.validFlag in (2,3,9) and productCategory = 1
  105. order by productID desc
  106. </select>
  107. <!-- 供应商 -->
  108. <sql id="Search_Supplier_List">
  109. s.shopID as s_id,
  110. s.name as s_name,
  111. s.logo as s_logo,
  112. s.authorizationCertificateImage as s_license,
  113. s.businessScope as s_business,
  114. s.townID as s_town_id,
  115. CONCAT(p.name, c.name) as s_address,
  116. s.status as s_valid
  117. </sql>
  118. <sql id="Supplier_Joins">
  119. left join town as t on s.townId = t.townID
  120. left join city as c on t.cityID = c.cityID
  121. left join province as p on c.provinceID = p.provinceID
  122. </sql>
  123. <select id="findSupplierCount" resultType="java.lang.Integer">
  124. select count(*)
  125. from shop
  126. where shopType = 1
  127. and (status = 90 or status = 9)
  128. and shopID != 1252
  129. </select>
  130. <select id="findSupplierInvalidCount" resultType="java.lang.Integer">
  131. select count(*)
  132. from shop
  133. where shopType = 1
  134. and (status not in (90, 9)
  135. or shopID = 1252)
  136. </select>
  137. <select id="findSupplierInvalidIds" resultType="java.lang.Integer">
  138. select shopID
  139. from shop
  140. where shopType = 1
  141. and (status not in (90, 9)
  142. or shopID = 1252)
  143. </select>
  144. <select id="searchSupplierList" resultType="com.caimei365.commodity.model.search.SupplierDO">
  145. select
  146. <include refid="Search_Supplier_List"/>
  147. from shop as s
  148. <include refid="Supplier_Joins"/>
  149. where shopType = 1
  150. and (status = 90 or status = 9)
  151. and shopID != 1252
  152. </select>
  153. <select id="searchSupplierById" resultType="com.caimei365.commodity.model.search.SupplierDO">
  154. select
  155. <include refid="Search_Supplier_List"/>
  156. from shop as s
  157. <include refid="Supplier_Joins"/>
  158. where shopType = 1
  159. and (status = 90 or status = 9)
  160. and shopID != 1252
  161. and s.shopID = #{shopId}
  162. </select>
  163. <!-- 项目仪器 -->
  164. <sql id="Search_Equipment_List">
  165. e.id as e_id,
  166. e.title as e_name,
  167. e.precisehKey as e_keyword,
  168. e.headImage as e_image,
  169. e.docBoost as e_sort
  170. </sql>
  171. <select id="findEquipmentCount" resultType="java.lang.Integer">
  172. select count(*)
  173. from cm_page
  174. where type = 2
  175. and enabledStatus = 1
  176. </select>
  177. <select id="findEquipmentInvalidCount" resultType="java.lang.Integer">
  178. select count(*)
  179. from cm_page
  180. where type != 2
  181. or enabledStatus != 1
  182. </select>
  183. <select id="findEquipmentInvalidIds" resultType="java.lang.Integer">
  184. select id
  185. from cm_page
  186. where type != 2
  187. or enabledStatus != 1
  188. </select>
  189. <select id="searchEquipmentList" resultType="com.caimei365.commodity.model.search.EquipmentDO">
  190. select
  191. <include refid="Search_Equipment_List"/>
  192. from cm_page as e
  193. where e.type = 2 and e.enabledStatus = 1
  194. order by e.docBoost desc
  195. </select>
  196. <select id="searchEquipmentById" resultType="com.caimei365.commodity.model.search.EquipmentDO">
  197. select
  198. <include refid="Search_Equipment_List"/>
  199. from cm_page as e
  200. where e.id = #{equipmentId}
  201. and e.type = 2 and e.enabledStatus = 1
  202. </select>
  203. <!-- 文章 -->
  204. <sql id="Search_Article_List">
  205. a.id as a_id,
  206. a.title as a_title,
  207. a.guidanceImage as a_image,
  208. a.publisher as a_publisher,
  209. a.pubdate as a_publish_date,
  210. a.recommendContent as a_intro,
  211. a.infoContent as a_content,
  212. (IFNULL(c.pv, 0) +IFNULL(c.num, 0) + IFNULL(a.basePv, 0) + IFNULL(a.basePraise, 0)) as a_pv,
  213. a.label as a_label,
  214. a.typeId as a_type_id,
  215. b.name as a_type_text,
  216. a.priorityIndex as a_sort,
  217. a.relatedLabels as a_labels
  218. </sql>
  219. <sql id="Article_Joins">
  220. left join info_type b on a.typeId = b.id
  221. left join info_praise c on a.id = c.infoId
  222. </sql>
  223. <select id="findArticleCount" resultType="java.lang.Integer">
  224. select count(*)
  225. from info
  226. where enabledStatus = 1
  227. and auditStatus = 2
  228. and onlineStatus = 2
  229. </select>
  230. <select id="findArticleInvalidCount" resultType="java.lang.Integer">
  231. select count(*)
  232. from info
  233. where enabledStatus != 1
  234. or auditStatus != 2
  235. or onlineStatus != 2
  236. </select>
  237. <select id="findArticleInvalidIds" resultType="java.lang.Integer">
  238. select id
  239. from info
  240. where enabledStatus != 1
  241. or auditStatus != 2
  242. or onlineStatus != 2
  243. </select>
  244. <select id="searchArticleList" resultType="com.caimei365.commodity.model.search.ArticleDO">
  245. select
  246. <include refid="Search_Article_List"/>
  247. from info as a
  248. <include refid="Article_Joins"/>
  249. where a.enabledStatus = 1 and a.auditStatus = 2 and a.onlineStatus = 2
  250. order by a.id desc
  251. </select>
  252. <select id="searchArticleById" resultType="com.caimei365.commodity.model.search.ArticleDO">
  253. select
  254. <include refid="Search_Article_List"/>
  255. from info as a
  256. <include refid="Article_Joins"/>
  257. where a.id = #{articleId}
  258. and a.enabledStatus = 1
  259. and a.auditStatus = 2
  260. and a.onlineStatus = 2
  261. </select>
  262. <select id="findLabelIdsByName" resultType="java.lang.Integer">
  263. select id
  264. from info_label
  265. where infoLabelStatus = 1
  266. and name in
  267. <foreach collection="labelTexts" item="label" open="(" close=")" index="index" separator=",">
  268. #{label}
  269. </foreach>
  270. group by name
  271. order by clickRate desc
  272. </select>
  273. <select id="findLabelNameById" resultType="java.lang.String">
  274. select name
  275. from info_label
  276. where infoLabelStatus = 1
  277. and id = #{labelId}
  278. limit 1
  279. </select>
  280. <!-- 搜索容错 商品列表 -->
  281. <select id="queryProduct" resultType="com.caimei365.commodity.model.search.ProductListVo">
  282. select
  283. p.productID as productId,
  284. p.actStatus,
  285. p.`name` as name,
  286. p.mainImage as image,
  287. br.name as brandName,
  288. (select unit from cm_sku where productID=p.productID order by price asc limit 1) as unit,
  289. p.productCode as code,
  290. p.priceFlag,
  291. (select price from cm_sku where productID=p.productID order by price asc limit 1)as price,
  292. p.shopID as shopId,
  293. p.productType,
  294. p.searchKey as keyword
  295. from product p
  296. left join cm_brand as br on p.brandID = br.id
  297. left join cm_organize_product_info copi on copi.productId = p.productID and copi.organizeId = 0
  298. where p.productCategory = 1 and p.newvalidFlag=1
  299. <choose>
  300. <when test="identity == 1">
  301. and copi.validFlag in (2,3,9)
  302. </when>
  303. <when test="identity == 5">
  304. and copi.validFlag = 2
  305. </when>
  306. <when test="identity == 2">
  307. and p.visibility in (1,2,3) and copi.validFlag = 2
  308. </when>
  309. <when test="identity == 4">
  310. and p.visibility in (2,3) and copi.validFlag = 2
  311. </when>
  312. <when test="identity == 5">
  313. and p.visibility in (1,2,3,4) and copi.validFlag = 2
  314. </when>
  315. <otherwise>
  316. and p.visibility = 3 and copi.validFlag = 2
  317. </otherwise>
  318. </choose>
  319. <if test="shopId != null and shopId != ''">
  320. and p.shopID = #{shopId}
  321. </if>
  322. <if test="bigTypeId != null and bigTypeId != ''">
  323. and p.bigTypeID = #{bigTypeId}
  324. </if>
  325. <if test="smallTypeId != null and smallTypeId != ''">
  326. and p.smallTypeID = #{smallTypeId}
  327. </if>
  328. <if test="tinyTypeId != null and tinyTypeId != ''">
  329. and p.tinyTypeID = #{tinyTypeId}
  330. </if>
  331. <if test="keyword != null and keyword != ''">
  332. and p.name like concat('%',#{keyword},'%')
  333. </if>
  334. <choose>
  335. <when test="sortField != null and sortField != ''">
  336. <choose>
  337. <when test="sortField == 'price'">
  338. order by price
  339. </when>
  340. <when test="sortField == 'sales'">
  341. order by p.sellNumber
  342. </when>
  343. <when test="sortField == 'favorite'">
  344. order by p.favoriteTimes
  345. </when>
  346. <otherwise>
  347. order by p.productID
  348. </otherwise>
  349. </choose>
  350. </when>
  351. <otherwise>
  352. order by p.productID
  353. </otherwise>
  354. </choose>
  355. <choose>
  356. <when test="sortType == 1">
  357. desc
  358. </when>
  359. <otherwise>
  360. asc
  361. </otherwise>
  362. </choose>
  363. </select>
  364. <!-- 搜索容错 供应商列表 -->
  365. <select id="querySupplier" resultType="com.caimei365.commodity.model.search.ShopListVo">
  366. select s.shopID as shopId,
  367. s.name as name,
  368. s.logo as logo,
  369. s.authorizationCertificateImage as license,
  370. s.businessScope as business,
  371. CONCAT(p.name, c.name) as address
  372. from shop as s
  373. left join town as t on s.townId = t.townID
  374. left join city as c on t.cityID = c.cityID
  375. left join province as p on c.provinceID = p.provinceID
  376. where (status = 90 or status = 9)
  377. and shopID != 1252
  378. and s.name like concat('%', #{keyword}, '%')
  379. </select>
  380. <!-- 搜索容错 项目仪器列表 -->
  381. <select id="queryEquipment" resultType="com.caimei365.commodity.model.search.EquipmentListVo">
  382. select e.id as id,
  383. e.title as name,
  384. e.precisehKey as keyword,
  385. e.headImage as image
  386. from cm_page as e
  387. where e.type = 2
  388. and e.enabledStatus = 1
  389. and e.title like concat('%', #{keyword}, '%')
  390. order by e.docBoost desc
  391. </select>
  392. <!-- 搜索容错 文章列表 -->
  393. <select id="queryArticle" resultType="com.caimei365.commodity.model.search.ArticleListVo">
  394. select
  395. a.id as articleId,
  396. a.title as title,
  397. a.guidanceImage as image,
  398. a.publisher as publisher,
  399. a.pubdate as publishDate,
  400. a.recommendContent as intro,
  401. (IFNULL(c.pv, 0) +IFNULL(c.num, 0) + IFNULL(a.basePv, 0) + IFNULL(a.basePraise, 0)) as pv,
  402. a.typeId as typeId,
  403. b.name as typeText,
  404. a.label as labelText
  405. from info as a
  406. left join info_type b on a.typeId = b.id
  407. left join info_praise c on a.id = c.infoId
  408. <where>
  409. a.enabledStatus = 1
  410. and a.auditStatus = 2
  411. and a.onlineStatus = 2
  412. and a.delFlag = 0
  413. <if test="articleId != null">
  414. and a.id = #{articleId}
  415. </if>
  416. <if test="typeId != null">
  417. and a.typeId = #{typeId}
  418. </if>
  419. <if test="keyword != null and keyword != ''">
  420. and a.title like concat ('%',#{keyword},'%')
  421. </if>
  422. <if test="labelText != null">
  423. and a.label like concat ('%',#{labelText},'%')
  424. </if>
  425. </where>
  426. <choose>
  427. <when test="typeId != null or labelText != null">
  428. order by a.id desc
  429. </when>
  430. <otherwise>
  431. order by a.priorityIndex desc
  432. </otherwise>
  433. </choose>
  434. </select>
  435. <!-- 搜索容错 文章列表 v1.2.8新加根据时间排序-->
  436. <select id="queryArticleByProDate" resultType="com.caimei365.commodity.model.search.ArticleListVo">
  437. select
  438. a.id as articleId,
  439. a.title as title,
  440. a.guidanceImage as image,
  441. a.publisher as publisher,
  442. a.pubdate as publishDate,
  443. a.recommendContent as intro,
  444. (IFNULL(c.pv, 0) +IFNULL(c.num, 0) + IFNULL(a.basePv, 0) + IFNULL(a.basePraise, 0)) as pv,
  445. a.typeId as typeId,
  446. b.name as typeText,
  447. a.label as labelText
  448. from info as a
  449. left join info_type b on a.typeId = b.id
  450. left join info_praise c on a.id = c.infoId
  451. <where>
  452. a.enabledStatus = 1
  453. and a.auditStatus = 2
  454. <if test="articleId != null">
  455. and a.id = #{articleId}
  456. </if>
  457. <if test="typeId != null">
  458. and a.typeId = #{typeId}
  459. </if>
  460. <if test="keyword != null and keyword != ''">
  461. and a.title like concat ('%',#{keyword},'%')
  462. </if>
  463. <if test="labelText != null">
  464. and a.label like concat ('%',#{labelText},'%')
  465. </if>
  466. </where>
  467. <if test="sortType != null">
  468. <if test="sortType == 1">
  469. order by a.pubdate desc
  470. </if>
  471. <if test="sortType != 1">
  472. order by (IFNULL(c.pv, 0) +IFNULL(c.num, 0) + IFNULL(a.basePv, 0) + IFNULL(a.basePraise, 0)) desc
  473. </if>
  474. </if>
  475. </select>
  476. <select id="getHistoryList" resultType="com.caimei365.commodity.model.po.SearchHistoryPo">
  477. select id, userId, searchWord, searchDate, delFlag
  478. from user_search_history
  479. where userId = #{userId}
  480. order by searchDate desc
  481. </select>
  482. <select id="getHistoryIdByWord" resultType="java.lang.Integer">
  483. select id
  484. from user_search_history
  485. where searchWord = #{keyword}
  486. and userId = #{userId}
  487. limit 1
  488. </select>
  489. <select id="getHistoryCount" resultType="java.lang.Integer">
  490. select COUNT(*)
  491. from user_search_history
  492. where userId = #{userId}
  493. AND delFlag = 0
  494. </select>
  495. <update id="updateSearchHistoryById">
  496. update user_search_history
  497. <set>
  498. <if test="userId != null">
  499. userId = #{userId},
  500. </if>
  501. <if test="searchWord != null">
  502. searchWord = #{searchWord},
  503. </if>
  504. <if test="searchDate != null">
  505. searchDate = #{searchDate},
  506. </if>
  507. <if test="delFlag != null">
  508. delFlag = #{delFlag},
  509. </if>
  510. </set>
  511. where id = #{id}
  512. </update>
  513. <insert id="insertSearchHistory">
  514. INSERT INTO user_search_history
  515. <trim prefix="(" suffix=")" suffixOverrides=",">
  516. <if test="userId != null">
  517. userId,
  518. </if>
  519. <if test="searchWord != null">
  520. searchWord,
  521. </if>
  522. <if test="searchDate != null">
  523. searchDate,
  524. </if>
  525. <if test="delFlag != null">
  526. delFlag,
  527. </if>
  528. </trim>
  529. <trim prefix="values (" suffix=")" suffixOverrides=",">
  530. <if test="userId != null">
  531. #{userId},
  532. </if>
  533. <if test="searchWord != null">
  534. #{searchWord},
  535. </if>
  536. <if test="searchDate != null">
  537. #{searchDate},
  538. </if>
  539. <if test="delFlag != null">
  540. #{delFlag},
  541. </if>
  542. </trim>
  543. </insert>
  544. <delete id="deleteSearchHistory">
  545. DELETE
  546. FROM user_search_history
  547. WHERE userId = #{userId}
  548. AND id NOT IN (SELECT temp.id
  549. FROM (SELECT id
  550. FROM user_search_history
  551. WHERE userId = #{userId}
  552. ORDER BY searchDate DESC
  553. LIMIT 10) AS temp)
  554. </delete>
  555. <delete id="deleteAllSearchHistory">
  556. DELETE
  557. FROM user_search_history
  558. WHERE userId = #{userId}
  559. </delete>
  560. <select id="findNew" resultType="java.lang.Integer">
  561. select productID from product
  562. where TIMESTAMP(ADDTIME) <![CDATA[ > ]]> TIMESTAMP(#{n})
  563. <if test="productIds != null">
  564. and productID in
  565. <foreach collection="productIds" item="productId" index="index" open="(" separator="," close=")">
  566. #{productId}
  567. </foreach>
  568. </if>
  569. </select>
  570. <select id="findPromotion" resultType="java.lang.Integer">
  571. select cpp.productID from cm_promotions_product cpp
  572. left join cm_promotions cp on cpp.promotionsId = cp.id
  573. where cpp.productId in
  574. <foreach collection="productIds" item="productId" index="index" open="(" separator="," close=")">
  575. #{productId}
  576. </foreach>
  577. and cp.endTime>now() or (cp.status='1')
  578. and cp.delflag = 0
  579. </select>
  580. <select id="findPromotionId" resultType="java.lang.Integer">
  581. select pr.id
  582. from cm_promotions pr
  583. left join cm_promotions_product prp on pr.id = prp.promotionsId
  584. where (prp.productId = #{p_id} or
  585. prp.supplierId = (select p.shopID from product p where p.productID = #{p_id})
  586. )
  587. and (pr.status = 1 or (pr.status = 2 and (NOW() between pr.beginTime and pr.endTime)))
  588. and pr.delFlag not in (1, 2)
  589. order by pr.type desc
  590. limit 1
  591. </select>
  592. <select id="countViewsByDate" resultType="java.lang.Integer">
  593. select IFNULL(sum(views), 0)
  594. from cm_product_views_record
  595. where productId = #{productId}
  596. and viewTime > #{date}
  597. </select>
  598. <select id="countSalesByDate" resultType="java.lang.Integer">
  599. select IFNULL(sum(sales), 0)
  600. from cm_product_sales_record
  601. where productId = #{productId}
  602. and saleTime > #{date}
  603. </select>
  604. <select id="findPromotions" resultType="java.lang.Integer">
  605. SELECT DISTINCT cpp.productId
  606. FROM cm_promotions cp
  607. LEFT JOIN cm_promotions_product cpp ON cp.id = cpp.promotionsId
  608. WHERE (cp.status = 1 OR (cp.status = 2 AND cp.endTime > NOW()))
  609. AND cp.delFlag = 0
  610. </select>
  611. <select id="findServiceProductIds" resultType="java.lang.String">
  612. SELECT GROUP_CONCAT(cpp.productId SEPARATOR '|') AS ps
  613. FROM cm_provider_product cpp
  614. LEFT JOIN serviceprovider sp ON cpp.providerId = sp.serviceProviderID
  615. WHERE sp.serviceProviderId = #{serviceProviderId}
  616. </select>
  617. </mapper>