FsPackageOrderMapper.xml 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.fs.his.mapper.FsPackageOrderMapper">
  6. <resultMap type="FsPackageOrder" id="FsPackageOrderResult">
  7. <result property="orderId" column="order_id" />
  8. <result property="orderSn" column="order_sn" />
  9. <result property="userId" column="user_id" />
  10. <result property="doctorId" column="doctor_id" />
  11. <result property="packageId" column="package_id" />
  12. <result property="packageName" column="package_name" />
  13. <result property="packageSecondName" column="package_second_name" />
  14. <result property="payMoney" column="pay_money" />
  15. <result property="isPay" column="is_pay" />
  16. <result property="days" column="days" />
  17. <result property="status" column="status" />
  18. <result property="startTime" column="start_time" />
  19. <result property="finishTime" column="finish_time" />
  20. <result property="createTime" column="create_time" />
  21. <result property="companyId" column="company_id" />
  22. <result property="companyUserId" column="company_user_id" />
  23. <result property="formJson" column="form_json" />
  24. <result property="inquiryOrderId" column="inquiry_order_id" />
  25. <result property="storeOrderId" column="store_order_id" />
  26. <result property="packageJson" column="package_json" />
  27. <result property="patientJson" column="patient_json" />
  28. <result property="refundImg" column="refund_img" />
  29. <result property="refundExplain" column="refund_explain" />
  30. <result property="refundReason" column="refund_reason" />
  31. <result property="refundTime" column="refund_time" />
  32. <result property="refundMoney" column="refund_money" />
  33. <result property="payTime" column="pay_time" />
  34. <result property="refundStatus" column="refund_status" />
  35. <result property="totalPrice" column="total_price" />
  36. <result property="addressJson" column="address_json" />
  37. <result property="payType" column="pay_type" />
  38. <result property="remark" column="remark" />
  39. <result property="storeId" column="store_id" />
  40. <result property="payPrice" column="pay_price" />
  41. <result property="payRemain" column="pay_remain" />
  42. <result property="payDelivery" column="pay_delivery" />
  43. <result property="discountMoney" column="discount_money" />
  44. <result property="followNum" column="follow_num" />
  45. <result property="followCount" column="follow_count" />
  46. <result property="productCostPrice" column="product_cost_price" />
  47. <result property="inquiryCostPrice" column="inquiry_cost_price" />
  48. <result property="totalCostPrice" column="total_cost_price" />
  49. <result property="costDiscountMoney" column="cost_discount_money" />
  50. <result property="cycle" column="cycle" />
  51. <result property="packageSubType" column="package_sub_type" />
  52. <result property="followTempId" column="follow_temp_id" />
  53. <result property="followRate" column="follow_rate" />
  54. <result property="followTime" column="follow_time" />
  55. <result property="followDoctorId" column="follow_doctor_id" />
  56. <result property="prescribePrice" column="prescribe_price" />
  57. <result property="deptId" column="dept_id" />
  58. <result property="doctorRemark" column="doctor_remark" />
  59. <result property="userCouponId" column="user_coupon_id" />
  60. <result property="icdCode" column="icd_code" />
  61. <result property="source" column="source" />
  62. </resultMap>
  63. <sql id="selectFsPackageOrderVo">
  64. select order_id,icd_code,source,pay_price,user_coupon_id,follow_temp_id,doctor_remark,dept_id,prescribe_price,follow_doctor_id,follow_rate,follow_time,follow_num,package_sub_type,cycle,product_cost_price,inquiry_cost_price,total_cost_price,cost_discount_money,follow_count,pay_remain,pay_delivery,discount_money, order_sn,refund_status,store_id,total_price,address_json,pay_type,remark,inquiry_order_id,pay_time,package_json,patient_json,refund_img,refund_explain,refund_reason,refund_time,refund_money,store_order_id, user_id,form_json, company_id,company_user_id,doctor_id, package_id, package_name,package_second_name, pay_money, is_pay, days, status, start_time, finish_time, create_time from fs_package_order
  65. </sql>
  66. <select id="selectFsPackageOrderList" parameterType="FsPackageOrder" resultMap="FsPackageOrderResult">
  67. <include refid="selectFsPackageOrderVo"/>
  68. <where>
  69. <if test="orderSn != null and orderSn != ''"> and order_sn = #{orderSn}</if>
  70. <if test="userId != null "> and user_id = #{userId}</if>
  71. <if test="doctorId != null "> and doctor_id = #{doctorId}</if>
  72. <if test="packageId != null "> and package_id = #{packageId}</if>
  73. <if test="packageName != null and packageName != ''"> and package_name like concat('%', #{packageName}, '%')</if>
  74. <if test="payMoney != null "> and pay_money = #{payMoney}</if>
  75. <if test="isPay != null "> and is_pay = #{isPay}</if>
  76. <if test="days != null "> and days = #{days}</if>
  77. <if test="status != null "> and status = #{status}</if>
  78. <if test="startTime != null "> and start_time = #{startTime}</if>
  79. <if test="finishTime != null "> and finish_time = #{finishTime}</if>
  80. <if test="companyUserId != null "> and company_user_id = #{companyUserId}</if>
  81. <if test="companyId != null "> and company_id = #{companyId}</if>
  82. </where>
  83. </select>
  84. <select id="selectFsPackageOrderByOrderId" parameterType="Long" resultMap="FsPackageOrderResult">
  85. <include refid="selectFsPackageOrderVo"/>
  86. where order_id = #{orderId}
  87. </select>
  88. <insert id="insertFsPackageOrder" parameterType="FsPackageOrder" useGeneratedKeys="true" keyProperty="orderId">
  89. insert into fs_package_order
  90. <trim prefix="(" suffix=")" suffixOverrides=",">
  91. <if test="orderSn != null">order_sn,</if>
  92. <if test="userId != null">user_id,</if>
  93. <if test="doctorId != null">doctor_id,</if>
  94. <if test="packageId != null">package_id,</if>
  95. <if test="packageName != null">package_name,</if>
  96. <if test="packageSecondName != null">package_second_name,</if>
  97. <if test="payMoney != null">pay_money,</if>
  98. <if test="isPay != null">is_pay,</if>
  99. <if test="days != null">days,</if>
  100. <if test="status != null">status,</if>
  101. <if test="startTime != null">start_time,</if>
  102. <if test="finishTime != null">finish_time,</if>
  103. <if test="createTime != null">create_time,</if>
  104. <if test="companyId != null">company_id,</if>
  105. <if test="companyUserId != null">company_user_id,</if>
  106. <if test="formJson != null">form_json,</if>
  107. <if test="inquiryOrderId != null">inquiry_order_id,</if>
  108. <if test="storeOrderId != null">inquiry_order_id,</if>
  109. <if test="packageJson != null">package_json,</if>
  110. <if test="patientJson != null">patient_json,</if>
  111. <if test="refundImg != null">refund_img,</if>
  112. <if test="refundExplain != null">refund_explain,</if>
  113. <if test="refundReason != null">refund_reason,</if>
  114. <if test="refundTime != null">refund_time,</if>
  115. <if test="refundMoney != null">refund_money,</if>
  116. <if test="payTime != null">pay_time,</if>
  117. <if test="refundStatus != null">refund_status,</if>
  118. <if test="totalPrice != null">total_price,</if>
  119. <if test="addressJson != null">address_json,</if>
  120. <if test="payType != null">pay_type,</if>
  121. <if test="remark != null">remark,</if>
  122. <if test="storeId != null">store_id,</if>
  123. <if test="payPrice != null">pay_price,</if>
  124. <if test="payRemain != null">pay_remain,</if>
  125. <if test="payDelivery != null">pay_delivery,</if>
  126. <if test="discountMoney != null">discount_money,</if>
  127. <if test="followNum != null">follow_num,</if>
  128. <if test="followCount != null">follow_count,</if>
  129. <if test="productCostPrice != null">product_cost_price,</if>
  130. <if test="inquiryCostPrice != null">inquiry_cost_price,</if>
  131. <if test="totalCostPrice != null">total_cost_price,</if>
  132. <if test="costDiscountMoney != null">cost_discount_money,</if>
  133. <if test="cycle != null">`cycle`,</if>
  134. <if test="packageSubType != null">package_sub_type,</if>
  135. <if test="followTempId != null">follow_temp_id,</if>
  136. <if test="followRate != null">follow_rate,</if>
  137. <if test="followTime != null">follow_time,</if>
  138. <if test="followDoctorId != null">follow_doctor_id,</if>
  139. <if test="prescribePrice != null">prescribe_price,</if>
  140. <if test="deptId != null">dept_id,</if>
  141. <if test="doctorRemark != null">doctor_remark,</if>
  142. <if test="userCouponId != null">user_coupon_id,</if>
  143. <if test="icdCode != null">icd_code,</if>
  144. <if test="source != null">source,</if>
  145. </trim>
  146. <trim prefix="values (" suffix=")" suffixOverrides=",">
  147. <if test="orderSn != null">#{orderSn},</if>
  148. <if test="userId != null">#{userId},</if>
  149. <if test="doctorId != null">#{doctorId},</if>
  150. <if test="packageId != null">#{packageId},</if>
  151. <if test="packageName != null">#{packageName},</if>
  152. <if test="packageSecondName != null">#{packageSecondName},</if>
  153. <if test="payMoney != null">#{payMoney},</if>
  154. <if test="isPay != null">#{isPay},</if>
  155. <if test="days != null">#{days},</if>
  156. <if test="status != null">#{status},</if>
  157. <if test="startTime != null">#{startTime},</if>
  158. <if test="finishTime != null">#{finishTime},</if>
  159. <if test="createTime != null">#{createTime},</if>
  160. <if test="companyId != null">#{companyId},</if>
  161. <if test="companyUserId != null">#{companyUserId},</if>
  162. <if test="formJson != null">#{formJson},</if>
  163. <if test="inquiryOrderId != null">#{inquiryOrderId},</if>
  164. <if test="storeOrderId != null">#{storeOrderId},</if>
  165. <if test="packageJson != null">#{packageJson},</if>
  166. <if test="patientJson != null">#{patientJson},</if>
  167. <if test="refundImg != null">#{refundImg},</if>
  168. <if test="refundExplain != null">#{refundExplain},</if>
  169. <if test="refundReason != null">#{refundReason},</if>
  170. <if test="refundTime != null">#{refundTime},</if>
  171. <if test="refundMoney != null">#{refundMoney},</if>
  172. <if test="payTime != null">#{payTime},</if>
  173. <if test="refundStatus != null">#{refundStatus},</if>
  174. <if test="totalPrice != null">#{totalPrice},</if>
  175. <if test="addressJson != null">#{addressJson},</if>
  176. <if test="payType != null">#{payType},</if>
  177. <if test="remark != null">#{remark},</if>
  178. <if test="storeId != null">#{storeId},</if>
  179. <if test="payPrice != null">#{payPrice},</if>
  180. <if test="payRemain != null">#{payRemain},</if>
  181. <if test="payDelivery != null">#{payDelivery},</if>
  182. <if test="discountMoney != null">#{discountMoney},</if>
  183. <if test="followNum != null">#{followNum},</if>
  184. <if test="followCount != null">#{followCount},</if>
  185. <if test="productCostPrice != null">#{productCostPrice},</if>
  186. <if test="inquiryCostPrice != null">#{inquiryCostPrice},</if>
  187. <if test="totalCostPrice != null">#{totalCostPrice},</if>
  188. <if test="costDiscountMoney != null">#{costDiscountMoney},</if>
  189. <if test="cycle != null">#{cycle},</if>
  190. <if test="packageSubType != null">#{packageSubType},</if>
  191. <if test="followTempId != null">#{followTempId},</if>
  192. <if test="followRate != null">#{followRate},</if>
  193. <if test="followTime != null">#{followTime},</if>
  194. <if test="followDoctorId != null">#{followDoctorId},</if>
  195. <if test="prescribePrice != null">#{prescribePrice},</if>
  196. <if test="deptId != null">#{deptId},</if>
  197. <if test="doctorRemark != null">#{doctorRemark},</if>
  198. <if test="userCouponId != null">#{userCouponId},</if>
  199. <if test="icdCode != null">#{icdCode},</if>
  200. <if test="source != null">#{source},</if>
  201. </trim>
  202. </insert>
  203. <update id="updateFsPackageOrder" parameterType="FsPackageOrder">
  204. update fs_package_order
  205. <trim prefix="SET" suffixOverrides=",">
  206. <if test="orderSn != null">order_sn = #{orderSn},</if>
  207. <if test="userId != null">user_id = #{userId},</if>
  208. <if test="doctorId != null">doctor_id = #{doctorId},</if>
  209. <if test="packageId != null">package_id = #{packageId},</if>
  210. <if test="packageName != null">package_name = #{packageName},</if>
  211. <if test="packageSecondName != null">package_second_name = #{packageSecondName},</if>
  212. <if test="payMoney != null">pay_money = #{payMoney},</if>
  213. <if test="isPay != null">is_pay = #{isPay},</if>
  214. <if test="days != null">days = #{days},</if>
  215. <if test="status != null">status = #{status},</if>
  216. <if test="startTime != null">start_time = #{startTime},</if>
  217. <if test="finishTime != null">finish_time = #{finishTime},</if>
  218. <if test="createTime != null">create_time = #{createTime},</if>
  219. <if test="companyId != null">company_id = #{companyId},</if>
  220. <if test="companyUserId != null">company_user_id = #{companyUserId},</if>
  221. <if test="formJson != null">form_json = #{formJson},</if>
  222. <if test="packageJson != null">package_json = #{packageJson},</if>
  223. <if test="inquiryOrderId != null">inquiry_order_id = #{inquiryOrderId},</if>
  224. <if test="storeOrderId != null">store_order_id = #{storeOrderId},</if>
  225. <if test="patientJson != null">patient_json = #{patientJson},</if>
  226. <if test="refundImg != null">refund_img = #{refundImg},</if>
  227. <if test="refundExplain != null">refund_explain = #{refundExplain},</if>
  228. <if test="refundReason != null">refund_reason = #{refundReason},</if>
  229. <if test="refundTime != null">refund_time = #{refundTime},</if>
  230. <if test="refundMoney != null">refund_money = #{refundMoney},</if>
  231. <if test="payTime != null">pay_time = #{payTime},</if>
  232. <if test="refundStatus != null">refund_status = #{refundStatus},</if>
  233. <if test="totalPrice != null">total_price = #{totalPrice},</if>
  234. <if test="addressJson != null">address_json = #{addressJson},</if>
  235. <if test="payType != null">pay_type = #{payType},</if>
  236. <if test="remark != null">remark = #{remark},</if>
  237. <if test="storeId != null">store_id = #{storeId},</if>
  238. <if test="payPrice != null">pay_price = #{payPrice},</if>
  239. <if test="payRemain != null">pay_remain = #{payRemain},</if>
  240. <if test="payDelivery != null">pay_delivery = #{payDelivery},</if>
  241. <if test="discountMoney != null">discount_money = #{discountMoney},</if>
  242. <if test="followNum != null">follow_num = #{followNum},</if>
  243. <if test="followCount != null">follow_count = #{followCount},</if>
  244. <if test="productCostPrice != null">product_cost_price = #{productCostPrice},</if>
  245. <if test="inquiryCostPrice != null">inquiry_cost_price = #{inquiryCostPrice},</if>
  246. <if test="totalCostPrice != null">total_cost_price = #{totalCostPrice},</if>
  247. <if test="costDiscountMoney != null">cost_discount_money = #{costDiscountMoney},</if>
  248. <if test="cycle != null">`cycle` = #{cycle},</if>
  249. <if test="packageSubType != null">package_sub_type = #{packageSubType},</if>
  250. <if test="followTempId != null">follow_temp_id = #{followTempId},</if>
  251. <if test="followRate != null">follow_rate = #{followRate},</if>
  252. <if test="followTime != null">follow_time = #{followTime},</if>
  253. <if test="followDoctorId != null">follow_doctor_id = #{followDoctorId},</if>
  254. <if test="prescribePrice != null">prescribe_price = #{prescribePrice},</if>
  255. <if test="deptId != null">dept_id = #{deptId},</if>
  256. <if test="doctorRemark != null">doctor_remark = #{doctorRemark},</if>
  257. <if test="userCouponId != null">user_coupon_id = #{userCouponId},</if>
  258. <if test="icdCode != null">icd_code = #{icdCode},</if>
  259. <if test="source != null">source = #{source},</if>
  260. </trim>
  261. where order_id = #{orderId}
  262. </update>
  263. <delete id="deleteFsPackageOrderByOrderId" parameterType="Long">
  264. delete from fs_package_order where order_id = #{orderId}
  265. </delete>
  266. <delete id="deleteFsPackageOrderByOrderIds" parameterType="String">
  267. delete from fs_package_order where order_id in
  268. <foreach item="orderId" collection="array" open="(" separator="," close=")">
  269. #{orderId}
  270. </foreach>
  271. </delete>
  272. <select id="selectFsPackageOrderListVO_COUNT" parameterType="com.fs.his.param.FsStoreOrderParam" resultType="Long">
  273. select count(1) from fs_package_order o
  274. <if test="maps.phone != null"> LEFT JOIN fs_user u ON u.user_id = o.user_id </if>
  275. <if test="maps.doctorName != null">LEFT JOIN fs_doctor d ON d.doctor_id = o.doctor_id </if>
  276. <if test="(maps.userPhone != null and maps.userPhone != '') or (maps.userName != null and maps.userName != '')">LEFT JOIN fs_store_order fso ON fso.order_id = o.store_order_id </if>
  277. <if test='maps.companyUserName != null or maps.params.dataScope!=null'>LEFT JOIN company_user cu ON cu.user_id = o.company_user_id </if>
  278. where 1=1
  279. <if test="maps.orderSn != null and maps.orderSn != ''"> and o.order_sn = #{maps.orderSn}</if>
  280. <if test="maps.phone != null">and u.phone like concat( #{maps.phone}, '%')</if>
  281. <if test="maps.doctorName != null">and d.doctor_name like concat('%', #{maps.doctorName}, '%')</if>
  282. <if test="maps.packageName != null and maps.packageName != ''"> and o.package_name like concat('%', #{maps.packageName}, '%')</if>
  283. <if test="maps.isPay != null"> and o.is_pay = #{maps.isPay}</if>
  284. <if test="maps.packageSubType != null"> and o.package_sub_type = #{maps.packageSubType}</if>
  285. <if test="maps.userName != null and maps.userName != ''"> and fso.user_name like concat('%', #{maps.userName}, '%')</if>
  286. <if test="maps.userPhone != null and maps.userPhone != ''"> and fso.user_phone = #{maps.userPhone}</if>
  287. <if test="maps.status != null"> and o.status = #{maps.status}</if>
  288. <if test="maps.sTime != null"> and DATE(o.start_time) &gt;= DATE(#{maps.sTime})</if>
  289. <if test="maps.eTime != null"> and DATE(o.start_time) &lt;= DATE(#{maps.eTime})</if>
  290. <if test="maps.stTime != null"> and DATE(o.create_time) &gt;= DATE(#{maps.stTime})</if>
  291. <if test="maps.endTime != null"> and DATE(o.create_time) &lt;= DATE(#{maps.endTime})</if>
  292. <if test="maps.endStartTime != null"> and DATE(o.finish_time) &gt;= DATE(#{maps.endStartTime})</if>
  293. <if test="maps.endEndTime != null"> and DATE(o.finish_time) &lt;= DATE(#{maps.endEndTime})</if>
  294. <if test="maps.companyUserId != null"> and o.company_user_id = #{maps.companyUserId}</if>
  295. <if test='maps.companyUserName != null'> and cu.user_name like concat('%', #{maps.companyUserName}, '%')</if>
  296. <if test='maps.companyId != null and maps.companyId != "-1"'> and o.company_id =#{maps.companyId} </if>
  297. <if test=' maps.companyId == "-1"'> and o.company_id is null </if>
  298. <if test='maps.deptId != null'> AND (o.dept_id = #{maps.deptId} OR o.dept_id IN ( SELECT t.dept_id FROM company_dept t WHERE find_in_set(#{maps.deptId}, ancestors) )) </if>
  299. </select>
  300. <select id="selectFsPackageByOrderId" resultType="com.fs.his.domain.FsPackage">
  301. SELECT fp.* FROM fs_package_order fpo LEFT JOIN fs_package fp ON fpo.package_id = fp.package_id WHERE fpo.order_id = #{packageOrderId}
  302. </select>
  303. <select id="getNewOrder" resultType="com.fs.his.dto.PackageOrderDTO">
  304. SELECT O.pay_time AS payTime, O.create_time AS createTime , O.order_sn AS orderSn , O.user_id AS userId ,
  305. U.nick_name AS fsUserName , P.package_name AS packageName
  306. FROM fs_barrage_package_order O
  307. LEFT JOIN fs_user U ON O.user_id = U.user_id
  308. LEFT JOIN fs_package P ON O.package_id = P.package_id
  309. where O.is_pay = 1 ORDER BY RAND() desc LIMIT 20
  310. </select>
  311. <select id="selectOutTimeOrderList" resultType="com.fs.his.domain.FsPackageOrder">
  312. select * from fs_package_order where status = 1 AND NOW() &gt; DATE_ADD(create_time, INTERVAL ${unPayTime} MINUTE)
  313. </select>
  314. <select id="selectFsPackageOrderReportVO" resultType="com.fs.his.vo.FsPackageOrderReportVO">
  315. WITH order_stats AS (
  316. SELECT
  317. po.company_id,
  318. COUNT(po.order_id) AS order_count,
  319. COALESCE(SUM(po.pay_money), 0) AS total_money,
  320. COUNT(CASE WHEN so.status = 3 THEN po.order_id END) AS receipt_order_count,
  321. COALESCE(SUM(CASE WHEN so.status = 3 THEN po.pay_money ELSE 0 END), 0) AS receipt_money
  322. FROM fs_package_order po
  323. INNER JOIN fs_store_order so ON po.order_id = so.order_id
  324. <where>
  325. <if test="stTime != null">
  326. AND po.start_time &gt;= #{stTime}
  327. </if>
  328. <if test="endEndTime != null">
  329. AND po.finish_time &lt;= #{endEndTime}
  330. </if>
  331. <!-- 训练营条件 -->
  332. <if test="trainingCampId != null">
  333. AND EXISTS (
  334. SELECT 1 FROM fs_user_company_user ucu
  335. JOIN fs_user_course_training_camp camp ON ucu.company_user_id = camp.user_id
  336. WHERE ucu.company_id = po.company_id
  337. AND camp.training_camp_id = #{trainingCampId}
  338. )
  339. </if>
  340. </where>
  341. GROUP BY po.company_id
  342. )
  343. <choose>
  344. <!-- 公司维度 -->
  345. <when test="dimension == 'company'">
  346. SELECT
  347. c.company_id AS id,
  348. c.company_name AS companyName,
  349. 'company' AS dimensionType,
  350. COALESCE(os.order_count, 0) AS orderNum,
  351. COALESCE(os.total_money, 0) AS money,
  352. COALESCE(os.receipt_order_count, 0) AS receiptOrder,
  353. COALESCE(os.receipt_money, 0) AS receiptMoney
  354. FROM company c
  355. LEFT JOIN order_stats os ON c.company_id = os.company_id
  356. WHERE COALESCE(os.order_count, 0) > 0
  357. <if test="companyId != null and companyId != ''">
  358. AND c.company_id = #{companyId}
  359. </if>
  360. <if test="deptId != null and deptId != ''">
  361. AND c.dept_id = #{deptId}
  362. </if>
  363. </when>
  364. <!-- 部门维度 -->
  365. <when test="dimension == 'dept'">
  366. SELECT
  367. cd.dept_id AS id,
  368. cd.dept_name AS deptName,
  369. 'dept' AS dimensionType,
  370. c.company_name AS companyName,
  371. COALESCE(os.order_count, 0) AS orderNum,
  372. COALESCE(os.total_money, 0) AS money,
  373. COALESCE(os.receipt_order_count, 0) AS receiptOrder,
  374. COALESCE(os.receipt_money, 0) AS receiptMoney
  375. FROM company_dept cd
  376. LEFT JOIN company c ON cd.company_id = c.company_id
  377. LEFT JOIN order_stats os ON c.company_id = os.company_id
  378. WHERE COALESCE(os.order_count, 0) > 0
  379. <if test="companyId != null and companyId != ''">
  380. AND c.company_id = #{companyId}
  381. </if>
  382. <if test="deptId != null and deptId != ''">
  383. AND cd.dept_id = #{deptId}
  384. </if>
  385. </when>
  386. </choose>
  387. ORDER BY orderNum DESC, money DESC
  388. </select>
  389. <select id="selectFsOrderReportVO" resultType="com.fs.his.vo.FsOrderReportVO">
  390. SELECT
  391. c.company_name AS companyName,
  392. COALESCE(SUM(CASE WHEN order_type = 'inquiry' THEN order_count END), 0) AS inquiryOrderCount,
  393. COALESCE(SUM(CASE WHEN order_type = 'inquiry' THEN order_amount END), 0) AS inquiryOrderAmount,
  394. COALESCE(SUM(CASE WHEN order_type = 'integral' THEN order_count END), 0) AS integralOrderCount,
  395. COALESCE(SUM(CASE WHEN order_type = 'integral' THEN order_amount END), 0) AS integralOrderAmount,
  396. COALESCE(SUM(CASE WHEN order_type = 'package' THEN order_count END), 0) AS packageOrderCount,
  397. COALESCE(SUM(CASE WHEN order_type = 'package' THEN order_amount END), 0) AS packageOrderAmount
  398. FROM company c
  399. LEFT JOIN (
  400. -- 合并三个查询,只扫描一次
  401. SELECT inquiry.company_id, 'inquiry' AS order_type, COUNT(*) AS order_count, COALESCE(SUM(pay_money), 0) AS order_amount
  402. FROM fs_inquiry_order inquiry
  403. WHERE inquiry.status = 3
  404. <if test="stTime != null and endEndTime != null">
  405. AND inquiry.create_time BETWEEN #{stTime} AND #{endEndTime}
  406. </if>
  407. <if test="trainingCampId != null">
  408. AND EXISTS (
  409. SELECT 1 FROM fs_user_company_user ucu
  410. JOIN fs_user_course_training_camp camp ON ucu.company_user_id = camp.user_id
  411. WHERE ucu.company_id = inquiry.company_id
  412. AND camp.training_camp_id = #{trainingCampId}
  413. )
  414. </if>
  415. GROUP BY inquiry.company_id
  416. UNION ALL
  417. SELECT integral.company_id, 'integral' AS order_type, COUNT(*) AS order_count, COALESCE(SUM(pay_money), 0) AS order_amount
  418. FROM fs_integral_order integral
  419. WHERE integral.status = 3 AND pay_money IS NOT NULL
  420. <if test="stTime != null and endEndTime != null">
  421. AND integral.create_time BETWEEN #{stTime} AND #{endEndTime}
  422. </if>
  423. <if test="trainingCampId != null">
  424. AND EXISTS (
  425. SELECT 1 FROM fs_user_company_user ucu
  426. JOIN fs_user_course_training_camp camp ON ucu.company_user_id = camp.user_id
  427. WHERE ucu.company_id = integral.company_id
  428. AND camp.training_camp_id = #{trainingCampId}
  429. )
  430. </if>
  431. GROUP BY integral.company_id
  432. UNION ALL
  433. SELECT package.company_id, 'package' AS order_type, COUNT(*) AS order_count, COALESCE(SUM(pay_money), 0) AS order_amount
  434. FROM fs_package_order package
  435. WHERE package.status = 3
  436. <if test="stTime != null and endEndTime != null">
  437. AND package.create_time BETWEEN #{stTime} AND #{endEndTime}
  438. </if>
  439. <if test="trainingCampId != null">
  440. AND EXISTS (
  441. SELECT 1 FROM fs_user_company_user ucu
  442. JOIN fs_user_course_training_camp camp ON ucu.company_user_id = camp.user_id
  443. WHERE ucu.company_id = package.company_id
  444. AND camp.training_camp_id = #{trainingCampId}
  445. )
  446. </if>
  447. GROUP BY package.company_id
  448. ) AS all_orders ON c.company_id = all_orders.company_id
  449. WHERE 1=1
  450. <if test="companyId != null and companyId != ''">
  451. AND c.company_id = #{companyId}
  452. </if>
  453. GROUP BY c.company_id, c.company_name
  454. HAVING inquiryOrderCount > 0 OR integralOrderCount > 0 OR packageOrderCount > 0
  455. ORDER BY inquiryOrderCount DESC, integralOrderCount DESC, packageOrderCount DESC
  456. </select>
  457. <select id="selectFsOrderReportVODept" resultType="com.fs.his.vo.FsOrderReportVO">
  458. SELECT
  459. d.dept_name AS deptName,
  460. COALESCE(SUM(CASE WHEN order_type = 'inquiry' THEN order_count END), 0) AS inquiryOrderCount,
  461. COALESCE(SUM(CASE WHEN order_type = 'inquiry' THEN order_amount END), 0) AS inquiryOrderAmount,
  462. COALESCE(SUM(CASE WHEN order_type = 'integral' THEN order_count END), 0) AS integralOrderCount,
  463. COALESCE(SUM(CASE WHEN order_type = 'integral' THEN order_amount END), 0) AS integralOrderAmount,
  464. COALESCE(SUM(CASE WHEN order_type = 'package' THEN order_count END), 0) AS packageOrderCount,
  465. COALESCE(SUM(CASE WHEN order_type = 'package' THEN order_amount END), 0) AS packageOrderAmount
  466. FROM company_dept d
  467. LEFT JOIN company c ON d.company_id = c.company_id
  468. LEFT JOIN (
  469. <!-- 问诊订单 -->
  470. SELECT
  471. company_id,
  472. 'inquiry' AS order_type,
  473. COUNT(*) AS order_count,
  474. COALESCE(SUM(pay_money), 0) AS order_amount
  475. FROM fs_inquiry_order
  476. WHERE status = 3
  477. <if test="stTime != null and endEndTime != null">
  478. AND create_time BETWEEN #{stTime} AND #{endEndTime}
  479. </if>
  480. <if test="trainingCampId != null">
  481. AND EXISTS (
  482. SELECT 1 FROM fs_user_company_user ucu
  483. JOIN fs_user_course_training_camp camp ON ucu.company_user_id = camp.user_id
  484. WHERE ucu.company_id = fs_inquiry_order.company_id
  485. AND camp.training_camp_id = #{trainingCampId}
  486. )
  487. </if>
  488. GROUP BY company_id
  489. UNION ALL
  490. <!-- 积分订单 -->
  491. SELECT
  492. company_id,
  493. 'integral' AS order_type,
  494. COUNT(*) AS order_count,
  495. COALESCE(SUM(pay_money), 0) AS order_amount
  496. FROM fs_integral_order
  497. WHERE status = 3 AND pay_money IS NOT NULL
  498. <if test="stTime != null and endEndTime != null">
  499. AND create_time BETWEEN #{stTime} AND #{endEndTime}
  500. </if>
  501. <if test="trainingCampId != null">
  502. AND EXISTS (
  503. SELECT 1 FROM fs_user_company_user ucu
  504. JOIN fs_user_course_training_camp camp ON ucu.company_user_id = camp.user_id
  505. WHERE ucu.company_id = fs_integral_order.company_id
  506. AND camp.training_camp_id = #{trainingCampId}
  507. )
  508. </if>
  509. GROUP BY company_id
  510. UNION ALL
  511. <!-- 套餐订单 -->
  512. SELECT
  513. company_id,
  514. 'package' AS order_type,
  515. COUNT(*) AS order_count,
  516. COALESCE(SUM(pay_money), 0) AS order_amount
  517. FROM fs_package_order
  518. WHERE status = 3
  519. <if test="stTime != null and endEndTime != null">
  520. AND create_time BETWEEN #{stTime} AND #{endEndTime}
  521. </if>
  522. <if test="trainingCampId != null">
  523. AND EXISTS (
  524. SELECT 1 FROM fs_user_company_user ucu
  525. JOIN fs_user_course_training_camp camp ON ucu.company_user_id = camp.user_id
  526. WHERE ucu.company_id = fs_package_order.company_id
  527. AND camp.training_camp_id = #{trainingCampId}
  528. )
  529. </if>
  530. GROUP BY company_id
  531. ) AS all_orders ON c.company_id = all_orders.company_id
  532. WHERE 1=1
  533. <if test="deptId != null and deptId != ''">
  534. AND d.dept_id = #{deptId}
  535. </if>
  536. <if test="companyId != null">
  537. AND d.company_id = #{companyId}
  538. </if>
  539. GROUP BY d.dept_id, d.dept_name
  540. HAVING inquiryOrderCount > 0 OR integralOrderCount > 0 OR packageOrderCount > 0
  541. ORDER BY inquiryOrderCount DESC, integralOrderCount DESC, packageOrderCount DESC
  542. </select>
  543. </mapper>